Model Endpoints

Hosting predictive models using scikit-learn and Keras as web endpoints.

We'll cover the following...

Now that we know how to set up a web service and load pre-trained predictive models, we can set up a web service that provides a prediction result in response to a passed-in instance. We’ll deploy models for the games data set using scikit-learn and Keras.

Scikit-Learn

To use scikit-learn to host a predictive model, we’ll modify our echo service built with Flask.

The main changes we need to make are loading a scikit-learn model using MLflow, parsing out the feature vector to pass to the model from the input parameters, and adding the model result to the response payload. The updated Flask application for using scikit-learn is shown in the following snippet:

Press + to interact
import pandas as pd
from sklearn.linear_model import LogisticRegression
import mlflow
import mlflow.sklearn
import flask
model_path = "models/logit_games_v1"
mlflow.sklearn.save_model(model, model_path)
model = mlflow.sklearn.load_model(model_path)
app = flask.Flask(__name__)
@app.route("/", methods=["GET","POST"])
def predict():
data = {"success": False}
params = flask.request.args
if "G1" in params.keys():
new_row = { "G1": params.get("G1"),"G2": params.get("G2"),
"G3": params.get("G3"),"G4": params.get("G4"),
"G5": params.get("G5"),"G6": params.get("G6"),
"G7": params.get("G7"),"G8": params.get("G8"),
"G9": params.get("G9"),"G10":params.get("G10")}
new_x = pd.DataFrame.from_dict(new_row,
orient = "index").transpose()
data["response"] = str(model.predict_proba(new_x)[0][1])
data["success"] = True
return flask.jsonify(data)
if __name__ == '__main__':
app.run(host='0.0.0.0')

After loading the required libraries, we use load_model to load the scikit-learn model object using MLflow. In this setup, the model is loaded only once, and it will not be updated unless we relaunch the application. The main change from the ...