Spaces:
Build error
Build error
try chroma direct loading and wait and run
Browse files
app.py
CHANGED
@@ -125,33 +125,30 @@ def load_documents(file_path: str, mode: str = "elements"):
|
|
125 |
docs = loader.load()
|
126 |
return [doc.page_content for doc in docs]
|
127 |
|
128 |
-
def wait_for_chroma_server(
|
129 |
-
url = f"http://{host}:{port}/"
|
130 |
for _ in range(retries):
|
131 |
try:
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
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:
|
|
|
125 |
docs = loader.load()
|
126 |
return [doc.page_content for doc in docs]
|
127 |
|
128 |
+
def wait_for_chroma_server(client, retries=10, delay=0.5):
|
|
|
129 |
for _ in range(retries):
|
130 |
try:
|
131 |
+
client.heartbeat()
|
132 |
+
print("Chroma server is up and running!")
|
133 |
+
return True
|
134 |
+
except Exception as e:
|
|
|
|
|
|
|
135 |
print(f"Attempt to connect to Chroma server failed: {e}")
|
136 |
time.sleep(delay)
|
137 |
print("Failed to connect to Chroma server after multiple attempts.")
|
138 |
return False
|
139 |
+
|
|
|
140 |
def initialize_chroma(collection_name: str, embedding_function: MyEmbeddingFunction):
|
141 |
host = 'localhost'
|
142 |
port = 8000
|
|
|
|
|
143 |
|
144 |
client = HttpClient(host=host, port=port, settings=Settings(allow_reset=True, anonymized_telemetry=False))
|
145 |
+
|
146 |
+
if not wait_for_chroma_server(client):
|
147 |
+
raise ConnectionError("Could not connect to Chroma server. Ensure it is running.")
|
148 |
+
|
149 |
+
client.reset() # Empties and completely resets the database
|
150 |
collection = client.create_collection(collection_name)
|
151 |
+
return client, collection
|
152 |
|
153 |
def add_documents_to_chroma(client, collection, documents: list, embedding_function: MyEmbeddingFunction):
|
154 |
for doc in documents:
|