File size: 1,095 Bytes
e427f06
 
ceb2483
 
e427f06
 
4e29901
 
ceb2483
4e29901
 
 
ceb2483
 
4e29901
 
 
 
 
e427f06
ceb2483
4e29901
 
 
 
 
 
 
e427f06
 
 
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
32
import gradio as gr
import torch
from PIL import Image
from transformers import DonutProcessor, VisionEncoderDecoderModel

device = "cuda" if torch.cuda.is_available() else "cpu"
model_repo_id = "selvakumarcts/sk_invoice_receipts"

# Load model and processor
processor = DonutProcessor.from_pretrained(model_repo_id)
model = VisionEncoderDecoderModel.from_pretrained(model_repo_id).to(device)

# Inference function
def infer(image):
    image = image.convert("RGB")
    pixel_values = processor(image, return_tensors="pt").pixel_values.to(device)
    output = model.generate(pixel_values, max_length=512)
    result = processor.batch_decode(output, skip_special_tokens=True)[0]
    return result

# UI
with gr.Blocks() as demo:
    gr.Markdown(" # Invoice/Receipt Reader (Donut Model)")
    with gr.Column():
        image_input = gr.Image(type="pil", label="Upload Image")
        run_button = gr.Button("Run")
        result = gr.Textbox(label="Extracted JSON", lines=10)
        run_button.click(fn=infer, inputs=[image_input], outputs=[result])

if __name__ == "__main__":
    demo.launch()