part-1 )( Dockerize and deploy Machine learning model as REST API using Flask

#machinelearning #docker #deployment #python

Sumit Kumar Sept 22 2020 · 2 min read
Share this

Part-1 + Part-2

we will not build the machine learning model from the very start. I will just serialize the model and grab the pickle file of the model and we will good to go. You can download the whole code from my github repo ( with very explained readme.md file).

It is a good practice to create environment for your new project. this is how you can create new env with anaconda…

step 1: conda create -n "<env_name>" python=3.x

step 2: conda activate "<env_name>"

step 3: pip install required packages

Now i m assuming that, you have pickle file classifier.pklof our model. let’s creat a Flask api quickly.

from pickle
from flask import Flask, request

app = Flask(__name__) # Important

pickle_in = open("classifier.pkl", "rb")
classifier = pickle.load(pickle_in)

@app.route('/')
def Welcome():
        return "Hello World"

@app.route('/predict')
def predict_note_authentication():
        variance=request.args.get('variance')
        skewness=request.args.get('skewness')
        curtosis=request.args.get('curtosis')
        entropy=request.args.get('entropy')
        prediction =classifier.predict([[variance,skewness,curtosis,entropy]])

      return "Predicted value is"+str(prediction)

if __name__=='__main__':
app.run()

You do not need to install flask individually, latest version of anaconda comes with flask.type in cmdpython flask_app.py to run and go to http address to check if it working will print Hello World. Cool!

Now we have our flask api for our model.Now it is time to dockerize but before it lets understand …

What is Docker and why use Docker?

Docker is one of the most popular tool used to containerize and deploy applicaitions over the internet.

let’s understand docker with a example : let assume you have bought a new home and you want to shift your furniture and stuff from old home to new home. How can u do that ? There are two ways

  • Shift your furniture (eg: television, fan, bed, table etc) in chunks one by one.There is high probability that u will miss something at your old home.
  • just put all the stuff in a container (eg: truck) and unpack that container at new home .there is very less probability that you will miss something.
  • So what we are trying to do is, irrespective to operating system and hardware configration, we are trying to create a container.the requirements like os and hardware required on other side will be putted in the docker image and take the whole container to other side and run from other side environment.

    In other words, It is fixing the environment standardization#Build once and deploy everywhere.

    Containers allows developers to package up an application with all the parts it needs.

    Diffrence between Docker Containers and Virtual Machines

    1. Docker Containers

  • They don’t contain a guest OS for each container and rely on the underlying OS kernel, which makes the containers lightweight.
  • Containers share resources with other containers if it is out of resources in the same host OS and provide OS level process isolation.
  • 2. Virtual Machines

  • Virtual Machines (VMs) run on Hypervisors, which allow multiple Virtual Machines to run on a single Machine along with its own operating system.
  • Each VM has its own copy of an operating system along with the application and necessary binaries, which makes it significantly larger and it requires more resources.
  • They provide Hardware-level process isolation and are slow to boot
  • Docker Image is a file, comprised of multipul layers which is used to execute code in a Doker Container.

    Docker Container is a runtime instance of an image.It allows developers to package application with all parts needed such as librariesand other dependencies.

    Docker File is a text document that contains necessary commands which on execution helps assemble a Docker Image.

    Docker Engine hosts the containers. Docker engine is a client-server based application.

    Docker Hub is the official online repo where you can find other docker images that are available to use. it makes it easy to find, manage and share container images with others.

    Installation of Docker

    Install Docker in Ubuntu ( skip if u have already installed )

    Install Docker in Windows ( skip if u have already installed )

    Love if it helps you and yes wait for Second part :)

    Thank You

    Comments
    Read next