Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
# Load the model and processor
|
| 5 |
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-handwritten')
|
| 6 |
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-handwritten')
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 11 |
-
|
| 12 |
-
# Generate the text
|
| 13 |
generated_ids = model.generate(pixel_values)
|
| 14 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 15 |
-
|
| 16 |
return generated_text
|
| 17 |
|
| 18 |
-
# Create
|
| 19 |
-
|
| 20 |
-
fn=
|
| 21 |
inputs="image",
|
| 22 |
outputs="text",
|
| 23 |
title="Image OCR",
|
| 24 |
-
description="
|
| 25 |
)
|
| 26 |
|
| 27 |
# Launch the interface
|
| 28 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import VisionEncoderDecoderModel, TrOCRProcessor
|
| 4 |
|
| 5 |
# Load the model and processor
|
| 6 |
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-handwritten')
|
| 7 |
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-handwritten')
|
| 8 |
|
| 9 |
+
# Define a function to perform OCR
|
| 10 |
+
def perform_ocr(image):
|
| 11 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
|
|
|
|
|
|
| 12 |
generated_ids = model.generate(pixel_values)
|
| 13 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
|
|
|
| 14 |
return generated_text
|
| 15 |
|
| 16 |
+
# Create a Gradio interface
|
| 17 |
+
iface = gr.Interface(
|
| 18 |
+
fn=perform_ocr,
|
| 19 |
inputs="image",
|
| 20 |
outputs="text",
|
| 21 |
title="Image OCR",
|
| 22 |
+
description="Upload an image to extract text"
|
| 23 |
)
|
| 24 |
|
| 25 |
# Launch the interface
|
| 26 |
+
iface.launch()
|