Skip to content

Instantly share code, notes, and snippets.

@ResidentMario
Created January 18, 2019 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ResidentMario/5adbfec39a9543f2379d0b18e0c05666 to your computer and use it in GitHub Desktop.
Save ResidentMario/5adbfec39a9543f2379d0b18e0c05666 to your computer and use it in GitHub Desktop.
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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment