Spaces:
Runtime error
Runtime error
# app.py | |
import gradio as gr | |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel | |
from PIL import Image | |
# Load model and processor | |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten") | |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten") | |
def predict_handwriting(image): | |
""" | |
Function to process handwritten text image and return transcription | |
""" | |
try: | |
# Preprocess the image | |
image = image.convert("RGB") | |
# Prepare image pixel values | |
pixel_values = processor(image, return_tensors="pt").pixel_values | |
# Generate text | |
generated_ids = model.generate(pixel_values) | |
transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
return transcription | |
except Exception as e: | |
return f"Error processing image: {str(e)}" | |
# Create Gradio interface | |
demo = gr.Interface( | |
fn=predict_handwriting, | |
inputs=gr.Image(type="pil", label="Upload Handwritten Text Image"), | |
outputs=gr.Textbox(label="Transcription"), | |
title="Handwritten Text to Text Converter", | |
description="Upload a handwritten text image and get the transcribed text. Best results with clear, high-contrast images." | |
) | |
if __name__ == "__main__": | |
demo.launch() |