awacke1 commited on
Commit
8aff174
·
verified ·
1 Parent(s): 916ad2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -49
app.py CHANGED
@@ -244,69 +244,59 @@ def push_to_github(local_path, repo, github_token):
244
  origin.push(refspec=f'{current_branch}:{current_branch}')
245
 
246
 
247
-
248
  def save_or_clone_to_cosmos_db(container, query=None, response=None, clone_id=None):
249
- max_retries = 5
250
- retry_delay = 1 # seconds
251
-
252
  def generate_unique_id():
253
  return f"{datetime.utcnow().strftime('%Y%m%d%H%M%S%f')}-{str(uuid.uuid4())}"
254
 
255
- for attempt in range(max_retries):
256
- try:
257
- if not container:
258
- st.error("Cosmos DB container is not initialized.")
259
- return
260
 
261
- new_id = generate_unique_id()
262
-
263
- if clone_id:
264
- # If clone_id is provided, we're cloning an existing document
265
- try:
266
- existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
267
- new_doc = existing_doc.copy()
268
- new_doc['id'] = new_id
269
- new_doc['cloned_from'] = clone_id
270
- new_doc['cloned_at'] = datetime.utcnow().isoformat()
271
- except exceptions.CosmosResourceNotFoundError:
272
- st.error(f"Document with ID {clone_id} not found for cloning.")
273
- return
274
- else:
275
- # If no clone_id, we're creating a new document
276
  new_doc = {
277
  'id': new_id,
278
- 'query': query,
279
- 'response': response,
280
- 'created_at': datetime.utcnow().isoformat()
 
281
  }
 
 
 
 
 
 
 
 
 
 
 
282
 
283
- # Insert the new document
284
- container.create_item(body=new_doc)
285
-
286
- st.success(f"{'Cloned' if clone_id else 'New'} document saved successfully with ID: {new_id}")
287
-
288
- # Refresh the documents in the session state
289
- st.session_state.documents = list(container.query_items(
290
- query="SELECT * FROM c ORDER BY c._ts DESC",
291
- enable_cross_partition_query=True
292
- ))
293
 
294
- return new_id
295
 
296
- except exceptions.CosmosHttpResponseError as e:
297
- if e.status_code == 409: # Conflict error
298
- st.warning(f"ID conflict occurred. Retrying... (Attempt {attempt + 1})")
299
- time.sleep(retry_delay)
300
- else:
301
- st.error(f"Error saving to Cosmos DB: {e}")
302
- return
303
- except Exception as e:
304
- st.error(f"An unexpected error occurred: {str(e)}")
305
- return
306
 
307
- st.error("Failed to save document after maximum retries.")
308
 
 
 
 
 
309
 
 
310
 
311
 
312
 
 
244
  origin.push(refspec=f'{current_branch}:{current_branch}')
245
 
246
 
 
247
  def save_or_clone_to_cosmos_db(container, query=None, response=None, clone_id=None):
 
 
 
248
  def generate_unique_id():
249
  return f"{datetime.utcnow().strftime('%Y%m%d%H%M%S%f')}-{str(uuid.uuid4())}"
250
 
251
+ try:
252
+ if not container:
253
+ st.error("Cosmos DB container is not initialized.")
254
+ return
 
255
 
256
+ new_id = generate_unique_id()
257
+
258
+ if clone_id:
259
+ # If clone_id is provided, we're creating a new document based on an existing one
260
+ try:
261
+ existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
 
 
 
 
 
 
 
 
 
262
  new_doc = {
263
  'id': new_id,
264
+ 'originalText': existing_doc.get('originalText', ''),
265
+ 'qtPrompts': existing_doc.get('qtPrompts', []),
266
+ 'cloned_from': clone_id,
267
+ 'cloned_at': datetime.utcnow().isoformat()
268
  }
269
+ except exceptions.CosmosResourceNotFoundError:
270
+ st.error(f"Document with ID {clone_id} not found for cloning.")
271
+ return
272
+ else:
273
+ # If no clone_id, we're creating a new document
274
+ new_doc = {
275
+ 'id': new_id,
276
+ 'query': query,
277
+ 'response': response,
278
+ 'created_at': datetime.utcnow().isoformat()
279
+ }
280
 
281
+ # Insert the new document
282
+ response = container.create_item(body=new_doc)
 
 
 
 
 
 
 
 
283
 
284
+ st.success(f"{'Cloned' if clone_id else 'New'} document saved successfully with ID: {response['id']}")
285
 
286
+ # Refresh the documents in the session state
287
+ st.session_state.documents = list(container.query_items(
288
+ query="SELECT * FROM c ORDER BY c._ts DESC",
289
+ enable_cross_partition_query=True
290
+ ))
 
 
 
 
 
291
 
292
+ return response['id']
293
 
294
+ except exceptions.CosmosHttpResponseError as e:
295
+ st.error(f"Error saving to Cosmos DB: {e}")
296
+ except Exception as e:
297
+ st.error(f"An unexpected error occurred: {str(e)}")
298
 
299
+ return None
300
 
301
 
302