-
Notifications
You must be signed in to change notification settings - Fork 58
/
server.py
260 lines (220 loc) · 8.08 KB
/
server.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
"""HTTP API implementation.
"""
import sys
from docopt import docopt
from flask import Flask
from flask import make_response
from flask import request
import numpy as np
import ujson
from werkzeug.exceptions import BadRequest
from . import __version__
from .interfaces import PredictError
from .util import args_from_config
from .util import get_config
from .util import get_metadata
from .util import initialize_config
from .util import logger
from .util import memory_usage_psutil
from .util import PluggableDecorator
from .util import process_store
app = Flask(__name__)
def make_ujson_response(obj, status_code=200):
"""Encodes the given *obj* to json and wraps it in a response.
:return:
A Flask response.
"""
json_encoded = ujson.encode(obj, ensure_ascii=False, double_precision=-1)
resp = make_response(json_encoded)
resp.mimetype = 'application/json'
resp.content_type = 'application/json; charset=utf-8'
resp.status_code = status_code
return resp
class PredictService:
"""A default :class:`palladium.interfaces.PredictService`
implementation.
Aims to work out of the box for the most standard use cases.
Allows overriding of specific parts of its logic by using granular
methods to compose the work.
"""
types = {
'float': float,
'int': int,
'str': str,
'bool': lambda x: x.lower() == 'true',
}
def __init__(self, mapping, params=(), predict_proba=False, **kwargs):
"""
:param mapping:
A list of query parameters and their type that should be
included in the request. These will be processed in the
:meth:`sample_from_data` method to construct a sample
that can be used for prediction. An example that expects
two request parameters called ``pos`` and ``neg`` that are
both of type str::
{ ...
'mapping': [('pos', 'str'), ('neg', 'str')]
... }
:param params:
Similarly to *mapping*, this is a list of name and type of
parameters that will be passed to the model's
:meth:`~palladium.interfaces.Model.predict` method as keyword
arguments.
:param predict_proba:
Instead of returning a single class (the default), when
*predict_proba* is set to true, the result will instead
contain a list of class probabilities.
"""
self.mapping = mapping
self.params = params
self.predict_proba = predict_proba
vars(self).update(kwargs)
def __call__(self, model, request):
try:
return self.do(model, request)
except Exception as e:
return self.response_from_exception(e)
def do(self, model, request):
if request.method == 'GET':
single = True
samples = np.array([self.sample_from_data(model, request.args)])
else:
single = False
samples = []
for data in request.json:
samples.append(self.sample_from_data(model, data))
samples = np.array(samples)
params = self.params_from_data(model, request.args)
y_pred = self.predict(model, samples, **params)
return self.response_from_prediction(y_pred, single=single)
def sample_from_data(self, model, data):
"""Convert incoming sample *data* into a numpy array.
:param model:
The :class:`~Model` instance to use for making predictions.
:param data:
A dict-like with the sample's data, typically retrieved from
``request.args`` or similar.
"""
values = []
for key, type_name in self.mapping:
value_type = self.types[type_name]
values.append(value_type(data[key]))
return np.array(values, dtype=object)
def params_from_data(self, model, data):
"""Retrieve additional parameters (keyword arguments) for
``model.predict`` from request *data*.
:param model:
The :class:`~Model` instance to use for making predictions.
:param data:
A dict-like with the parameter data, typically retrieved
from ``request.args`` or similar.
"""
params = {}
for key, type_name in self.params:
value_type = self.types[type_name]
if key in data:
params[key] = value_type(data[key])
elif hasattr(model, key):
params[key] = getattr(model, key)
return params
def predict(self, model, sample, **kwargs):
if self.predict_proba:
return model.predict_proba(sample, **kwargs)
else:
return model.predict(sample, **kwargs)
def response_from_prediction(self, y_pred, single=True):
"""Turns a model's prediction in *y_pred* into a JSON
response.
"""
result = y_pred.tolist()
if single:
result = result[0]
response = {
'metadata': get_metadata(),
'result': result,
}
return make_ujson_response(response, status_code=200)
def response_from_exception(self, exc):
if isinstance(exc, PredictError):
return make_ujson_response({
'metadata': get_metadata(
error_code=exc.error_code,
error_message=exc.error_message,
status="ERROR"
)
}, status_code=500)
elif isinstance(exc, BadRequest):
return make_ujson_response({
'metadata': get_metadata(
error_code=-1,
error_message="BadRequest: {}".format(exc.args),
status="ERROR"
)
}, status_code=400)
else:
logger.exception("Unexpected error")
return make_ujson_response({
'metadata': get_metadata(
error_code=-1,
error_message="{}: {}".format(
exc.__class__.__name__, str(exc)),
status="ERROR"
)
}, status_code=500)
@app.route('/predict', methods=['GET', 'POST'])
@PluggableDecorator('predict_decorators')
@args_from_config
def predict(model_persister, predict_service):
try:
model = model_persister.read()
response = predict_service(model, request)
except Exception as exc:
logger.exception("Unexpected error")
response = make_ujson_response({
"status": "ERROR",
"error_code": -1,
"error_message": "{}: {}".format(exc.__class__.__name__, str(exc)),
}, status_code=500)
return response
@app.route('/alive')
@PluggableDecorator('alive_decorators')
@args_from_config
def alive(alive=None):
if alive is None:
alive = {}
info = {
'memory_usage': int(memory_usage_psutil()),
'palladium_version': __version__,
}
info['service_metadata'] = get_config().get('service_metadata', {})
status_code = 200
for attr in alive.get('process_store_required', ()):
obj = process_store.get(attr)
if obj is not None:
obj_info = {}
obj_info['updated'] = process_store.mtime['model'].isoformat()
if hasattr(obj, '__metadata__'):
obj_info['metadata'] = obj.__metadata__
info[attr] = obj_info
else:
info[attr] = "N/A"
status_code = 503
return make_ujson_response(info, status_code=status_code)
def devserver_cmd(argv=sys.argv[1:]): # pragma: no cover
"""\
Serve the web API for development.
Usage:
pld-devserver [options]
Options:
-h --help Show this screen.
--host=<host> The host to use [default: 0.0.0.0].
--port=<port> The port to use [default: 5000].
--debug=<debug> Whether or not to use debug mode [default: 0].
"""
arguments = docopt(devserver_cmd.__doc__, argv=argv)
initialize_config()
app.run(
host=arguments['--host'],
port=int(arguments['--port']),
debug=int(arguments['--debug']),
)