Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,38 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from document_chat import ingest_pdf, process_query_with_memory
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
st.
|
7 |
-
st.
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
st.
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from document_chat import ingest_pdf, process_query_with_memory
|
3 |
+
from langchain.memory import ConversationBufferMemory
|
4 |
+
|
5 |
+
# Configure Streamlit app
|
6 |
+
st.set_page_config(page_title="AI Document Q&A Chatbot", layout="wide")
|
7 |
+
st.title("📄 AI-Powered Document Chatbot")
|
8 |
+
st.write("Upload a document and ask questions!")
|
9 |
+
|
10 |
+
# Upload document
|
11 |
+
uploaded_file = st.file_uploader("Upload a PDF", type=["pdf"])
|
12 |
+
if uploaded_file:
|
13 |
+
file_path = "uploaded_doc.pdf"
|
14 |
+
with open(file_path, "wb") as f:
|
15 |
+
f.write(uploaded_file.getbuffer())
|
16 |
+
|
17 |
+
st.success("File uploaded! Processing...")
|
18 |
+
ingest_pdf(file_path)
|
19 |
+
|
20 |
+
# Initialize memory if not exists
|
21 |
+
if "memory" not in st.session_state:
|
22 |
+
st.session_state["memory"] = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
23 |
+
|
24 |
+
query = st.text_input("Ask a question:")
|
25 |
+
if query:
|
26 |
+
with st.spinner("Thinking..."):
|
27 |
+
response = process_query_with_memory(query, st.session_state["memory"])
|
28 |
+
st.session_state["memory"].save_context({"input": query}, {"output": response})
|
29 |
+
st.write(response)
|
30 |
+
|
31 |
+
# Show chat history
|
32 |
+
if st.session_state["memory"].chat_memory.messages:
|
33 |
+
st.subheader("Chat History")
|
34 |
+
for i in range(0, len(st.session_state["memory"].chat_memory.messages), 2):
|
35 |
+
user_message = st.session_state["memory"].chat_memory.messages[i].content
|
36 |
+
bot_response = st.session_state["memory"].chat_memory.messages[i + 1].content if i + 1 < len(st.session_state["memory"].chat_memory.messages) else "..."
|
37 |
+
st.write(f"**User:** {user_message}")
|
38 |
+
st.write(f"**Bot:** {bot_response}")
|