Spaces:
Runtime error
Runtime error
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from PIL import Image
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
import torch
|
6 |
+
from transformers import DonutProcessor, VisionEncoderDecoderModel
|
7 |
+
|
8 |
+
processor = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa")
|
9 |
+
model = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-docvqa")
|
10 |
+
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
model.to(device)
|
13 |
+
|
14 |
+
def process_document(image):
|
15 |
+
# prepare encoder inputs
|
16 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values
|
17 |
+
|
18 |
+
# prepare decoder inputs
|
19 |
+
task_prompt = "<s_docvqa><s_question>{user_input}</s_question><s_answer>"
|
20 |
+
question = "When is the coffee break?"
|
21 |
+
prompt = task_prompt.replace("{user_input}", question)
|
22 |
+
decoder_input_ids = processor.tokenizer(prompt, add_special_tokens=False, return_tensors="pt").input_ids
|
23 |
+
|
24 |
+
# generate answer
|
25 |
+
outputs = model.generate(
|
26 |
+
pixel_values.to(device),
|
27 |
+
decoder_input_ids=decoder_input_ids.to(device),
|
28 |
+
max_length=model.decoder.config.max_position_embeddings,
|
29 |
+
early_stopping=True,
|
30 |
+
pad_token_id=processor.tokenizer.pad_token_id,
|
31 |
+
eos_token_id=processor.tokenizer.eos_token_id,
|
32 |
+
use_cache=True,
|
33 |
+
num_beams=1,
|
34 |
+
bad_words_ids=[[processor.tokenizer.unk_token_id]],
|
35 |
+
return_dict_in_generate=True,
|
36 |
+
)
|
37 |
+
|
38 |
+
# postprocess
|
39 |
+
sequence = processor.batch_decode(outputs.sequences)[0]
|
40 |
+
sequence = sequence.replace(processor.tokenizer.eos_token, "").replace(processor.tokenizer.pad_token, "")
|
41 |
+
sequence = re.sub(r"<.*?>", "", sequence, count=1).strip() # remove first task start token
|
42 |
+
|
43 |
+
return processor.token2json(sequence)
|
44 |
+
|
45 |
+
image = Image.open("./example_1.png")
|
46 |
+
image.save("example_1.png")
|
47 |
+
|
48 |
+
demo = gr.Interface(
|
49 |
+
fn=process_document,
|
50 |
+
inputs= gr.inputs.Image(type="pil"),
|
51 |
+
outputs="json",
|
52 |
+
title=f"Interactive demo: Donut 🍩 for DocVQA",
|
53 |
+
description="""This model is fine-tuned on the DocVQA dataset. <br>
|
54 |
+
Documentation: https://huggingface.co/docs/transformers/main/en/model_doc/donut
|
55 |
+
Notebooks: https://github.com/NielsRogge/Transformers-Tutorials/tree/master/Donut
|
56 |
+
|
57 |
+
More details are available at:
|
58 |
+
- Paper: https://arxiv.org/abs/2111.15664
|
59 |
+
- Original repository: https://github.com/clovaai/donut""",
|
60 |
+
examples=[["example_1.png"]],
|
61 |
+
cache_examples=False,
|
62 |
+
)
|
63 |
+
demo.launch()
|