ignaciaginting commited on
Commit
e7ce40a
·
verified ·
1 Parent(s): e22af09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -11,34 +11,45 @@ def load_model():
11
 
12
  qa_pipeline = load_model()
13
 
14
- st.title("📄 Document Question Answering App")
15
- st.write("Upload a PDF file, enter a question, and get answers from the document.")
16
 
17
- # Upload PDF
18
- pdf_file = st.file_uploader("Upload PDF", type=["pdf"])
19
 
20
  # Ask a question
21
  question = st.text_input("Ask a question about the document:")
22
 
23
- if pdf_file and question:
24
- # Convert first page of PDF to image using PyMuPDF
25
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
26
- tmp_file.write(pdf_file.read())
27
- pdf_path = tmp_file.name
 
28
 
29
- doc = fitz.open(pdf_path)
30
- page = doc.load_page(0) # just first page for now
31
- pix = page.get_pixmap(dpi=150)
32
- img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
 
33
 
34
- # Show the rendered page
35
- st.image(img, caption="Page 1 of PDF")
 
 
36
 
37
  # Run the pipeline
38
  with st.spinner("Searching for the answer..."):
39
- result = qa_pipeline(img, question)
40
- if result:
41
- top_answer = result[0] # get the highest-scoring answer
 
42
  st.success(f"**Answer:** {top_answer['answer']} (score: {top_answer['score']:.2f})")
 
 
 
 
 
 
43
  else:
44
- st.warning("No answer found.")
 
11
 
12
  qa_pipeline = load_model()
13
 
14
+ st.title("\ud83d\udcc4 Document Question Answering App")
15
+ st.write("Upload a PDF or Image file, enter a question, and get answers from the document.")
16
 
17
+ # Upload PDF or image
18
+ uploaded_file = st.file_uploader("Upload PDF or Image", type=["pdf", "png", "jpg", "jpeg"])
19
 
20
  # Ask a question
21
  question = st.text_input("Ask a question about the document:")
22
 
23
+ if uploaded_file and question:
24
+ # Handle PDF file
25
+ if uploaded_file.type == "application/pdf":
26
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
27
+ tmp_file.write(uploaded_file.read())
28
+ pdf_path = tmp_file.name
29
 
30
+ doc = fitz.open(pdf_path)
31
+ page = doc.load_page(0) # just first page for now
32
+ pix = page.get_pixmap(dpi=150)
33
+ img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
34
+ st.image(img, caption="Page 1 of PDF")
35
 
36
+ # Handle image file
37
+ else:
38
+ img = Image.open(uploaded_file)
39
+ st.image(img, caption="Uploaded Image")
40
 
41
  # Run the pipeline
42
  with st.spinner("Searching for the answer..."):
43
+ results = qa_pipeline(img, question)
44
+
45
+ if results:
46
+ top_answer = results[0] # get the highest-scoring answer
47
  st.success(f"**Answer:** {top_answer['answer']} (score: {top_answer['score']:.2f})")
48
+
49
+ # Show top 3 options if available
50
+ if len(results) > 1:
51
+ st.markdown("\n**Other possible answers:**")
52
+ for idx, ans in enumerate(results[1:3], start=2):
53
+ st.markdown(f"- Option {idx}: {ans['answer']} (score: {ans['score']:.2f})")
54
  else:
55
+ st.warning("No answer found.")