|
|
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" |
|
|
|
|
|
|
|
|
processor = DonutProcessor.from_pretrained(model_repo_id) |
|
|
model = VisionEncoderDecoderModel.from_pretrained(model_repo_id).to(device) |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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() |
|
|
|