awacke1 commited on
Commit
54d0e82
β€’
1 Parent(s): b039b0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -77
app.py CHANGED
@@ -806,91 +806,67 @@ def main():
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':
811
- st.markdown("# 🧬 Clone functionality - Copy wisely, paste precisely")
812
  st.markdown("#### Clone a document:")
813
-
814
- # Displaying document IDs and clone buttons
815
  for idx, doc in enumerate(documents_to_display):
816
  st.markdown(f"##### Document ID: {doc.get('id', '')}")
817
-
818
- clone_id = doc.get('id', '')
819
-
820
- # Button to initiate cloning of the document
821
  if st.button("πŸ“„ Clone Document", key=f'clone_button_{idx}'):
822
- with st.spinner("Cloning document..."):
823
-
824
- existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
825
- new_doc = existing_doc.copy()
826
- new_doc['id'] = generate_unique_id() # Generate new unique ID with timestamp
827
- new_doc['name'] = generate_unique_id() # Generate new unique ID with timestamp
828
- new_doc['createdAt'] = datetime.utcnow().isoformat() # Update the creation time
829
- new_doc['_rid'] = None # Reset _rid or any system-managed fields
830
- new_doc['_self'] = None
831
- new_doc['_etag'] = None
832
- new_doc['_attachments'] = None
833
- new_doc['_ts'] = None # Reset timestamp to be updated by Cosmos DB automatically
834
-
835
- # Insert the cloned document
836
- response = container.create_item(body=new_doc)
837
- st.success(f"Cloned document saved successfully with ID: {new_doc['id']} πŸŽ‰")
838
-
839
- # Refresh the documents in session state
840
- st.session_state.documents = list(container.query_items(
841
- query="SELECT * FROM c ORDER BY c._ts DESC",
842
- enable_cross_partition_query=True
843
- ))
844
-
845
- response = container.create_item(body=cloned_doc)
846
- st.success(f"Cloned document saved successfully with ID: {cloned_doc['id']} πŸŽ‰")
847
-
848
- # Refresh session state documents to show the new cloned document
849
- st.session_state.documents = list(container.query_items(
850
- query="SELECT * FROM c ORDER BY c._ts DESC",
851
- enable_cross_partition_query=True
852
- ))
853
-
854
  elif selected_view == 'New Record':
855
- Label = '# πŸ†• New Record view - Every new record is a new beginning'
856
- st.markdown(Label)
857
-
858
- if st.button("βž• Create New Blank Document"):
859
- (container)
860
-
861
-
862
  st.markdown("#### Create a new document:")
863
- #if st.button("πŸ€– Insert Auto-Generated Record"):
864
- success, message = save_or_clone_to_cosmos_db(container,
865
- query="Auto-generated",
866
- response="This is an auto-generated record.")
867
- if success:
868
- st.success(message)
869
- #st.rerun()
870
  else:
871
- st.error(message)
872
- #else:
873
-
874
- new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
875
- new_doc_str = st.text_area("Document Content (in JSON format)",
876
- value='{}', height=300)
877
- if st.button("βž• Create New Document"):
878
- try:
879
- new_doc = json.loads(new_doc_str)
880
- new_doc['id'] = new_id
881
- success, message = insert_record(container, new_doc)
882
- if success:
883
- st.success(f"New document created with id: {new_doc['id']} πŸŽ‰")
884
- st.session_state.selected_document_id = new_doc['id']
885
- #st.rerun()
886
- else:
887
- st.error(message)
888
- except json.JSONDecodeError as e:
889
- st.error(f"Invalid JSON: {str(e)} 🚫")
890
-
891
- else:
892
- st.sidebar.info("No documents found in this container. πŸ“­")
893
-
894
 
895
 
896
  st.subheader(f"πŸ“Š Container: {st.session_state.selected_container}")
 
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':
811
+ # 🧬 Clone Document per record
812
  st.markdown("#### Clone a document:")
 
 
813
  for idx, doc in enumerate(documents_to_display):
814
  st.markdown(f"##### Document ID: {doc.get('id', '')}")
 
 
 
 
815
  if st.button("πŸ“„ Clone Document", key=f'clone_button_{idx}'):
816
+ cloned_doc = doc.copy()
817
+ # Generate a unique ID
818
+ cloned_doc['id'] = generate_unique_id()
819
+ cloned_doc['name'] = generate_unique_id()
820
+ st.session_state.cloned_doc = cloned_doc
821
+ st.session_state.cloned_doc_str = json.dumps(cloned_doc, indent=2)
822
+ st.session_state.clone_mode = True
823
+ st.rerun()
824
+ if st.session_state.get('clone_mode', False):
825
+ st.markdown("#### Edit Cloned Document and Name Your Clone:")
826
+ cloned_doc_str = st.text_area("Cloned Document Content (in JSON format) - Update name please to make it unique before saving!", value=st.session_state.cloned_doc_str, height=300)
827
+ if st.button("πŸ’Ύ Save Cloned Document"):
828
+ try:
829
+ new_doc = json.loads(cloned_doc_str)
830
+ success, message = insert_record(container, new_doc)
831
+ if success:
832
+ st.success(f"Cloned document saved with id: {new_doc['id']} πŸŽ‰")
833
+ st.session_state.selected_document_id = new_doc['id']
834
+ st.session_state.clone_mode = False
835
+ st.session_state.cloned_doc = None
836
+ st.session_state.cloned_doc_str = ''
837
+ st.rerun()
838
+ else:
839
+ st.error(message)
840
+ except json.JSONDecodeError as e:
841
+ st.error(f"Invalid JSON: {str(e)} 🚫")
842
+
 
 
 
 
 
843
  elif selected_view == 'New Record':
844
+ # πŸ†• New Record
 
 
 
 
 
 
845
  st.markdown("#### Create a new document:")
846
+ if st.button("πŸ€– Insert Auto-Generated Record"):
847
+ success, message = save_or_clone_to_cosmos_db(container, query="Auto-generated", response="This is an auto-generated record.")
848
+ if success:
849
+ st.success(message)
850
+ st.rerun()
851
+ else:
852
+ st.error(message)
853
  else:
854
+ new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
855
+ new_doc_str = st.text_area("Document Content (in JSON format)", value='{}', height=300)
856
+ if st.button("βž• Create New Document"):
857
+ try:
858
+ new_doc = json.loads(new_doc_str)
859
+ new_doc['id'] = new_id # Use the provided ID
860
+ success, message = insert_record(container, new_doc)
861
+ if success:
862
+ st.success(f"New document created with id: {new_doc['id']} πŸŽ‰")
863
+ st.session_state.selected_document_id = new_doc['id']
864
+ # Switch to 'Show as Edit and Save' mode
865
+ st.rerun()
866
+ else:
867
+ st.error(message)
868
+ except json.JSONDecodeError as e:
869
+ st.error(f"Invalid JSON: {str(e)} 🚫")
 
 
 
 
 
 
 
870
 
871
 
872
  st.subheader(f"πŸ“Š Container: {st.session_state.selected_container}")