Garfy commited on
Commit
62274b9
·
1 Parent(s): fb60255

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
+ import requests
4
+ from PIL import Image
5
+
6
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed")
7
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-printed")
8
+
9
+ def process_image(image):
10
+ # prepare image
11
+ pixel_values = processor(image, return_tensors="pt").pixel_values
12
+
13
+ # generate (no beam search)
14
+ generated_ids = model.generate(pixel_values)
15
+
16
+ # decode
17
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
18
+
19
+ return generated_text
20
+
21
+ title = "Demo: TrOCR"
22
+
23
+ #css = """.output_image, .input_image {height: 600px !important}"""
24
+
25
+ iface = gr.Interface(
26
+ fn=process_image,
27
+ inputs=gr.inputs.Image(type="pil"),
28
+ outputs=gr.outputs.Textbox(),
29
+ title=title,
30
+ )
31
+ iface.launch(debug=True)