Spaces:
Sleeping
Sleeping
Flavio de Oliveira
commited on
Commit
·
a95b69d
1
Parent(s):
db681f0
Try to solve race condition
Browse files
app.py
CHANGED
@@ -3,34 +3,68 @@ import subprocess
|
|
3 |
from PIL import Image
|
4 |
import tempfile
|
5 |
import os
|
|
|
6 |
|
7 |
def predict(input_image: Image.Image):
|
8 |
-
# Save the input image to a temporary file
|
9 |
-
temp_dir = tempfile.mkdtemp()
|
10 |
-
temp_image_path = os.path.join(temp_dir, 'temp_image.jpg')
|
11 |
-
input_image.save(temp_image_path)
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
subprocess.run(['pylaia-htr-decode-ctc', '--config', 'my_decode_config.yaml'])
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Gradio interface
|
31 |
title = "PyLaia HTR"
|
32 |
description = "Inference using PyLaia models."
|
33 |
-
examples =[["examples/example01.jpg"], ["examples/example02.jpg"]]
|
34 |
|
35 |
iface = gr.Interface(
|
36 |
fn=predict,
|
|
|
3 |
from PIL import Image
|
4 |
import tempfile
|
5 |
import os
|
6 |
+
import yaml
|
7 |
|
8 |
def predict(input_image: Image.Image):
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
try:
|
11 |
+
# Used as a context manager. Takes care of cleaning up the directory.
|
12 |
+
# Even if an error is raised within the with block, the directory is removed.
|
13 |
+
# No finally block needed
|
14 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
15 |
+
temp_image_path = os.path.join(temp_dir, 'temp_image.jpg')
|
16 |
+
temp_list_path = os.path.join(temp_dir, 'temp_img_list.txt')
|
17 |
+
temp_config_path = os.path.join(temp_dir, 'temp_config.yaml')
|
18 |
|
19 |
+
input_image.save(temp_image_path)
|
|
|
20 |
|
21 |
+
# Create a temporary img_list file
|
22 |
+
with open(temp_list_path, 'w') as f:
|
23 |
+
f.write(temp_image_path)
|
24 |
|
25 |
+
# Read the original config file and create a temporary one
|
26 |
+
with open('my_decode_config.yaml', 'r') as f:
|
27 |
+
config_data = yaml.safe_load(f)
|
28 |
+
|
29 |
+
config_data['img_list'] = temp_list_path
|
30 |
|
31 |
+
with open(temp_config_path, 'w') as f:
|
32 |
+
yaml.dump(config_data, f)
|
33 |
+
|
34 |
+
try:
|
35 |
+
subprocess.run("pylaia-htr-decode-ctc --config my_decode_config.yaml | tee predict.txt", shell=True, check=True)
|
36 |
+
except subprocess.CalledProcessError as e:
|
37 |
+
print(f"Command failed with error {e.returncode}, output:\n{e.output}")
|
38 |
+
|
39 |
+
# subprocess.run(f"pylaia-htr-decode-ctc --config {temp_config_path} | tee predict.txt", shell=True, check=True)
|
40 |
+
|
41 |
+
# Alternative to shell=True (ChatGPT suggestion)
|
42 |
+
# from subprocess import Popen, PIPE
|
43 |
+
|
44 |
+
# # Run the first command and capture its output
|
45 |
+
# p1 = Popen(["pylaia-htr-decode-ctc", "--config", temp_config_path], stdout=PIPE)
|
46 |
+
# output = p1.communicate()[0]
|
47 |
+
|
48 |
+
# # Write the output to predict.txt
|
49 |
+
# with open('predict.txt', 'wb') as f:
|
50 |
+
# f.write(output)
|
51 |
+
|
52 |
+
# Read the output from predict.txt
|
53 |
+
if os.path.exists('predict.txt'):
|
54 |
+
with open('predict.txt', 'r') as f:
|
55 |
+
output = f.read().strip().split('\n')[-1]
|
56 |
+
else:
|
57 |
+
print('predict.txt does not exist')
|
58 |
+
|
59 |
+
return output
|
60 |
+
|
61 |
+
except subprocess.CalledProcessError as e:
|
62 |
+
return f"Command failed with error {e.returncode}"
|
63 |
|
64 |
# Gradio interface
|
65 |
title = "PyLaia HTR"
|
66 |
description = "Inference using PyLaia models."
|
67 |
+
examples = [["examples/example01.jpg"], ["examples/example02.jpg"]]
|
68 |
|
69 |
iface = gr.Interface(
|
70 |
fn=predict,
|