philschmid HF staff commited on
Commit
4248e54
1 Parent(s): f019a93

Create new file

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Donut
3
+ Copyright (c) 2022-present NAVER Corp.
4
+ MIT License
5
+ https://github.com/clovaai/donut
6
+ """
7
+ import gradio as gr
8
+ import torch
9
+ from PIL import Image
10
+
11
+ from donut import DonutModel
12
+
13
+ def demo_process(input_img):
14
+ global pretrained_model, task_prompt, task_name
15
+ # input_img = Image.fromarray(input_img)
16
+ output = pretrained_model.inference(image=input_img, prompt=task_prompt)["predictions"][0]
17
+ return output
18
+
19
+ task_prompt = f"<s_cord-v2>"
20
+
21
+ image = Image.open("./sample_image_cord_test_receipt_00004.png")
22
+ image.save("cord_sample_receipt1.png")
23
+ image = Image.open("./sample_image_cord_test_receipt_00012.png")
24
+ image.save("cord_sample_receipt2.png")
25
+
26
+ pretrained_model = DonutModel.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
27
+ pretrained_model.encoder.to(torch.bfloat16)
28
+ pretrained_model.eval()
29
+
30
+ demo = gr.Interface(
31
+ fn=demo_process,
32
+ inputs= gr.inputs.Image(type="pil"),
33
+ outputs="json",
34
+ title=f"Donut 🍩 demonstration for `cord-v2` task",
35
+ description="""This model is trained with 800 Indonesian receipt images of CORD dataset. <br>
36
+ Demonstrations for other types of documents/tasks are available at https://github.com/clovaai/donut <br>
37
+ More CORD receipt images are available at https://huggingface.co/datasets/naver-clova-ix/cord-v2
38
+ More details are available at:
39
+ - Paper: https://arxiv.org/abs/2111.15664
40
+ - GitHub: https://github.com/clovaai/donut""",
41
+ examples=[["cord_sample_receipt1.png"], ["cord_sample_receipt2.png"]],
42
+ cache_examples=False,
43
+ )
44
+ demo.launch()