Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load model and processor
|
| 6 |
+
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed")
|
| 7 |
+
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-printed")
|
| 8 |
+
|
| 9 |
+
def extract_text(image):
|
| 10 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 11 |
+
generated_ids = model.generate(pixel_values)
|
| 12 |
+
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 13 |
+
return text
|
| 14 |
+
|
| 15 |
+
# Gradio interface
|
| 16 |
+
iface = gr.Interface(
|
| 17 |
+
fn=extract_text,
|
| 18 |
+
inputs=gr.inputs.Image(type="pil"),
|
| 19 |
+
outputs="text",
|
| 20 |
+
title="OCR Text Extraction",
|
| 21 |
+
description="Extract printed text from images."
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
iface.launch()
|