Spaces:
Runtime error
Runtime error
IZERE HIRWA Roger
commited on
Commit
·
c0550e9
1
Parent(s):
e7abb2f
ok
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Load model
|
| 7 |
+
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-large-handwritten")
|
| 8 |
+
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-large-handwritten")
|
| 9 |
+
|
| 10 |
+
# Define prediction function
|
| 11 |
+
def recognize_text(image):
|
| 12 |
+
image = Image.fromarray(image).convert("RGB")
|
| 13 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 14 |
+
generated_ids = model.generate(pixel_values)
|
| 15 |
+
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 16 |
+
return text
|
| 17 |
+
|
| 18 |
+
# Create Gradio interface
|
| 19 |
+
interface = gr.Interface(
|
| 20 |
+
fn=recognize_text,
|
| 21 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 22 |
+
outputs=gr.Textbox(label="Recognized Text"),
|
| 23 |
+
title="Handwritten Text Recognition",
|
| 24 |
+
description="Upload an image of handwritten text to recognize it using TrOCR.",
|
| 25 |
+
server_name="0.0.0.0", # Ensure compatibility with Hugging Face GPUs
|
| 26 |
+
server_port=7860 # Default Gradio port
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Launch interface
|
| 30 |
+
interface.launch(share=True)
|