Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -263,6 +263,7 @@ def save_or_clone_to_cosmos_db(container, query=None, response=None, clone_id=No
|
|
263 |
|
264 |
if clone_id:
|
265 |
try:
|
|
|
266 |
existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
|
267 |
new_doc = {
|
268 |
'id': new_id,
|
@@ -312,55 +313,6 @@ def save_or_clone_to_cosmos_db(container, query=None, response=None, clone_id=No
|
|
312 |
return None
|
313 |
|
314 |
|
315 |
-
# πΎ Save or clone to Cosmos DB - Because every document deserves a twin
|
316 |
-
def save_or_clone_to_cosmos_db2(container, query=None, response=None, clone_id=None):
|
317 |
-
try:
|
318 |
-
if not container:
|
319 |
-
st.error("Cosmos DB container is not initialized.")
|
320 |
-
return
|
321 |
-
|
322 |
-
# Generate a new unique ID
|
323 |
-
new_id = str(uuid.uuid4())
|
324 |
-
|
325 |
-
# Create a new document
|
326 |
-
if clone_id:
|
327 |
-
# If clone_id is provided, we're cloning an existing document
|
328 |
-
try:
|
329 |
-
existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
|
330 |
-
new_doc = existing_doc.copy()
|
331 |
-
new_doc['id'] = new_id
|
332 |
-
new_doc['cloned_from'] = clone_id
|
333 |
-
new_doc['cloned_at'] = datetime.utcnow().isoformat()
|
334 |
-
except exceptions.CosmosResourceNotFoundError:
|
335 |
-
st.error(f"Document with ID {clone_id} not found for cloning.")
|
336 |
-
return
|
337 |
-
else:
|
338 |
-
# If no clone_id, we're creating a new document
|
339 |
-
new_doc = {
|
340 |
-
'id': new_id,
|
341 |
-
'query': query,
|
342 |
-
'response': response,
|
343 |
-
'created_at': datetime.utcnow().isoformat()
|
344 |
-
}
|
345 |
-
|
346 |
-
# Insert the new document
|
347 |
-
container.create_item(body=new_doc)
|
348 |
-
|
349 |
-
st.success(f"{'Cloned' if clone_id else 'New'} document saved successfully with ID: {new_id}")
|
350 |
-
|
351 |
-
# Refresh the documents in the session state
|
352 |
-
st.session_state.documents = list(container.query_items(
|
353 |
-
query="SELECT * FROM c ORDER BY c._ts DESC",
|
354 |
-
enable_cross_partition_query=True
|
355 |
-
))
|
356 |
-
|
357 |
-
return new_id
|
358 |
-
|
359 |
-
except exceptions.CosmosHttpResponseError as e:
|
360 |
-
st.error(f"Error saving to Cosmos DB: {e}")
|
361 |
-
except Exception as e:
|
362 |
-
st.error(f"An unexpected error occurred: {str(e)}")
|
363 |
-
|
364 |
# π¦ Archive current container - Packing up data like you're moving to a new digital house
|
365 |
def archive_current_container(database_name, container_name, client):
|
366 |
try:
|
@@ -500,6 +452,68 @@ def display_saved_files_in_sidebar():
|
|
500 |
st.rerun()
|
501 |
|
502 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
503 |
|
504 |
|
505 |
|
@@ -782,6 +796,10 @@ def main():
|
|
782 |
if st.button("π€ Run AI", key=f'run_with_ai_button_{idx}'):
|
783 |
search_glossary(json.dumps(editable_doc, indent=2))
|
784 |
if st.button("π Clone Document", key=f'clone_button_{idx}'):
|
|
|
|
|
|
|
|
|
785 |
with st.spinner("Cloning document..."):
|
786 |
cloned_id = save_or_clone_to_cosmos_db(container,
|
787 |
clone_id=doc['id'])
|
@@ -794,6 +812,10 @@ def main():
|
|
794 |
|
795 |
|
796 |
elif selected_view == 'Clone Document':
|
|
|
|
|
|
|
|
|
797 |
Label = '# 𧬠Clone functionality - Copy wisely, paste precisely'
|
798 |
st.markdown(Label)
|
799 |
st.markdown("#### Clone a document:")
|
@@ -834,6 +856,11 @@ def main():
|
|
834 |
elif selected_view == 'New Record':
|
835 |
Label = '# π New Record view - Every new record is a new beginning'
|
836 |
st.markdown(Label)
|
|
|
|
|
|
|
|
|
|
|
837 |
st.markdown("#### Create a new document:")
|
838 |
#if st.button("π€ Insert Auto-Generated Record"):
|
839 |
success, message = save_or_clone_to_cosmos_db(container,
|
|
|
263 |
|
264 |
if clone_id:
|
265 |
try:
|
266 |
+
|
267 |
existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
|
268 |
new_doc = {
|
269 |
'id': new_id,
|
|
|
313 |
return None
|
314 |
|
315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
# π¦ Archive current container - Packing up data like you're moving to a new digital house
|
317 |
def archive_current_container(database_name, container_name, client):
|
318 |
try:
|
|
|
452 |
st.rerun()
|
453 |
|
454 |
|
455 |
+
def clone_record(container, clone_id):
|
456 |
+
try:
|
457 |
+
existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
|
458 |
+
new_doc = existing_doc.copy()
|
459 |
+
new_doc['id'] = generate_unique_id() # Generate new unique ID with timestamp
|
460 |
+
new_doc['createdAt'] = datetime.utcnow().isoformat() # Update the creation time
|
461 |
+
new_doc['_rid'] = None # Reset _rid or any system-managed fields
|
462 |
+
new_doc['_self'] = None
|
463 |
+
new_doc['_etag'] = None
|
464 |
+
new_doc['_attachments'] = None
|
465 |
+
new_doc['_ts'] = None # Reset timestamp to be updated by Cosmos DB automatically
|
466 |
+
|
467 |
+
# Insert the cloned document
|
468 |
+
response = container.create_item(body=new_doc)
|
469 |
+
st.success(f"Cloned document saved successfully with ID: {new_doc['id']} π")
|
470 |
+
|
471 |
+
# Refresh the documents in session state
|
472 |
+
st.session_state.documents = list(container.query_items(
|
473 |
+
query="SELECT * FROM c ORDER BY c._ts DESC",
|
474 |
+
enable_cross_partition_query=True
|
475 |
+
))
|
476 |
+
|
477 |
+
except exceptions.CosmosResourceNotFoundError:
|
478 |
+
st.error(f"Document with ID {clone_id} not found for cloning.")
|
479 |
+
except exceptions.CosmosHttpResponseError as e:
|
480 |
+
st.error(f"HTTP error occurred: {str(e)} π¨")
|
481 |
+
except Exception as e:
|
482 |
+
st.error(f"An unexpected error occurred: {str(e)} π±")
|
483 |
+
|
484 |
+
|
485 |
+
|
486 |
+
|
487 |
+
def create_new_blank_record(container):
|
488 |
+
try:
|
489 |
+
# Get the structure of the latest document (to preserve schema)
|
490 |
+
latest_doc = container.query_items(query="SELECT * FROM c ORDER BY c._ts DESC", enable_cross_partition_query=True, max_item_count=1)
|
491 |
+
if latest_doc:
|
492 |
+
new_doc_structure = latest_doc[0].copy()
|
493 |
+
else:
|
494 |
+
new_doc_structure = {}
|
495 |
+
|
496 |
+
new_doc = {key: "" for key in new_doc_structure.keys()} # Set all fields to blank
|
497 |
+
new_doc['id'] = generate_unique_id() # Generate new unique ID
|
498 |
+
new_doc['createdAt'] = datetime.utcnow().isoformat() # Set creation time
|
499 |
+
|
500 |
+
# Insert the new blank document
|
501 |
+
response = container.create_item(body=new_doc)
|
502 |
+
st.success(f"New blank document saved successfully with ID: {new_doc['id']} π")
|
503 |
+
|
504 |
+
# Refresh the documents in session state
|
505 |
+
st.session_state.documents = list(container.query_items(
|
506 |
+
query="SELECT * FROM c ORDER BY c._ts DESC",
|
507 |
+
enable_cross_partition_query=True
|
508 |
+
))
|
509 |
+
|
510 |
+
except exceptions.CosmosHttpResponseError as e:
|
511 |
+
st.error(f"HTTP error occurred: {str(e)} π¨")
|
512 |
+
except Exception as e:
|
513 |
+
st.error(f"An unexpected error occurred: {str(e)} π±")
|
514 |
+
|
515 |
+
|
516 |
+
|
517 |
|
518 |
|
519 |
|
|
|
796 |
if st.button("π€ Run AI", key=f'run_with_ai_button_{idx}'):
|
797 |
search_glossary(json.dumps(editable_doc, indent=2))
|
798 |
if st.button("π Clone Document", key=f'clone_button_{idx}'):
|
799 |
+
|
800 |
+
|
801 |
+
|
802 |
+
|
803 |
with st.spinner("Cloning document..."):
|
804 |
cloned_id = save_or_clone_to_cosmos_db(container,
|
805 |
clone_id=doc['id'])
|
|
|
812 |
|
813 |
|
814 |
elif selected_view == 'Clone Document':
|
815 |
+
if st.button("π Clone Document"):
|
816 |
+
clone_id = st.text_input("Enter Document ID to Clone")
|
817 |
+
if clone_id:
|
818 |
+
clone_record(container, clone_id)
|
819 |
Label = '# 𧬠Clone functionality - Copy wisely, paste precisely'
|
820 |
st.markdown(Label)
|
821 |
st.markdown("#### Clone a document:")
|
|
|
856 |
elif selected_view == 'New Record':
|
857 |
Label = '# π New Record view - Every new record is a new beginning'
|
858 |
st.markdown(Label)
|
859 |
+
|
860 |
+
if st.button("β Create New Blank Document"):
|
861 |
+
(container)
|
862 |
+
|
863 |
+
|
864 |
st.markdown("#### Create a new document:")
|
865 |
#if st.button("π€ Insert Auto-Generated Record"):
|
866 |
success, message = save_or_clone_to_cosmos_db(container,
|