File size: 1,912 Bytes
7b43238
4a49cfa
7b43238
 
 
8ea07b8
 
 
 
 
 
 
4a49cfa
 
 
7b43238
 
 
 
4a49cfa
7b43238
 
 
 
 
4a49cfa
7b43238
 
 
4a49cfa
 
 
7b43238
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import gradio as gr
from transformers import  VisionEncoderDecoderModel, AutoImageProcessor, BertTokenizerFast
import requests
from PIL import Image

urls = ['https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSoolxi9yWGAT5SLZShv8vVd0bz47UWRzQC19fDTeE8GmGv_Rn-PCF1pP1rrUx8kOjA4gg&usqp=CAU',
        'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRNYtTuSBpZPV_nkBYPMFwVVD9asZOPgHww4epu9EqWgDmXW--sE2o8og40ZfDGo87j5w&usqp=CAU']
for idx, url in enumerate(urls):
  image = Image.open(requests.get(url, stream=True).raw)
  image.save(f"image_{idx}.png")


image_processor = AutoImageProcessor.from_pretrained("microsoft/swin-base-patch4-window7-224")
tokenizer = tokenizer =BertTokenizerFast.from_pretrained("onlplab/alephbert-base")
model = VisionEncoderDecoderModel.from_pretrained("sivan22/hdd-words-ocr")


def process_image(image):
    # prepare image
    pixel_values = image_processor(image, return_tensors="pt").pixel_values

    # generate (no beam search)
    generated_ids = model.generate(pixel_values)

    # decode
    generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)

    return generated_text

title = "讛讚讙诪讛: 驻注谞讜讞 讻转讘 讬讚 讘讗诪爪注讜转 讘讬谞讛 诪诇讗讻讜转讬转"
description = "注诇 讘住讬住 诪讜讚诇 swin 讘爪讚 讛转诪讜谞讛, 讜诪讜讚诇 alephbert 讘爪讚 讛讟拽住讟."
article = "<p style='text-align: center'>sivan22</p>"
examples =[["image_0.png"], ["image_1.png"], ["image_2.png"]]

#css = """.output_image, .input_image {height: 600px !important}"""

iface = gr.Interface(fn=process_image, 
                     inputs=gr.inputs.Image(type="pil"), 
                     outputs=gr.outputs.Textbox(),
                     title=title,
                     description=description,
                     article=article,
                     examples=examples)
iface.launch(debug=True)