|
import torch |
|
from transformers import AutoModel, AutoTokenizer |
|
from PIL import Image |
|
import gradio as gr |
|
import tempfile |
|
|
|
|
|
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, |
|
pad_token_id=tokenizer.eos_token_id).eval() |
|
|
|
|
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') |
|
model = model.to(device) |
|
|
|
|
|
def perform_ocr(image): |
|
|
|
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file: |
|
image.save(temp_file.name) |
|
temp_image_path = temp_file.name |
|
|
|
|
|
result = model.chat(tokenizer, temp_image_path, ocr_type='ocr') |
|
return result |
|
|
|
|
|
interface = gr.Interface( |
|
fn=perform_ocr, |
|
inputs=gr.Image(type="pil"), |
|
outputs=gr.Textbox(), |
|
title="OCR Web App", |
|
description="Upload an image to extract text using the GOT-OCR2.0 model." |
|
) |
|
|
|
|
|
interface.launch() |