-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
62 lines (48 loc) · 1.9 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
from bottle import route, run, request, response, post
from sklearn.externals import joblib
import json
import pandas
model = joblib.load('model.pkl')
@route('/predict', method="POST")
def predict():
response.content_type = 'application/json'
try:
body = request.json
body = body if isinstance(body, list) else [body]
X = pandas.DataFrame.from_dict(body)
if (hasattr(model, 'get_booster')):
# HACK https://github.com/dmlc/xgboost/issues/1238
X = X[model.get_booster().feature_names]
result = model.predict(X)
return json.dumps(result.tolist())
except Exception as error:
response.status = 500
return json.dumps({'error': str(error)})
@route('/predictproba', method="POST")
def predictproba():
response.content_type = 'application/json'
try:
body = request.json
body = body if isinstance(body, list) else [body]
X = pandas.DataFrame.from_dict(body)
if (hasattr(model, 'get_booster')):
# HACK https://github.com/dmlc/xgboost/issues/1238
X = X[model.get_booster().feature_names]
result = pandas.DataFrame(model.predict_proba(X),
columns=model.classes_)
return result.to_json(orient="records")
except Exception as error:
response.status = 500
return json.dumps({'error': str(error)})
@route('/featurenames')
def featurenames():
response.content_type = 'application/json'
try:
result = model.get_booster().feature_names
return json.dumps(result)
except Exception as error:
response.status = 500
return json.dumps({'error': str(error)})
#run(host='localhost', port=8080, debug=True, reloader=True)
run(host='0.0.0.0', port=8080)
# curl -H "Content-Type: application/json" -X GET -d '[{"sepal length (cm)":4.4}]' http://localhost:8080/predictproba