Jangai commited on
Commit
b0ac62c
·
verified ·
1 Parent(s): 7574fa9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -1,32 +1,29 @@
1
  import gradio as gr
2
- from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
  from PIL import Image
4
- import numpy as np
5
 
6
  # Load the processor and model
7
- processor = TrOCRProcessor.from_pretrained('microsoft/trocr-large-handwritten')
8
- model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-large-handwritten')
9
 
 
10
  def recognize_handwriting(image):
11
- if isinstance(image, dict):
12
- image = image["image"]
13
- pil_image = Image.fromarray(image).convert("RGB")
14
- pixel_values = processor(images=pil_image, return_tensors="pt").pixel_values
15
  generated_ids = model.generate(pixel_values)
16
  generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
17
  return generated_text
18
 
19
- # Gradio interface
20
- image_input = gr.Image(tool="editor", type="numpy", label="Draw or Upload an Image")
21
- output_text = gr.Textbox(label="Recognized Text")
22
-
23
- iface = gr.Interface(
24
- fn=recognize_handwriting,
25
- inputs=image_input,
26
- outputs=output_text,
27
- title="Handwriting Recognition",
28
- description="Draw or upload an image of handwritten text, and the model will recognize the text.",
29
- live=True,
30
- )
31
 
32
- iface.launch()
 
 
1
  import gradio as gr
 
2
  from PIL import Image
3
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
4
 
5
  # Load the processor and model
6
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-large-handwritten")
7
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-large-handwritten")
8
 
9
+ # Define the function to recognize handwriting
10
  def recognize_handwriting(image):
11
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
 
 
 
12
  generated_ids = model.generate(pixel_values)
13
  generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
14
  return generated_text
15
 
16
+ # Create the Gradio interface
17
+ with gr.Blocks() as demo:
18
+ gr.Markdown("# Handwriting Recognition")
19
+ with gr.Row():
20
+ with gr.Column():
21
+ image_input = gr.Image(tool="editor", type="numpy", label="Draw or Upload an Image")
22
+ recognize_button = gr.Button("Recognize Handwriting")
23
+ with gr.Column():
24
+ output_text = gr.Textbox(label="Recognized Text")
25
+
26
+ recognize_button.click(fn=recognize_handwriting, inputs=image_input, outputs=output_text)
 
27
 
28
+ # Launch the Gradio app
29
+ demo.launch()