Create sidebar.py
Browse files- sidebar.py +44 -0
sidebar.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from api_utils import upload_document, list_documents, delete_document
|
| 3 |
+
|
| 4 |
+
def display_sidebar():
|
| 5 |
+
# Sidebar: Model Selection
|
| 6 |
+
model_options = ["gpt-4o", "gpt-4o-mini"]
|
| 7 |
+
st.sidebar.selectbox("Select Model", options=model_options, key="model")
|
| 8 |
+
|
| 9 |
+
# Sidebar: Upload Document
|
| 10 |
+
st.sidebar.header("Upload Document")
|
| 11 |
+
uploaded_file = st.sidebar.file_uploader("Choose a file", type=["pdf", "docx", "html"])
|
| 12 |
+
if uploaded_file is not None:
|
| 13 |
+
if st.sidebar.button("Upload"):
|
| 14 |
+
with st.spinner("Uploading..."):
|
| 15 |
+
upload_response = upload_document(uploaded_file)
|
| 16 |
+
if upload_response:
|
| 17 |
+
st.sidebar.success(f"File '{uploaded_file.name}' uploaded successfully with ID {upload_response['file_id']}.")
|
| 18 |
+
st.session_state.documents = list_documents() # Refresh the list after upload
|
| 19 |
+
|
| 20 |
+
# Sidebar: List Documents
|
| 21 |
+
st.sidebar.header("Uploaded Documents")
|
| 22 |
+
if st.sidebar.button("Refresh Document List"):
|
| 23 |
+
with st.spinner("Refreshing..."):
|
| 24 |
+
st.session_state.documents = list_documents()
|
| 25 |
+
|
| 26 |
+
# Initialize document list if not present
|
| 27 |
+
if "documents" not in st.session_state:
|
| 28 |
+
st.session_state.documents = list_documents()
|
| 29 |
+
|
| 30 |
+
documents = st.session_state.documents
|
| 31 |
+
if documents:
|
| 32 |
+
for doc in documents:
|
| 33 |
+
st.sidebar.text(f"{doc['filename']} (ID: {doc['id']}, Uploaded: {doc['upload_timestamp']})")
|
| 34 |
+
|
| 35 |
+
# Delete Document
|
| 36 |
+
selected_file_id = st.sidebar.selectbox("Select a document to delete", options=[doc['id'] for doc in documents], format_func=lambda x: next(doc['filename'] for doc in documents if doc['id'] == x))
|
| 37 |
+
if st.sidebar.button("Delete Selected Document"):
|
| 38 |
+
with st.spinner("Deleting..."):
|
| 39 |
+
delete_response = delete_document(selected_file_id)
|
| 40 |
+
if delete_response:
|
| 41 |
+
st.sidebar.success(f"Document with ID {selected_file_id} deleted successfully.")
|
| 42 |
+
st.session_state.documents = list_documents() # Refresh the list after deletion
|
| 43 |
+
else:
|
| 44 |
+
st.sidebar.error(f"Failed to delete document with ID {selected_file_id}.")
|