awacke1 commited on
Commit
0d1aedf
1 Parent(s): fecb862

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -77
app.py CHANGED
@@ -169,81 +169,53 @@ def generate_unique_id():
169
  return str(uuid.uuid4())
170
 
171
 
172
- def save_to_cosmos_db(container, query, response1, response2):
173
- max_retries = 5
174
- base_wait_time = 0.1 # 100 ms
175
-
176
- def generate_unique_id():
177
- timestamp = datetime.utcnow().strftime('%Y%m%d%H%M%S%f')
178
- unique_uuid = str(uuid.uuid4())
179
- return f"{timestamp}-{unique_uuid}"
180
-
181
- def generate_short_filename(query):
182
- # Create a hash of the query
183
- hash_object = hashlib.md5(query.encode())
184
- query_hash = hash_object.hexdigest()[:8]
185
-
186
- # Use the first few words of the query (up to 50 characters)
187
- short_query = ' '.join(query.split()[:5])[:50]
188
-
189
- # Remove any non-alphanumeric characters and replace spaces with underscores
190
- safe_query = ''.join(c if c.isalnum() else '_' for c in short_query)
191
-
192
- # Combine timestamp, hash, and safe query
193
- timestamp = datetime.utcnow().strftime('%Y%m%d%H%M%S')
194
- return f"{timestamp}_{query_hash}_{safe_query}.md"
195
-
196
- for attempt in range(max_retries):
197
- try:
198
- if container:
199
- base_id = generate_unique_id()
200
- record = {
201
- "id": base_id,
202
- "query": query,
203
- "response1": response1,
204
- "response2": response2,
205
- "timestamp": datetime.utcnow().isoformat()
206
- }
207
-
208
- # Check if document with this ID exists
209
- try:
210
- existing_doc = container.read_item(item=base_id, partition_key=base_id)
211
- # If it exists, append a random string to the ID
212
- record["id"] = f"{base_id}-{str(uuid.uuid4())[:8]}"
213
- except exceptions.CosmosResourceNotFoundError:
214
- # If it doesn't exist, we can use the original ID
215
- pass
216
-
217
- container.upsert_item(body=record)
218
- st.success(f"Record saved successfully with ID: {record['id']}")
219
- st.session_state.documents = container.query_items(
220
- query="SELECT * FROM c ORDER BY c._ts DESC",
221
- enable_cross_partition_query=True
222
- )
223
-
224
- # Generate a short filename for local storage
225
- filename = generate_short_filename(query)
226
- with open(filename, 'w', encoding='utf-8') as file:
227
- file.write(f"Query: {query}\n\nResponse: {response1}")
228
- st.success(f"File saved locally as: {filename}")
229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  return
231
- else:
232
- st.error("Cosmos DB container is not initialized.")
233
- return
234
- except exceptions.CosmosHttpResponseError as e:
235
- if e.status_code == 409: # Conflict error
236
- wait_time = (2 ** attempt) * base_wait_time
237
- st.warning(f"ID conflict occurred. Retrying in {wait_time:.2f} seconds... (Attempt {attempt + 1})")
238
- time.sleep(wait_time)
239
- else:
240
- st.error(f"Error saving record to Cosmos DB: {e}")
241
- return
242
- except Exception as e:
243
- st.error(f"An unexpected error occurred: {str(e)}")
244
- return
 
 
 
 
 
 
 
245
 
246
- st.error("Failed to save record after maximum retries.")
 
 
 
247
 
248
  # Add dropdowns for model and database choices
249
  def search_glossary(query):
@@ -320,11 +292,20 @@ def search_glossary(query):
320
 
321
  # When saving results, pass the container
322
  try:
323
- save_to_cosmos_db(st.session_state.cosmos_container, query, result, result)
324
- save_to_cosmos_db(st.session_state.cosmos_container, query, result2, result2)
325
- save_to_cosmos_db(st.session_state.cosmos_container, query, result3, result3)
326
- save_to_cosmos_db(st.session_state.cosmos_container, query, response2[0], response2[0])
327
- save_to_cosmos_db(st.session_state.cosmos_container, query, response2[1], response2[1])
 
 
 
 
 
 
 
 
 
328
  except exceptions.CosmosHttpResponseError as e:
329
  return False, f"HTTP error occurred: {str(e)} 🚨"
330
  except Exception as e:
 
169
  return str(uuid.uuid4())
170
 
171
 
172
+ def save_or_clone_to_cosmos_db(container, query=None, response=None, clone_id=None):
173
+ try:
174
+ if not container:
175
+ st.error("Cosmos DB container is not initialized.")
176
+ return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
+ # Generate a new unique ID
179
+ new_id = str(uuid.uuid4())
180
+
181
+ # Create a new document
182
+ if clone_id:
183
+ # If clone_id is provided, we're cloning an existing document
184
+ try:
185
+ existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
186
+ new_doc = existing_doc.copy()
187
+ new_doc['id'] = new_id
188
+ new_doc['cloned_from'] = clone_id
189
+ new_doc['cloned_at'] = datetime.utcnow().isoformat()
190
+ except exceptions.CosmosResourceNotFoundError:
191
+ st.error(f"Document with ID {clone_id} not found for cloning.")
192
  return
193
+ else:
194
+ # If no clone_id, we're creating a new document
195
+ new_doc = {
196
+ 'id': new_id,
197
+ 'query': query,
198
+ 'response': response,
199
+ 'created_at': datetime.utcnow().isoformat()
200
+ }
201
+
202
+ # Insert the new document
203
+ container.create_item(body=new_doc)
204
+
205
+ st.success(f"{'Cloned' if clone_id else 'New'} document saved successfully with ID: {new_id}")
206
+
207
+ # Refresh the documents in the session state
208
+ st.session_state.documents = list(container.query_items(
209
+ query="SELECT * FROM c ORDER BY c._ts DESC",
210
+ enable_cross_partition_query=True
211
+ ))
212
+
213
+ return new_id
214
 
215
+ except exceptions.CosmosHttpResponseError as e:
216
+ st.error(f"Error saving to Cosmos DB: {e}")
217
+ except Exception as e:
218
+ st.error(f"An unexpected error occurred: {str(e)}")
219
 
220
  # Add dropdowns for model and database choices
221
  def search_glossary(query):
 
292
 
293
  # When saving results, pass the container
294
  try:
295
+ #save_to_cosmos_db(st.session_state.cosmos_container, query, result, result)
296
+ #save_to_cosmos_db(st.session_state.cosmos_container, query, result2, result2)
297
+ #save_to_cosmos_db(st.session_state.cosmos_container, query, result3, result3)
298
+ #save_to_cosmos_db(st.session_state.cosmos_container, query, response2[0], response2[0])
299
+ #save_to_cosmos_db(st.session_state.cosmos_container, query, response2[1], response2[1])
300
+
301
+ save_or_clone_to_cosmos_db(container=st.session_state.cosmos_container, query=query, response=result, clone_id=None)
302
+ save_or_clone_to_cosmos_db(container=st.session_state.cosmos_container, query=query, response=result2, clone_id=None)
303
+ save_or_clone_to_cosmos_db(container=st.session_state.cosmos_container, query=query, response=result3, clone_id=None)
304
+ save_or_clone_to_cosmos_db(container=st.session_state.cosmos_container, query=query, response=response2[0], clone_id=None)
305
+ save_or_clone_to_cosmos_db(container=st.session_state.cosmos_container, query=query, response=response2[1], clone_id=None)
306
+
307
+
308
+
309
  except exceptions.CosmosHttpResponseError as e:
310
  return False, f"HTTP error occurred: {str(e)} 🚨"
311
  except Exception as e: