|
import torch |
|
from transformers import AutoModel, AutoTokenizer |
|
from PIL import Image |
|
import gradio as gr |
|
import os |
|
|
|
|
|
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('cpu') |
|
model = model.to(device) |
|
|
|
|
|
def perform_ocr(image_file_path): |
|
|
|
image = Image.open(image_file_path) |
|
|
|
|
|
temp_image_path = "temp_image.png" |
|
image.save(temp_image_path) |
|
|
|
|
|
with torch.no_grad(): |
|
|
|
result = model.chat(tokenizer, temp_image_path, ocr_type='ocr') |
|
|
|
|
|
os.remove(temp_image_path) |
|
|
|
|
|
return result |
|
|
|
|
|
iface = gr.Interface(fn=perform_ocr, inputs="file", outputs="text", |
|
title="OCR Application", description="Upload an image to extract text.") |
|
|
|
|
|
iface.launch() |
|
|