|
import gradio as gr |
|
from transformers import TrOCRProcessor, VisionEncoderDecoderModel |
|
from PIL import Image |
|
import requests |
|
|
|
|
|
|
|
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-large-stage1') |
|
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-large-stage1') |
|
|
|
def generate_text(input_image): |
|
|
|
image = Image.fromarray(input_image) |
|
|
|
pixel_values = processor(images=image, return_tensors="pt").pixel_values |
|
generated_ids = model.generate(pixel_values) |
|
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] |
|
return generated_text |
|
|
|
|
|
iface = gr.Interface( |
|
fn=generate_text, |
|
inputs=gr.Image(), |
|
outputs=gr.Textbox(), |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|