File size: 934 Bytes
7e01765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd55f1c
7e01765
 
 
 
 
 
99c08fb
 
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
import gradio as gr
from PIL import Image
from transformers import TrOCRProcessor, VisionEncoderDecoderModel

# Load the model and processor
processor = TrOCRProcessor.from_pretrained("kkatiz/thai-trocr-thaigov-v2")
model = VisionEncoderDecoderModel.from_pretrained("kkatiz/thai-trocr-thaigov-v2")

# Define the prediction function
def predict(image):
    image = image.convert("RGB")
    pixel_values = processor(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

# Define the Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="Thai Image-to-Text Prediction",
    description="Upload an image, and the model will predict the text in the image.",
)

# Launch the app
if __name__ == "__main__":
    iface.launch()