-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·737 lines (589 loc) · 19.3 KB
/
app.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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#!/usr/bin/env python
# encoding: utf-8
from flasgger import Swagger
from flask import Flask, g, \
jsonify, make_response, redirect, render_template, render_template_string, \
request, safe_join, send_file, send_from_directory, session, url_for
from flask_caching import Cache
from flask_cors import CORS
from http import HTTPStatus
from pathlib import Path
from richcontext import server as rc_server
import argparse
import codecs
import copy
import csv
import datetime
import diskcache as dc
import hashlib
import json
import jwt
import os
import string
import sys
import traceback
import tempfile
import time
import uuid
######################################################################
## web app definitions
class RCServerApp (Flask):
DEFAULT_CORPUS = "min_kg.jsonld"
DEFAULT_PRECOMPUTE = False # CLI flag - pre-compute results
DEFAULT_PORT = 5000 # CLI arg - port used for dev/test
DEFAULT_SCHEME = "https" # CLI arg - HTTP scheme for OpenAPI
DEFAULT_TOKEN = None # CLI arg - input TSV file for web tokens
PATH_DC_CACHE = "/tmp/richcontext" # TODO: move to flask.cfg
def __init__ (self, name, no_load=False):
"""
initialize the web app
"""
super(RCServerApp, self).__init__(name, static_folder="static", template_folder="templates")
self.config.from_pyfile("flask.cfg")
self.disk_cache = dc.Cache(self.PATH_DC_CACHE)
self.corpus_path = Path(self.DEFAULT_CORPUS)
self.net = rc_server.RCNetwork()
self.net.setup_render(self.template_folder)
if not no_load:
self.links = self.net.deserialize()
######################################################################
## support for pre-computing and caching results
@classmethod
def get_hash (cls, strings, prefix=None, digest_size=10):
"""
construct a unique identifier from a collection of strings
"""
m = hashlib.blake2b(digest_size=digest_size)
for elem in sorted(map(lambda x: x.encode("utf-8").lower().strip(), strings)):
m.update(elem)
if prefix:
id = prefix + m.hexdigest()
else:
id = m.hexdigest()
return "".join(filter(lambda x: x in string.printable, id))
def build_links (self):
elapsed_time = self.net.load_network(self.corpus_path)
print("{:.2f} ms corpus parse time".format(elapsed_time))
t0 = time.time()
links = self.net.render_links()
t1 = time.time()
print("{:.2f} ms link format time".format((t1 - t0) * 1000.0))
print(f"{len(self.net.labels)} elements in the knowledge graph")
return links
######################################################################
## manage web tokens, scoped roles, and identifying HITL feedback
SCOPE_AGENCY = "agency"
SCOPE_CI = "ci"
SCOPE_EXPERT = "expert"
SCOPE_OPS = "ops"
SCOPE_TEMPLATE = {
"id": "email@agency.gov",
"roles": [ SCOPE_AGENCY ]
}
JWT_ISSUER = "urn:coleridgeinitiative.org:richcontext"
LEEWAY_DELTA = datetime.timedelta(minutes=3)
@classmethod
def jwt_encode (cls, key, expiry, scopes):
"""
encode a JWT payload for a web token
"""
payload = {
"iss": cls.JWT_ISSUER,
"exp": datetime.datetime.utcnow() + expiry,
"sco": scopes
}
return jwt.encode(payload, key, algorithm="HS256").decode("utf-8")
@classmethod
def jwt_decode (cls, key, token):
"""
decode and verify a JWT payload for a web token
"""
payload = jwt.decode(
token,
key,
algorithms=["HS256"],
leeway=cls.LEEWAY_DELTA,
issuer=cls.JWT_ISSUER
)
return payload["sco"]
def generate_tokens (self, token_input):
"""
generate a list of web tokens based on an input file
"""
results = []
# parse the input and generate a web token for each entry
with codecs.open(Path(token_input), "r", encoding="utf8") as f:
reader = csv.reader(f, delimiter="\t")
next(reader) # skip headers
for email, expiry, roles in reader:
scope = copy.deepcopy(self.SCOPE_TEMPLATE)
try:
scope["id"] = email
scope["roles"] = [ r.lower().strip() for r in roles.split(",") ]
expiry_days = int(expiry)
expiry = datetime.timedelta(days=expiry_days)
token = self.jwt_encode(self.config["SECRET_KEY"], expiry, scope)
results.append([ email, token ])
except:
traceback.print_exc()
print("bad format: |{}| |{}| |{}|".format(email, expiry, roles))
# write the results to send to users
out_path = Path("token.txt")
with codecs.open(out_path, "wb", encoding="utf8") as f:
for email, token in results:
f.write(str(self.jwt_decode(self.config["SECRET_KEY"], token)))
f.write("\n\n")
f.write(token)
f.write("\n\n\n")
print(f"{len(results)} web tokens generated and saved in {out_path}")
######################################################################
## support for API calls to query the KG
def get_entity_phrases (self):
"""
get the phrases used for autocompletion
"""
response = []
status = HTTPStatus.OK.value
for id, entity in self.net.prov.items():
if "used" in entity.view:
response.append({
"text": entity.view["title"],
"kind": "provider"
})
for id, entity in self.net.data.items():
if "used" in entity.view:
response.append({
"text": entity.view["title"],
"kind": "provider"
})
for id, entity in self.net.jour.items():
if "used" in entity.view:
response.append({
"text": entity.view["title"],
"kind": "provider"
})
return response, status
def get_entity_links (self, index):
"""
render HTML for the link viewer for the entity referenced by
`index`
"""
html = None
status = HTTPStatus.BAD_REQUEST.value
try:
id = int(index)
except:
id = -1
if id >= 0 and id < len(self.net.id_list):
uuid = self.net.id_list[id]
if uuid in self.net.auth:
html = self.net.render_auth(self.net.auth[uuid], rerank=session["last_node"])
elif uuid in self.links:
html = self.links[uuid]
if html:
status = HTTPStatus.OK.value
return html, status
def extract_query_home (self, request):
"""
extract and validate the query parameters from an HTTP request
"""
query = request.args.to_dict()
if "entity" in query:
query["entity"] = query["entity"].strip()
# TODO: remove invalid or unknown entity names
if len(query["entity"]) < 1:
del query["entity"]
if "radius" in query:
try:
radius = int(query["radius"].strip())
except:
# force a valid radius value
radius = 2
finally:
query["radius"] = str(radius)
return query
def run_entity_query (self, radius, entity):
"""
run a neighborhood query for the given entity and radius
"""
t0 = time.time()
entity = entity.strip()
try:
radius_val = int(radius)
radius_val = max(radius_val, 1)
radius_val = min(radius_val, 10)
except:
radius_val = 2
cache_token = self.get_hash([ entity, str(radius_val) ], prefix="hood-")
handle, html_path = tempfile.mkstemp(suffix=".html", prefix="rc_hood", dir="/tmp")
subgraph, paths, node_id = self.net.get_subgraph(entity, radius_val)
hood = self.net.extract_neighborhood(radius_val, subgraph, paths, node_id, html_path)
session["last_node"] = node_id
with open(html_path, "r") as f:
html = f.read()
self.disk_cache[cache_token] = html
os.remove(html_path)
response = hood.serialize(t0, cache_token)
status = HTTPStatus.OK.value
return response, status
def fetch_graph (self, cache_token):
"""
fetch the HTML to render the graph diagram referenced by the
`cache_token` parameter
"""
if cache_token in self.disk_cache:
html = self.disk_cache[cache_token]
response = render_template_string(html)
status = HTTPStatus.OK.value
else:
response = f"<strong>NOT FOUND: {cache_token}</strong>"
status = HTTPStatus.BAD_REQUEST.value
return response, status
APP = RCServerApp(__name__)
CACHE = Cache(APP, config={"CACHE_TYPE": "simple"})
CORS(APP)
######################################################################
## session management
def update_session ():
session.modified = True
if not "uuid" in session:
session["uuid"] = uuid.uuid4().hex
try:
session["counter"] += 1
except KeyError:
session["counter"] = 1
session.permanent = True
@APP.route("/dump/session/")
def dump_session ():
response = make_response(repr(session))
response.content_type = "text/plain"
return response
######################################################################
## page routes
@APP.route("/index.html")
@APP.route("/home/")
def home_redirects ():
return redirect(url_for("home_page"))
@APP.route("/")
def home_page ():
update_session()
query = APP.extract_query_home(request)
return render_template("index.html", query=query)
@APP.route("/feedback")
@APP.route("/feedback/")
@APP.route("/feedback.html")
@APP.route("/hitl.html")
@APP.route("/hitl")
def hitl_redirect ():
return redirect(url_for("hitl_page"))
@APP.route("/hitl/")
def hitl_page ():
update_session()
return render_template("hitl.html")
@APP.route("/research")
@APP.route("/research.html")
@APP.route("/workbench")
@APP.route("/workbench.html")
@APP.route("/work")
@APP.route("/work.html")
def work_redirect ():
return redirect(url_for("work_page"))
@APP.route("/work/")
def work_page ():
update_session()
return render_template("work.html")
@APP.route("/settings")
@APP.route("/settings/")
@APP.route("/configure")
@APP.route("/configure/")
@APP.route("/config")
@APP.route("/config/")
@APP.route("/conf")
@APP.route("/conf.html")
def conf_redirect ():
return redirect(url_for("conf_page"))
@APP.route("/conf/")
def conf_page ():
update_session()
if "token" in session:
token = session["token"]
else:
token = None
return render_template("conf.html", token=token)
@APP.route("/test")
def test_page ():
"""
route reserved for testing
"""
return render_template("test.html")
## CSS, JavaScript, images, etc.
@APP.route("/css/pure-min.css")
@APP.route("/css/grids-responsive-min.css")
@APP.route("/magnify.svg")
## plus other well-known routes
@APP.route("/favicon.png")
@APP.route("/apple-touch-icon.png")
def static_from_root ():
return send_from_directory(APP.static_folder, request.path[1:])
######################################################################
## OpenAPI support
API_TEMPLATE = {
"swagger": "2.0",
"info": {
"title": "Rich Context",
"description": "Rich Context microservices based on OpenAPI",
"contact": {
"responsibleOrganization": "Coleridge Initiative",
"name": "API Support",
"url": "https://coleridgeinitiative.org/richcontext"
},
"termsOfService": "https://coleridgeinitiative.org/computing"
},
"basePath": "/",
"schemes": [ APP.DEFAULT_SCHEME, "http" ],
"externalDocs": {
"description": "Documentation",
"url": "https://github.com/Coleridge-Initiative/RCServer"
}
}
SWAGGER = Swagger(APP, template=API_TEMPLATE)
######################################################################
## API routes
@CACHE.cached(timeout=3000)
@APP.route("/api/v1/lookup/<entity>", methods=["GET"])
def api_lookup_entity (entity):
"""
lookup metadata for a given entity
---
tags:
- knowledge_graph
description: 'lookup metadata for a given entity'
parameters:
- name: entity
in: path
required: true
type: string
description: entity UUID
produces:
- application/json
responses:
'200':
description: JSON description of the entity metadata
'400':
description: bad request; is the entity UUID correct?
"""
update_session()
response = APP.net.lookup_entity(entity)
if not response:
status = HTTPStatus.BAD_REQUEST.value
else:
status = HTTPStatus.OK.value
return jsonify(response), status
@CACHE.cached(timeout=3000)
@APP.route("/api/v1/phrases", methods=["GET"])
def api_entity_phrases ():
"""
get the list of entity phrases for autocompletion
---
tags:
- web_app
description: 'get the entity phrases used for autocompletion'
produces:
- application/json
responses:
'200':
description: phrases used for autocompletion
"""
update_session()
response, status = APP.get_entity_phrases()
return jsonify(response), status
@CACHE.cached(timeout=3000)
@APP.route("/api/v1/query/<radius>/<entity>", methods=["GET"])
def api_entity_query (radius, entity):
"""
query a subgraph for an entity
---
tags:
- web_app
description: 'query with a radius near an entity, using BFS'
parameters:
- name: radius
in: path
required: true
type: integer
description: radius for BFS neighborhood
- name: entity
in: path
required: true
type: string
description: entity name to search
produces:
- application/json
responses:
'200':
description: neighborhood search within the knowledge graph
"""
update_session()
response, status = APP.run_entity_query(radius, entity)
return response, status
@CACHE.cached(timeout=3000)
@APP.route("/api/v1/links/<index>", methods=["GET"])
def api_entity_links (index):
"""
lookup the links for an entity
---
tags:
- web_app
description: 'lookup the links for an entity'
parameters:
- name: index
in: path
required: true
type: integer
description: index of entity to lookup
produces:
- application/json
responses:
'200':
description: links for an entity within the knowledge graph
'400':
description: bad request; is the `index` parameter valid?
"""
update_session()
html, status = APP.get_entity_links(index)
return jsonify(html), status
@APP.route("/api/v1/conf_web_token/", methods=["POST"])
def conf_post_web_token ():
"""
set a web token
---
tags:
- configuration
description: 'set a web token'
parameters:
- name: token
in: formData
required: true
type: string
description: set a web token for specifying roles and identifying HITL feedback from a known user
produces:
- application/json
responses:
'200':
description: web token was set
'400':
description: bad request; is the web token correct?
"""
update_session()
try:
#print(request.form)
token = request.form["token"].strip()
#print(token)
payload = APP.jwt_decode(APP.config["SECRET_KEY"], token)
#print(payload)
except:
traceback.print_exc()
payload = None
if payload:
session["token"] = token
session["roles"] = payload["roles"]
response = "web token setting succeeded"
status = HTTPStatus.OK.value
else:
response = "web token value was not valid"
status = HTTPStatus.BAD_REQUEST.value
return jsonify(response), status
@CACHE.cached(timeout=3000)
@APP.route("/api/v1/download/<entity>", methods=["GET"])
def api_download_links (entity):
"""
download the links for a given entity
---
tags:
- knowledge_graph
description: 'initiate a download of the links for a given entity'
parameters:
- name: entity
in: path
required: true
type: string
description: entity UUID
produces:
- application/json
responses:
'200':
description: initiates an browser-based download
'400':
description: bad request; is the entity UUID correct?
"""
update_session()
if entity not in APP.net.data:
response = "there is no entity with that UUID in the graph"
status = HTTPStatus.BAD_REQUEST.value
else:
data_rows, data_name = APP.net.download_links(entity)
filename = "export-{}.csv".format(data_name)
response = make_response(data_rows)
response.headers["Content-Type"] = "text/csv"
response.headers["Content-Disposition"] = ("attachment; filename=%s" % filename)
status = HTTPStatus.OK.value
return response, status
@CACHE.cached(timeout=3000)
@APP.route("/graph/<cache_token>", methods=["GET"])
def fetch_graph_html (cache_token):
"""
fetch the HTML to render a cached network diagram
"""
update_session()
response, status = APP.fetch_graph(cache_token)
return response, status
######################################################################
## main
def main (args):
"""
dev/test entry point
"""
global APP
if args.token:
# generate a list of web tokens based on an input file
APP.generate_tokens(args.token)
elif args.pre:
# pre-compute KG links as the `precomp.json` file
print(f"pre-computing links with: {args.corpus}")
APP = RCServerApp(__name__, no_load=True)
APP.corpus_path = Path(args.corpus)
links = APP.build_links()
APP.net.serialize(links)
else:
# run the app in a test environment
APP.run(host="0.0.0.0", port=args.port, debug=True)
if __name__ == "__main__":
# parse the command line arguments, if any
parser = argparse.ArgumentParser(
description="Rich Context: server, web app, API, UI"
)
parser.add_argument(
"--port",
type=int,
default=APP.DEFAULT_PORT,
help="web IP port"
)
parser.add_argument(
"--corpus",
type=str,
default=APP.DEFAULT_CORPUS,
help="corpus file as JSON-LD"
)
parser.add_argument(
"--pre",
type=bool,
default=APP.DEFAULT_PRECOMPUTE,
help="pre-compute links with the corpus file"
)
parser.add_argument(
"--token",
type=str,
default=APP.DEFAULT_TOKEN,
help="input TSV file for generating web tokens"
)
main(parser.parse_args())