Donlapark commited on
Commit
7e01765
·
verified ·
1 Parent(s): ec7556f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -0
app.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
4
+
5
+ # Load the model and processor
6
+ processor = TrOCRProcessor.from_pretrained("kkatiz/thai-trocr-thaigov-v2")
7
+ model = VisionEncoderDecoderModel.from_pretrained("kkatiz/thai-trocr-thaigov-v2")
8
+
9
+ # Define the prediction function
10
+ def predict(image):
11
+ image = image.convert("RGB")
12
+ pixel_values = processor(image, return_tensors="pt").pixel_values
13
+ generated_ids = model.generate(pixel_values)
14
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
15
+ return generated_text
16
+
17
+ # Define the Gradio interface
18
+ iface = gr.Interface(
19
+ fn=predict,
20
+ inputs=gr.inputs.Image(type="pil"),
21
+ outputs="text",
22
+ title="Thai Image-to-Text Prediction",
23
+ description="Upload an image, and the model will predict the text in the image.",
24
+ )
25
+
26
+ # Launch the app
27
+ iface.launch()