File size: 1,238 Bytes
c73f603
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Install Gradio if you haven't already


import gradio as gr
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image

# Load the processor and model
processor = TrOCRProcessor.from_pretrained('openthaigpt/thai-trocr')
model = VisionEncoderDecoderModel.from_pretrained('openthaigpt/thai-trocr')

# Define the prediction function
def extract_text_from_image(image):
    # Process the input image and run the OCR model
    pixel_values = processor(images=image, return_tensors="pt").pixel_values
    generated_ids = model.generate(pixel_values)
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return generated_text

# Set up the Gradio interface
interface = gr.Interface(
    fn=extract_text_from_image,  # Function to process the image
    inputs=gr.Image(type="pil"),  # Input is an image (PIL format)
    outputs="text",               # Output is text
    title="Thai OCR with TrOCR",
    description="Upload an image containing Thai text, and this model will extract the text using a Thai-adapted TrOCR model.",
    examples=["path/to/example_image.jpg"]  # Optional: add a sample image path here for users to try
)

# Launch the interface
interface.launch()