ivelin commited on
Commit
f0a0a4a
1 Parent(s): 8efac01

feat: initial version

Browse files

Signed-off-by: ivelin <ivelin.eth@gmail.com>

Files changed (3) hide show
  1. app.py +65 -0
  2. packages.txt +2 -0
  3. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import gradio as gr
3
+
4
+ import torch
5
+ from transformers import DonutProcessor, VisionEncoderDecoderModel
6
+
7
+ pretrained_repo_name = "ivelin/donut-refexp-draft"
8
+
9
+ processor = DonutProcessor.from_pretrained(pretrained_repo_name)
10
+ model = VisionEncoderDecoderModel.from_pretrained(pretrained_repo_name)
11
+
12
+ device = "cuda" if torch.cuda.is_available() else "cpu"
13
+ model.to(device)
14
+
15
+
16
+ def process_document(image, question):
17
+ # prepare encoder inputs
18
+ pixel_values = processor(image, return_tensors="pt").pixel_values
19
+
20
+ # prepare decoder inputs
21
+ task_prompt = "<s_refexp><s_prompt>{user_input}</s_prompt><s_refexp>"
22
+ prompt = task_prompt.replace("{user_input}", question)
23
+ decoder_input_ids = processor.tokenizer(
24
+ prompt, add_special_tokens=False, return_tensors="pt").input_ids
25
+
26
+ # generate answer
27
+ outputs = model.generate(
28
+ pixel_values.to(device),
29
+ decoder_input_ids=decoder_input_ids.to(device),
30
+ max_length=model.decoder.config.max_position_embeddings,
31
+ early_stopping=True,
32
+ pad_token_id=processor.tokenizer.pad_token_id,
33
+ eos_token_id=processor.tokenizer.eos_token_id,
34
+ use_cache=True,
35
+ num_beams=1,
36
+ bad_words_ids=[[processor.tokenizer.unk_token_id]],
37
+ return_dict_in_generate=True,
38
+ )
39
+
40
+ # postprocess
41
+ sequence = processor.batch_decode(outputs.sequences)[0]
42
+ sequence = sequence.replace(processor.tokenizer.eos_token, "").replace(
43
+ processor.tokenizer.pad_token, "")
44
+ # remove first task start token
45
+ sequence = re.sub(r"<.*?>", "", sequence, count=1).strip()
46
+
47
+ return processor.token2json(sequence)
48
+
49
+
50
+ description = "Gradio Demo for Donut RefExp task, an instance of `VisionEncoderDecoderModel` fine-tuned on UIBert RefExp Dataset (UI Referring Expression). 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."
51
+ 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>"
52
+
53
+ demo = gr.Interface(
54
+ fn=process_document,
55
+ inputs=["image", "text"],
56
+ outputs="json",
57
+ title="Demo: Donut 🍩 for DocVQA",
58
+ description=description,
59
+ article=article,
60
+ enable_queue=True,
61
+ examples=[["example_1.png", "When is the coffee break?"], [
62
+ "example_2.jpeg", "What's the population of Stoddard?"]],
63
+ cache_examples=False)
64
+
65
+ demo.launch()
packages.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ # debian dependencies
2
+ # https://huggingface.co/docs/hub/spaces-dependencies
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ Pillow