Deploying ML models using Django REST API Part 1
As a Data Scientist I ask myself what’s my business with REST API or why should I have a knowledge of learning the concept? This kind of stuff is commonly seen among the web developers, my job is just to build Machine Learning models and draw inferences from data, but as time goes on when I started building models I begin to get worried on how to deploy my ML models that is, getting it into production or letting users interact with it. Then that’s how my journey to the Django REST API began.
Getting Started
To get started It’s advisable learn some basic concepts such as:
- REST API
- API requests
- Django workflow
REST API
A Representational state transfer (REST) is simply a software architectural style that provide a way between computer systems on the web, making it easier for systems to communicate with each other while an application programming interface (API) is like a software intermediary that allows two applications to talk to each other, it’s also a set of routines, protocols, and tools for building software applications.
Assume you’re trying to find about Machine Learning on Wikipedia. You open up Wikipedia and then type “Machine Learning” into a search field, hit enter, and you see a list of articles about Machine Learning a REST API works in a similar way. You search for something, and you get a list of results(Data) back from the service you’re requesting from. Also, another instance is sending your data to a ML model you’ve built and the model gives you the prediction(s) based on the data you’ve passed.
REST simply determines how the API looks like. It is a set of rules that developers follow when they create their API. One of these rules states that you should be able to get a piece of data (called a resource) when you link to a specific URL.
Each URL is called a request while the data sent back to you(mostly as a JSON) is called a response.
API requests
An API request consist of four(4) basic fundamentals which are:
- The Endpoint
- The Method
- The Headers
- The Data
The Endpoint:
Endpoints are important aspects of interacting with server-side web APIs, as they specify where resources lie that can be accessed by third party software. Usually the access is via a URI to which HTTP requests are posted. Let’s simply put it this way, it’s the starting point of the API you’re requesting from take for instance, the endpoint of Github’s API is https://api.github.com
while the endpoint Tiwa’s API is https://api.tiwa.africa
Also Endpoints have something called path, The path determines the resource or data you’re requesting for. Take for example
https://api.github.com/users
Here, the endpoint is: https://api.github.com
While the path is: /users
The URL then returns a JSON (JavaScript Object Notation) object of dummy users from the Github API.
The Method:
A method is the request you send to the server. There are so many API methods you can choose from, but for now we’ll focus on these five types below:
- GET
- POST
- PUT
- PATCH
- DELETE
Most times, the commonly used method is the GET and POST method.
These methods provides information for the request you’re making from the API server. They are used to perform four possible actions: Create, Read, Update and Delete (CRUD).
The Headers:
The REST headers contain a wealth of information that can help you track down issues when you encounter them. HTTP Headers are an important part of an API request and response as they represent the meta-data associated with the API request and response. Headers carry information for:
- Request and Response Body
- Request Authorization
- Response Caching
- Response Cookies
Other than the above categories HTTP headers also carry a lot of other information around HTTP connection types, proxies etc. Most of these headers are for management of connections between client, server and proxies and do not require explicit validation through testing.
The Data:
You’ll always want to send data(Or sometimes called “body”) to an API request to get a response. For example, if we are creating a REST API to update student details using the PUT Method, then the request URI will be {endpoint}/students/{student_id}
, and the request body is:
{"id": student_id,"name": "student name","school_name": "Lagos State University"}
And when this is passed to the API, the Database will be updated automatically.
Testing an API
API testing is a type of software testing that involves testing application programming interfaces (APIs) directly and as part of integration testing to determine if they meet expectations for functionality, reliability, performance, and security — Wikipedia
How to Test An API?
To test an API you can use:
- CURL
- POSTMAN
There are other numerous ways of testing your API, but this two are the most common.
DJANGO WORKFLOW
Django is a Python-based Web Framework which allows you to build standard websites fast and easy, its free and open source. It’s basically for “Perfectionists with Deadlines”. Django also gives the liberty of building custom APIs too.
How it works:
Django simply handles request and return a response back to the web browser.
To get the basic fundamentals of Django, I strongly recommend you read this article.
Why Django?
- Django is easy and fast to set up, you basically don’t need to write your codes from scratch to start building websites or your API’s, Django does all that for you.
- It’s easy to integrate your ML models with it because most Machine Learning models are usually written with Python and so is your Django project, so there is an easy flow between the two frameworks.
Also, if Django doesn’t seem to work for you, you can check out Flask (Another Python Web Framework)
SUMMARY
So far we’ve seen the benefits of API and how crucial it is for Data Scientists to learn how they can build one themselves with Django.
For my next article(Part 2), I’ll work you through a step by step process of building a simple API that is deploying a Machine Learning model with the Django REST Framework. Happy reading 😃.