Publish your Machine Learning models with Flask!

Share this post

Flask… what is it?

In my previous article we saw how to simply maintain the training of a machine learning model through persistence. The objective is now to publish and use a model through an external program or better still a web application.

If you are focusing on a micro-service type architecture for example, it will seem logical to use REST services. And imagine that this is good because there is a fairly magical Python micro-framework for this: Flask .

Why Magic?

Quite simply because Flask, unlike its competitors ( Django , Pylons , Tornado , Bottle, Cherrypy, Web2Py, Web.py, etc.) is a framework of disconcerting simplicity and efficiency. In this article, I suggest you check it out for yourself.

In addition, it is 100% Open-Source… so what more could you ask for?

Small (light) but strong Flask allows the addition of additional components but also integrates a real template engine to associate HTML / CSS layers. In this article we will only focus on the REST aspects of this framework and we will mainly see how to use it in a pragmatic way to call a Machine Learning model.

Installing Flask

Working on Linux / Ubuntu you will only have to type in a shell:

sudo apt-get install python python-pip
sudo apt-get install python3-flask
sudo pip install flask

For those who persist on Windows (really ?) I suggest to install Anaconda first, then install the Flask module via the browser ( Anaconda ).

As for Apple / MAC fans, here are the commands to run:

curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py
easy_install pip
pip install flask

The really great thing about Flask is that you don’t need to install a web server. You will create a Python script which will first listen on an HTTP port and which will distribute the URLs to your code. And this in a few lines of code.

Your very first service

Our first service will respond to a GET request and will only display a string of characters.

from flask import Flask
app = Flask(__name__)
@app.route('/')

def index():
    return "Hello datacorner.fr !"

if __name__ == '__main__':
      app.run(debug=True, host='0.0.0.0', port=8080)

Some explanations, by line:

  • Line (1): Import / reference of the Python Flask module
  • Line (2): Creation of the Flask application, the __name__ reference will later be used to manage several instances.
  • Line (3): Creation of a route. A route is in a way the mapping of the REST URL that will be called with the Python function.
  • Line (5-6): This is the function called across the route above.
  • Lines (8-9): Launch of Flask which then goes on hold
    • on the specified port: 8080
    • in debug mode (very practical, we will see in particular that this mode makes it possible to take into account the backups over time and directly).
    • on host 0.0.0.0

Save this file in a file with a * .py extension (especially do not name it flask.py on the other hand in which case you would have an error). In my case I called it: flask_test.py

Launch a shell (command line) and type:

python flask_test.py

The command should specify the host and port on which Flask is queuing:

$ python flask_test.py 
 * Serving Flask app "flask_test" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 212-625-496

Open a web browser and type the url: http://0.0.0.0/8080, you should observe the response:

Call your model with Flask

Step 1: Calling up a model with training

from flask import Flask
import pandas as pd
from sklearn import linear_model
from joblib import dump, load

app = Flask(__name__)

def fitgen():
    data = pd.read_csv("./data/univariate_linear_regression_dataset.csv")
    X = data.col2.values.reshape(-1, 1)
    y = data.col1.values.reshape(-1, 1)
    regr = linear_model.LinearRegression()
    regr.fit(X, y)
    return regr

@app.route('/fit30/')
def fit30():
    regr = fitgen()
    return str(regr.predict([[30]]))

if __name__ == '__main__':
      app.run(debug=True, host='0.0.0.0', port=8080)

In this example we create a REST / GET service which allows to train a model (that of the article on the persistence of Machine Learning models ), and to return a prediction value on the fixed value 30. Once the command Python launched your browser should display on the URL http://0.0.0.0:8080/fit30

[[22.37707681]]

Step 2: The parameterization is made via the URL the value to predict

For this we will use the possibility of “variability” the Flask routes. For that we must add a parameter in the route (between ) and add the same parameter in the function. The code then becomes:

<pre class="wp-block-syntaxhighlighter-code">from flask import Flask
import pandas as pd
from sklearn import linear_model
from joblib import dump, load

app = Flask(__name__)

def fitgen():
    data = pd.read_csv("./data/univariate_linear_regression_dataset.csv")
    X = data.col2.values.reshape(-1, 1)
    y = data.col1.values.reshape(-1, 1)
    regr = linear_model.LinearRegression()
    regr.fit(X, y)
    return regr

@app.route('/fit/<prediction>')
def fit(prediction):
    regr = fitgen()
    return str(regr.predict([[int(prediction)]]))

if __name__ == '__main__':
      app.run(debug=True, host='0.0.0.0', port=8080)</pre>

Let’s run the Python command and open a browser. Type the URL http://0.0.0.0:8080/fit/30

The same result as before should be displayed.

Step 3: Let’s make the prediction with a model already trained

For this, we refer to the article on the persistence of Machine learning models . We will now speed up the performance using a model already trained:

<pre class="wp-block-syntaxhighlighter-code">from flask import Flask
import pandas as pd
from sklearn import linear_model
from joblib import dump, load

app = Flask(__name__)

@app.route('/predict/<prediction>')
def predict(prediction):
    regr = load('monpremiermodele.modele')
    return str(regr.predict([[int(prediction)]]))

if __name__ == '__main__':
      app.run(debug=True, host='0.0.0.0', port=8080)</pre>

We changed the route, so type in your browser the URL http://0.0.0.0:8080/fit/30 to get the same answer as before. See above all the performance gain by changing the value.

You are therefore equipped to create REST services that will use your machine learning models. As usual the source codes are available on GitHub .

Share this post

Benoit Cayla

In more than 15 years, I have built-up a solid experience around various integration projects (data & applications). I have, indeed, worked in nine different companies and successively adopted the vision of the service provider, the customer and the software editor. This experience, which made me almost omniscient in my field naturally led me to be involved in large-scale projects around the digitalization of business processes, mainly in such sectors like insurance and finance. Really passionate about AI (Machine Learning, NLP and Deep Learning), I joined Blue Prism in 2019 as a pre-sales solution consultant, where I can combine my subject matter skills with automation to help my customers to automate complex business processes in a more efficient way. In parallel with my professional activity, I run a blog aimed at showing how to understand and analyze data as simply as possible: datacorner.fr Learning, convincing by the arguments and passing on my knowledge could be my caracteristic triptych.

View all posts by Benoit Cayla →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Privacy Preference Center

Analytics

NOTICE RELATING TO COOKIES
What is a cookie and what is it used for?

A cookie (or connection witness) is a text file that can be saved, subject to your choices, in a dedicated space on the hard drive of your terminal (computer, tablet, etc.) when consulting a online service through your browser software.
It is transmitted by a website's server to your browser. Each cookie is assigned an anonymous identifier. The cookie file allows its issuer to identify the terminal in which it is registered during the period of validity or registration of the cookie concerned. A cookie cannot be traced back to a natural person.

When you visit this site, it may be required to install, subject to your choice, various statistical cookies.
What types of cookies are placed by the website?


Google Analytics & Matomo Statistics Cookies

These cookies are used to establish statistics of visits to my site and to detect navigation problems in order to monitor and improve the quality of our services.
Exercise your choices according to the browser you use

You can configure your browser at any time in order to express and modify your wishes in terms of cookies, and in particular regarding statistical cookies. You can express your choices by setting your browser to refuse certain cookies.

If you refuse cookies, your visit to the site will no longer be counted in Google Analytics & Matomo and you will no longer be able to benefit from a number of features that are nevertheless necessary to navigate certain pages of this site.
However, you can oppose the registration of cookies by following the operating procedure available below:

On Internet Explorer
1. Go to Tools> Internet Options.
2. Click on the privacy tab.
3. Click on the advanced button, check the box "Ignore automatic management of cookies".

On Firefox
1. At the top of the Firefox window, click the Firefox button (Tools menu in Windows XP), then select Options.
2. Select the Privacy panel.
3. Configure Conservation rules: to use the personalized parameters for the history.
4. Uncheck Accept cookies.

On Chrome
1. Click on the wrench icon which is located in the browser toolbar.
2. Select Settings.
3. Click Show advanced settings.
4. In the “Confidentiality” section, click on the Content settings button.
5. In the "Cookies" section, you can block cookies and data from third-party sites

On Safari
1. Go to Settings> Preferences
2. Click on the Privacy tab
3. In the "Block cookies" area, check the "always" box.

About Opera
1. Go to Settings> Preferences
2. Click on the advanced tab
3. In the "Cookies" area, check the "Never accept cookies" box.
social network sharing cookies

On certain pages of this site there are buttons or modules of third-party social networks that allow you to use the functionalities of these networks and in particular to share content on this site with other people.
When you go to a web page on which one of these buttons or modules is located, your browser can send information to the social network which can then associate this visualization with your profile.

Social network cookies, over which this site has no control, may then be placed in your browser by these networks. I invite you to consult the confidentiality policies specific to each of these social networking sites, in order to become aware of the purposes for using the browsing information that social networks can collect using these buttons and modules.
- Twitter
- Google+
- LinkedIn

Statistiqcs only

Fork me on GitHub