paran3xus's picture
Update app.py
70e272c verified
import gradio as gr
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
import requests
from PIL import Image
processor = TrOCRProcessor.from_pretrained("paran3xus/typress_ocr")
model = VisionEncoderDecoderModel.from_pretrained('paran3xus/typress_ocr')
# load image examples
urls = ["https://huggingface.co/spaces/paran3xus/typress_ocr_space/resolve/main/test_img/1.png", "https://huggingface.co/spaces/paran3xus/typress_ocr_space/resolve/main/test_img/2.png", "https://huggingface.co/spaces/paran3xus/typress_ocr_space/resolve/main/test_img/3.png"]
for idx, url in enumerate(urls):
image = Image.open(requests.get(url, stream=True).raw)
image.save(f"image_{idx}.png")
def process_image(image):
# prepare image
pixel_values = processor(image, return_tensors="pt").pixel_values
# generate (no beam search)
generated_ids = model.generate(pixel_values)
# decode
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
return generated_text
title = "Interactive demo: Typress OCR"
description = "Demo for Typress OCR, an TrOCR model for Typst Mathematical Expressions Recognition. To use it, simply upload a image or use one of the example images below and click 'submit'. Results will show up in a few seconds."
article = "<p style='text-align: center'><a href='https://github.com/ParaN3xus/typress'>Github Repo</a></p>"
examples =[["image_0.png"], ["image_1.png"], ["image_2.png"]]
iface = gr.Interface(fn=process_image,
inputs=gr.Image(type="pil"),
outputs=gr.Textbox(),
title=title,
description=description,
article=article,
examples=examples)
iface.launch(debug=True)