Spaces:
Build error
Build error
binhnase04854
commited on
Commit
•
93771d4
1
Parent(s):
eb313df
Init commit
Browse files- .gitignore +1 -0
- app.py +66 -0
- requirements.txt +3 -0
- sample_1.jpeg +0 -0
- sample_2.jpg +0 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
/.idea/
|
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from transformers import DonutProcessor, VisionEncoderDecoderModel
|
6 |
+
|
7 |
+
processor = DonutProcessor.from_pretrained("binhnase04854/donut-invoice-docvqa")
|
8 |
+
model = VisionEncoderDecoderModel.from_pretrained("binhnase04854/donut-invoice-docvqa")
|
9 |
+
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
model.to(device)
|
12 |
+
|
13 |
+
|
14 |
+
def process_document(image, question):
|
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 |
+
prompt = task_prompt.replace("{user_input}", question)
|
21 |
+
decoder_input_ids = processor.tokenizer(prompt, add_special_tokens=False, return_tensors="pt").input_ids
|
22 |
+
|
23 |
+
# generate answer
|
24 |
+
outputs = model.generate(
|
25 |
+
pixel_values.to(device),
|
26 |
+
decoder_input_ids=decoder_input_ids.to(device),
|
27 |
+
max_length=model.decoder.config.max_position_embeddings,
|
28 |
+
early_stopping=True,
|
29 |
+
pad_token_id=processor.tokenizer.pad_token_id,
|
30 |
+
eos_token_id=processor.tokenizer.eos_token_id,
|
31 |
+
use_cache=True,
|
32 |
+
num_beams=1,
|
33 |
+
bad_words_ids=[[processor.tokenizer.unk_token_id]],
|
34 |
+
return_dict_in_generate=True,
|
35 |
+
)
|
36 |
+
|
37 |
+
# postprocess
|
38 |
+
sequence = processor.batch_decode(outputs.sequences)[0]
|
39 |
+
sequence = sequence.replace(processor.tokenizer.eos_token, "").replace(processor.tokenizer.pad_token, "")
|
40 |
+
sequence = re.sub("<[^>]*>", "", sequence, count=1).strip() # remove first task start token
|
41 |
+
|
42 |
+
return processor.token2json(sequence)
|
43 |
+
|
44 |
+
|
45 |
+
description = "Gradio Demo for Donut, an instance of `VisionEncoderDecoderModel` fine-tuned on DocVQA (document visual question answering). To use it, simply upload your image and type a question and click 'submit', or click one of the examples to load them. Read more at the links below."
|
46 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2111.15664' target='_blank'>Donut: OCR-free Document Understanding Transformer</a> | <a href='https://github.com/clovaai/donut' target='_blank'>Github Repo</a></p>"
|
47 |
+
|
48 |
+
sample_1 = "sample_1.jpeg"
|
49 |
+
sample_2 = "sample_2.jpg"
|
50 |
+
demo = gr.Interface(
|
51 |
+
fn=process_document,
|
52 |
+
inputs=["image", "text"],
|
53 |
+
outputs="json",
|
54 |
+
title="Demo: Donut 🍩 for DocVQA",
|
55 |
+
description=description,
|
56 |
+
article=article,
|
57 |
+
enable_queue=True,
|
58 |
+
examples=[
|
59 |
+
[sample_1, "What is total price?"],
|
60 |
+
[sample_1, "How much is Sale VAT?"],
|
61 |
+
[sample_1, "The bill's printing date?"],
|
62 |
+
[sample_2, "What is total price?"],
|
63 |
+
],
|
64 |
+
cache_examples=False)
|
65 |
+
|
66 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
git+https://github.com/huggingface/transformers.git
|
3 |
+
sentencepiece
|
sample_1.jpeg
ADDED
sample_2.jpg
ADDED