Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("paran3xus/typress_ocr")
|
7 |
+
model = VisionEncoderDecoderModel.from_pretrained('paran3xus/typress_ocr')
|
8 |
+
|
9 |
+
# load image examples
|
10 |
+
urls = ["https://huggingface.co/paran3xus/typress_ocr_space/raw/main/test_img/1.png", "https://huggingface.co/paran3xus/typress_ocr_space/raw/main/test_img/2.png", "https://huggingface.co/paran3xus/typress_ocr_space/raw/main/test_img/3.png"]
|
11 |
+
for idx, url in enumerate(urls):
|
12 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
13 |
+
image.save(f"image_{idx}.png")
|
14 |
+
|
15 |
+
def process_image(image):
|
16 |
+
# prepare image
|
17 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values
|
18 |
+
|
19 |
+
# generate (no beam search)
|
20 |
+
generated_ids = model.generate(pixel_values)
|
21 |
+
|
22 |
+
# decode
|
23 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
24 |
+
|
25 |
+
return generated_text
|
26 |
+
|
27 |
+
title = "Interactive demo: Typress OCR"
|
28 |
+
description = "Demo for Typress OCR, an TrOCR model for Typst Mathematical Expressions Recognition. To use it, simply upload a (single-text line) image or use one of the example images below and click 'submit'. Results will show up in a few seconds."
|
29 |
+
article = "<p style='text-align: center'><a href='https://github.com/ParaN3xus/typress'>Github Repo</a></p>"
|
30 |
+
examples =[["image_0.png"], ["image_1.png"], ["image_2.png"]]
|
31 |
+
|
32 |
+
iface = gr.Interface(fn=process_image,
|
33 |
+
inputs=gr.Image(type="pil"),
|
34 |
+
outputs=gr.Textbox(),
|
35 |
+
title=title,
|
36 |
+
description=description,
|
37 |
+
article=article,
|
38 |
+
examples=examples)
|
39 |
+
iface.launch(debug=True)
|