samim2024 commited on
Commit
6867b0c
·
verified ·
1 Parent(s): 9dd1247

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -17
app.py CHANGED
@@ -1,27 +1,44 @@
1
  import streamlit as st
2
- from model import load_vectorstore, ask_question
3
  import os
 
4
 
5
- st.set_page_config(page_title="RAG with Mistral AI", layout="centered")
6
-
7
- st.title("RAG Q&A App with Mistral AI")
8
- st.write("Upload a document and ask questions about it.")
9
 
 
10
  uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
 
11
 
12
  if uploaded_file:
13
- with open("document.pdf", "wb") as f:
 
14
  f.write(uploaded_file.read())
15
- st.success("File uploaded!")
 
 
 
 
 
 
 
16
 
17
- if st.button("Index Document"):
18
- with st.spinner("Processing..."):
19
- load_vectorstore("document.pdf")
20
- st.success("Vectorstore ready.")
21
-
22
- query = st.text_input("Enter your question")
23
  if st.button("Ask") and query:
24
- with st.spinner("Generating answer..."):
25
- answer = ask_question(query)
26
- st.write("**Answer:**", answer)
27
-
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
 
2
  import os
3
+ from model import load_vectorstore, ask_question
4
 
5
+ st.set_page_config(page_title="Simple RAG Q&A", layout="centered")
6
+ st.title("RAG Q&A with Mistral AI")
7
+ st.write("Upload a PDF and ask questions about its content.")
 
8
 
9
+ # PDF upload
10
  uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
11
+ pdf_path = "/app/data/document.pdf"
12
 
13
  if uploaded_file:
14
+ os.makedirs("/app/data", exist_ok=True)
15
+ with open(pdf_path, "wb") as f:
16
  f.write(uploaded_file.read())
17
+ st.success("PDF uploaded!")
18
+
19
+ with st.spinner("Indexing document..."):
20
+ try:
21
+ load_vectorstore(pdf_path)
22
+ st.success("Document indexed!")
23
+ except Exception as e:
24
+ st.error(f"Indexing failed: {str(e)}")
25
 
26
+ # Query input
27
+ query = st.text_input("Enter your question",
28
+ "How many articles are there in the Selenium webdriver python course?")
 
 
 
29
  if st.button("Ask") and query:
30
+ if not os.path.exists(pdf_path):
31
+ st.error("Please upload a PDF first.")
32
+ else:
33
+ with st.spinner("Generating answer..."):
34
+ try:
35
+ result = ask_question(query, pdf_path)
36
+ st.subheader("Answer")
37
+ st.write(result["answer"])
38
+
39
+ st.subheader("Retrieved Contexts")
40
+ for i, context in enumerate(result["contexts"], 1):
41
+ with st.expander(f"Context {i}"):
42
+ st.write(context)
43
+ except Exception as e:
44
+ st.error(f"Failed to generate answer: {str(e)}")