Machine Learning Engineering Part 1: how to create a REST API from a custom algorithm, using Python and Flask.

Part 1 of hands-on coding tutorials on the fundamentals of Machine Learning Engineering. Creating models is fun, operationalizing them can be hard!

Bram Bloks
LaunchAI
8 min readFeb 9, 2019

--

Why this series?

Working in the field of data and machine learning engineering I notice that many have difficulties in deploying their models into production. Meaning, to operationalize models and create real-time and scalable applications out of them. At a larger scale this results in companies not benefiting from their expensive AI/ML/Modelling efforts, leaving valuable algorithms and models on the shelves. Clearly the creation of models has become easier and very accessible, however operationalizing them remains a challenge.

It all makes sense though. Deployment of machine learning, or any type of model, just requires other skills than data science and analytics. It is the domain of software engineering and DevOps (Development and Operations), with expertises such as networking, cloud-computing, cyber security, etc. A separate and another large field in computer science. This technology and knowledge gap between data science and DevOps is the reason I decided to start writing a series on the parts where both domains come together: Machine Learning Engineering. It will be a series that provides explanations and hands-on tutorials on the fundamentals, with which I hope to help anyone interested in transforming their models into amazing applications.

Enough of that, let's go to part one and get started!

PART 1: Algorithms as REST APIs

In the first part of this series we will go over REST APIs, their essential benefits and functions and create a REST API from a Python script. REST APIs are one of the key core components for operationalization. Through a REST API you can enable your model to work as a stand-alone software component (a microservice) and let it interact with other software components. You may have come across the term REST API before, let's quickly recap what it actually is.

What is a REST API?

An API is an Application Programming Interface, which is code or a piece of software that allows software programs to communicate with each other.

REST, short for REpresentational State Transfer is a specific architectural style and approach for communication. REST makes it easier for software systems and components to communicate with each other, especially when they are separate and decoupled microservices. Communication is done over HTTP using requests to GET, PUT, POST and DELETE data.

What is a microservice?

Another term that may need some clarification is ‘microservices’. Microservices are software components that operate individually and perform a specific task. The latest trend in software architecture and engineering is to split software in microservices instead of building a monolith (one big chunck of software). The use of a microservices architecture has a number of benefits, some of them are:

  1. Better maintainability: maintenance can be reduced to only the microservice in demand.
  2. More flexibility: each microservice can have it's own unique internal layout or even be written in a different programming language.
  3. Possibility to re-use components: since each microservice is able to operate individually it can be re-used or integrated in other software.
  4. Easier expandability: software with a microservices architecture can be relatively easily expanded by adding new microservices.
  5. Higher velocity in development: developers and teams can work on small pieces of the software and specific services.

Time to create our first service in the form of a REST API and dive into some code example to make things more clear.

Hands-on tutorial!

What we want to achieve in this tutorial today, is a stand-alone web server that hosts an algorithm as a REST API. The server should be able to receive an input from any user, which will be done via a POST request. After reading this input the data has to be processed by the algorithm and the results should be send back automatically to the user that made the request. Sounds doable, things will get more clear while coding!

The Algorithm

For this example we will use a custom made algorithm. One that determines prime numbers in a given range of numbers specified by the user. Maybe not the most exciting algorithm for now, but it is merely to show the concept. Obviously you can replace the sample algorithm with any type of your own. Let's write the algorithm as a Python function.

Create a web server for the REST API with Flask

Next we will use Flask to create a simple web server. Flask is a light weight Python framework, that allows you to easily create a web servers. The goal of this web server is to host the REST API (containing the algorithm) as a individual component to which users can communicate.

The above code creates a Flask server (app) from the script, which will initiate when the script is run from the terminal. This app opens an endpoint at your localhost port 6000 with affix ‘/process’, with options to interact via POST requests only.

The server is able to retrieve data in a JSON format. It prints it and simply returns it to the user for now.

Testing the Flask server using cURL

Time to test the server and see what it does! Run your script containing the simple server code in your terminal. You should see something like this:

The server is now running on your pc. Time to send it some data!

We only enabled interaction using POST requests, so let’s send a POST request using cURL. If it is the first time you hear about cURL: it is an open-source command line tool to transfer data using various protocols. In this way we can mimic online interaction between systems on our local pc. Make sure you have cURL installed on your pc.

Open a new terminal, or make sure that the Flask server is running in the background. Run the following cURL request in the terminal.

I think the content of the request speaks for its own. First we specified the data type we are sending, which is JSON, then the request type (POST) and subsequently the data, which actually is our JSON input. Followed by the URL endpoint we are sending the request to.

We keep the data simple to a dictionary object with a single ‘key: value’ pair. Remember the web server now just simply returns the inputted data, so you should see something like this in your terminal:

The server got the data and returned it. That was not too complicated.

In the server we included a print statement as a simple logging tool, to check which data was inputted. We defined the data as the value of the pair, that was inputted as the content. In the terminal where the server is running you can now see that the data, which is equal to the value of 1. Send over the same request and it will be outputted again, with some meta-data.

That was interesting! However there sure must be another way to do this in Python too…

Sending data via another Python script

Of course Python offers a way to replicate cURL requests via a script. For that we use the requests library. The below code shows how to do that same trick, but in Python now.

It also offers us to retrieve the response, which we can store as a variable and use later on. That may be very useful some day. Now run the Python script in the terminal. You can see that it has the same functions as the cURL request and once more outputs the response.

Okay, basic things are working. We can now create a server, run it locally, send data to it and retrieve it’s output. Time to include the algorithm we made earlier in it!

Wrapping the algorithm in a REST API

Now that we both have the algorithm and our web server working, it is time to combine them. Since both components are very simple, we can just wrap the function in the web server.

Import the function from the Python file. Make sure that you forward the request input data to the server to the function and return the outputted result. We do this by putting the result (the number of primes for the given range, and the list of prime numbers) into a dictionary, which we can then return as a JSON object using the jsonify() function to be able to send it over HTTP protocol.

I have kept the print statements in as comments. You can uncomment them to see what happens at the server side in the next step when we use the new api.

Using the REST API

Now that the REST API is ready, it is time to restart it in the terminal, just the way as we started the simple Flask server above, and send some requests as a final test.

Let’s ask it for the number of prime numbers in the range 2–100 using the Python request script we have written earlier. Note: Don’t forget to change your input data to the range you would like to inspect with your algorithm.

Your python script should look like this:

Your output should look something like this:

And for an arbitrary range of 250–500…

As you probably can imagine, returning both input and different outputs as a large object may come in handy when processing the data later on.

Great! We just wrapped and algorithm in a REST API, that can be run as a stand-alone web server.

What’s next?

In the coming tutorials we will go deeper into the REST API, upgrade it and also create some more advanced types, such as APIs from specific Machine Learning models. We will also spend some time on deploying them as Docker containers, adding some additional (micro)services and error handling to make sure operations run smooth at any place your models are deployed.

Like this article?

Was this tutorial was useful?! Feel free to share this story or connect with me on LinkedIn to stay up to date on new posts on this series!

The field of software engineering and DevOps is broad, options are endless and things may get complex quickly. I hope to help you in your journey of creating awesome AI applications!

--

--

Bram Bloks
LaunchAI

Entrepreneur, founder, developer & open source software contributor. Tech enthusiast and active AI/ML community member.