Skip to content

Instantly share code, notes, and snippets.

@bgweber
Created May 1, 2019 04:37
Show Gist options
  • Save bgweber/a632ae023b70bf357ce067c5a02e8554 to your computer and use it in GitHub Desktop.
Save bgweber/a632ae023b70bf357ce067c5a02e8554 to your computer and use it in GitHub Desktop.
# Load libraries
import flask
import pandas as pd
import tensorflow as tf
import keras
from keras.models import load_model
# instantiate flask
app = flask.Flask(__name__)
# we need to redefine our metric function in order
# to use it when loading the model
def auc(y_true, y_pred):
auc = tf.metrics.auc(y_true, y_pred)[1]
keras.backend.get_session().run(tf.local_variables_initializer())
return auc
# load the model, and pass in the custom metric function
global graph
graph = tf.get_default_graph()
model = load_model('games.h5', custom_objects={'auc': auc})
# define a predict function as an endpoint
@app.route("/predict", methods=["GET","POST"])
def predict():
data = {"success": False}
params = flask.request.json
if (params == None):
params = flask.request.args
# if parameters are found, return a prediction
if (params != None):
x=pd.DataFrame.from_dict(params, orient='index').transpose()
with graph.as_default():
data["prediction"] = str(model.predict(x)[0][0])
data["success"] = True
# return a response in json format
return flask.jsonify(data)
# start the flask app, allow remote connections
app.run(host='0.0.0.0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment