Spaces:
Paused
Paused
Update service.py
Browse files- service.py +20 -15
service.py
CHANGED
@@ -1,43 +1,53 @@
|
|
1 |
import http.server
|
2 |
-
import select
|
3 |
import socketserver
|
4 |
import subprocess
|
5 |
import threading
|
6 |
import queue
|
7 |
import io
|
|
|
|
|
8 |
|
9 |
PORT = 8080
|
10 |
command_executed = False
|
11 |
command_output = queue.Queue()
|
12 |
command_queue = queue.Queue()
|
13 |
|
|
|
|
|
|
|
14 |
|
15 |
def run_command(command):
|
|
|
16 |
process = subprocess.Popen(
|
17 |
command,
|
18 |
stdout=subprocess.PIPE,
|
19 |
stderr=subprocess.PIPE,
|
20 |
-
text=True
|
|
|
21 |
)
|
22 |
while True:
|
23 |
reads = [process.stdout.fileno(), process.stderr.fileno()]
|
24 |
-
ret = select.select(reads, [], [])
|
25 |
for fd in ret[0]:
|
26 |
if fd == process.stdout.fileno():
|
27 |
output = process.stdout.readline()
|
28 |
if output:
|
29 |
-
command_output.put(output)
|
|
|
30 |
error = process.stderr.readline()
|
31 |
if error:
|
32 |
-
command_output.put(error)
|
33 |
if process.poll() is not None:
|
34 |
break
|
|
|
|
|
35 |
process.stdout.close()
|
36 |
process.stderr.close()
|
37 |
process.wait()
|
38 |
command_output.put("Leaving")
|
39 |
|
40 |
def command_worker():
|
|
|
41 |
while True:
|
42 |
command = command_queue.get()
|
43 |
if command is None:
|
@@ -48,15 +58,10 @@ def command_worker():
|
|
48 |
|
49 |
def start_commands():
|
50 |
commands = [
|
51 |
-
['
|
52 |
-
|
53 |
-
|
54 |
-
['
|
55 |
-
'jatsEval', '-Pp2t=/Volumes/ExtremePro/sciencialab/science-miner/grobid/grobid-evaluation/PLOS_1000',
|
56 |
-
'-Prun=1', '-PfileRatio=1'],
|
57 |
-
# ['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/PLOS_1000', '-Prun=1', '-PfileRatio=1'],
|
58 |
-
# ['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/biorxiv-10k-test-2000', '-Prun=1', '-PfileRatio=1'],
|
59 |
-
# ['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/eLife_984', '-Prun=1', '-PfileRatio=1']
|
60 |
]
|
61 |
for command in commands:
|
62 |
command_queue.put(command)
|
@@ -69,7 +74,7 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
|
69 |
if not command_executed:
|
70 |
command_executed = True
|
71 |
threading.Thread(target=start_commands).start()
|
72 |
-
response = "
|
73 |
else:
|
74 |
response = io.StringIO()
|
75 |
response.write("Command output:\n")
|
|
|
1 |
import http.server
|
|
|
2 |
import socketserver
|
3 |
import subprocess
|
4 |
import threading
|
5 |
import queue
|
6 |
import io
|
7 |
+
import select
|
8 |
+
import re
|
9 |
|
10 |
PORT = 8080
|
11 |
command_executed = False
|
12 |
command_output = queue.Queue()
|
13 |
command_queue = queue.Queue()
|
14 |
|
15 |
+
def sanitize_output(output):
|
16 |
+
# Remove any non-printable characters except for carriage return
|
17 |
+
return re.sub(r'[^\x20-\x7E\r]+', '', output)
|
18 |
|
19 |
def run_command(command):
|
20 |
+
print("Running command ", command)
|
21 |
process = subprocess.Popen(
|
22 |
command,
|
23 |
stdout=subprocess.PIPE,
|
24 |
stderr=subprocess.PIPE,
|
25 |
+
text=True,
|
26 |
+
bufsize=1 # Line-buffered
|
27 |
)
|
28 |
while True:
|
29 |
reads = [process.stdout.fileno(), process.stderr.fileno()]
|
30 |
+
ret = select.select(reads, [], [], 0.1) # Timeout to periodically flush
|
31 |
for fd in ret[0]:
|
32 |
if fd == process.stdout.fileno():
|
33 |
output = process.stdout.readline()
|
34 |
if output:
|
35 |
+
command_output.put(sanitize_output(output) + "\n")
|
36 |
+
if fd == process.stderr.fileno():
|
37 |
error = process.stderr.readline()
|
38 |
if error:
|
39 |
+
command_output.put(sanitize_output(error) + "\n")
|
40 |
if process.poll() is not None:
|
41 |
break
|
42 |
+
process.stdout.flush()
|
43 |
+
process.stderr.flush()
|
44 |
process.stdout.close()
|
45 |
process.stderr.close()
|
46 |
process.wait()
|
47 |
command_output.put("Leaving")
|
48 |
|
49 |
def command_worker():
|
50 |
+
print("Starting worker command")
|
51 |
while True:
|
52 |
command = command_queue.get()
|
53 |
if command is None:
|
|
|
58 |
|
59 |
def start_commands():
|
60 |
commands = [
|
61 |
+
['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/PMC_sample_1943', '-Prun=1', '-PfileRatio=1'],
|
62 |
+
['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/PLOS_1000', '-Prun=1', '-PfileRatio=1'],
|
63 |
+
['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/biorxiv-10k-test-2000', '-Prun=1', '-PfileRatio=1'],
|
64 |
+
['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/eLife_984', '-Prun=1', '-PfileRatio=1']
|
|
|
|
|
|
|
|
|
|
|
65 |
]
|
66 |
for command in commands:
|
67 |
command_queue.put(command)
|
|
|
74 |
if not command_executed:
|
75 |
command_executed = True
|
76 |
threading.Thread(target=start_commands).start()
|
77 |
+
response = "Starting evaluation."
|
78 |
else:
|
79 |
response = io.StringIO()
|
80 |
response.write("Command output:\n")
|