Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -16,12 +16,14 @@ qa_engine = QAEngine()
|
|
16 |
# Ensure temp directory exists
|
17 |
os.makedirs("temp", exist_ok=True)
|
18 |
|
19 |
-
|
|
|
20 |
uploaded_file = st.sidebar.file_uploader("Choose a PDF file", type=["pdf"])
|
21 |
|
|
|
22 |
if uploaded_file:
|
23 |
pdf_path = os.path.join("temp", uploaded_file.name)
|
24 |
-
|
25 |
with open(pdf_path, "wb") as f:
|
26 |
f.write(uploaded_file.read())
|
27 |
|
@@ -31,19 +33,33 @@ if uploaded_file:
|
|
31 |
document_processor.process_document(pdf_path)
|
32 |
|
33 |
st.sidebar.success("β
Document processed successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
question = st.text_input("Ask a question from the document:", placeholder="What are the key insights?")
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
answer = qa_engine.query(question)
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
st.markdown("---")
|
49 |
st.caption("π€ Powered by ChromaDB + Groq LLM | Built with β€οΈ using Streamlit")
|
|
|
16 |
# Ensure temp directory exists
|
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 |
|
|
|
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)
|
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")
|