-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
543 lines (449 loc) · 17.6 KB
/
api.py
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import json
import os
import re
from subprocess import call
from flask import Flask, flash, request, redirect
from flask import Response
from flask_cors import CORS
from werkzeug.utils import secure_filename
import form_updater
import util
from csv_to_form import parse_questions_from_feature_csv as parse_questions
from worker import *
from forms import *
application = Flask(__name__)
CORS(application)
def clean_text(text):
return (re.sub(r"""
[,.;@#?!&$]+
\ *
""",
"_",
text.lower(), flags=re.VERBOSE)).replace(' ', '_')
def allowed_file(filename):
allowed_extensions = {'csv'}
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in allowed_extensions
def get_files(files, path):
for (directory_path, directory_names, file_names) in os.walk(path):
for d in directory_names:
get_files(files, directory_path + '/' + d)
for f in file_names:
if f.endswith('nlpql'):
path = (directory_path + '/' + f[0:f.find('.')]).replace('nlpql/', '')
files.append(path)
def get_nlpql_options(with_sorting=True):
results = list()
get_files(results, 'nlpql')
unique = list(set(results))
if with_sorting:
return sorted(unique)
else:
return unique
def get_json(files, path):
for (directory_path, directory_names, file_names) in os.walk(path):
for d in directory_names:
get_json(files, directory_path + '/' + d)
for f in file_names:
if f.endswith('json'):
path = (directory_path + '/' + f[0:f.find('.')]).replace('nlpql/', '')
files.append(path)
def get_nlpql_forms(results=None, with_sorting=True):
results = list()
get_json(results, 'nlpql')
limited_results = list()
for r in results:
if r.endswith('questions'):
limited_results.append(r)
unique = list(set(limited_results))
if with_sorting:
return sorted(unique)
else:
return unique
def valid_job(job_type):
return job_type in get_nlpql_options(with_sorting=False)
def get_api_routes():
return ', '.join(get_nlpql_options())
@application.route("/")
def hello():
util.set_logger(application.logger)
return "Welcome to ClarityNLPaaS."
def get_host(r):
# print(request.headers)
if r.is_secure:
return 'https://{0}/'.format(r.host)
else:
return 'http://{0}/'.format(r.host)
@application.route('/form_update', methods=['GET', 'POST'])
def upload_file():
util.set_logger(application.logger)
status = ''
if request.method == 'POST':
status = form_updater.update_form(request.form.get('slug'), request.form)
radios = ''
forms = get_question_list()
slugs = set()
for f in forms:
slugs.add(f.get('url'))
for s in slugs:
radios = radios + form_updater.radio_template.format(s, s, s, s) + '\n'
form = '''
<form method=post>
<p>
{}
</p>
<p> <label for="lname">Google Doc </label><br>
<input type="text" id="url" name="url">
</p>
<p>
<input type=submit value="Update Form" class="btn btn-success">
</p>
<br>
<p>
{}
<p>
</form>
'''.format(radios, status)
return form_updater.template % ("NLPQL/CQL Feature CSV Parser", form)
@application.route("/report/upload", methods=['POST'])
def upload_report():
util.set_logger(application.logger)
if request.method == 'POST':
# Checking if the selected job is valid
data = request.get_json()
status, source_id, report_ids, is_fhir_resource, report_payload = upload_reports(data)
if not status:
return Response(json.dumps({
'message': 'Could not upload reports to Solr. Reason: ' + source_id,
'success': 'false'
}, indent=4),
status=500,
mimetype='application/json')
else:
return Response(json.dumps({
'message': 'Success',
'source_id': source_id,
'reports': report_ids,
'success': 'true'
}, indent=4),
status=200,
mimetype='application/json')
else:
return Response("Invalid route. Only POST accepted",
status=200,
mimetype='text/plain')
@application.route("/report/delete/<source_id>", methods=['POST'])
def delete_reports(source_id: str):
util.set_logger(application.logger)
if request.method == 'POST':
# Checking if the selected job is valid
delete_obj = delete_report(source_id)
if not delete_obj[0]:
return Response(
json.dumps({
'message': 'Could not delete reports from Solr. Reason: ' + delete_obj[1],
'success': 'false'
}, indent=4),
status=500,
mimetype='application/json')
else:
return Response(json.dumps({
'success': 'true',
'message': 'Success',
'reason': delete_obj[1]
}, indent=4),
status=200,
mimetype='application/json')
else:
return Response("Invalid route. Only POST accepted",
status=200,
mimetype='text/plain')
@application.route("/jobs", methods=['POST'])
def submit_job_with_nlpql(j):
util.set_logger(application.logger)
the_json = request.get_json()
nlpql = the_json.get('nlpql', '')
try:
async_job = request.args.get('async') == 'true'
except:
async_job = False
try:
return_null_results = request.args.get('return_null_results') == 'true'
except:
return_null_results = False
synchronous = not async_job
if nlpql == '':
return Response(json.dumps({'message': 'Invalid body for this endpoint. Please make sure NLPQL is passed in.'},
indent=4, sort_keys=True), status=400,
mimetype='application/json')
return worker('', the_json, synchronous=synchronous, return_null_results=return_null_results, nlpql=nlpql)
@application.route("/job/<job_category>/<job_subcategory>/<job_name>", methods=['POST', 'GET'])
def submit_job_with_subcategory(job_category: str, job_subcategory: str, job_name: str):
util.set_logger(application.logger)
"""
API for triggering jobs
"""
h = get_host(request)
# print(h)
try:
async_job = request.args.get('async') == 'true'
except:
async_job = False
try:
return_null_results = request.args.get('return_null_results') == 'true'
except:
return_null_results = False
synchronous = not async_job
job_type = "{}/{}/{}".format(job_category, job_subcategory, job_name)
job_file_path = "./nlpql/" + job_type + ".nlpql"
if not valid_job(job_type):
return Response(json.dumps({'message': 'Invalid API route. Valid Routes: ' + get_api_routes()}, indent=4,
sort_keys=True), status=400,
mimetype='application/json')
if request.method == 'POST':
# Checking if the selected job is valid
data = request.get_json()
return worker(job_file_path, data, synchronous=synchronous, return_null_results=return_null_results)
else:
return Response(get_nlpql(job_file_path),
status=200,
mimetype='text/plain')
@application.route("/job/<job_category>/<job_name>", methods=['POST', 'GET'])
def submit_job_with_category(job_category: str, job_name: str):
"""
API for triggering jobs
"""
util.set_logger(application.logger)
h = get_host(request)
# print(h)
try:
async_arg = request.args.get('async').lower()
async_job = async_arg == 'true' or async_arg == 't' or async_arg == '1'
except:
async_job = False
synchronous = not async_job
job_type = "{}/{}".format(job_category, job_name)
job_file_path = "./nlpql/" + job_type + ".nlpql"
if not valid_job(job_type):
return Response(json.dumps({'message': 'Invalid API route. Valid Routes: ' + get_api_routes()}, indent=4,
sort_keys=True), status=400,
mimetype='application/json')
if request.method == 'POST':
# Checking if the selected job is valid
data = request.get_json()
return worker(job_file_path, data, synchronous=synchronous)
else:
return Response(get_nlpql(job_file_path),
status=200,
mimetype='text/plain')
@application.route("/job/<job_type>", methods=['POST', 'GET'])
def submit_job(job_type: str):
"""
API for triggering jobs
"""
util.set_logger(application.logger)
h = get_host(request)
# print(h)
job_type = job_type.replace('~', '/')
job_file_path = "./nlpql/" + job_type + ".nlpql"
valid = valid_job(job_type)
try:
async_job = request.args.get('async') == 'true'
except:
async_job = False
try:
return_null_results = request.args.get('return_null_results') == 'false'
except:
return_null_results = False
synchronous = not async_job
if request.method == 'POST':
# Checking if the selected job is valid
if (job_type == 'validate_nlpql' or job_type == 'nlpql_tester') and request.data:
_, res = submit_test(request.data)
return json.dumps(res, indent=4, sort_keys=True)
elif job_type == 'register_nlpql' and request.data:
res = request.data.decode("utf-8")
return json.dumps(add_custom_nlpql(res), indent=4, sort_keys=True)
elif job_type == 'results' and request.data:
res = request.get_json()
return async_results(res['job_id'], res['source_id'], return_null_results=return_null_results)
else:
if not valid:
return Response(
json.dumps({'message': 'Invalid API route. Valid Routes: ' + get_api_routes()}, indent=4,
sort_keys=True), status=400,
mimetype='application/json')
data = request.get_json()
return worker(job_file_path, data, synchronous=synchronous)
else:
if not valid:
return Response(json.dumps({'message': 'Invalid API route. Valid Routes: ' + get_api_routes()}, indent=4,
sort_keys=True), status=400,
mimetype='application/json')
return Response(get_nlpql(job_file_path),
status=200,
mimetype='text/plain')
@application.route("/job/results/<job_id>", methods=['GET'])
def get_results(job_id):
"""
API for getting Job results
"""
util.set_logger(application.logger)
if request.method == 'GET':
return get_results(job_id)
else:
return Response(json.dumps({'message': 'API supports only GET requests'}, indent=4, sort_keys=True), status=400,
mimetype='application/json')
@application.route("/update/nlpql", methods=['GET'])
def update_nlpql():
util.set_logger(application.logger)
os.environ['CUSTOM_DIR'] = util.custom_nlpql_folder
os.environ['CUSTOM_S3_URL'] = util.custom_nlpql_s3_bucket
call(['sh /api/load_nlpql.sh'])
return "NLPQL update triggered"
@application.route("/job/list/all", methods=['GET'])
def get_nlpql_list():
"""
API for getting NLPQL Options
"""
util.set_logger(application.logger)
if request.method == 'GET':
return Response(json.dumps(get_nlpql_options()), status=200, mimetype='application/json')
else:
return Response(json.dumps({'message': 'API supports only GET requests'}, indent=4, sort_keys=True), status=400,
mimetype='application/json')
@application.route("/forms", methods=['GET'])
def get_question_list():
"""
API for getting NLPQL questions
"""
util.set_logger(application.logger)
if request.method == 'GET':
form_display = list()
forms = get_nlpql_forms()
for f in forms:
form_obj = dict()
form_obj['url'] = f
keys = f.split('/')
if len(keys) == 0:
slug = "unknown"
elif len(keys) > 1:
slug = keys[-2]
else:
slug = keys[-1]
form_obj['slug'] = slug
if len(keys) == 3:
form_res = get_form_with_subcategory(keys[0], keys[1], keys[2])
elif len(keys) == 2:
form_res = get_form_with_category(keys[0], keys[1])
else:
form_res = get_form(keys[0])
try:
the_form = form_res.get_json()
except Exception as ex:
the_form = dict()
util.log(ex, util.ERROR)
the_form_name = the_form.get("name", "Unknown")
form_obj['name'] = the_form_name
the_form_owner = the_form.get("owner", "Unknown")
form_obj['owner'] = the_form_owner
the_form_version = the_form.get("version", "-1")
form_obj['version'] = the_form_version
the_form_description = the_form.get('description', '')
form_obj['description'] = the_form_description
form_display.append(form_obj)
return Response(json.dumps(form_display, indent=4), status=200, mimetype='application/json')
else:
return Response(json.dumps({'message': 'API supports only GET requests'}, indent=4, sort_keys=True), status=400,
mimetype='application/json')
@application.route("/form/<form_category>/<form_subcategory>/<form_name>", methods=['POST', 'GET'])
def get_form_with_subcategory(form_category: str, form_subcategory: str, form_name: str):
util.set_logger(application.logger)
file_type = "{}/{}/{}".format(form_category, form_subcategory, form_name)
file_path = "./nlpql/" + file_type + ".json"
return Response(get_file(file_path),
status=200,
mimetype='application/json')
@application.route("/form/<form_category>/<form_name>", methods=['POST', 'GET'])
def get_form_with_category(form_category: str, form_name: str):
util.set_logger(application.logger)
file_type = "{}/{}".format(form_category, form_name)
file_path = "./nlpql/" + file_type + ".json"
return Response(get_file(file_path),
status=200,
mimetype='application/json')
@application.route("/form/<form_type>", methods=['POST', 'GET'])
def get_form(form_type: str):
util.set_logger(application.logger)
form_type = form_type.replace('~', '/')
file_path = "./nlpql/" + form_type + ".json"
return Response(get_file(file_path),
status=200,
mimetype='application/json')
@application.route("/upload/form", methods=['POST', 'GET'])
def upload_form():
util.set_logger(application.logger)
upload_folder = 'nlpql/NLPQL_form_content'
if not os.path.exists(upload_folder):
os.makedirs(upload_folder)
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
form_name = request.form['formname']
if not form_name or len(form_name) == 0:
return redirect(request.url)
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(upload_folder, filename)
file.save(filepath)
status = "File parsed successfully. Added to available forms."
try:
parse_questions(folder_prefix=clean_text(form_name),
form_name=form_name.replace('_', ' '),
file_name=filepath,
output_dir=upload_folder)
except Exception as ex:
status = "Failed to upload, {}".format(repr(ex))
util.log(ex, util.ERROR)
response_data = {
'status': status,
'available_forms': get_nlpql_forms()
}
return Response(json.dumps(response_data, indent=4),
status=200,
mimetype='application/json')
else:
return '''
<!doctype html>
<head>
<title>Upload CSV for NLPQL Form Parsing</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Upload CSV for NLPQL Form Parsing</h1>
<br>
<div>
<a href="https://github.com/ClarityNLP/Utilities/blob/master/custom_query/afib.csv" target="_blank">
(See sample CSV)
</a>
</div>
<form method=post enctype=multipart/form-data>
<br>
<input type=text name="formname" class="form-control" placeholder="Form Name">
<br>
<input type=file name=file class="form-control-file">
<br>
<input type=submit value=Upload class="btn btn-primary">
</form>
</div>
</body>
'''
if __name__ == '__main__':
util.app_token()
application.run(host='0.0.0.0', port=5000, debug=True)
util.set_logger(application.logger)