awacke1 commited on
Commit
f0faf34
β€’
1 Parent(s): 5ecf9e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -133
app.py CHANGED
@@ -805,19 +805,6 @@ def main():
805
  with col_ai:
806
  if st.button("πŸ€– Run AI", key=f'run_with_ai_button_{idx}'):
807
  search_glossary(json.dumps(editable_doc, indent=2))
808
- if st.button("πŸ“„ Clone Document", key=f'clone_button_{idx}'):
809
-
810
-
811
-
812
-
813
- with st.spinner("Cloning document..."):
814
- cloned_id = save_or_clone_to_cosmos_db(container,
815
- clone_id=doc['id'])
816
- if cloned_id:
817
- st.success(f"Document cloned successfully with new ID: {cloned_id}")
818
- st.rerun()
819
- else:
820
- st.error("Failed to clone document. Please try again.")
821
 
822
 
823
  elif selected_view == 'Clone Document':
@@ -831,50 +818,27 @@ def main():
831
  # Button to initiate cloning of the document
832
  if st.button("πŸ“„ Clone Document", key=f'clone_button_{idx}'):
833
  with st.spinner("Cloning document..."):
834
-
835
- try:
836
- existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
837
- new_doc = existing_doc.copy()
838
- new_doc['id'] = generate_unique_id() # Generate new unique ID with timestamp
839
- new_doc['name'] = generate_unique_id() # Generate new unique ID with timestamp
840
- new_doc['createdAt'] = datetime.utcnow().isoformat() # Update the creation time
841
- new_doc['_rid'] = None # Reset _rid or any system-managed fields
842
- new_doc['_self'] = None
843
- new_doc['_etag'] = None
844
- new_doc['_attachments'] = None
845
- new_doc['_ts'] = None # Reset timestamp to be updated by Cosmos DB automatically
846
-
847
- # Insert the cloned document
848
- response = container.create_item(body=new_doc)
849
- st.success(f"Cloned document saved successfully with ID: {new_doc['id']} πŸŽ‰")
850
-
851
- # Refresh the documents in session state
852
- st.session_state.documents = list(container.query_items(
853
- query="SELECT * FROM c ORDER BY c._ts DESC",
854
- enable_cross_partition_query=True
855
- ))
856
-
857
- except exceptions.CosmosResourceNotFoundError:
858
- st.error(f"Document with ID {clone_id} not found for cloning.")
859
- except exceptions.CosmosHttpResponseError as e:
860
- st.error(f"HTTP error occurred: {str(e)} 🚨")
861
- except Exception as e:
862
- st.error(f"An unexpected error occurred: {str(e)} 😱")
863
-
864
-
865
- #try:
866
- # Copy the document and reset system-managed fields
867
- cloned_doc = doc.copy()
868
- cloned_doc['id'] = generate_unique_id() # Generate new unique ID
869
- cloned_doc['name'] = generate_unique_id() # Generate new unique ID
870
- cloned_doc['createdAt'] = datetime.utcnow().isoformat() # Set new creation time
871
- cloned_doc['_rid'] = None # Clear system-managed fields
872
- cloned_doc['_self'] = None
873
- cloned_doc['_etag'] = None
874
- cloned_doc['_attachments'] = None
875
- cloned_doc['_ts'] = None
876
 
877
- # Save the cloned document
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
878
 
879
  response = container.create_item(body=cloned_doc)
880
  st.success(f"Cloned document saved successfully with ID: {cloned_doc['id']} πŸŽ‰")
@@ -884,83 +848,6 @@ def main():
884
  query="SELECT * FROM c ORDER BY c._ts DESC",
885
  enable_cross_partition_query=True
886
  ))
887
-
888
- #except exceptions.CosmosHttpResponseError as e:
889
- # st.error(f"HTTP error occurred: {str(e)} 🚨")
890
- #except Exception as e:
891
- # st.error(f"An unexpected error occurred: {str(e)} 😱")
892
-
893
- # If clone mode is activated, allow user to edit the cloned document
894
- if st.session_state.get('clone_mode', False):
895
- st.markdown("#### Edit Cloned Document and Name Your Clone:")
896
- cloned_doc_str = st.text_area(
897
- "Cloned Document Content (in JSON format) - Update name before saving!",
898
- value=st.session_state.cloned_doc_str,
899
- height=300
900
- )
901
-
902
- # Save the edited cloned document
903
- if st.button("πŸ’Ύ Save Cloned Document"):
904
- try:
905
- new_doc = json.loads(cloned_doc_str)
906
- success, message = insert_record(container, new_doc)
907
- if success:
908
- st.success(f"Cloned document saved with id: {new_doc['id']} πŸŽ‰")
909
- st.session_state.selected_document_id = new_doc['id']
910
- st.session_state.clone_mode = False
911
- st.session_state.cloned_doc = None
912
- st.session_state.cloned_doc_str = ''
913
- st.rerun()
914
- else:
915
- st.error(message)
916
- except json.JSONDecodeError as e:
917
- st.error(f"Invalid JSON: {str(e)} 🚫")
918
-
919
-
920
-
921
-
922
-
923
- elif selected_view == 'Clone Old Document':
924
- if st.button("πŸ“„ Clone Document"):
925
- clone_id = st.text_input("Enter Document ID to Clone")
926
- if clone_id:
927
- clone_record(container, clone_id)
928
- Label = '# 🧬 Clone functionality - Copy wisely, paste precisely'
929
- st.markdown(Label)
930
- st.markdown("#### Clone a document:")
931
- for idx, doc in enumerate(documents_to_display):
932
- st.markdown(f"##### Document ID: {doc.get('id', '')}")
933
- if st.button("πŸ“„ Clone Document", key=f'clone_button_{idx}'):
934
- cloned_doc = doc.copy()
935
- cloned_doc['id'] = generate_unique_id()
936
- st.session_state.cloned_doc = cloned_doc
937
- st.session_state.cloned_doc_str = json.dumps(cloned_doc, indent=2)
938
- st.session_state.clone_mode = True
939
- st.rerun()
940
- if st.session_state.get('clone_mode', False):
941
- st.markdown("#### Edit Cloned Document and Name Your Clone:")
942
- cloned_doc_str = st.text_area("Cloned Document Content (in JSON format) - Update name please to make it unique before saving!",
943
- value=st.session_state.cloned_doc_str,
944
- height=300)
945
-
946
-
947
- if st.button("πŸ’Ύ Save Cloned Document"):
948
- try:
949
- new_doc = json.loads(cloned_doc_str)
950
- success, message = insert_record(container, new_doc)
951
- if success:
952
- st.success(f"Cloned document saved with id: {new_doc['id']} πŸŽ‰")
953
- st.session_state.selected_document_id = new_doc['id']
954
- st.session_state.clone_mode = False
955
- st.session_state.cloned_doc = None
956
- st.session_state.cloned_doc_str = ''
957
- st.rerun()
958
- else:
959
- st.error(message)
960
- except json.JSONDecodeError as e:
961
- st.error(f"Invalid JSON: {str(e)} 🚫")
962
-
963
-
964
 
965
  elif selected_view == 'New Record':
966
  Label = '# πŸ†• New Record view - Every new record is a new beginning'
 
805
  with col_ai:
806
  if st.button("πŸ€– Run AI", key=f'run_with_ai_button_{idx}'):
807
  search_glossary(json.dumps(editable_doc, indent=2))
 
 
 
 
 
 
 
 
 
 
 
 
 
808
 
809
 
810
  elif selected_view == 'Clone Document':
 
818
  # Button to initiate cloning of the document
819
  if st.button("πŸ“„ Clone Document", key=f'clone_button_{idx}'):
820
  with st.spinner("Cloning document..."):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
821
 
822
+ existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
823
+ new_doc = existing_doc.copy()
824
+ new_doc['id'] = generate_unique_id() # Generate new unique ID with timestamp
825
+ new_doc['name'] = generate_unique_id() # Generate new unique ID with timestamp
826
+ new_doc['createdAt'] = datetime.utcnow().isoformat() # Update the creation time
827
+ new_doc['_rid'] = None # Reset _rid or any system-managed fields
828
+ new_doc['_self'] = None
829
+ new_doc['_etag'] = None
830
+ new_doc['_attachments'] = None
831
+ new_doc['_ts'] = None # Reset timestamp to be updated by Cosmos DB automatically
832
+
833
+ # Insert the cloned document
834
+ response = container.create_item(body=new_doc)
835
+ st.success(f"Cloned document saved successfully with ID: {new_doc['id']} πŸŽ‰")
836
+
837
+ # Refresh the documents in session state
838
+ st.session_state.documents = list(container.query_items(
839
+ query="SELECT * FROM c ORDER BY c._ts DESC",
840
+ enable_cross_partition_query=True
841
+ ))
842
 
843
  response = container.create_item(body=cloned_doc)
844
  st.success(f"Cloned document saved successfully with ID: {cloned_doc['id']} πŸŽ‰")
 
848
  query="SELECT * FROM c ORDER BY c._ts DESC",
849
  enable_cross_partition_query=True
850
  ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
851
 
852
  elif selected_view == 'New Record':
853
  Label = '# πŸ†• New Record view - Every new record is a new beginning'