Tonic commited on
Commit
8c90037
1 Parent(s): 363d4a7

add chroma await

Browse files
Files changed (1) hide show
  1. app.py +30 -5
app.py CHANGED
@@ -19,6 +19,9 @@ import torch.nn.functional as F
19
  from dotenv import load_dotenv
20
  from utils import load_env_variables, parse_and_route , escape_special_characters
21
  from globalvars import API_BASE, intention_prompt, tasks, system_message, model_name , metadata_prompt
 
 
 
22
 
23
 
24
  load_dotenv()
@@ -121,12 +124,34 @@ def load_documents(file_path: str, mode: str = "elements"):
121
  loader = UnstructuredFileLoader(file_path, mode=mode)
122
  docs = loader.load()
123
  return [doc.page_content for doc in docs]
124
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  def initialize_chroma(collection_name: str, embedding_function: MyEmbeddingFunction):
126
- client = HttpClient(host='localhost', port=8000, settings = Settings(allow_reset=True, anonymized_telemetry=False))
127
- client.reset() # resets the database
 
 
 
 
 
128
  collection = client.create_collection(collection_name)
129
- return client, collection
130
 
131
  def add_documents_to_chroma(client, collection, documents: list, embedding_function: MyEmbeddingFunction):
132
  for doc in documents:
@@ -224,5 +249,5 @@ with gr.Blocks() as demo:
224
  query_button.click(query_documents, inputs=query_input, outputs=query_output)
225
 
226
  if __name__ == "__main__":
227
- os.system("chroma run --host localhost --port 8000")
228
  demo.launch()
 
19
  from dotenv import load_dotenv
20
  from utils import load_env_variables, parse_and_route , escape_special_characters
21
  from globalvars import API_BASE, intention_prompt, tasks, system_message, model_name , metadata_prompt
22
+ import time
23
+ import httpx
24
+
25
 
26
 
27
  load_dotenv()
 
124
  loader = UnstructuredFileLoader(file_path, mode=mode)
125
  docs = loader.load()
126
  return [doc.page_content for doc in docs]
127
+
128
+ def wait_for_chroma_server(host, port, retries=5, delay=5):
129
+ url = f"http://{host}:{port}/"
130
+ for _ in range(retries):
131
+ try:
132
+ response = httpx.get(url)
133
+ if response.status_code == 200:
134
+ print("Chroma server is up and running!")
135
+ return True
136
+ except httpx.HTTPStatusError as e:
137
+ print(f"Attempt to connect to Chroma server failed with status code: {e.response.status_code}")
138
+ except httpx.RequestError as e:
139
+ print(f"Attempt to connect to Chroma server failed: {e}")
140
+ time.sleep(delay)
141
+ print("Failed to connect to Chroma server after multiple attempts.")
142
+ return False
143
+
144
+
145
  def initialize_chroma(collection_name: str, embedding_function: MyEmbeddingFunction):
146
+ host = 'localhost'
147
+ port = 8000
148
+ if not wait_for_chroma_server(host, port):
149
+ raise ConnectionError("Could not connect to Chroma server. Ensure it is running.")
150
+
151
+ client = HttpClient(host=host, port=port, settings=Settings(allow_reset=True, anonymized_telemetry=False))
152
+ client.reset()
153
  collection = client.create_collection(collection_name)
154
+ return client, collection
155
 
156
  def add_documents_to_chroma(client, collection, documents: list, embedding_function: MyEmbeddingFunction):
157
  for doc in documents:
 
249
  query_button.click(query_documents, inputs=query_input, outputs=query_output)
250
 
251
  if __name__ == "__main__":
252
+ os.system("chroma run --host localhost --port 8000 &")
253
  demo.launch()