Intelligent application building basically consist of integrating machine learning based predictive components for the apps and systems. Mostly data scientists or the AI engineers are accountable of building these machine learning models.
When it comes to integration and deployment in production environment, the problem occurs with platform dependency. Most of the data scientists and AI engineers are pretty comfortable with python or R and they develop their models with them, though the rest of the system would be on .NET or Java based application.
One of the best approaches to connect these components together is deploying the ML predictive module as a web API and calling the API through the application. When it comes to APIs any programmer can work with it when they have the API definition.
Flask is a small and powerful web framework for Python. It’s easy to learn and simple to use, enabling you to build your web app in a short amount of time. Visual Studio provides an easy way to create Python flask web applications through it’s templates. Here’s the steps I’ve gone through for deploying the ML experiment as a REST API.
01. Create the machine learning model, train, tune and evaluate it.
Here what I’ve done is a simple linear regression for predicting the monthly salary according to the years of experience. Sci-kit learn python library has been used for performing the regression operation. The dataset used for the experiment is from SuperDataScience.
The code is available in the GitHub repository .
02. Creating the pickle
When you deploy the predictive model in production environment, no need of training the model with code again and again. Python has a built-in method of persisting data called pickle
. The pickle
module can serialize objects or data into a file that we can save and load from. You can just use the pickle as a binary reference generating the output. scikit-learn
has their own model persistence method we will use: joblib
. This is more efficient to use with scikit-learn
models due to it being better at handling larger numpy
arrays that may be stored in the models.
03. Create a Python Flask web application.
Simply go for Visual Studio. (I’m using VS2017 which comes with python by default) Select web project. The step by step guide is here. I would recommend you to go with option 2 mentioned in the blog because it reduces lot of unnecessary overhead.
For the safe side, use python virtual environments. It would avoid many hassles occurs with library dependencies. I’ve used anaconda environment as the base of virtual environment.
04. Create the API.
Create a new python file in your project and set it as the startup file. (In my case MLService.py is the startup file which contains the API code). The pickle file that contains the model binaries is the only dependency the API is getting when it is deployed.
Here the API operates through POST methods which accepts the input in JSON.
04. Run & Test
You can run the API and test by sending POST requests to the URL with a JSON body. Here I’ve used postman to send a POST request and it gives me the predicted salary for the entered number of months.
You can access the whole code of the project through my GitHub repo here.
Do comment if you have any suggestion to change the API structure.