File size: 1,059 Bytes
b719e63
583d619
8a655f5
b719e63
 
 
9414621
b719e63
9414621
be946d7
 
 
8a655f5
 
 
 
 
 
be946d7
f06ba45
b719e63
 
 
 
 
 
 
11927ca
 
8a655f5
b719e63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gradio as gr
import os
import shutil
import argparse
import torch
from PIL import Image
from pdf2image import convert_from_path
from donut import DonutModel


def demo_process(doc):
    global model, task_prompt, task_name
    file_name = os.path.basename(doc)
    new_file_location = os.path.join(os.getcwd(),file_name)
    
    shutil.copyfile(doc, new_file_location)
    img = convert_from_path(new_file_location)[0]
    output = model.inference(image=img, prompt=task_prompt)["predictions"][0]
    return output
    
parser = argparse.ArgumentParser()
parser.add_argument("--task", type=str, default="SGSInvoice")
parser.add_argument("--pretrained_path", type=str, default="uartimcs/donut-invoice-extract")
args, left_argv = parser.parse_known_args()
task_name = args.task
task_prompt = f"<s_{task_name}>"

model = DonutModel.from_pretrained("uartimcs/donut-invoice-extract")
model.eval()
demo = gr.Interface(fn=demo_process,inputs=gr.File(label="Upload PDF"),outputs="json", title=f"Donut 🍩 demonstration for `{task_name}` task",)
demo.launch()