Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,37 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import json
|
|
|
|
|
|
|
|
|
|
| 4 |
from utils.ingestion import DocumentProcessor
|
| 5 |
from utils.llm import LLMProcessor
|
| 6 |
from utils.qa import QAEngine
|
| 7 |
|
|
|
|
| 8 |
st.set_page_config(page_title="AI-Powered Document QA", layout="wide")
|
| 9 |
-
st.title("π AI-Powered Document QA")
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
document_processor = DocumentProcessor()
|
| 13 |
llm_processor = LLMProcessor()
|
| 14 |
qa_engine = QAEngine()
|
|
@@ -17,49 +40,59 @@ qa_engine = QAEngine()
|
|
| 17 |
os.makedirs("temp", exist_ok=True)
|
| 18 |
|
| 19 |
# Sidebar for file upload
|
| 20 |
-
st.sidebar.header("
|
| 21 |
uploaded_file = st.sidebar.file_uploader("Choose a PDF file", type=["pdf"])
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Document upload & processing
|
| 24 |
-
if uploaded_file:
|
| 25 |
pdf_path = os.path.join("temp", uploaded_file.name)
|
| 26 |
-
|
| 27 |
with open(pdf_path, "wb") as f:
|
| 28 |
f.write(uploaded_file.read())
|
| 29 |
|
| 30 |
-
st.sidebar.success("
|
| 31 |
|
| 32 |
-
with st.spinner("
|
| 33 |
document_processor.process_document(pdf_path)
|
| 34 |
|
| 35 |
-
st.sidebar.success("
|
| 36 |
st.session_state["document_uploaded"] = True
|
| 37 |
-
else:
|
| 38 |
-
st.session_state["document_uploaded"] = False
|
| 39 |
|
| 40 |
-
#
|
|
|
|
| 41 |
st.markdown("---")
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
if
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
if st.session_state["document_uploaded"]:
|
| 52 |
-
# Use document-based QA if a file is uploaded
|
| 53 |
-
answer = qa_engine.query(question)
|
| 54 |
-
else:
|
| 55 |
-
# Use AI-based response if no document is uploaded
|
| 56 |
-
answer = llm_processor.generate_answer("", question)
|
| 57 |
-
st.warning("β οΈ No document uploaded. This response is generated from general AI knowledge and may not be document-specific.")
|
| 58 |
|
| 59 |
-
|
| 60 |
-
st.
|
| 61 |
-
else:
|
| 62 |
-
st.warning("β οΈ Please enter a question.")
|
| 63 |
|
| 64 |
-
st.
|
| 65 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
+
import base64
|
| 5 |
+
from langchain.memory import ConversationBufferWindowMemory
|
| 6 |
+
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
| 7 |
from utils.ingestion import DocumentProcessor
|
| 8 |
from utils.llm import LLMProcessor
|
| 9 |
from utils.qa import QAEngine
|
| 10 |
|
| 11 |
+
# Configure Streamlit page
|
| 12 |
st.set_page_config(page_title="AI-Powered Document QA", layout="wide")
|
|
|
|
| 13 |
|
| 14 |
+
# Background Image
|
| 15 |
+
def add_bg_from_local(image_file):
|
| 16 |
+
with open(image_file, "rb") as image_file:
|
| 17 |
+
encoded_string = base64.b64encode(image_file.read())
|
| 18 |
+
st.markdown(
|
| 19 |
+
f"""
|
| 20 |
+
<style>
|
| 21 |
+
.stApp {{
|
| 22 |
+
background-image: url(data:image/png;base64,{encoded_string.decode()});
|
| 23 |
+
background-size: cover;
|
| 24 |
+
}}
|
| 25 |
+
</style>
|
| 26 |
+
""",
|
| 27 |
+
unsafe_allow_html=True,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Path to background image
|
| 31 |
+
image_bg = "./image/background.jpeg" # Change this path accordingly
|
| 32 |
+
add_bg_from_local(image_bg)
|
| 33 |
+
|
| 34 |
+
# Initialize document processing & AI components
|
| 35 |
document_processor = DocumentProcessor()
|
| 36 |
llm_processor = LLMProcessor()
|
| 37 |
qa_engine = QAEngine()
|
|
|
|
| 40 |
os.makedirs("temp", exist_ok=True)
|
| 41 |
|
| 42 |
# Sidebar for file upload
|
| 43 |
+
st.sidebar.header("Upload a PDF")
|
| 44 |
uploaded_file = st.sidebar.file_uploader("Choose a PDF file", type=["pdf"])
|
| 45 |
|
| 46 |
+
# Initialize chat memory
|
| 47 |
+
memory_storage = StreamlitChatMessageHistory(key="chat_messages")
|
| 48 |
+
memory = ConversationBufferWindowMemory(
|
| 49 |
+
memory_key="chat_history", human_prefix="User", chat_memory=memory_storage, k=5
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
# Document upload & processing
|
| 53 |
+
if uploaded_file and "document_uploaded" not in st.session_state:
|
| 54 |
pdf_path = os.path.join("temp", uploaded_file.name)
|
| 55 |
+
|
| 56 |
with open(pdf_path, "wb") as f:
|
| 57 |
f.write(uploaded_file.read())
|
| 58 |
|
| 59 |
+
st.sidebar.success("File uploaded successfully!")
|
| 60 |
|
| 61 |
+
with st.spinner("Processing document..."):
|
| 62 |
document_processor.process_document(pdf_path)
|
| 63 |
|
| 64 |
+
st.sidebar.success("Document processed successfully!")
|
| 65 |
st.session_state["document_uploaded"] = True
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# Chat interface layout
|
| 68 |
+
st.markdown("<h2 style='text-align: center;'>AI Chat Assistant</h2>", unsafe_allow_html=True)
|
| 69 |
st.markdown("---")
|
| 70 |
|
| 71 |
+
# Display chat history
|
| 72 |
+
for message in memory_storage.messages:
|
| 73 |
+
role = "user" if message.type == "human" else "assistant"
|
| 74 |
+
with st.chat_message(role):
|
| 75 |
+
st.markdown(message.content)
|
| 76 |
|
| 77 |
+
# User input at the bottom
|
| 78 |
+
user_input = st.chat_input("Ask me anything...")
|
| 79 |
|
| 80 |
+
if user_input:
|
| 81 |
+
# Store user message in memory
|
| 82 |
+
memory_storage.add_user_message(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
with st.chat_message("user"):
|
| 85 |
+
st.markdown(user_input)
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
with st.spinner("Generating response..."):
|
| 88 |
+
if st.session_state.get("document_uploaded", False):
|
| 89 |
+
answer = qa_engine.query(user_input)
|
| 90 |
+
else:
|
| 91 |
+
answer = llm_processor.generate_answer("", user_input)
|
| 92 |
+
st.warning("No document uploaded. This response is generated from general AI knowledge and may not be document-specific.")
|
| 93 |
+
|
| 94 |
+
# Store AI response in memory
|
| 95 |
+
memory_storage.add_ai_message(answer)
|
| 96 |
+
|
| 97 |
+
with st.chat_message("assistant"):
|
| 98 |
+
st.markdown(answer)
|