Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -516,7 +516,17 @@ def preprocess_text(text):
|
|
| 516 |
text = text.strip()
|
| 517 |
return text
|
| 518 |
|
| 519 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
|
| 521 |
|
| 522 |
# π Main function - "All the world's a stage, and all the code merely players" -Shakespeare, probably
|
|
@@ -790,10 +800,9 @@ def main():
|
|
| 790 |
height=300,
|
| 791 |
key=f'doc_str_{idx}')
|
| 792 |
|
| 793 |
-
|
| 794 |
-
|
| 795 |
# πΎπ€ Save and AI operations
|
| 796 |
col_save, col_ai, col_delete = st.columns(3)
|
|
|
|
| 797 |
with col_save:
|
| 798 |
if st.button("πΎ Save Changes", key=f'save_button_{idx}'):
|
| 799 |
try:
|
|
@@ -808,20 +817,20 @@ def main():
|
|
| 808 |
st.error(message)
|
| 809 |
except json.JSONDecodeError as e:
|
| 810 |
st.error(f"Invalid JSON: {str(e)} π«")
|
| 811 |
-
|
| 812 |
with col_ai:
|
| 813 |
if st.button("π€ Run AI", key=f'run_with_ai_button_{idx}'):
|
| 814 |
search_glossary(json.dumps(editable_doc, indent=2))
|
| 815 |
-
|
| 816 |
with col_delete:
|
| 817 |
if st.button("ποΈ Delete Record", key=f'delete_button_{idx}'):
|
| 818 |
-
|
|
|
|
| 819 |
if success:
|
| 820 |
st.success(message)
|
| 821 |
st.rerun() # Refresh the page after deletion
|
| 822 |
else:
|
| 823 |
st.error(message)
|
| 824 |
-
|
| 825 |
|
| 826 |
elif selected_view == 'Clone Document':
|
| 827 |
st.markdown("#### Clone a document:")
|
|
@@ -857,6 +866,8 @@ def main():
|
|
| 857 |
st.error("Failed to create new document")
|
| 858 |
except Exception as e:
|
| 859 |
st.error(f"Error creating document: {str(e)}")
|
|
|
|
|
|
|
| 860 |
elif selected_view == 'New Record':
|
| 861 |
st.markdown("#### Create a new document:")
|
| 862 |
|
|
@@ -912,58 +923,6 @@ def main():
|
|
| 912 |
else:
|
| 913 |
st.info("No documents to display. π§")
|
| 914 |
|
| 915 |
-
elif selected_view == 'New Record2':
|
| 916 |
-
st.markdown("#### Create a new document:")
|
| 917 |
-
|
| 918 |
-
if st.button("π€ Insert Auto-Generated Record"):
|
| 919 |
-
auto_doc = {
|
| 920 |
-
"id": generate_unique_id(),
|
| 921 |
-
"name": f"Auto-generated Record {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
|
| 922 |
-
"content": "This is an auto-generated record.",
|
| 923 |
-
"timestamp": datetime.now().isoformat()
|
| 924 |
-
}
|
| 925 |
-
success, message = save_or_clone_to_cosmos_db(container, document=auto_doc)
|
| 926 |
-
if success:
|
| 927 |
-
st.success(message)
|
| 928 |
-
st.rerun()
|
| 929 |
-
else:
|
| 930 |
-
st.error(message)
|
| 931 |
-
else:
|
| 932 |
-
new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
|
| 933 |
-
default_doc = {
|
| 934 |
-
"id": new_id,
|
| 935 |
-
"name": "New Document",
|
| 936 |
-
"content": "",
|
| 937 |
-
"timestamp": datetime.now().isoformat()
|
| 938 |
-
}
|
| 939 |
-
new_doc_str = st.text_area("Document Content (in JSON format)",
|
| 940 |
-
value=json.dumps(default_doc, indent=2),
|
| 941 |
-
height=300)
|
| 942 |
-
|
| 943 |
-
if st.button("β Create New Document"):
|
| 944 |
-
try:
|
| 945 |
-
new_doc = json.loads(new_doc_str)
|
| 946 |
-
new_doc['id'] = new_id # Ensure ID matches input field
|
| 947 |
-
success, message = insert_record(container, new_doc)
|
| 948 |
-
if success:
|
| 949 |
-
st.success(f"New document created with id: {new_doc['id']} π")
|
| 950 |
-
st.session_state.selected_document_id = new_doc['id']
|
| 951 |
-
st.rerun()
|
| 952 |
-
else:
|
| 953 |
-
st.error(message)
|
| 954 |
-
except json.JSONDecodeError as e:
|
| 955 |
-
st.error(f"Invalid JSON: {str(e)} π«")
|
| 956 |
-
|
| 957 |
-
|
| 958 |
-
st.subheader(f"π Container: {st.session_state.selected_container}")
|
| 959 |
-
if st.session_state.selected_container:
|
| 960 |
-
if documents_to_display:
|
| 961 |
-
Label = '# π Data display - Data tells tales that words cannot'
|
| 962 |
-
st.markdown(Label)
|
| 963 |
-
df = pd.DataFrame(documents_to_display)
|
| 964 |
-
st.dataframe(df)
|
| 965 |
-
else:
|
| 966 |
-
st.info("No documents to display. π§")
|
| 967 |
|
| 968 |
|
| 969 |
|
|
|
|
| 516 |
text = text.strip()
|
| 517 |
return text
|
| 518 |
|
| 519 |
+
# ποΈ Delete record - Saying goodbye to data (with just the id)
|
| 520 |
+
def delete_record(container, id):
|
| 521 |
+
try:
|
| 522 |
+
container.delete_item(item=id, partition_key=id)
|
| 523 |
+
return True, f"Successfully deleted record with id: {id} ποΈ"
|
| 524 |
+
except exceptions.CosmosResourceNotFoundError:
|
| 525 |
+
return False, f"Record with id {id} not found. It may have been already deleted. π΅οΈββοΈ"
|
| 526 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 527 |
+
return False, f"HTTP error occurred: {str(e)} π¨"
|
| 528 |
+
except Exception as e:
|
| 529 |
+
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
| 530 |
|
| 531 |
|
| 532 |
# π Main function - "All the world's a stage, and all the code merely players" -Shakespeare, probably
|
|
|
|
| 800 |
height=300,
|
| 801 |
key=f'doc_str_{idx}')
|
| 802 |
|
|
|
|
|
|
|
| 803 |
# πΎπ€ Save and AI operations
|
| 804 |
col_save, col_ai, col_delete = st.columns(3)
|
| 805 |
+
|
| 806 |
with col_save:
|
| 807 |
if st.button("πΎ Save Changes", key=f'save_button_{idx}'):
|
| 808 |
try:
|
|
|
|
| 817 |
st.error(message)
|
| 818 |
except json.JSONDecodeError as e:
|
| 819 |
st.error(f"Invalid JSON: {str(e)} π«")
|
| 820 |
+
|
| 821 |
with col_ai:
|
| 822 |
if st.button("π€ Run AI", key=f'run_with_ai_button_{idx}'):
|
| 823 |
search_glossary(json.dumps(editable_doc, indent=2))
|
| 824 |
+
|
| 825 |
with col_delete:
|
| 826 |
if st.button("ποΈ Delete Record", key=f'delete_button_{idx}'):
|
| 827 |
+
# Use only the 'id' to delete the record
|
| 828 |
+
success, message = delete_record(container, id=doc.get('id', ''))
|
| 829 |
if success:
|
| 830 |
st.success(message)
|
| 831 |
st.rerun() # Refresh the page after deletion
|
| 832 |
else:
|
| 833 |
st.error(message)
|
|
|
|
| 834 |
|
| 835 |
elif selected_view == 'Clone Document':
|
| 836 |
st.markdown("#### Clone a document:")
|
|
|
|
| 866 |
st.error("Failed to create new document")
|
| 867 |
except Exception as e:
|
| 868 |
st.error(f"Error creating document: {str(e)}")
|
| 869 |
+
|
| 870 |
+
|
| 871 |
elif selected_view == 'New Record':
|
| 872 |
st.markdown("#### Create a new document:")
|
| 873 |
|
|
|
|
| 923 |
else:
|
| 924 |
st.info("No documents to display. π§")
|
| 925 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 926 |
|
| 927 |
|
| 928 |
|