import gradio as gr from transformers import VisionEncoderDecoderModel, TrOCRProcessor import torch from PIL import Image def recognize_captcha(input, mdl): # Load model and processor processor = TrOCRProcessor.from_pretrained(mdl) model = VisionEncoderDecoderModel.from_pretrained(mdl) # Prepare image pixel_values = processor(input, return_tensors="pt").pixel_values # Generate text 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=recognize_captcha, inputs=[ gr.Image(), gr.Dropdown( ['anuashok/ocr-captcha-v3','anuashok/ocr-captcha-v2','anuashok/ocr-captcha-v1','microsoft/trocr-base-printed'], label='Model to use' ) ], outputs=['text'], title = "Character Sequence Recognition From Captcha Image", description = "Using some TrOCR models found on the HF Hub to test/break tough text captchas. Will you have to train your own?", examples = [ ['krcx5.jpg','anuashok/ocr-captcha-v3'], ['hyp2a.jpg','microsoft/trocr-base-printed'], ['k4kyf.jpg','anuashok/ocr-captcha-v2'] ], article="Created by JSGR with ❤️ !!!" ) iface.queue(max_size=10) iface.launch()