NEXAS commited on
Commit
212478c
Β·
verified Β·
1 Parent(s): 811c7c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -30
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
- # Initialize processors
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("πŸ“‚ Upload a PDF")
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("βœ… File uploaded successfully!")
31
 
32
- with st.spinner("πŸ”„ Processing document..."):
33
  document_processor.process_document(pdf_path)
34
 
35
- st.sidebar.success("βœ… Document processed successfully!")
36
  st.session_state["document_uploaded"] = True
37
- else:
38
- st.session_state["document_uploaded"] = False
39
 
40
- # Divider between sections
 
41
  st.markdown("---")
42
 
43
- # Q&A Section
44
- st.header("πŸ” Ask a Question")
 
 
 
45
 
46
- question = st.text_input("Ask a question:", placeholder="What are the key insights?")
 
47
 
48
- if st.button("πŸ’‘ Get Answer"):
49
- if question:
50
- with st.spinner("🧠 Generating response..."):
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
- st.subheader("πŸ“ Answer:")
60
- st.write(answer.content)
61
- else:
62
- st.warning("⚠️ Please enter a question.")
63
 
64
- st.markdown("---")
65
- st.caption("πŸ€– Powered by ChromaDB + Groq LLM | Built with ❀️ using Streamlit")
 
 
 
 
 
 
 
 
 
 
 
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)