Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,7 +52,6 @@ import re
|
|
| 52 |
# Set up logging to suppress Streamlit warnings about experimental functions
|
| 53 |
logging.getLogger('streamlit').setLevel(logging.ERROR)
|
| 54 |
|
| 55 |
-
st.session_state["documents"] = {}
|
| 56 |
if "chat_history" not in st.session_state:
|
| 57 |
st.session_state["chat_history"] = []
|
| 58 |
if "documents" not in st.session_state:
|
|
@@ -720,7 +719,7 @@ def side():
|
|
| 720 |
if "documents" not in st.session_state:
|
| 721 |
try:
|
| 722 |
docs = db.child("users").child(st.session_state["wix_user_id"]).child("KnowledgeBase").get().val()
|
| 723 |
-
st.write("Fetched documents:", docs) # Debugging output
|
| 724 |
|
| 725 |
st.session_state["documents"] = docs if docs else {}
|
| 726 |
except Exception as e:
|
|
@@ -743,24 +742,43 @@ def side():
|
|
| 743 |
disabled=True
|
| 744 |
)
|
| 745 |
|
|
|
|
| 746 |
# File uploader
|
| 747 |
uploaded_file = st.file_uploader("", type=["pdf", "docx", "txt"], key="file_uploader", label_visibility="collapsed")
|
| 748 |
if st.sidebar.button("Upload", key="upload_button"):
|
| 749 |
if uploaded_file:
|
| 750 |
try:
|
| 751 |
-
content, _= upload_to_firebase(st.session_state["wix_user_id"], uploaded_file)
|
| 752 |
if content:
|
| 753 |
doc_id = str(uuid.uuid4())
|
|
|
|
|
|
|
| 754 |
db.child("users").child(st.session_state["wix_user_id"]).child("KnowledgeBase").child(doc_id).set(
|
| 755 |
{"content": content, "name": uploaded_file.name}
|
| 756 |
)
|
|
|
|
|
|
|
| 757 |
if "documents" not in st.session_state:
|
| 758 |
st.session_state["documents"] = {}
|
| 759 |
st.session_state["documents"][doc_id] = {"content": content, "name": uploaded_file.name}
|
| 760 |
-
|
| 761 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 762 |
st.sidebar.success(f"Document '{uploaded_file.name}' uploaded successfully!")
|
| 763 |
-
st.rerun()
|
| 764 |
else:
|
| 765 |
st.sidebar.warning("File upload failed.")
|
| 766 |
except Exception as e:
|
|
@@ -768,6 +786,7 @@ def side():
|
|
| 768 |
else:
|
| 769 |
st.sidebar.warning("Please select a file to upload.")
|
| 770 |
|
|
|
|
| 771 |
# Delete Button
|
| 772 |
if st.session_state["documents"]:
|
| 773 |
selected_doc_to_delete = st.selectbox(
|
|
@@ -780,8 +799,23 @@ def side():
|
|
| 780 |
try:
|
| 781 |
db.child("users").child(st.session_state["wix_user_id"]).child("KnowledgeBase").child(selected_doc_to_delete).remove()
|
| 782 |
st.session_state["documents"].pop(selected_doc_to_delete, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 783 |
st.sidebar.success("Document deleted successfully!")
|
| 784 |
-
st.rerun()
|
| 785 |
except Exception as e:
|
| 786 |
st.sidebar.error(f"Error deleting document: {e}")
|
| 787 |
|
|
|
|
| 52 |
# Set up logging to suppress Streamlit warnings about experimental functions
|
| 53 |
logging.getLogger('streamlit').setLevel(logging.ERROR)
|
| 54 |
|
|
|
|
| 55 |
if "chat_history" not in st.session_state:
|
| 56 |
st.session_state["chat_history"] = []
|
| 57 |
if "documents" not in st.session_state:
|
|
|
|
| 719 |
if "documents" not in st.session_state:
|
| 720 |
try:
|
| 721 |
docs = db.child("users").child(st.session_state["wix_user_id"]).child("KnowledgeBase").get().val()
|
| 722 |
+
st.sidebar.write("Fetched documents:", docs) # Debugging output
|
| 723 |
|
| 724 |
st.session_state["documents"] = docs if docs else {}
|
| 725 |
except Exception as e:
|
|
|
|
| 742 |
disabled=True
|
| 743 |
)
|
| 744 |
|
| 745 |
+
# File uploader
|
| 746 |
# File uploader
|
| 747 |
uploaded_file = st.file_uploader("", type=["pdf", "docx", "txt"], key="file_uploader", label_visibility="collapsed")
|
| 748 |
if st.sidebar.button("Upload", key="upload_button"):
|
| 749 |
if uploaded_file:
|
| 750 |
try:
|
| 751 |
+
content, _ = upload_to_firebase(st.session_state["wix_user_id"], uploaded_file)
|
| 752 |
if content:
|
| 753 |
doc_id = str(uuid.uuid4())
|
| 754 |
+
|
| 755 |
+
# Save to Firebase
|
| 756 |
db.child("users").child(st.session_state["wix_user_id"]).child("KnowledgeBase").child(doc_id).set(
|
| 757 |
{"content": content, "name": uploaded_file.name}
|
| 758 |
)
|
| 759 |
+
|
| 760 |
+
# Update session state
|
| 761 |
if "documents" not in st.session_state:
|
| 762 |
st.session_state["documents"] = {}
|
| 763 |
st.session_state["documents"][doc_id] = {"content": content, "name": uploaded_file.name}
|
| 764 |
+
|
| 765 |
+
# Refresh the text area dynamically
|
| 766 |
+
saved_docs = "\n".join(
|
| 767 |
+
[
|
| 768 |
+
f"{doc_data.get('content', f'Document {doc_id[:8]}')}"
|
| 769 |
+
for doc_id, doc_data in st.session_state["documents"].items()
|
| 770 |
+
]
|
| 771 |
+
)
|
| 772 |
+
st.sidebar.text_area(
|
| 773 |
+
label="",
|
| 774 |
+
value=saved_docs,
|
| 775 |
+
height=150,
|
| 776 |
+
key="saved_documents_text_area",
|
| 777 |
+
disabled=True
|
| 778 |
+
)
|
| 779 |
+
|
| 780 |
+
# Show success message without rerunning the app
|
| 781 |
st.sidebar.success(f"Document '{uploaded_file.name}' uploaded successfully!")
|
|
|
|
| 782 |
else:
|
| 783 |
st.sidebar.warning("File upload failed.")
|
| 784 |
except Exception as e:
|
|
|
|
| 786 |
else:
|
| 787 |
st.sidebar.warning("Please select a file to upload.")
|
| 788 |
|
| 789 |
+
|
| 790 |
# Delete Button
|
| 791 |
if st.session_state["documents"]:
|
| 792 |
selected_doc_to_delete = st.selectbox(
|
|
|
|
| 799 |
try:
|
| 800 |
db.child("users").child(st.session_state["wix_user_id"]).child("KnowledgeBase").child(selected_doc_to_delete).remove()
|
| 801 |
st.session_state["documents"].pop(selected_doc_to_delete, None)
|
| 802 |
+
|
| 803 |
+
# Update the text area dynamically after deletion
|
| 804 |
+
saved_docs = "\n".join(
|
| 805 |
+
[
|
| 806 |
+
f"{doc_data.get('content', f'Document {doc_id[:8]}')}"
|
| 807 |
+
for doc_id, doc_data in st.session_state["documents"].items()
|
| 808 |
+
]
|
| 809 |
+
)
|
| 810 |
+
st.sidebar.text_area(
|
| 811 |
+
label="",
|
| 812 |
+
value=saved_docs,
|
| 813 |
+
height=150,
|
| 814 |
+
key="saved_documents_text_area",
|
| 815 |
+
disabled=True
|
| 816 |
+
)
|
| 817 |
+
|
| 818 |
st.sidebar.success("Document deleted successfully!")
|
|
|
|
| 819 |
except Exception as e:
|
| 820 |
st.sidebar.error(f"Error deleting document: {e}")
|
| 821 |
|