Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -177,24 +177,44 @@ def format_with_line_numbers(text):
|
|
177 |
formatted_text = '\n'.join(f"{i+1}: {line}" for i, line in enumerate(lines))
|
178 |
return formatted_text
|
179 |
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
def save_to_cosmos_db(query, response1, response2):
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
|
200 |
# Add dropdowns for model and database choices
|
|
|
177 |
formatted_text = '\n'.join(f"{i+1}: {line}" for i, line in enumerate(lines))
|
178 |
return formatted_text
|
179 |
|
180 |
+
|
181 |
+
def generate_unique_id():
|
182 |
+
return str(uuid.uuid4())
|
183 |
+
|
184 |
+
def get_databases(client):
|
185 |
+
return [db['id'] for db in client.list_databases()]
|
186 |
+
|
187 |
+
def get_containers(database):
|
188 |
+
return [container['id'] for container in database.list_containers()]
|
189 |
+
|
190 |
+
def get_documents(container, limit=None):
|
191 |
+
query = "SELECT * FROM c ORDER BY c._ts DESC"
|
192 |
+
items = list(container.query_items(query=query, enable_cross_partition_query=True, max_item_count=limit))
|
193 |
+
return items
|
194 |
+
|
195 |
def save_to_cosmos_db(query, response1, response2):
|
196 |
+
try:
|
197 |
+
cosmos_container = st.session_state.get("cosmos_container")
|
198 |
+
if cosmos_container:
|
199 |
+
record = {
|
200 |
+
"id": generate_unique_id(),
|
201 |
+
"query": query,
|
202 |
+
"response1": response1,
|
203 |
+
"response2": response2
|
204 |
+
}
|
205 |
+
try:
|
206 |
+
cosmos_container.create_item(body=record)
|
207 |
+
st.success(f"Record saved successfully with ID: {record['id']}")
|
208 |
+
# Refresh the documents display
|
209 |
+
st.session_state.documents = get_documents(cosmos_container)
|
210 |
+
except exceptions.CosmosHttpResponseError as e:
|
211 |
+
st.error(f"Error saving record to Cosmos DB: {e}")
|
212 |
+
else:
|
213 |
+
st.error("Cosmos DB is not initialized.")
|
214 |
+
except Exception as e:
|
215 |
+
st.error(f"An unexpected error occurred: {str(e)}")
|
216 |
+
|
217 |
+
|
218 |
|
219 |
|
220 |
# Add dropdowns for model and database choices
|