Updates
Browse files
app.py
CHANGED
|
@@ -1,24 +1,40 @@
|
|
|
|
|
| 1 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
)
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import tempfile
|
| 5 |
+
import fitz # PyMuPDF
|
| 6 |
|
| 7 |
+
# Load the model
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_model():
|
| 10 |
+
return pipeline("document-question-answering", model="impira/layoutlm-document-qa")
|
| 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 |
+
st.success(f"**Answer:** {result['answer']} (score: {result['score']:.2f})")
|