MLOps: Deploying ML model using Flask and Swagger.
Machine Learning (ML) models perform predictions for the specific task they are trained for. There are many steps that need to be performed before ending up with such a tool that takes in data and outputs a result. Therefore, this model can be seen as a black-box and one could take benefit from that tool for a desired task. The deployment part consists of making available the functionalities of the model to the end users and it is such an important task in real-world projects. In this blog we will show a “getting started” way of ML model deployment using tools such as Flask and Swagger.
Overview
- Motivation
- ML model training and evaluation
- Model Deployment
1. Motivation
The machine learning workflow can be summarised as shown below.
Each step is an important part that helps to achieve better performance. We would first need to define the problem we are trying to solve (supervised, unsupervised etc). After looking at the data and pre-processing it, we choose the hypothesis space that better suits to our task and check the metrics that define success in our case. Once the tasks of Data Collection and Modelling are done, one could think about how end users can take benefits from the model. Here is where the Deployment part comes into play. It allows users with no Machine Learning clue to take advantage of the model to take decisions using the model predictions.
2. Model training and evaluation
In this blog, we will walk through a simple example of supervised learning problem. We will train a Logistic Regression Classifier on the classical Iris Dataset. But before we get started, it is recommended that you set up a conda environment where some packages will be needed throughout this mini-project. You can create that environment using:
>> conda create -n my_env_name
All the training and evaluation steps are described in the train file.
3. Model Deployment
Now that we’ve trained our model and the results during evaluation are satisfactory, let us give value to what we have done so far by deploying it. To do so, we will need two main frameworks: a) Flask b) Swagger
a. Flask is a micro web framework written in Python that is used for developing web applications. It contains a web server that we can make requests (such as GET and POST requests) to get back response.
b. Swagger is a framework for user-interface useful for describing REST API expressed in JSON. It can make nicely-looking user interfaces such that any end user could consume the service.
Please make sure you pip install them in your environment with:
>> pip install flask
>> pip install flasgger
Now that they are installed them, let’s have a look at the deploy.py script. First we import all the necessary packages. After defining the app, we “swaggerify” it and we load our model that we pickled.
Before we write our prediction function, we first add a route to it. Since we would like to pass input through a form, we enable the POST method. By default it is a GET method whereby we pass the values through the URL. Now we define the decorator that helps us specify the input features we are passing to our model. We then gather all the information and pass it to our model for prediction.
To use the micro-service, run python deploy.py on Terminal. Type 127.0.0.1:7000/apidocs to the URL. We can view our UI with Swagger and feed data for prediction.
For such an input, result is shown below
This marks the end of a quick introduction to model deployment with Flask and Swagger. Please find the code here.