-
Notifications
You must be signed in to change notification settings - Fork 35
/
app.py
52 lines (41 loc) · 1.71 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
import flask
from flask import Flask, Response
import pandas as pd
from io import StringIO
app = Flask(__name__)
from keras.models import load_model
clf = load_model('clf.h5')
clf._make_predict_function() # GH#6462 @ keras-team/keras
def run_model(input_arr):
"""Predictor function."""
# reshape the flat [0, 255]-entry list into a [0, 1]-entry grid, as desired by the CNN.
input_arr = input_arr.reshape(input_arr.shape[0], 28, 28, 1).astype('float') / 255
return clf.predict(input_arr).argmax(axis=1)
@app.route('/ping', methods=['GET'])
def ping():
"""
Determine if the container is healthy by running a sample through the algorithm.
"""
# we will return status ok if the model doesn't barf
# but you can also insert slightly more sophisticated tests here
health_check_arr = pd.read_csv("health-check-data.csv").values
try:
result = run_model(health_check_arr)
return Response(response='{"status": "ok"}', status=200, mimetype='application/json')
except:
return Response(response='{"status": "error"}', status=500, mimetype='application/json')
@app.route('/invocations', methods=['POST'])
def predict():
"""
Do an inference on a single batch of data.
"""
if flask.request.content_type == 'text/csv':
X_train = flask.request.data.decode('utf-8')
X_train = pd.read_csv(StringIO(X_train), header=None).values
else:
return flask.Response(response='This predictor only supports CSV data', status=415, mimetype='text/plain')
results = run_model(X_train)
# format into a csv
results_str = ",\n".join(results.astype('str'))
# return
return Response(response=results_str, status=200, mimetype='text/csv')