Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -736,9 +736,70 @@ def main():
|
|
| 736 |
st.rerun()
|
| 737 |
except Exception as e:
|
| 738 |
st.error(f"Error deleting document: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 739 |
|
| 740 |
|
| 741 |
-
elif selected_view == 'Show as Run AI':
|
| 742 |
Label = '#### ✏️ Run AI with wisdom, save with precision'
|
| 743 |
st.markdown(Label)
|
| 744 |
num_cols = len(documents_to_display)
|
|
@@ -799,9 +860,69 @@ def main():
|
|
| 799 |
|
| 800 |
|
| 801 |
#search_glossary(json.dumps(editable_doc, indent=2))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 802 |
|
| 803 |
|
| 804 |
-
elif selected_view == 'Clone Document':
|
| 805 |
st.markdown("#### Clone a document:")
|
| 806 |
|
| 807 |
for idx, doc in enumerate(documents_to_display):
|
|
|
|
| 736 |
st.rerun()
|
| 737 |
except Exception as e:
|
| 738 |
st.error(f"Error deleting document: {str(e)}")
|
| 739 |
+
|
| 740 |
+
|
| 741 |
+
elif selected_view == 'Show as Run AI':
|
| 742 |
+
Label = '#### ✏️ Run AI with wisdom, save with precision'
|
| 743 |
+
st.markdown(Label)
|
| 744 |
+
num_cols = len(documents_to_display)
|
| 745 |
+
cols = st.columns(num_cols)
|
| 746 |
+
|
| 747 |
+
for idx, (col, doc) in enumerate(zip(cols, documents_to_display)):
|
| 748 |
+
with col:
|
| 749 |
+
# ID and Name fields
|
| 750 |
+
editable_id = st.text_input("ID", value=doc.get('id', ''), key=f'edit_id_{idx}')
|
| 751 |
+
editable_name = st.text_input("Name", value=doc.get('name', ''), key=f'edit_name_{idx}')
|
| 752 |
+
|
| 753 |
+
# Create editable document copy without id and name
|
| 754 |
+
editable_doc = doc.copy()
|
| 755 |
+
editable_doc.pop('id', None)
|
| 756 |
+
editable_doc.pop('name', None)
|
| 757 |
+
|
| 758 |
+
doc_str = st.text_area("Document Content (in JSON format)",
|
| 759 |
+
value=json.dumps(editable_doc, indent=2),
|
| 760 |
+
height=300,
|
| 761 |
+
key=f'doc_str_{idx}')
|
| 762 |
+
|
| 763 |
+
# Save and AI operations columns
|
| 764 |
+
col_save, col_ai, col_delete = st.columns(3)
|
| 765 |
+
|
| 766 |
+
with col_save:
|
| 767 |
+
if st.button("💾 Save Changes", key=f'save_runai_{idx}'):
|
| 768 |
+
try:
|
| 769 |
+
updated_doc = json.loads(doc_str)
|
| 770 |
+
# Reinsert ID and name from editable fields
|
| 771 |
+
updated_doc['id'] = editable_id
|
| 772 |
+
updated_doc['name'] = editable_name
|
| 773 |
+
response = container.upsert_item(body=updated_doc)
|
| 774 |
+
if response:
|
| 775 |
+
st.success(f"Document {updated_doc['id']} saved successfully.")
|
| 776 |
+
st.session_state.selected_document_id = updated_doc['id']
|
| 777 |
+
st.rerun()
|
| 778 |
+
except Exception as e:
|
| 779 |
+
st.error(f"Error saving document: {str(e)}")
|
| 780 |
+
|
| 781 |
+
with col_ai:
|
| 782 |
+
if st.button("🤖 Run AI", key=f'run_with_ai_button_{idx}'):
|
| 783 |
+
# Your existing AI processing code here
|
| 784 |
+
values_with_space = []
|
| 785 |
+
def extract_values2(obj):
|
| 786 |
+
if isinstance(obj, dict):
|
| 787 |
+
for k, v in obj.items():
|
| 788 |
+
extract_values2(v)
|
| 789 |
+
elif isinstance(obj, list):
|
| 790 |
+
for item in obj:
|
| 791 |
+
extract_values2(item)
|
| 792 |
+
elif isinstance(obj, str):
|
| 793 |
+
if ' ' in obj:
|
| 794 |
+
values_with_space.append(obj)
|
| 795 |
+
|
| 796 |
+
extract_values2(doc)
|
| 797 |
+
for term in values_with_space:
|
| 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)
|
|
|
|
| 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):
|