awacke1 commited on
Commit
ffe1240
β€’
1 Parent(s): 4d9099d

Update backup12.app.py

Browse files
Files changed (1) hide show
  1. backup12.app.py +85 -156
backup12.app.py CHANGED
@@ -798,165 +798,94 @@ def main():
798
  display_glossary_entity(term)
799
  search_glossary(term)
800
 
801
-
802
- elif selected_view == 'Show as Run AI - Old':
803
- Label = '#### ✏️ Run AI with wisdom, save with precision'
804
- st.markdown(Label)
805
- num_cols = len(documents_to_display)
806
- cols = st.columns(num_cols)
807
-
808
- for idx, (col, doc) in enumerate(zip(cols, documents_to_display)):
809
- with col:
810
-
811
- # st.markdown(f"##### Document ID: {doc.get('id', '')}")
812
- editable_id = st.text_input("ID", value=doc.get('id', ''), key=f'edit_id_{idx}')
813
- editable_doc = doc.copy()
814
- editable_doc.pop('id', None)
815
-
816
- # st.markdown(f"##### Document Name: {doc.get('name', '')}")
817
- editable_id = st.text_input("Name", value=doc.get('name', ''), key=f'edit_name_{idx}')
818
- editable_doc = doc.copy()
819
- editable_doc.pop('name', None)
820
-
821
- doc_str = st.text_area("Document Content (in JSON format)",
822
- value=json.dumps(editable_doc, indent=2),
823
- height=300,
824
- key=f'doc_str_{idx}')
825
-
826
- # πŸ’ΎπŸ€– Save and AI operations
827
- col_ai, col_delete = st.columns(2)
828
-
829
- with col_ai:
830
- if st.button("πŸ€– Run AI", key=f'run_with_ai_button_{idx}'):
831
-
832
-
833
-
834
-
835
- # Experiment to get content fields πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–
836
- total_docs = len(documents)
837
- doc = documents[st.session_state.current_index]
838
- values_with_space = []
839
- def extract_values2(obj):
840
- if isinstance(obj, dict):
841
- for k, v in obj.items():
842
- extract_values2(v)
843
- elif isinstance(obj, list):
844
- for item in obj:
845
- extract_values2(item)
846
- elif isinstance(obj, str):
847
- if ' ' in obj:
848
- values_with_space.append(obj)
849
- extract_values2(doc)
850
- #st.markdown("#### πŸ”— Links for Extracted Texts")
851
- for term in values_with_space:
852
- display_glossary_entity(term)
853
- search_glossary(term)
854
-
855
- #content = json.dumps(doc, indent=2)
856
- #st.markdown(f"```json\n{content}\n```")
857
- # Experiment to get content fields πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–πŸ€–
858
-
859
-
860
-
861
-
862
- #search_glossary(json.dumps(editable_doc, indent=2))
863
-
864
-
865
-
866
-
867
  elif selected_view == 'Clone Document':
868
- st.markdown("#### Clone a document:")
869
 
870
- for idx, doc in enumerate(documents_to_display):
871
- st.markdown(f"##### Original Document ID: {doc.get('id', '')}")
872
-
873
- if st.button("πŸ“„ Clone Document", key=f'clone_button_{idx}'):
874
- # Generate unique filename using the provided function
875
- unique_filename = gen_AI_IO_filename("Clone", doc.get('name', ''))
876
-
877
- # Create new document with unique ID and name based on timestamp
878
- new_doc = {
879
- 'id': unique_filename,
880
- 'name': f"Clone_{unique_filename[:8]}",
881
- **{k: v for k, v in doc.items() if k not in ['id', 'name', '_rid', '_self', '_etag', '_attachments', '_ts']}
882
- }
883
-
884
- # Show editable preview
885
- edited_doc = st.text_area(
886
- "Edit cloned document:",
887
- value=json.dumps(new_doc, indent=2),
888
- height=300,
889
- key=f'edit_clone_{idx}'
890
- )
891
-
892
- col_save, col_update = st.columns(2)
893
-
894
- with col_save:
895
- if st.button("πŸ’Ύ Save Clone", key=f'save_clone_{idx}'):
896
- try:
897
- final_doc = json.loads(edited_doc)
898
- response = container.create_item(body=final_doc)
899
- if response:
900
- st.success(f"New cloned document created with ID: {final_doc['id']}")
901
- st.rerun()
902
- else:
903
- st.error("Failed to create new document")
904
- except Exception as e:
905
- st.error(f"Error creating document: {str(e)}")
906
-
907
- with col_update:
908
- if st.button("πŸ”„ Update Clone ID/Name", key=f'update_clone_{idx}'):
909
- try:
910
- # Generate new unique filename
911
- new_unique_filename = gen_AI_IO_filename("Clone", doc.get('name', ''))
912
- current_doc = json.loads(edited_doc)
913
- current_doc['id'] = new_unique_filename
914
- current_doc['name'] = f"Clone_{new_unique_filename[:8]}"
915
-
916
- # Update the text area with new values
917
- st.session_state[f'edit_clone_{idx}'] = json.dumps(current_doc, indent=2)
918
- st.rerun()
919
- except Exception as e:
920
- st.error(f"Error updating clone: {str(e)}")
921
-
922
-
923
-
924
-
925
- elif selected_view == 'Clone Document - Old':
926
- st.markdown("#### Clone a document:")
927
 
928
- for idx, doc in enumerate(documents_to_display):
929
- st.markdown(f"##### Original Document ID: {doc.get('id', '')}")
930
-
931
- if st.button("πŸ“„ Clone Document", key=f'clone_button_{idx}'):
932
- # Create new document with unique ID and name
933
- new_doc = {
934
- 'id': str(uuid.uuid4()),
935
- 'name': f"Clone_{str(uuid.uuid4())[:8]}",
936
- **{k: v for k, v in doc.items() if k not in ['id', 'name', '_rid', '_self', '_etag', '_attachments', '_ts']}
937
- }
938
-
939
- # Show editable preview
940
- edited_doc = st.text_area(
941
- "Edit cloned document:",
942
- value=json.dumps(new_doc, indent=2),
943
- height=300,
944
- key=f'edit_clone_{idx}'
945
- )
946
-
947
- if st.button("πŸ’Ύ Save Clone", key=f'save_clone_{idx}'):
948
- try:
949
- final_doc = json.loads(edited_doc)
950
- # Use container.create_item() instead of update_record()
951
- response = container.create_item(body=final_doc)
952
- if response:
953
- st.success(f"New cloned document created with ID: {final_doc['id']}")
954
- st.rerun()
955
- else:
956
- st.error("Failed to create new document")
957
- except Exception as e:
958
- st.error(f"Error creating document: {str(e)}")
959
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
960
 
961
  elif selected_view == 'New Record':
962
  st.markdown("#### Create a new document:")
 
798
  display_glossary_entity(term)
799
  search_glossary(term)
800
 
801
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
802
  elif selected_view == 'Clone Document':
803
+ st.markdown("#### πŸ“„ Clone Document (Save As)")
804
 
805
+ total_docs = len(documents)
806
+ doc = documents[st.session_state.current_index]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
807
 
808
+ # Display current document info
809
+ st.markdown(f"**Original Document ID:** {doc.get('id', '')}")
810
+ st.markdown(f"**Original Document Name:** {doc.get('name', '')}")
811
+
812
+ # Generate new unique ID and name
813
+ unique_filename = gen_AI_IO_filename("Clone", doc.get('name', ''))
814
+ new_id = st.text_input("New Document ID", value=unique_filename, key='new_clone_id')
815
+ new_name = st.text_input("New Document Name", value=f"Clone_{unique_filename[:8]}", key='new_clone_name')
816
+
817
+ # Create new document with all original content except system fields
818
+ new_doc = {
819
+ 'id': new_id,
820
+ 'name': new_name,
821
+ **{k: v for k, v in doc.items() if k not in ['id', 'name', '_rid', '_self', '_etag', '_attachments', '_ts']}
822
+ }
823
+
824
+ # Show editable preview of the new document
825
+ doc_str = st.text_area(
826
+ "Edit Document Content (in JSON format)",
827
+ value=json.dumps(new_doc, indent=2),
828
+ height=300,
829
+ key='clone_preview'
830
+ )
831
+
832
+ col1, col2 = st.columns(2)
833
+
834
+ with col1:
835
+ if st.button("πŸ”„ Generate New ID/Name", key='regenerate_id'):
836
+ # Generate new unique filename
837
+ new_unique_filename = gen_AI_IO_filename("Clone", doc.get('name', ''))
838
+ st.session_state.new_clone_id = new_unique_filename
839
+ st.session_state.new_clone_name = f"Clone_{new_unique_filename[:8]}"
840
+ st.rerun()
841
+
842
+ with col2:
843
+ if st.button("πŸ’Ύ Save As New Document", key='save_clone'):
844
+ try:
845
+ # Parse the edited document content
846
+ final_doc = json.loads(doc_str)
847
+
848
+ # Ensure the new ID and name are used
849
+ final_doc['id'] = new_id
850
+ final_doc['name'] = new_name
851
+
852
+ # Remove any system fields that might have been copied
853
+ system_fields = ['_rid', '_self', '_etag', '_attachments', '_ts']
854
+ for field in system_fields:
855
+ final_doc.pop(field, None)
856
+
857
+ # Create the new document
858
+ response = container.create_item(body=final_doc)
859
+
860
+ if response:
861
+ st.success(f"""
862
+ βœ… New document created successfully!
863
+ - ID: {final_doc['id']}
864
+ - Name: {final_doc['name']}
865
+ """)
866
+ # Update session state to show the new document
867
+ st.session_state.selected_document_id = final_doc['id']
868
+ st.rerun()
869
+ else:
870
+ st.error("Failed to create new document")
871
+ except json.JSONDecodeError as e:
872
+ st.error(f"Invalid JSON format: {str(e)}")
873
+ except Exception as e:
874
+ st.error(f"Error creating document: {str(e)}")
875
+
876
+ # Navigation buttons for viewing other documents to clone
877
+ col_prev, col_next = st.columns([1, 1])
878
+ with col_prev:
879
+ if st.button("⬅️ Previous", key='prev_clone'):
880
+ if st.session_state.current_index > 0:
881
+ st.session_state.current_index -= 1
882
+ st.rerun()
883
+ with col_next:
884
+ if st.button("➑️ Next", key='next_clone'):
885
+ if st.session_state.current_index < total_docs - 1:
886
+ st.session_state.current_index += 1
887
+ st.rerun()
888
+
889
 
890
  elif selected_view == 'New Record':
891
  st.markdown("#### Create a new document:")