Machine Learning on Kubernetes – Containerizing a Model

In the 3rd session of “Machine Learning on Kubernetes” Book Club session we discussed how we can take a machine learning model and containerize it. You can find more details about the series and previous two recording here.

We started the session by doing the quick review of Machine Learning and then looked a basic flask application to expose certain endpoints.

from flask import Flask, request

app = Flask(__name__)

@app.route('/', methods=['POST'])
def add():
    a = request.form["a"]
    b = request.form["b"]
    return str( int(a) + int(b))

if __name__=='__main__':"8080")

We then built a basic linear regression model and packaged it with pickle module of Python.

import numpy as np
import pandas as pd
import pickle
from sklearn.model_selection import train_test_split
from sklearn import linear_model
from sklearn.linear_model import LinearRegression

import pickle

dataset = pd.read_csv('house_price.csv')

X = dataset.loc[:, ['size']].values
y = dataset.loc[:, ['price']].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)

lr = LinearRegression(), y_train)

y_pred = lr.predict(X_test)

print ("Pridict from the test set")

with open('lr.pkl', 'wb') as model_pkl:
    pickle.dump(lr, model_pkl)

model = pickle.load( open('lr.pkl','rb'))

print ("One sample prediction from the loaded model ")

Once our model is packaged we used another flask program to expose it via a web endpoint.

import numpy as np
import pickle
from flask import Flask, request

model = pickle.load( open('lr.pkl','rb'))

app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict_size():
    size = int(request.form["s"])
    print (size)
    prediction = model.predict([[size]])
    return str(prediction)

if __name__=='__main__':"", port="8080")

After running the program we could access the model /predict endpoint.

Once this is we just had to put all above Python along with csv file to get our model served by the Docker Container.

FROM continuumio/anaconda3
RUN mkdir /app
COPY . /app
RUN pip install -r requirements.txt
CMD python

and once the image is built, we can simply run the container and serve our model through it.

docker container run -p 8080:8080 <image_name>

Please go over the recording to follow the steps as we did during the session. In the next session we’ll be exploring how can use mlfow to track different experiments.

Join Our Newsletter

Share this article:

Table of Contents