Young Ho Shin
commited on
Commit
β’
2aeef74
1
Parent(s):
43abf8d
Upload placeholder app file
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-small-printed")
|
7 |
+
model = VisionEncoderDecoderModel.from_pretrained("yhshin/latex-ocr")
|
8 |
+
tokenizer = model.tokenizer
|
9 |
+
|
10 |
+
# load image examples
|
11 |
+
|
12 |
+
def process_image(image):
|
13 |
+
# prepare image
|
14 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values
|
15 |
+
|
16 |
+
# generate (no beam search)
|
17 |
+
generated_ids = model.generate(pixel_values)
|
18 |
+
|
19 |
+
# decode
|
20 |
+
generated_text = tokenizer.decode_batch(generated_ids.tolist(), skip_special_tokens=True)[0]
|
21 |
+
|
22 |
+
# Strip spaces
|
23 |
+
generated_text = generated_text.replace(" ", "")
|
24 |
+
|
25 |
+
return generated_text
|
26 |
+
|
27 |
+
title = "Interactive demo: latex-ocr"
|
28 |
+
description = "Demo for latex-ocr, a machine learning model to parse an image of equation and attempt to find the LaTeX source code that generated it. To use it, simply upload an image or use the example image below and click 'submit'. Results will show up in a few seconds."
|
29 |
+
article = "<p style='text-align: center'>Made by Young Ho Shin<a href='https://www.github.com/yhshin11'>Github</a> | <a href='https://github.com/microsoft/unilm/tree/master/trocr'>Github Repo</a></p>"
|
30 |
+
examples =[["examples/image_0.png"], ["image_1.png"], ["image_2.png"]]
|
31 |
+
|
32 |
+
iface = gr.Interface(fn=process_image,
|
33 |
+
inputs=gr.inputs.Image(type="pil"),
|
34 |
+
outputs=gr.outputs.Textbox(),
|
35 |
+
title=title,
|
36 |
+
description=description,
|
37 |
+
article=article,
|
38 |
+
examples=examples)
|
39 |
+
iface.launch()
|
40 |
+
|