Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-handwritten")
|
7 |
+
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
8 |
+
# 2 cpu and 16gib ram
|
9 |
+
def process_image(image):
|
10 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values
|
11 |
+
generated_ids = model.generate(pixel_values)
|
12 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
13 |
+
return generated_text
|
14 |
+
|
15 |
+
title = "Transforme(encoder-decoder) based Text OCR"
|
16 |
+
description = "Demo for Microsoft's TrOCR, an encoder-decoder model \
|
17 |
+
consisting of an image Transformer encoder and a text Transformer \
|
18 |
+
decoder for state-of-the-art optical character recognition (OCR) on \
|
19 |
+
single-text line images. This particular model is fine-tuned on IAM, \
|
20 |
+
a dataset of annotated handwritten images."
|
21 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.10282'>Transformer Optical Character Recognition with Pre-trained Models</a> | <a href='https://github.com/microsoft/unilm/tree/master/trocr'>Github Repo</a></p>"
|
22 |
+
|
23 |
+
iface = gr.Interface(fn=process_image,
|
24 |
+
inputs=gr.inputs.Image(type="pil"),
|
25 |
+
outputs=gr.outputs.Textbox(),
|
26 |
+
title=title,
|
27 |
+
description=description,
|
28 |
+
article=article)
|
29 |
+
iface.launch(debug=False)
|