Overview

In this tutorial, we will deploy a pre-trained model by first create a visual web interface using Flask web framework, and then containerize with Docker. Docker will “package up code and all its dependencies so the application runs quickly and reliably from one computing environment to another”. The containerized application can be deployed on hosting platforms like Heroku, or other hosts of your choice.

Prerequisite

  1. Install Docker (link).
  2. Have a pre-trained model. You can find an example here, along with all the files mentioned in this tutorial.

The file structure looks like this:

1
2
3
4
5
6
7
├── Dockerfile
├── app.py
├── pretrained
│ └── my_model.pkl
├── requirements.txt
└── templates
└── page.html

Create Flask app

Flask is a popular micro web framework written in Python. In this part, we will create a file 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
import numpy as np
from flask import Flask, request, jsonify, render_template
import pickle
from gevent.pywsgi import WSGIServer

app = Flask(__name__)
model = pickle.load(open('pretrained/my_model.pkl','rb'))

@app.route('/')
def home():
return render_template('home.html')

@app.route('/predict', methods=['POST'])
def predict():
features_list = [float(x) for x in request.form.values()]
features = np.array(features_list).reshape(1,-1)
predict_outcome_list = model.predict(features)
predict_outcome = round(predict_outcome_list[0],2)

return render_template('page.html',prediction_display_area='Predicted value:{}'.format(predict_outcome))

if __name__ == "__main__":
http_server = WSGIServer(('0.0.0.0', 5000), app)
http_server.serve_forever()

This app requests a number as input, and predicts a value using my_model.pkl. You will also need this HTML file to beautify the page.

Specify dependencies

Put the packages required in requirements.txt:

1
2
3
flask
sklearn
gevent

Create a Dockerfile

Create a Dockerfile:

1
2
3
4
5
6
7
8
9
10
FROM python:3.6-slim-stretch

ADD requirements.txt /
RUN pip install -r /requirements.txt

ADD . /app
WORKDIR /app

EXPOSE 5000
CMD ["python" , "app.py"]

This Dockerfile does four things:

  1. Create a layer from the python:3.6-slim-stretch Docker image. You can browse other docker official python images here.
  2. Add the requirements.txt file and install all dependencies on top of the basic docker layer.
  3. Add all other files and specify the work directory.
  4. Specify port and run the app.

Run with Docker

First, build a docker image (you can choose a different tag name)
docker build -t flask_app .
Confirm the image is there
docker image ls
Then run the app in a foreground mode
docker run -it --rm -p 5000:5000 flask_app
Check http://localhost:5000 to see if the webpage is loaded.

Next steps

We have created a simple flask app to host a pre-trained model, and use Docker to deploy on localhost. From now on we could extend this to all kinds of things. Here are several examples:

  1. Use more sophisticated pre-trained models to solve tasks like image classification (example) or language recognition.
  2. Automate the process to generate Dockerfile (example).
  3. Host app on platforms like Heroku. I have a post previously on hosting a dash app on Heroku. Heroku also supports deploying with Docker.

Some useful docker commands

1
2
3
4
5
6
7
docker build -t <tag-name> .  # create docker image
docker image ls # display images
docker image rm <image tag name> # remove images
docker ps # show all running containers
docker container ls --all # show all container
docker kill $(docker ps -q) # stop all containers
docker rm $(docker ps --filter status=exited -q) # remove all stopped containers