-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
369 lines (303 loc) · 12.4 KB
/
Copy pathserver.py
File metadata and controls
369 lines (303 loc) · 12.4 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"""
REFINE Local Bridge Server
============================
Bridges the browser-based refine.html app with the local filesystem and
MedTagger JAR. Exposes a small REST API over localhost so the HTML app can:
- Load existing rule files from the models/ folder
- Write updated rule files back to disk
- Execute the MedTagger shell script
- Read back the .ann output files for re-evaluation
Usage:
pip install flask flask-cors
python server.py [--port 5050] [--base /path/to/REFINE]
Default base directory: same folder as this script.
"""
import argparse
import glob
import json
import os
import subprocess
import time
from flask import Flask, jsonify, request, send_file, send_from_directory
from flask_cors import CORS
# ── Bootstrap ──────────────────────────────────────────────────────────────────
parser = argparse.ArgumentParser(description="REFINE local bridge server")
parser.add_argument("--port", type=int, default=5050, help="Port to listen on")
parser.add_argument(
"--base",
default=os.path.dirname(os.path.abspath(__file__)),
help="Base project directory (default: directory containing this script)",
)
args = parser.parse_args()
BASE_DIR = os.path.realpath(args.base)
PORT = args.port
app = Flask(__name__)
CORS(app, origins="*") # Allow the HTML file opened from any origin
# ── Helpers ────────────────────────────────────────────────────────────────────
def safe_path(*parts):
"""Join parts under BASE_DIR and verify it stays within BASE_DIR."""
p = os.path.realpath(os.path.join(BASE_DIR, *parts))
if not p.startswith(BASE_DIR):
raise ValueError(f"Path escape attempt: {p}")
return p
def read_text(path):
if not os.path.isfile(path):
return None
with open(path, "r", encoding="utf-8") as f:
return f.read()
def write_text(path, content):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(content)
# ── Routes ─────────────────────────────────────────────────────────────────────
@app.route("/")
def index():
html = os.path.join(BASE_DIR, "refine.html")
if os.path.isfile(html):
return send_file(html)
return "refine.html not found in " + BASE_DIR, 404
@app.route("/api/health")
def health():
tasks = []
models_dir = os.path.join(BASE_DIR, "models")
if os.path.isdir(models_dir):
tasks = [d for d in os.listdir(models_dir)
if os.path.isdir(os.path.join(models_dir, d)) and not d.startswith(".")]
scripts = [f for f in os.listdir(BASE_DIR) if f.endswith(".sh")]
return jsonify({
"ok": True,
"base": BASE_DIR,
"tasks": sorted(tasks),
"scripts": sorted(scripts),
})
@app.route("/api/read-rules")
def read_rules():
"""
GET /api/read-rules?task=Falls
Returns all rule files for the given task, discovering regexp files
dynamically from used_resources.txt (or by scanning regexp/).
Response shape:
{ context, matchrules, regexp: { [shortname]: { filename, norm, content, isRemove } } }
"""
import re as _re
task = request.args.get("task", "Falls")
models_task = safe_path("models", task)
context_file = os.path.join(models_task, "context", "contextRule.txt")
matchrules_file = os.path.join(models_task, "rules", "resources_rules_matchrules.txt")
regexp_dir = os.path.join(models_task, "regexp")
matchrules_content = read_text(matchrules_file)
# Parse matchrules: shortname → { norm, isRemove }
norm_map = {}
if matchrules_content:
for line in matchrules_content.splitlines():
m_re = _re.search(r'REGEXP="[^"]*%re(\w+)[^"]*"', line)
m_norm = _re.search(r'NORM="([^"]+)"', line)
if m_re and m_norm:
sn = m_re.group(1)
norm = m_norm.group(1)
norm_map[sn] = {"norm": norm, "isRemove": norm.upper() == "REMOVE"}
# Discover regexp files from used_resources.txt (preferred) or scan regexp/
regexp_files = {}
used_path = os.path.join(models_task, "used_resources.txt")
if os.path.isfile(used_path):
with open(used_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line or line.startswith("//"):
continue
abs_path = os.path.join(models_task, line.lstrip("./"))
fname = os.path.basename(abs_path)
m = _re.match(r"resources_regexp_re(\w+)\.txt$", fname)
if m and os.path.isfile(abs_path):
sn = m.group(1)
nm = norm_map.get(sn, {})
regexp_files[sn] = {
"filename": fname,
"content": read_text(abs_path),
"norm": nm.get("norm"),
"isRemove": nm.get("isRemove", False),
}
elif os.path.isdir(regexp_dir):
for fname in sorted(os.listdir(regexp_dir)):
m = _re.match(r"resources_regexp_re(\w+)\.txt$", fname)
if m:
sn = m.group(1)
nm = norm_map.get(sn, {})
regexp_files[sn] = {
"filename": fname,
"content": read_text(os.path.join(regexp_dir, fname)),
"norm": nm.get("norm"),
"isRemove": nm.get("isRemove", False),
}
return jsonify({
"context": read_text(context_file),
"matchrules": matchrules_content,
"regexp": regexp_files,
})
@app.route("/api/write-rules", methods=["POST"])
def write_rules():
"""
POST /api/write-rules
Body: {
task,
files: {
context?: string,
matchrules?: string,
regexp?: { [filename]: string } ← keyed by actual filename
}
}
"""
data = request.get_json(force=True)
task = data.get("task", "Falls")
files = data.get("files", {})
models_task = safe_path("models", task)
written = []
if files.get("context"):
path = os.path.join(models_task, "context", "contextRule.txt")
write_text(path, files["context"])
written.append(path)
if files.get("matchrules"):
path = os.path.join(models_task, "rules", "resources_rules_matchrules.txt")
write_text(path, files["matchrules"])
written.append(path)
for filename, content in (files.get("regexp") or {}).items():
if not content or not filename.endswith(".txt"):
continue
path = safe_path("models", task, "regexp", filename)
write_text(path, content)
written.append(path)
return jsonify({"ok": True, "written": written})
@app.route("/api/run-script", methods=["POST"])
def run_script():
"""
POST /api/run-script
Body: {script: "runMedTagger-fit-falls.sh"}
Executes the named .sh script from BASE_DIR, returns stdout/stderr.
"""
data = request.get_json(force=True)
script_name = data.get("script", "")
if not script_name.endswith(".sh"):
return jsonify({"ok": False, "error": "Only .sh scripts allowed"}), 400
script_path = safe_path(script_name)
if not os.path.isfile(script_path):
return jsonify({"ok": False, "error": f"Script not found: {script_name}"}), 404
# Make executable if needed
os.chmod(script_path, 0o755)
t0 = time.time()
try:
result = subprocess.run(
["bash", script_path],
cwd=BASE_DIR,
capture_output=True,
text=True,
timeout=300, # 5 min max
)
elapsed = round(time.time() - t0, 1)
return jsonify({
"ok": result.returncode == 0,
"returncode": result.returncode,
"stdout": result.stdout[-4000:] if result.stdout else "",
"stderr": result.stderr[-2000:] if result.stderr else "",
"elapsed": elapsed,
})
except subprocess.TimeoutExpired:
return jsonify({"ok": False, "error": "Script timed out after 300 s"}), 504
except Exception as e:
return jsonify({"ok": False, "error": str(e)}), 500
@app.route("/api/read-ann")
def read_ann():
"""
GET /api/read-ann?dir=output
Returns all .ann file contents from the specified directory.
"""
rel_dir = request.args.get("dir", "output")
ann_dir = safe_path(rel_dir)
if not os.path.isdir(ann_dir):
return jsonify({"files": {}, "error": f"Directory not found: {rel_dir}"})
files = {}
for path in glob.glob(os.path.join(ann_dir, "*.ann")):
name = os.path.basename(path)
files[name] = read_text(path)
return jsonify({"files": files, "dir": ann_dir})
@app.route("/api/read-gold")
def read_gold():
"""
GET /api/read-gold?dir=gold
Returns all .xml file contents from the gold directory.
"""
rel_dir = request.args.get("dir", "gold")
gold_dir = safe_path(rel_dir)
if not os.path.isdir(gold_dir):
return jsonify({"files": {}, "error": f"Directory not found: {rel_dir}"})
files = {}
for path in glob.glob(os.path.join(gold_dir, "*.xml")):
name = os.path.basename(path)
files[name] = read_text(path)
return jsonify({"files": files, "dir": gold_dir})
@app.route("/api/list-scripts")
def list_scripts():
scripts = [f for f in os.listdir(BASE_DIR) if f.endswith(".sh")]
return jsonify({"scripts": sorted(scripts)})
@app.route("/api/create-inputs", methods=["POST"])
def create_inputs():
"""
POST /api/create-inputs
Body: {sentences: [{uid, sentence}], backup: true}
Backs up current input/ files, then writes one s{uid}.txt per sentence.
Call /api/restore-inputs afterwards to put original files back.
"""
import shutil
data = request.get_json(force=True)
sentences = data.get("sentences", [])
do_backup = data.get("backup", True)
input_dir = safe_path("input")
backup_dir = safe_path("input_backup_csv")
output_dir = safe_path("output")
os.makedirs(input_dir, exist_ok=True)
# Backup existing input files so we can restore after CSV run
if do_backup:
if os.path.isdir(backup_dir):
shutil.rmtree(backup_dir)
shutil.copytree(input_dir, backup_dir)
# Clear input dir of .txt files
for f in glob.glob(os.path.join(input_dir, "*.txt")):
os.remove(f)
# Clear output dir too so old .ann files don't pollute results
if os.path.isdir(output_dir):
for f in glob.glob(os.path.join(output_dir, "*.ann")):
os.remove(f)
created = []
for s in sentences:
uid = s["uid"]
sentence = s["sentence"].strip()
fname = f"s{uid}.txt"
write_text(os.path.join(input_dir, fname), sentence + "\n")
created.append(fname)
return jsonify({"ok": True, "created": created, "count": len(created),
"input_dir": input_dir, "backed_up": do_backup})
@app.route("/api/restore-inputs", methods=["POST"])
def restore_inputs():
"""
POST /api/restore-inputs
Restores original input/ files from backup made by /api/create-inputs.
"""
import shutil
input_dir = safe_path("input")
backup_dir = safe_path("input_backup_csv")
if not os.path.isdir(backup_dir):
return jsonify({"ok": True, "restored": False, "note": "No backup found — nothing to restore"})
if os.path.isdir(input_dir):
shutil.rmtree(input_dir)
shutil.copytree(backup_dir, input_dir)
shutil.rmtree(backup_dir)
return jsonify({"ok": True, "restored": True})
# ── Entry point ────────────────────────────────────────────────────────────────
if __name__ == "__main__":
print(f"\n{'='*55}")
print(f" REFINE Local Bridge Server")
print(f" Base directory : {BASE_DIR}")
print(f" Listening on : http://localhost:{PORT}")
print(f"{'='*55}")
print(f" Open in browser : http://localhost:{PORT}")
print(f" Press Ctrl+C to stop.\n")
app.run(host="127.0.0.1", port=PORT, debug=False)