ignaciaginting commited on
Commit
fe0246c
·
verified ·
1 Parent(s): 8bc3f30
Files changed (1) hide show
  1. app.py +38 -22
app.py CHANGED
@@ -1,24 +1,40 @@
 
1
  from transformers import pipeline
 
 
 
2
 
3
- nlp = pipeline(
4
- "document-question-answering",
5
- model="impira/layoutlm-document-qa",
6
- )
7
-
8
- nlp(
9
- "https://templates.invoicehome.com/invoice-template-us-neat-750px.png",
10
- "What is the invoice number?"
11
- )
12
- # {'score': 0.9943977, 'answer': 'us-001', 'start': 15, 'end': 15}
13
-
14
- nlp(
15
- "https://miro.medium.com/max/787/1*iECQRIiOGTmEFLdWkVIH2g.jpeg",
16
- "What is the purchase amount?"
17
- )
18
- # {'score': 0.9912159, 'answer': '$1,000,000,000', 'start': 97, 'end': 97}
19
-
20
- nlp(
21
- "https://www.accountingcoach.com/wp-content/uploads/2013/10/[email protected]",
22
- "What are the 2020 net sales?"
23
- )
24
- # {'score': 0.59147286, 'answer': '$ 3,750', 'start': 19, 'end': 20}
 
 
 
 
 
 
 
 
 
 
 
 
 
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})")