Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,18 @@ import uuid
|
|
| 4 |
import streamlit as st
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from qdrant_client import models
|
| 7 |
-
|
| 8 |
from utils import setup_openai_embeddings, setup_qdrant_client, delete_collection, is_document_embedded
|
| 9 |
from embed import embed_documents_into_qdrant
|
| 10 |
from preprocess import split_documents, update_metadata, load_documents_OCR
|
| 11 |
-
from retrieve import
|
| 12 |
from summarize import summarize_documents
|
| 13 |
|
| 14 |
# Load environment variables
|
| 15 |
load_dotenv()
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
def main():
|
| 18 |
st.sidebar.title("PDF Management")
|
| 19 |
uploaded_files = st.sidebar.file_uploader("Upload PDF files", type=["pdf"], accept_multiple_files=True)
|
|
@@ -26,6 +28,8 @@ def main():
|
|
| 26 |
st.session_state['data_bank_answer'] = None
|
| 27 |
if 'uploaded_docs_answer' not in st.session_state:
|
| 28 |
st.session_state['uploaded_docs_answer'] = None
|
|
|
|
|
|
|
| 29 |
|
| 30 |
if uploaded_files:
|
| 31 |
if st.sidebar.button("Add Docs to Data Bank"):
|
|
@@ -38,8 +42,9 @@ def main():
|
|
| 38 |
|
| 39 |
pages = {
|
| 40 |
"Lex Document Summarization": page_summarization,
|
| 41 |
-
"Chat with
|
| 42 |
-
"Chat with Uploaded Docs": page_chat_with_uploaded_docs
|
|
|
|
| 43 |
}
|
| 44 |
|
| 45 |
st.sidebar.title("Page Navigation")
|
|
@@ -86,7 +91,7 @@ def page_summarization(uploaded_files):
|
|
| 86 |
|
| 87 |
def page_qna(uploaded_files):
|
| 88 |
"""Page for Q&A functionality."""
|
| 89 |
-
st.title("Chat with
|
| 90 |
user_query = st.text_area("Enter your question here:", height=300)
|
| 91 |
if st.button('Get Answer'):
|
| 92 |
if user_query:
|
|
@@ -121,6 +126,21 @@ def page_chat_with_uploaded_docs(uploaded_files):
|
|
| 121 |
st.session_state['uploaded_collection_name'] = None
|
| 122 |
st.success(f"Deleted collection {collection_name}")
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
def embed_documents_to_data_bank(files_info):
|
| 125 |
"""Function to embed documents into the data bank."""
|
| 126 |
for temp_path, original_name in files_info:
|
|
@@ -172,7 +192,7 @@ def add_docs_to_current_chat(files_info):
|
|
| 172 |
def handle_query(query):
|
| 173 |
"""Retrieve answers based on the query."""
|
| 174 |
try:
|
| 175 |
-
answer =
|
| 176 |
return answer or "No relevant answer found."
|
| 177 |
except Exception as e:
|
| 178 |
return f"Error processing the query: {str(e)}"
|
|
@@ -185,6 +205,14 @@ def handle_uploaded_docs_query(query, collection_name):
|
|
| 185 |
except Exception as e:
|
| 186 |
return f"Error processing the query: {str(e)}"
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
def delete_collection(collection_name, qdrant_url, qdrant_api_key):
|
| 189 |
"""Delete a Qdrant collection."""
|
| 190 |
client = setup_qdrant_client(qdrant_url, qdrant_api_key)
|
|
@@ -194,4 +222,4 @@ def delete_collection(collection_name, qdrant_url, qdrant_api_key):
|
|
| 194 |
print("Failed to delete collection:", e)
|
| 195 |
|
| 196 |
if __name__ == "__main__":
|
| 197 |
-
main()
|
|
|
|
| 4 |
import streamlit as st
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from qdrant_client import models
|
|
|
|
| 7 |
from utils import setup_openai_embeddings, setup_qdrant_client, delete_collection, is_document_embedded
|
| 8 |
from embed import embed_documents_into_qdrant
|
| 9 |
from preprocess import split_documents, update_metadata, load_documents_OCR
|
| 10 |
+
from retrieve import retrieve_documents_from_collection
|
| 11 |
from summarize import summarize_documents
|
| 12 |
|
| 13 |
# Load environment variables
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
def main():
|
| 20 |
st.sidebar.title("PDF Management")
|
| 21 |
uploaded_files = st.sidebar.file_uploader("Upload PDF files", type=["pdf"], accept_multiple_files=True)
|
|
|
|
| 28 |
st.session_state['data_bank_answer'] = None
|
| 29 |
if 'uploaded_docs_answer' not in st.session_state:
|
| 30 |
st.session_state['uploaded_docs_answer'] = None
|
| 31 |
+
if 'voo_answer' not in st.session_state:
|
| 32 |
+
st.session_state['voo_answer'] = None
|
| 33 |
|
| 34 |
if uploaded_files:
|
| 35 |
if st.sidebar.button("Add Docs to Data Bank"):
|
|
|
|
| 42 |
|
| 43 |
pages = {
|
| 44 |
"Lex Document Summarization": page_summarization,
|
| 45 |
+
"Chat with Data Bank": page_qna,
|
| 46 |
+
"Chat with Uploaded Docs": page_chat_with_uploaded_docs,
|
| 47 |
+
"Chat with VOO": page_chat_with_voo
|
| 48 |
}
|
| 49 |
|
| 50 |
st.sidebar.title("Page Navigation")
|
|
|
|
| 91 |
|
| 92 |
def page_qna(uploaded_files):
|
| 93 |
"""Page for Q&A functionality."""
|
| 94 |
+
st.title("Chat with Data Bank")
|
| 95 |
user_query = st.text_area("Enter your question here:", height=300)
|
| 96 |
if st.button('Get Answer'):
|
| 97 |
if user_query:
|
|
|
|
| 126 |
st.session_state['uploaded_collection_name'] = None
|
| 127 |
st.success(f"Deleted collection {collection_name}")
|
| 128 |
|
| 129 |
+
def page_chat_with_voo(uploaded_files):
|
| 130 |
+
"""Page for chatting with VOO documents."""
|
| 131 |
+
st.title("Chat with VOO")
|
| 132 |
+
user_query = st.text_area("Enter your question here:", height=300)
|
| 133 |
+
if st.button('Get Answer'):
|
| 134 |
+
if user_query:
|
| 135 |
+
answer = handle_voo_query(user_query)
|
| 136 |
+
st.session_state['voo_answer'] = answer # Store the answer in session state
|
| 137 |
+
st.write(answer)
|
| 138 |
+
else:
|
| 139 |
+
st.error("Please enter a question to get an answer.")
|
| 140 |
+
# Display stored answer if it exists
|
| 141 |
+
if st.session_state['voo_answer']:
|
| 142 |
+
st.write(st.session_state['voo_answer'])
|
| 143 |
+
|
| 144 |
def embed_documents_to_data_bank(files_info):
|
| 145 |
"""Function to embed documents into the data bank."""
|
| 146 |
for temp_path, original_name in files_info:
|
|
|
|
| 192 |
def handle_query(query):
|
| 193 |
"""Retrieve answers based on the query."""
|
| 194 |
try:
|
| 195 |
+
answer = retrieve_documents_from_collection(query, os.getenv('OPENAI_API_KEY'), os.getenv('QDRANT_URL'), os.getenv('QDRANT_API_KEY'),'Lex-v1')
|
| 196 |
return answer or "No relevant answer found."
|
| 197 |
except Exception as e:
|
| 198 |
return f"Error processing the query: {str(e)}"
|
|
|
|
| 205 |
except Exception as e:
|
| 206 |
return f"Error processing the query: {str(e)}"
|
| 207 |
|
| 208 |
+
def handle_voo_query(query):
|
| 209 |
+
"""Retrieve answers from the VOO collection."""
|
| 210 |
+
try:
|
| 211 |
+
answer = retrieve_documents_from_collection(query, os.getenv('OPENAI_API_KEY'), os.getenv('QDRANT_URL'), os.getenv('QDRANT_API_KEY'), 'Lex-v2')
|
| 212 |
+
return answer or "No relevant answer found."
|
| 213 |
+
except Exception as e:
|
| 214 |
+
return f"Error processing the query: {str(e)}"
|
| 215 |
+
|
| 216 |
def delete_collection(collection_name, qdrant_url, qdrant_api_key):
|
| 217 |
"""Delete a Qdrant collection."""
|
| 218 |
client = setup_qdrant_client(qdrant_url, qdrant_api_key)
|
|
|
|
| 222 |
print("Failed to delete collection:", e)
|
| 223 |
|
| 224 |
if __name__ == "__main__":
|
| 225 |
+
main()
|