Publishing Machine Learning API With Python Flask

DZone 's Guide to

Publishing Machine Learning API With Python Flask

Let's see how to publish a machine learning API with Python Flask.

· AI Zone ·
Free Resource

Flask is fun and easy to set up, as it says on the Flask website, and that's true! This microframework for Python offers a powerful way of annotating Python function with REST endpoint. I'm using Flask to publish ML model API to be accessible by the 3rd party business applications.

This example is based on XGBoost.Image title

For better code maintenance, I would recommend using a separate Jupyter notebook where ML model API will be published. Import Flask module along with Flask CORS:

The model is trained on Pima Indians Diabetes Database. CSV data can be downloaded from here. To construct a Pandas data frame variable as input for model predict function, we need to define an array of dataset columns:

The previously trained and saved model is loaded using Pickle:

It is always a good practice to do a test run and check if the model performs well. Construct a data frame with an array of column names and an array of data (using new data, the one which is not present in train or test datasets). Calling two functions — model.predict and model.predict_proba. Often, I prefer model.predict_proba, and it returns probability, which describes how likely it will be 0/1. This helps to interpret the result based on a certain range (0.25 to 0.75 for example). Pandas data frame is constructed with sample payload and then the model prediction is executed:

Flask API. Make sure you enable CORS, otherwise the API call will not work from another host. Write the annotation before the function you want to expose through the REST API. Provide an endpoint name and supported REST methods (POST in this example). Payload data is retrieved from the request, and the Pandas data frame is constructed and the model predict_proba function is executed:

The response JSON string is constructed and returned as a function result. I'm running Flask in a Docker container, that's why using as the host on which it runs. Port 5000 is mapped as an external port, and this allows calls from the outside.

While it works to start Flask interface directly in Jupyter notebook, I would recommend converting it to Python script and running it from the command line as a service. Use the Jupyter nbconvert command to convert to Python script:

jupyter nbconvert - to python diabetes_redsamurai_endpoint_db.ipynb

The Python script with Flask endpoint can be started as the background process with PM2 process manager. This allows to run the endpoint as a service and start other processes on different ports. PM2 start command:

pm2 start diabetes_redsamurai_endpoint_db.py

pm2 monit helps to display info about running processes:

ML model classification REST API call from Postman through endpoint served by Flask:


  • GitHub repo with source code
  • Previous post about XGBoost model training
artificial intelligence, machine learning api, python flask, tutorial

Published at DZone with permission of Andrejus Baranovskis , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}