How to create a simple API from a machine learning model in Python using Flask

Lakshitha Perera
4 min readJan 8, 2023

--

Flask is one of the most straightforward but widely used Python microservices web frameworks for building APIs. It is designed in a way that every beginner can take a quick start from it building an API as it’s pretty simple and easy.

In this article, I’m going to show you how to create your own Restful API service with Python and Flask

Here I am going to walk you through how to build a minimum viable REST API using Flask-RESTful with an example and all codes you need to follow along. There are 4 main steps in this post:

Step 1: Make an ML model: A simple model using a BMI dataset

Step 2: Build a REST API: Main part of the post. To serve the ML model just made

Step 3: Run the REST API: made API run in the environment

Step 4: Test the API: Use the model to make predictions by calling the API

Step 1: Make a machine-learning model

You can build your machine learning model using this bmi-prediction-of-health-status.ipynb file.

I used as dataset.csv this dataset. download this dataset and rename it to the dataset.csv. after that, you can use Jupyter Notebook or Google Colab to run this code. finally, your model is saved as a pickle file. We will copy it to the base directory of the project, so we can reference it in the API. Now that we have the model file, let’s deploy it as a REST API.

Technically speaking, you will serialize this model. In Python, you call this Pickling.

Step 2: Build a REST API

To build an API from our trained model, we will be using the popular web development package Flask and Flask-RESTful. Further, we import pickle to load our model and pandas to handle the input and output data.

In a new script, namely app.py and prediction.py, we can now set up an instance of a Flask app and an API and load the trained model (this requires saving the model in the same directory as the script).

Project File Structure

There are also routes like the PUT, PATCH, and DELETE methods. But we will not talk about it further. We will use only the GET and POST methods for this time.

Here is the code for setting up routes and their functions:

main python script
model prediction script

Step 3: Run the REST API

After we build functions and their routes, now let’s run the API. Now go to your terminal, and run one of this commands (make sure that you are on the same path with the API file):

flask run

OR

python app.py

If you run the command, it will return a result like this:

Now let’s access the API by address http://127.0.0.1:5000/, where 127.0.0.1 is your local address, and 5000 is the port number to access the API. If it is a success, it will show like this on the web:

Step 4: Test the API

Great! Our API starts perfectly. Let’s try the API to see if it can predict a Body Mass Index (Extremely Weak, Weak, Normal, Overweight, Obesity, Extreme Obesity).

We can use an application called Postman to test the API that we have created before. You can download it here.

Here is the preview of the result,

postman result

**Additional Notes**

  • Fully functional fitness app project example (Frontend — Angular, Backend — Python, Flask )

Conclusion

That’s the end of this tutorial on how to create a simple API from a machine-learning model in Python using Flask. I hope this article helps you to understand the REST API concept. And also, it guides you to develop your own API in your project. comments and suggestions are welcome.

One Code Can Change Your Project !!

--

--

Lakshitha Perera
Lakshitha Perera

Written by Lakshitha Perera

I’m a passionate individual and a technophile obsessed with the latest technologies. I prefer to find new solutions using the latest approaches and systems. ✌👦

No responses yet