Pisethan commited on
Commit
922cf03
·
verified ·
1 Parent(s): 79d8e1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -17
app.py CHANGED
@@ -1,23 +1,27 @@
1
  import gradio as gr
2
- from datasets import load_dataset
3
  from PIL import Image
4
 
5
- # Load dataset
6
- dataset = load_dataset("Pisethan/sangapac_ocr")
 
7
 
8
- # Function to display image and text
9
- def display_data(index):
10
- row = dataset["train"][index]
11
- image = Image.open(row["image_path"])
12
- text = row["text"]
13
- return image, text
 
14
 
15
- # Create Gradio Interface
16
- with gr.Blocks() as demo:
17
- gr.Markdown("### OCR Dataset Viewer")
18
- index_input = gr.Slider(0, len(dataset["train"]) - 1, step=1, value=0, label="Select Index")
19
- output_image = gr.Image(label="Image")
20
- output_text = gr.Textbox(label="OCR Text")
21
- index_input.change(display_data, inputs=index_input, outputs=[output_image, output_text])
 
22
 
23
- demo.launch()
 
 
1
  import gradio as gr
2
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
  from PIL import Image
4
 
5
+ # Load model and processor
6
+ processor = TrOCRProcessor.from_pretrained("Pisethan/sangapac_ocr")
7
+ model = VisionEncoderDecoderModel.from_pretrained("Pisethan/sangapac_ocr")
8
 
9
+ # Define the prediction function
10
+ def recognize_text(image):
11
+ image = Image.open(image).convert("RGB")
12
+ inputs = processor(images=image, return_tensors="pt").pixel_values
13
+ outputs = model.generate(inputs)
14
+ predicted_text = processor.tokenizer.decode(outputs[0], skip_special_tokens=True)
15
+ return predicted_text
16
 
17
+ # Gradio Interface
18
+ interface = gr.Interface(
19
+ fn=recognize_text,
20
+ inputs=gr.inputs.Image(type="file", label="Upload an Image"),
21
+ outputs=gr.outputs.Textbox(label="Recognized Text"),
22
+ title="OCR Model Demo",
23
+ description="Upload an image containing text to test the OCR model.",
24
+ )
25
 
26
+ # Launch the app
27
+ interface.launch()