samim2024 commited on
Commit
890ba78
·
verified ·
1 Parent(s): 2cfdb3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -1,5 +1,6 @@
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")
@@ -7,21 +8,21 @@ 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",
@@ -41,4 +42,14 @@ if st.button("Ask") and query:
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)}")
 
 
 
 
 
 
 
 
 
 
 
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")
 
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",
 
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)})