For those that aren’t familiar with my writings, in a past post titled "Using Machine Learning To Detect Fraud" we started building the first parts of our Machine Learning package.
Now, the package is complete but what do we do next? How do we integrate this to wherever we need to use it – Package Repository on Github.
I hereby introduce you to the REST API.
Note: This project is highly inspired by the "Deploying Machine Learning Models" course on Udemy, therefore there will be snippets of source code taken from that project.
What is a REST API?
API is an acronym that is short for Application Programming Interface. Essentially, it is a software mediator that allows for two applications to talk to one another. If you aren’t familiar with API’s then you wouldn’t know you more than likely use one every day. Don’t believe me? When was the last time you sent an instant text message? You used an API then.
The REST acronym stands for REpresentation State Transfer and it is what determines how the API looks. Like any other architectural style, REST has its own guiding constraints which ought to be satisfied if an interface is to be referred to as RESTful – For more on REST see "What is REST".
Simply, a REST API transfers to the client the state of a requested resource. In our case, the requested resource will be a prediction from our Machine Learning Model. Therefore, our server will be passing the predictions to a client of which may be anything from a web app to a mobile device.
Why Use a REST API?
There are many benefits to designing our system this way. For instance, serving our model via REST API permits for us too;
- Serve predictions on the fly to multiply clients
- Potentially combine multiple models at different API endpoints
- Scale by adding more instances of the application behind a load balancer
- Decouple our model environment from the client-facing layer – teams can work independently
Introduction to Flask
For us to build our API, we are going to leverage the Flask micro-framework. Due to various reasons such as being extremely powerful, simple to use, and very good documentation, Flask is a popular choice for microservices in Python – See Documentation.
"Micro" does not mean that your whole web application has to fit into a single Python file (although it certainly can), nor does it mean that Flask is lacking in functionality. The "micro" in microframework means Flask aims to keep the core simple but extensible.

Our REST API
Basic Structure
Let’s first take a look at our directory structure…

The topmost directory is ml_api
and within this directory there is requirements.txt
and run.py
. requirements.txt
is simply the packages we require for the virtual environment.

Hence, we start by installing the requirements.txt
file into our virtual environment with pip install -r pathtorequirements.txt
which will install flask
and find our distribution of the random_forest_model
package.
The run.py
file is an entry point that we will use to start flask with the create_app()
function we defined in the app.py
module.
Let’s see the app.py
module. From Figure 1 we see that we can navigate to that file via the api
sub-directory.
In app.py
we have our factory function which creates our flask API and sets up blueprints which creates one endpoint that lives in controller.py
.
The controller is a simple health endpoint that occurs during an HTTP GET request and it returns "Working Fine".
Before we spin up this instance, it’s important we set the entry point of our flask app in our command prompt. Do that by navigating to our ml_api
directory in the command prompt and simply typing set FLASK_APP=run.py
. When we have done that we simply type python run.py
and the results look like so…

Perfect, our endpoint is working as we hoped for it too. Now that we have our basic skeleton, we can add some more sophistication.
Note: Adding the config and logging is beyond the scope of this post however in the updated repository, you will see that they are present. Therefore, the next section deals with add the prediction endpoint.
The next step is to add an inference endpoint to our health endpoint in controller.py
We have called the predict
function from our random_forest_model
package to get results from our model. A very important part of any software engineering project is testing so let’s test our endpoint to see if it is working as we expect.
PyTest is beyond the scope of this post, however for us to run our test we must run pytest pathtotest_dir
and that will run all the tests in our test directory – If we pass the test then our prediction endpoint is healthy and running.

And there you have it!
Thank you for reading to the end, let’s connect on LinkedIn…