Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import TrOCRProcessor, VisionEncoderDecoderModel | |
| from PIL import Image | |
| # Load model and processor once at startup | |
| MODEL_ID = "kazars24/trocr-base-handwritten-ru" | |
| processor = TrOCRProcessor.from_pretrained(MODEL_ID) | |
| model = VisionEncoderDecoderModel.from_pretrained(MODEL_ID) | |
| def ocr_ru(image: Image.Image) -> str: | |
| # Ensure 3-channel RGB | |
| image = image.convert("RGB") | |
| pixel_values = processor(images=image, return_tensors="pt").pixel_values | |
| generated_ids = model.generate(pixel_values) | |
| text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
| return text | |
| # Gradio interface – also defines API schema | |
| demo = gr.Interface( | |
| fn=ocr_ru, | |
| inputs=gr.Image(type="pil", label="Upload Cyrillic handwriting"), | |
| outputs=gr.Textbox(label="Recognized text"), | |
| title="TrOCR Russian Handwriting OCR", | |
| description="TrOCR model fine-tuned on Cyrillic Handwriting Dataset (kazars24/trocr-base-handwritten-ru)." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |