File size: 1,155 Bytes
b01b23a |
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 33 34 35 36 37 38 39 40 41 42 43 |
import gradio as gr
from transformers import AutoModel, AutoTokenizer
from PIL import Image
import torch
# Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
model = AutoModel.from_pretrained(
'ucaslcl/GOT-OCR2_0',
trust_remote_code=True,
low_cpu_mem_usage=True,
device_map='cuda' if torch.cuda.is_available() else 'cpu',
use_safetensors=True,
pad_token_id=tokenizer.eos_token_id
)
model = model.eval()
if torch.cuda.is_available():
model = model.cuda()
# OCR function
def ocr_from_image(image, ocr_type):
image_path = "temp.jpg"
image.save(image_path)
res = model.chat(tokenizer, image_path, ocr_type=ocr_type)
return res
# Gradio interface
ocr_types = ["ocr", "format"]
iface = gr.Interface(
fn=ocr_from_image,
inputs=[
gr.Image(type="pil", label="Upload Image"),
gr.Radio(ocr_types, label="OCR Type", value="ocr")
],
outputs="text",
title="GOT-OCR2.0: OCR with Transformers",
description="Upload an image and select OCR type (plain text or formatted)."
)
if __name__ == "__main__":
iface.launch()
|