ignaciaginting commited on
Commit
755dd12
·
verified ·
1 Parent(s): add4161

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import torch
4
+ from pdf2image import convert_from_bytes
5
+ from PIL import Image
6
+
7
+ @st.cache_resource
8
+ def load_model():
9
+ return pipeline(
10
+ task="document-question-answering",
11
+ model="naver-clova-ix/donut-base-finetuned-docvqa",
12
+ device=0 if torch.cuda.is_available() else -1,
13
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
14
+ )
15
+
16
+ qa_pipeline = load_model()
17
+
18
+ st.set_page_config(page_title="Donut PDF QA", layout="centered")
19
+ st.title("📄 Donut: PDF Question Answering")
20
+
21
+ uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
22
+ question = st.text_input("Ask a question about the document")
23
+
24
+ if uploaded_file and question:
25
+ st.write("Reading and converting PDF...")
26
+ images = convert_from_bytes(uploaded_file.read(), dpi=200)
27
+
28
+ page_number = st.number_input("Select page", min_value=1, max_value=len(images), value=1, step=1)
29
+ page_image = images[page_number - 1]
30
+ st.image(page_image, caption=f"Page {page_number}")
31
+
32
+ with st.spinner("Finding answer..."):
33
+ result = qa_pipeline(image=page_image, question=question)
34
+ st.success("Answer:")
35
+ st.write(result[0]['answer'])