DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Publishing Machine Learning API With Python Flask

Publishing Machine Learning API With Python Flask

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

Andrejus Baranovskis user avatar by
Andrejus Baranovskis
·
Apr. 10, 19 · Tutorial
Like (5)
Save
Tweet
Share
9.58K Views

Join the DZone community and get the full member experience.

Join For Free

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 0.0.0.0 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:

Resources

  • GitHub repo with source code
  • Previous post about XGBoost model training
API Machine learning Flask (web framework) Python (language) jupyter notebook Data (computing)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • Unlocking the Power of Polymorphism in JavaScript: A Deep Dive
  • Memory Debugging: A Deep Level of Insight
  • Debugging Threads and Asynchronous Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: