File size: 1,238 Bytes
db681f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
import gradio as gr
import subprocess
from PIL import Image
import tempfile
import os

def predict(input_image: Image.Image):
    # Save the input image to a temporary file
    temp_dir = tempfile.mkdtemp()
    temp_image_path = os.path.join(temp_dir, 'temp_image.jpg')
    input_image.save(temp_image_path)

    # Update img_list to include the path of the temporary image
    with open('test_img_list.txt', 'w') as f:
        f.write(temp_image_path)

    # Run the pylaia-htr-decode-ctc command
    subprocess.run(['pylaia-htr-decode-ctc', '--config', 'my_decode_config.yaml'])

    # Read the output from predict.txt
    with open('predict.txt', 'r') as f:
        output = f.read().strip().split('\n')[-1]  # Assuming the last line of predict.txt contains the desired output

    # Cleanup temporary files
    os.remove(temp_image_path)
    os.rmdir(temp_dir)

    return output

# Gradio interface
title = "PyLaia HTR"
description = "Inference using PyLaia models."
examples =[["examples/example01.jpg"], ["examples/example02.jpg"]]

iface = gr.Interface(
    fn=predict,
    inputs=gr.inputs.Image(type="pil"),
    outputs=gr.outputs.Textbox(),
    title=title,
    description=description,
    examples=examples
)

iface.launch()