NEXAS commited on
Commit
3f6512f
Β·
verified Β·
1 Parent(s): db1aee6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -11
app.py CHANGED
@@ -16,12 +16,14 @@ qa_engine = QAEngine()
16
  # Ensure temp directory exists
17
  os.makedirs("temp", exist_ok=True)
18
 
19
- st.sidebar.header("Upload a PDF")
 
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
- # Query input
36
- question = st.text_input("Ask a question from the document:", placeholder="What are the key insights?")
37
 
38
- if st.button("πŸ” Search & Answer"):
39
- if question:
40
- with st.spinner("🧠 Searching for relevant context..."):
 
 
41
  answer = qa_engine.query(question)
 
 
 
 
42
 
43
- st.subheader("πŸ“ Answer:")
44
- st.write(answer.content)
45
- else:
46
- st.warning("⚠️ Please enter a question.")
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")