Spaces:
Sleeping
Sleeping
phonefern commited on
Commit ·
c73f603
1
Parent(s): 17220ee
rainy
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install Gradio if you haven't already
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# Load the processor and model
|
| 9 |
+
processor = TrOCRProcessor.from_pretrained('openthaigpt/thai-trocr')
|
| 10 |
+
model = VisionEncoderDecoderModel.from_pretrained('openthaigpt/thai-trocr')
|
| 11 |
+
|
| 12 |
+
# Define the prediction function
|
| 13 |
+
def extract_text_from_image(image):
|
| 14 |
+
# Process the input image and run the OCR model
|
| 15 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 16 |
+
generated_ids = model.generate(pixel_values)
|
| 17 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 18 |
+
return generated_text
|
| 19 |
+
|
| 20 |
+
# Set up the Gradio interface
|
| 21 |
+
interface = gr.Interface(
|
| 22 |
+
fn=extract_text_from_image, # Function to process the image
|
| 23 |
+
inputs=gr.Image(type="pil"), # Input is an image (PIL format)
|
| 24 |
+
outputs="text", # Output is text
|
| 25 |
+
title="Thai OCR with TrOCR",
|
| 26 |
+
description="Upload an image containing Thai text, and this model will extract the text using a Thai-adapted TrOCR model.",
|
| 27 |
+
examples=["path/to/example_image.jpg"] # Optional: add a sample image path here for users to try
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Launch the interface
|
| 31 |
+
interface.launch()
|