Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -806,78 +806,60 @@ 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 Document2':
|
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 |
-
|
818 |
-
# Generate a unique ID
|
819 |
-
st.code(cloned_doc, )
|
820 |
-
cloned_doc['id'] = generate_unique_id()
|
821 |
-
cloned_doc['name'] = generate_unique_id()
|
822 |
-
cloned_doc_str = st.text_area("Cloned Document Content (in JSON format) - Update name please to make it unique before saving!", value=cloned_doc, height=300)
|
823 |
-
|
824 |
-
st.markdown("#### Edit Cloned Document and Name Your Clone:")
|
825 |
-
if st.button("๐พ Save Cloned Document"):
|
826 |
-
success, message = insert_record(container, cloned_doc)
|
827 |
-
if success:
|
828 |
-
st.success(f"Cloned document saved with id: {new_doc['id']} ๐")
|
829 |
-
st.session_state.selected_document_id = new_doc['id']
|
830 |
-
st.session_state.clone_mode = False
|
831 |
-
st.session_state.cloned_doc = None
|
832 |
-
st.session_state.cloned_doc_str = ''
|
833 |
-
#st.rerun()
|
834 |
-
else:
|
835 |
-
st.error(message)
|
836 |
|
837 |
elif selected_view == 'Clone Document':
|
838 |
-
# ๐งฌ Clone Document per record
|
839 |
st.markdown("#### Clone a document:")
|
840 |
|
841 |
for idx, doc in enumerate(documents_to_display):
|
842 |
st.markdown(f"##### Document ID: {doc.get('id', '')}")
|
843 |
|
844 |
if st.button("๐ Clone Document", key=f'clone_button_{idx}'):
|
|
|
845 |
cloned_doc = doc.copy()
|
846 |
|
847 |
-
# Generate new unique IDs
|
848 |
-
|
849 |
-
|
850 |
|
851 |
-
#
|
852 |
-
|
853 |
-
|
854 |
-
|
855 |
-
|
856 |
-
|
857 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
858 |
|
859 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
860 |
|
861 |
-
# Allow user to save the cloned document
|
862 |
if st.button("๐พ Save Cloned Document"):
|
863 |
-
# Convert the string back to dictionary (if edited)
|
864 |
try:
|
865 |
-
|
866 |
-
|
867 |
-
success, message = insert_record(container, cloned_doc)
|
868 |
|
869 |
if success:
|
870 |
-
st.success(f"Cloned document saved with
|
871 |
-
st.session_state.selected_document_id = cloned_doc['id']
|
872 |
-
st.session_state.clone_mode = False
|
873 |
-
st.session_state.cloned_doc = None
|
874 |
-
st.session_state.cloned_doc_str = ''
|
875 |
st.rerun()
|
876 |
else:
|
877 |
-
st.error(message)
|
878 |
except json.JSONDecodeError as e:
|
879 |
-
st.error(f"
|
880 |
-
|
881 |
|
882 |
|
883 |
elif selected_view == 'New Record':
|
|
|
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 a document:")
|
812 |
|
813 |
for idx, doc in enumerate(documents_to_display):
|
814 |
st.markdown(f"##### Document ID: {doc.get('id', '')}")
|
815 |
|
816 |
if st.button("๐ Clone Document", key=f'clone_button_{idx}'):
|
817 |
+
# Create copy of original document
|
818 |
cloned_doc = doc.copy()
|
819 |
|
820 |
+
# Generate new unique IDs
|
821 |
+
new_id = generate_unique_id()
|
822 |
+
new_name = generate_unique_id()
|
823 |
|
824 |
+
# Update ID and name fields at root level
|
825 |
+
cloned_doc['id'] = new_id
|
826 |
+
if 'name' in cloned_doc:
|
827 |
+
cloned_doc['name'] = new_name
|
828 |
+
|
829 |
+
# Update nested ID and name fields if they exist
|
830 |
+
def update_nested_ids(obj):
|
831 |
+
if isinstance(obj, dict):
|
832 |
+
if 'id' in obj:
|
833 |
+
obj['id'] = new_id
|
834 |
+
if 'name' in obj:
|
835 |
+
obj['name'] = new_name
|
836 |
+
for value in obj.values():
|
837 |
+
update_nested_ids(value)
|
838 |
+
elif isinstance(obj, list):
|
839 |
+
for item in obj:
|
840 |
+
update_nested_ids(item)
|
841 |
|
842 |
+
update_nested_ids(cloned_doc)
|
843 |
+
|
844 |
+
# Display editable JSON
|
845 |
+
edited_doc_str = st.text_area(
|
846 |
+
"Edit cloned document before saving:",
|
847 |
+
value=json.dumps(cloned_doc, indent=2),
|
848 |
+
height=300
|
849 |
+
)
|
850 |
|
|
|
851 |
if st.button("๐พ Save Cloned Document"):
|
|
|
852 |
try:
|
853 |
+
final_doc = json.loads(edited_doc_str)
|
854 |
+
success, message = insert_record(container, final_doc)
|
|
|
855 |
|
856 |
if success:
|
857 |
+
st.success(f"Cloned document saved successfully with ID: {final_doc['id']}")
|
|
|
|
|
|
|
|
|
858 |
st.rerun()
|
859 |
else:
|
860 |
+
st.error(f"Failed to save clone: {message}")
|
861 |
except json.JSONDecodeError as e:
|
862 |
+
st.error(f"Invalid JSON format: {str(e)}")
|
|
|
863 |
|
864 |
|
865 |
elif selected_view == 'New Record':
|