awacke1 commited on
Commit
ed0b7b5
1 Parent(s): 37c8622

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -17
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
- # 💾 Save responses to Cosmos DB
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  def save_to_cosmos_db(query, response1, response2):
182
- # 💾 Saving responses to Cosmos DB—because even the cosmos needs backups! 💿
183
- cosmos_container = st.session_state.get("cosmos_container")
184
- if cosmos_container:
185
- record = {
186
- "id": generate_unique_id(),
187
- "query": query,
188
- "response1": json.loads(response1),
189
- "response2": json.loads(response2)
190
- }
191
- try:
192
- cosmos_container.create_item(body=record)
193
- st.success(f"Record saved successfully with ID: {record['id']}")
194
- except exceptions.CosmosHttpResponseError as e:
195
- st.error(f"Error saving record to Cosmos DB: {e}")
196
- else:
197
- st.error("Cosmos DB is not initialized.")
 
 
 
 
 
 
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