ZennyKenny commited on
Commit
44342ba
·
verified ·
1 Parent(s): f1b5c18

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -0
app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
+ from PIL import Image
4
+
5
+ # Load TrOCR model
6
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
7
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
8
+
9
+ def recognize_text(image):
10
+ image = image.convert("RGB")
11
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
12
+ generated_ids = model.generate(pixel_values)
13
+ text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
14
+ return text
15
+
16
+ # Gradio UI
17
+ note = gr.Interface(
18
+ fn=recognize_text,
19
+ inputs=gr.Image(type="pil"),
20
+ outputs="text",
21
+ title="Handwritten Note to Digital Text",
22
+ description="Upload an image of handwritten text, and the AI will convert it to digital text."
23
+ )
24
+
25
+ note.launch()