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