Update app.py
Browse files
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
|
| 6 |
-
|
| 7 |
-
st.
|
| 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 |
-
|
|
|
|
| 14 |
f.write(uploaded_file.read())
|
| 15 |
-
st.success("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
st.success("Vectorstore ready.")
|
| 21 |
-
|
| 22 |
-
query = st.text_input("Enter your question")
|
| 23 |
if st.button("Ask") and query:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 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)}")
|