Deploy your machine learning model using Flask

Aadhil imam
3 min readNov 21, 2019

Machine learning is a process which is widely used for prediction. N number of algorithms are available in various libraries which can be used for prediction. When we develops a machine learning model using Scikit-Learn, TensorFlow, Keras, PyTorch etc, the ultimate goal is to make it available in production. Often times when working on a machine learning project, we focus a lot on Exploratory Data Analysis(EDA), Feature Engineering, tweaking with hyper-parameters etc. But we tend to forget our main goal, which is to extract real value from the model predictions.

In this article, we are going to build a prediction model on historic data using machine learning algorithms model on the testing data.Building/Training a model using various algorithms on a large dataset is one part of the data. But using these model within different application is second part of deploying machine learning in the real world. However, there is complexity in the deployment of machine learning models. This article aims to make you get started with putting your trained machine learning models into production using Flask API.

Project structure

This project has four parts :

  1. model.ipynb(jupyter notebook) — This contains code for the machine learning model to predict sales in the third month based on the sales in the first two months.
  2. app.py — This contains Flask APIs that receives sales details through GUI or API calls, computes the predicted value based on our model and returns it.
  3. request.py — This uses requests module to call APIs defined in app.py and displays the returned value.
  4. HTML/CSS — This contains the HTML template and CSS styling to allow user to enter sales detail and displays the predicted sales in the third month.

We have to install many required libraries which will be used in this model. Use pip command to install all the libraries.

pip install pandas
pip install numpy
pip install scikit-learn
pip install flask

creating the simple machine learning model model.ipynb

We have used linear regression here as a predicting model. We fed the training part of the data to train the model.

The next part was to make an API which receives sales details through GUI and computes the predicted salary on our model. For this I de- serialized the pickled model in the form of python object. I set the main page using index.html. On submitting the form values using POST request to /predict, we get the predicted sales value.

The results can be shown by making another POST request to /results. It receives JSON inputs, uses the trained model to make a prediction and returns that prediction in JSON format which can be accessed through the API endpoint.

Finally used requests model.py to call APIs defined in app.py

Here, we have created a simple form using html only. If you want to make the form more interactive you can do so as well. There are three fields which need to be filled by the user experience , test score and interview score

here use css to make the interactive web interface

here you can download the project from github : https://github.com/aadhil96/machine-learning_flask

sourec : https://www.kdnuggets.com/2019/10/easily-deploy-machine-learning-models-using-flask.html

--

--