wjm55 commited on
Commit
9a02fc3
·
1 Parent(s): 1beed4c

Refactor Weaviate client initialization into a separate function for improved code organization and resource management. The client is now created within the `search_images` function and closed after use.

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -11,15 +11,18 @@ load_dotenv()
11
  # Initialize the model
12
  model = SentenceTransformer('all-MiniLM-L6-v2')
13
 
14
- # Initialize Weaviate client
15
- client = weaviate.connect_to_weaviate_cloud(
16
- cluster_url=os.getenv("WEAVIATE_URL"),
17
- auth_credentials=Auth.api_key(os.getenv("WEAVIATE_API_KEY"))
18
- )
19
-
20
- lux_collection = client.collections.get("LuxData")
21
 
22
  def search_images(query):
 
 
 
 
23
  # Encode the query and search
24
  query_vector = model.encode(query)
25
  results = lux_collection.query.near_vector(
@@ -36,6 +39,9 @@ def search_images(query):
36
  "image_url": result.properties.get('image_url', "No image available")
37
  })
38
 
 
 
 
39
  return formatted_results
40
 
41
  # Create Gradio interface
 
11
  # Initialize the model
12
  model = SentenceTransformer('all-MiniLM-L6-v2')
13
 
14
+ # Move client initialization into search function
15
+ def get_client():
16
+ return weaviate.connect_to_weaviate_cloud(
17
+ cluster_url=os.getenv("WEAVIATE_URL"),
18
+ auth_credentials=Auth.api_key(os.getenv("WEAVIATE_API_KEY"))
19
+ )
 
20
 
21
  def search_images(query):
22
+ # Create client connection
23
+ client = get_client()
24
+ lux_collection = client.collections.get("LuxData")
25
+
26
  # Encode the query and search
27
  query_vector = model.encode(query)
28
  results = lux_collection.query.near_vector(
 
39
  "image_url": result.properties.get('image_url', "No image available")
40
  })
41
 
42
+ # Close the client connection
43
+ client.close()
44
+
45
  return formatted_results
46
 
47
  # Create Gradio interface