File size: 1,167 Bytes
f405c14 442a9e7 |
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 |
import gradio as gr # type: ignore
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer # type: ignore
import torch
# Load your trained model and tokenizer
model_name = "yashvoladoddi37/movie-title-OCR-corrector-t5"
tokenizer = AutoTokenizer.from_pretrained(model_name)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
def correct_text(input_text):
inputs = tokenizer(input_text, return_tensors="pt", padding=True).to(device)
with torch.no_grad():
outputs = model.generate(
input_ids=inputs["input_ids"],
attention_mask=inputs["attention_mask"],
max_length=512
)
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return corrected_text
iface = gr.Interface(
fn=correct_text,
inputs=gr.Textbox(lines=2, placeholder="Enter text to correct"),
outputs="text",
title="OCR Correction Demo",
description="Enter text with OCR errors, and the model will attempt to correct them."
)
iface.launch() # Remove share=True for deployment |