Spaces:
Runtime error
Runtime error
ADD : app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Donut
|
| 3 |
+
"""
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import torch
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
from donut import DonutModel
|
| 9 |
+
|
| 10 |
+
def demo_process(input_img):
|
| 11 |
+
global pretrained_model, task_prompt, task_name
|
| 12 |
+
# input_img = Image.fromarray(input_img)
|
| 13 |
+
output = pretrained_model.inference(image=input_img, prompt=task_prompt)["predictions"][0]
|
| 14 |
+
return output
|
| 15 |
+
|
| 16 |
+
task_prompt = f"<s_cord-v2>"
|
| 17 |
+
|
| 18 |
+
image = Image.open("./sample_image_cord_test_receipt_00004.png")
|
| 19 |
+
image.save("cord_sample_receipt1.png")
|
| 20 |
+
image = Image.open("./sample_image_cord_test_receipt_00012.png")
|
| 21 |
+
image.save("cord_sample_receipt2.png")
|
| 22 |
+
|
| 23 |
+
pretrained_model = DonutModel.from_pretrained("Raj-Master/donut-demo-123")
|
| 24 |
+
pretrained_model.encoder.to(torch.bfloat16)
|
| 25 |
+
pretrained_model.eval()
|
| 26 |
+
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=demo_process,
|
| 29 |
+
inputs= gr.inputs.Image(type="pil"),
|
| 30 |
+
outputs="json",
|
| 31 |
+
title=f"Donut 🍩 demonstration",
|
| 32 |
+
description="""This model is trained with 140 receipt images """,
|
| 33 |
+
examples=[["cord_sample_receipt1.png"], ["cord_sample_receipt2.png"]],
|
| 34 |
+
cache_examples=False,
|
| 35 |
+
)
|
| 36 |
+
demo.launch()
|