-
Notifications
You must be signed in to change notification settings - Fork 15
/
serve.py
272 lines (235 loc) · 7.39 KB
/
serve.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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import json
import logging
import os
import sys
from datetime import datetime
from functools import wraps
from os import path
import SPARQLWrapper
from cachetools import LFUCache
from rdflib import URIRef
from splendid import make_dirs_for
from splendid import timedelta_to_s
from flask import Flask
from flask import abort
from flask import jsonify
from flask import request
from flask_cors import CORS
# noinspection PyUnresolvedReferences
import logging_config
# not all import on top due to scoop and init...
logger = logging.getLogger(__name__)
app = Flask(__name__)
CORS(app)
@app.route("/api/ping", methods=["GET"])
def ping():
return jsonify({
'success': True
})
@app.route("/api/graph_patterns", methods=["GET"])
def graph_patterns():
global GPS_DICT
if not GPS_DICT:
GPS_DICT = {
'graph_patterns': [
{
k: v
for k, v in gp.to_dict().items()
if k in (
'fitness',
'fitness_weighted',
'fitness_description',
'sparql',
'graph_triples',
# 'matching_node_pairs',
# 'gtp_precisions',
'prefixes',
)
}
for gp in GPS
],
}
return jsonify(GPS_DICT)
@app.route("/api/predict", methods=["POST"])
def predict():
source = request.form.get('source')
# logger.info(request.data)
# logger.info(request.args)
# logger.info(request.form)
if not source:
abort(400, 'no source given')
logger.info('predicting: %s', source)
source = URIRef(source)
return jsonify(PREDICT_CACHE[source])
def _predict(source):
from gp_query import calibrate_query_timeout
from predict import predict
timeout = TIMEOUT if TIMEOUT > 0 else calibrate_query_timeout(SPARQL)
return predict(
SPARQL, timeout, GPS, source,
FUSION_METHODS, MAX_RESULTS, MAX_TARGET_CANDIDATES_PER_GP)
@app.route("/api/feedback", methods=["POST"])
def feedback():
# TODO: add timestamps, ips, log to different file
fb = {
'source': request.form.get('source'),
'target': request.form.get('target'),
'feedback': request.form.get('feedback') == 'true',
'fusion_method': request.form.get('fusion_method'),
'rank': int(request.form.get('rank')),
}
logger.info('received feedback: %s', json.dumps(fb))
res = {
'success': True,
'msg': 'thanks ;)',
}
return jsonify(res)
def parse_args():
import argparse
parser = argparse.ArgumentParser(
description='gp learner prediction model server',
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
# flask settings
parser.add_argument(
"--host",
help="listen on IP",
action="store",
default="0.0.0.0",
)
parser.add_argument(
"--port",
help="port to listen on",
action="store",
default="8080",
)
parser.add_argument(
"--flask_debug",
help="flask debug mode",
action="store_true",
default=False,
)
# gp learner settings
parser.add_argument(
"--resdir",
help="result directory of the model to serve (overrides --RESDIR)",
action="store",
required=True,
)
parser.add_argument(
"--sparql_endpoint",
help="the SPARQL endpoint to query",
action="store",
default=config.SPARQL_ENDPOINT,
)
parser.add_argument(
"--associations_filename",
help="ground truth source target file used for training and evaluation",
action="store",
default=config.GT_ASSOCIATIONS_FILENAME,
)
parser.add_argument(
"--max_queries",
help="limits the amount of queries per prediction (0: no limit)",
action="store",
type=int,
default=100,
)
parser.add_argument(
"--clustering_variant",
help="if specified use this clustering variant for query reduction, "
"otherwise select the best from various.",
action="store",
type=str,
default=None,
)
parser.add_argument(
"--print_query_patterns",
help="print the graph patterns which are used to make predictions",
action="store_true",
default=False,
)
parser.add_argument(
"--fusion_methods",
help="Which fusion methods to train / use. During prediction, each of "
"the learned patterns can generate a list of target candidates. "
"Fusion allows to re-combine these into a single ranked list of "
"predicted targets. By default this will train and use all "
"implemented fusion methods. Any of them, or a ',' delimited list "
"can be used to reduce the output (just make sure you ran "
"--predict=train_set on them before). Also supports 'basic' and "
"'classifier' as shorthands.",
action="store",
type=str,
default=None,
)
# serve specific configs
parser.add_argument(
"--timeout",
help="sets the timeout in seconds for each query (0: auto calibrate)",
action="store",
type=float,
default=.5,
)
parser.add_argument(
"--max_results",
help="limits the result list lengths to save bandwidth (0: no limit)",
action="store",
type=int,
default=100,
)
parser.add_argument(
"--max_target_candidates_per_gp",
help="limits the target candidate list lengths to save bandwidth "
"(0: no limit)",
action="store",
type=int,
default=100,
)
parser.add_argument(
"--predict_cache_size",
help="how many prediction results to cache",
action="store",
type=int,
default=1000,
)
cfg_group = parser.add_argument_group(
'Advanced config overrides',
'The following allow overriding default values from config/defaults.py'
)
config.arg_parse_config_vars(cfg_group)
prog_args = vars(parser.parse_args())
# the following were aliased above, make sure they're updated globally
prog_args.update({
'SPARQL_ENDPOINT': prog_args['sparql_endpoint'],
'GT_ASSOCIATIONS_FILENAME': prog_args['associations_filename'],
'RESDIR': prog_args['resdir'],
})
config.finalize(prog_args)
return prog_args
def init(**kwds):
from gp_learner import main
return main(**kwds)
if __name__ == "__main__":
logger.info('init run: origin')
import config
prog_kwds = parse_args()
SPARQL, GPS, FUSION_METHODS = init(**prog_kwds)
TIMEOUT = prog_kwds['timeout']
MAX_RESULTS = prog_kwds['max_results']
MAX_TARGET_CANDIDATES_PER_GP = prog_kwds['max_target_candidates_per_gp']
GPS_DICT = None
PREDICT_CACHE = LFUCache(prog_kwds['predict_cache_size'], _predict)
if prog_kwds['flask_debug']:
logger.warning('flask debugging is active, do not use in production!')
app.run(
host=prog_kwds['host'],
port=prog_kwds['port'],
debug=prog_kwds['flask_debug'],
)
else:
logger.info('init run: worker')