Update app.py
Browse files
app.py
CHANGED
@@ -11,34 +11,45 @@ def load_model():
|
|
11 |
|
12 |
qa_pipeline = load_model()
|
13 |
|
14 |
-
st.title("
|
15 |
-
st.write("Upload a PDF file, enter a question, and get answers from the document.")
|
16 |
|
17 |
-
# Upload PDF
|
18 |
-
|
19 |
|
20 |
# Ask a question
|
21 |
question = st.text_input("Ask a question about the document:")
|
22 |
|
23 |
-
if
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
|
34 |
-
#
|
35 |
-
|
|
|
|
|
36 |
|
37 |
# Run the pipeline
|
38 |
with st.spinner("Searching for the answer..."):
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
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.")
|