Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,116 +1,116 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from typing import Dict, Any, List
|
| 3 |
-
import tempfile
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
from graph.workflow import LangGraphWorkflow
|
| 7 |
-
from utils.document_loader import DocumentLoader
|
| 8 |
-
from models.vector_store import VectorStore
|
| 9 |
-
|
| 10 |
-
from dotenv import load_dotenv
|
| 11 |
-
|
| 12 |
-
load_dotenv()
|
| 13 |
-
|
| 14 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 15 |
-
OPENWEATHERMAP_API_KEY = os.getenv("OPENWEATHERMAP_API_KEY")
|
| 16 |
-
LANGSMITH_TRACING= True
|
| 17 |
-
LANGSMITH_ENDPOINT= os.getenv("LANGSMITH_ENDPOINT")
|
| 18 |
-
LANGSMITH_API_KEY= os.getenv("LANGSMITH_API_KEY")
|
| 19 |
-
LANGSMITH_PROJECT= os.getenv("LANGSMITH_PROJECT")
|
| 20 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 21 |
-
db_url = os.getenv("db_url")
|
| 22 |
-
db_api = os.getenv("db_api")
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
def main():
|
| 26 |
-
st.title("
|
| 27 |
-
|
| 28 |
-
# Initialize components
|
| 29 |
-
doc_loader = DocumentLoader()
|
| 30 |
-
vector_store = VectorStore()
|
| 31 |
-
workflow = LangGraphWorkflow()
|
| 32 |
-
|
| 33 |
-
# Sidebar - Document Upload
|
| 34 |
-
st.sidebar.header("Upload Documents")
|
| 35 |
-
uploaded_file = st.sidebar.file_uploader("Upload a PDF document", type="pdf")
|
| 36 |
-
|
| 37 |
-
if uploaded_file:
|
| 38 |
-
with st.spinner("Processing document..."):
|
| 39 |
-
# Save the uploaded file
|
| 40 |
-
pdf_path = doc_loader.save_uploaded_pdf(uploaded_file)
|
| 41 |
-
|
| 42 |
-
if pdf_path:
|
| 43 |
-
# Load and process the document
|
| 44 |
-
documents = doc_loader.load_pdf(pdf_path)
|
| 45 |
-
|
| 46 |
-
if documents:
|
| 47 |
-
# Add documents to vector store
|
| 48 |
-
success = vector_store.add_documents(documents)
|
| 49 |
-
|
| 50 |
-
if success:
|
| 51 |
-
st.sidebar.success(f"Document '{uploaded_file.name}' processed and indexed successfully!")
|
| 52 |
-
else:
|
| 53 |
-
st.sidebar.error("Failed to index the document.")
|
| 54 |
-
else:
|
| 55 |
-
st.sidebar.error("Failed to process the document.")
|
| 56 |
-
|
| 57 |
-
# Available documents
|
| 58 |
-
st.sidebar.header("Available Documents")
|
| 59 |
-
documents = doc_loader.get_available_documents()
|
| 60 |
-
if documents:
|
| 61 |
-
st.sidebar.write(", ".join(documents))
|
| 62 |
-
else:
|
| 63 |
-
st.sidebar.write("No documents available")
|
| 64 |
-
|
| 65 |
-
# Chat interface
|
| 66 |
-
st.header("Chat Interface")
|
| 67 |
-
|
| 68 |
-
# Initialize chat history
|
| 69 |
-
if "messages" not in st.session_state:
|
| 70 |
-
st.session_state.messages = []
|
| 71 |
-
|
| 72 |
-
# Display chat history
|
| 73 |
-
for message in st.session_state.messages:
|
| 74 |
-
with st.chat_message(message["role"]):
|
| 75 |
-
st.write(message["content"])
|
| 76 |
-
|
| 77 |
-
# User input
|
| 78 |
-
user_query = st.chat_input("Ask about weather or document information")
|
| 79 |
-
|
| 80 |
-
if user_query:
|
| 81 |
-
# Add user message to chat history
|
| 82 |
-
st.session_state.messages.append({"role": "user", "content": user_query})
|
| 83 |
-
|
| 84 |
-
# Display user message
|
| 85 |
-
with st.chat_message("user"):
|
| 86 |
-
st.write(user_query)
|
| 87 |
-
|
| 88 |
-
# Process query
|
| 89 |
-
with st.spinner("Thinking..."):
|
| 90 |
-
result = workflow.invoke(user_query)
|
| 91 |
-
|
| 92 |
-
# Add assistant message to chat history
|
| 93 |
-
st.session_state.messages.append({"role": "assistant", "content": result["response"]})
|
| 94 |
-
|
| 95 |
-
# Display assistant message
|
| 96 |
-
with st.chat_message("assistant"):
|
| 97 |
-
st.write(result["response"])
|
| 98 |
-
|
| 99 |
-
# Additional debug info in expander
|
| 100 |
-
with st.expander("Debug Information"):
|
| 101 |
-
st.write(f"Action: {result['action']}")
|
| 102 |
-
|
| 103 |
-
if result['action'] == 'weather' and result['city']:
|
| 104 |
-
st.write(f"City: {result['city']}")
|
| 105 |
-
|
| 106 |
-
if result['action'] == 'document' and result['context']:
|
| 107 |
-
st.write("Retrieved Context:")
|
| 108 |
-
for i, ctx in enumerate(result['context']):
|
| 109 |
-
st.write(f"Document {i+1}:")
|
| 110 |
-
st.write(ctx['page_content'])
|
| 111 |
-
|
| 112 |
-
st.write("Evaluation Metrics:")
|
| 113 |
-
st.write(result['evaluation'])
|
| 114 |
-
|
| 115 |
-
if __name__ == "__main__":
|
| 116 |
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
from graph.workflow import LangGraphWorkflow
|
| 7 |
+
from utils.document_loader import DocumentLoader
|
| 8 |
+
from models.vector_store import VectorStore
|
| 9 |
+
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 15 |
+
OPENWEATHERMAP_API_KEY = os.getenv("OPENWEATHERMAP_API_KEY")
|
| 16 |
+
LANGSMITH_TRACING= True
|
| 17 |
+
LANGSMITH_ENDPOINT= os.getenv("LANGSMITH_ENDPOINT")
|
| 18 |
+
LANGSMITH_API_KEY= os.getenv("LANGSMITH_API_KEY")
|
| 19 |
+
LANGSMITH_PROJECT= os.getenv("LANGSMITH_PROJECT")
|
| 20 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 21 |
+
db_url = os.getenv("db_url")
|
| 22 |
+
db_api = os.getenv("db_api")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def main():
|
| 26 |
+
st.title("Doc weather Bot")
|
| 27 |
+
|
| 28 |
+
# Initialize components
|
| 29 |
+
doc_loader = DocumentLoader()
|
| 30 |
+
vector_store = VectorStore()
|
| 31 |
+
workflow = LangGraphWorkflow()
|
| 32 |
+
|
| 33 |
+
# Sidebar - Document Upload
|
| 34 |
+
st.sidebar.header("Upload Documents")
|
| 35 |
+
uploaded_file = st.sidebar.file_uploader("Upload a PDF document", type="pdf")
|
| 36 |
+
|
| 37 |
+
if uploaded_file:
|
| 38 |
+
with st.spinner("Processing document..."):
|
| 39 |
+
# Save the uploaded file
|
| 40 |
+
pdf_path = doc_loader.save_uploaded_pdf(uploaded_file)
|
| 41 |
+
|
| 42 |
+
if pdf_path:
|
| 43 |
+
# Load and process the document
|
| 44 |
+
documents = doc_loader.load_pdf(pdf_path)
|
| 45 |
+
|
| 46 |
+
if documents:
|
| 47 |
+
# Add documents to vector store
|
| 48 |
+
success = vector_store.add_documents(documents)
|
| 49 |
+
|
| 50 |
+
if success:
|
| 51 |
+
st.sidebar.success(f"Document '{uploaded_file.name}' processed and indexed successfully!")
|
| 52 |
+
else:
|
| 53 |
+
st.sidebar.error("Failed to index the document.")
|
| 54 |
+
else:
|
| 55 |
+
st.sidebar.error("Failed to process the document.")
|
| 56 |
+
|
| 57 |
+
# Available documents
|
| 58 |
+
st.sidebar.header("Available Documents")
|
| 59 |
+
documents = doc_loader.get_available_documents()
|
| 60 |
+
if documents:
|
| 61 |
+
st.sidebar.write(", ".join(documents))
|
| 62 |
+
else:
|
| 63 |
+
st.sidebar.write("No documents available")
|
| 64 |
+
|
| 65 |
+
# Chat interface
|
| 66 |
+
st.header("Chat Interface")
|
| 67 |
+
|
| 68 |
+
# Initialize chat history
|
| 69 |
+
if "messages" not in st.session_state:
|
| 70 |
+
st.session_state.messages = []
|
| 71 |
+
|
| 72 |
+
# Display chat history
|
| 73 |
+
for message in st.session_state.messages:
|
| 74 |
+
with st.chat_message(message["role"]):
|
| 75 |
+
st.write(message["content"])
|
| 76 |
+
|
| 77 |
+
# User input
|
| 78 |
+
user_query = st.chat_input("Ask about weather or document information")
|
| 79 |
+
|
| 80 |
+
if user_query:
|
| 81 |
+
# Add user message to chat history
|
| 82 |
+
st.session_state.messages.append({"role": "user", "content": user_query})
|
| 83 |
+
|
| 84 |
+
# Display user message
|
| 85 |
+
with st.chat_message("user"):
|
| 86 |
+
st.write(user_query)
|
| 87 |
+
|
| 88 |
+
# Process query
|
| 89 |
+
with st.spinner("Thinking..."):
|
| 90 |
+
result = workflow.invoke(user_query)
|
| 91 |
+
|
| 92 |
+
# Add assistant message to chat history
|
| 93 |
+
st.session_state.messages.append({"role": "assistant", "content": result["response"]})
|
| 94 |
+
|
| 95 |
+
# Display assistant message
|
| 96 |
+
with st.chat_message("assistant"):
|
| 97 |
+
st.write(result["response"])
|
| 98 |
+
|
| 99 |
+
# Additional debug info in expander
|
| 100 |
+
with st.expander("Debug Information"):
|
| 101 |
+
st.write(f"Action: {result['action']}")
|
| 102 |
+
|
| 103 |
+
if result['action'] == 'weather' and result['city']:
|
| 104 |
+
st.write(f"City: {result['city']}")
|
| 105 |
+
|
| 106 |
+
if result['action'] == 'document' and result['context']:
|
| 107 |
+
st.write("Retrieved Context:")
|
| 108 |
+
for i, ctx in enumerate(result['context']):
|
| 109 |
+
st.write(f"Document {i+1}:")
|
| 110 |
+
st.write(ctx['page_content'])
|
| 111 |
+
|
| 112 |
+
st.write("Evaluation Metrics:")
|
| 113 |
+
st.write(result['evaluation'])
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
main()
|