A Quick Guide to Deploy your Machine Learning Models using Django and Rest API


There is a rise of use in Machine Learning applications for business. And you will find a lot of Machine Learning models running online commercially. A number of machine learning models are running behind every search engine. You will find them inside google translator, apple’s Siri, facebook’s facial recognition algorithms. So how do they deploy them on the web?

If you so far worked with machine learning models locally, just applying ML algorithms on datasets and making predictions, you should know how to deploy them on the web. In this tutorial, I will walk you through different steps to build and deploy a machine learning model using Django and REST API, let’s dive deep into it!

What is Django?

Django is a high-level Python framework that lets you build robust and scalable web applications. This is the most popular framework available in python. It follows the MVT or Model-View-Template pattern. It is closely related to other MVC frameworks like Ruby on Rails and Laravel. In the MVC framework, the view and model parts are controlled by the Controller but in Django, the tasks of a controller are handled implicitly by the framework itself.

Django lets you create a number of applications under a single project. The application has all the functionalities to work independently. The app is considered as a package which you can reuse in other projects without making any major changes. This is the greatest advantage of using Django for building web applications.

What is Django REST Framework?

Django REST framework is a wonderful toolkit for developing robust web APIs using Django and Python. It gives an easy way to serialize the data and provide it to other applications. It is like a door between the database and the program which handles querying the database and formatting of the data. Generally, it uses JSON to format the data. 

Machine learning models are mostly written in Python and run locally in a Jupyter notebook or similar IDEs. But when you need to productionize your model that means you make it available in the web, you can do this by one of the following-

  • Hard code the ML model in the web applications. This is the easiest way to deploy ML models like simple linear regression or random forest classification on the web. But it has a lot of drawbacks if you are trying to implement some complex models like Neural Networks.

  • The most efficient way is to provide an interface that will communicate between the ML model and the web interface. It will fetch data to the model, the model will process it independently. After getting the prediction this interface will take it back to the web applications end. For this, we can use REST APIs, Websockets, or RPI.

Using Django REST frameworks, we can build powerful APIs for our machine learning models. Which will let us handle all the data retrieving tasks without any hassle.

Django vs Flask: Why We Should Use Django for Deploying Machine Learning Models?

Flask is more widely used for deploying machine learning models. This is because it is simple and easy. But it has some drawbacks compared to Django.

Firstly, as a general-purpose web framework, Django provides you more features than Flask. With more modules and features, it will be easier to build and deploy web-based machine learning models.

Secondly, Django is more mature than Flask(Flask was released in 2010 and Django was released in 2005). So it has a wider community for getting help with any issues.

Thirdly, Django is faster than Flask. If you are focusing more on web development and less on machine learning applications, you should take Django for development. For small scale ML applications, Django is a better choice than Flask.

Though both frameworks are based on Python, you can easily learn Flask and switch from Django. For now, let’s stick to Django and deploy your machine learning model.

Deploy your First Machine Learning Model using Django and Rest API

If you have read the above words or known before, I think you are determined to go with me to learn how to deploy your first ML project on the web. 

In the following sections, we are going to build a simple ML model and web API in Django. Let’s do that!

Here are the steps you need to deploy a machine learning model-

  1. Build a Machine Learning Model
  2. Install Django, Django REST Framework and Other Dependencies
  3. Create a New Django Project
  4. Create a New Django App
  5. Create a Django Model
  6. Update url.py File
  7. Build a Machine Learning Model
  8. Create a Form in Django
  9. Create a Superuser in Django
  10. Build The REST API
  11. Update View in Django
  12. Update the App's URL

Build a Machine Learning Model

Before going into production, we need a machine learning model to start with. You can take any machine learning model to deploy. In this article, we are going to focus more on deployment rather than building a complete machine learning model. So, I took a simple machine learning model to deploy. 

The model is built upon a simple dataset where needs to predict whether a customer would buy a car based on her age and salary. For that, I will build a simple Support Vector Machine classifier to make predictions upon the dataset.

import numpy as np 
import pandas as pd 
dataset = pd.read_csv('Social_Network_Ads.csv') 
X = dataset.iloc[:, 1:4] 
y = dataset.iloc[:, 4] 

from sklearn.preprocessing import OrdinalEncoder 
encoder = OrdinalEncoder() 
X = encoder.fit_transform(X) 

from sklearn.model_selection import train_test_split 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 1000) 

from sklearn.preprocessing import StandardScaler 
scaler = StandardScaler() 
X_train = scaler.fit_transform(X_train) 
X_test = scaler.transform(X_test) 

from sklearn.svm import SVC 
classifier = SVC(kernel = 'linear', random_state = 0) 
classifier.fit(X_train, y_train) 
y_pred = classifier.predict(X_test) 

from sklearn.metrics import confusion_matrix 
cm = confusion_matrix(y_test, y_pred)

This is a simple machine learning model to work on. Now, we need to save it in pickle files.

import pickle 
filename = 'Encoder.sav' 
pickle.dump(encoder, open(filename, 'wb')) 

filename = 'Scaler.sav' 
pickle.dump(scaler, open(filename, 'wb')) 

filename = 'Prediction.sav' 
pickle.dump(classifier, open(filename, 'wb'))

Install Django, Django REST Framework and Other Dependencies 

Before going to the development, you should create a virtual environment in your computer. This is helpful to manage the development process with ease. So, it is recommended to use a virtual environment. 

If you are using the Anaconda platform, go to the anaconda prompt, and write the following-

conda create -n DjangoML python=3.7

This will create a virtual environment. Now, activate the environment-

conda activate DjangoML

After activating the environment, install all the requirements for our project.

pip install numpy pandas sklearn pickle django djangorestframework

This will install all the dependencies into your virtual environment.

Create a New Django Project 

In Django, the first step is to create a project which will contain the applications(Django lets you build different application under a single project). This is super easy and can be created with a single command. 

In the command line, go to the specific directory where you want to create the project. Then write the following command-

django-admin createproject DeployML

 With this, you will get a Django project containing all the important files you need to build your applications. The project structure should look like this-


Create a New Django App

Django lets you build many apps under a single project. An app is a complete web application containing all the necessary files and codes to run independently from other apps. All you need do is to create an app and register it into the project and change some other settings to make it run. You can use apps from other projects too. 

Type the following command to create a new app in the project- 

django-admin createapp DjangoAPI

This will create a Django app inside the project. Remember rest_framework is itself an app to Django.

Now, go to the settings.py file and register both the rest_framework and your created app in the INSTALLED_APPS section.


Create a Django Model

The most important part of our project is to create a database where we can keep and retrieve the data. This database will take care of all the data users provide through the web interface. Upon this data, our machine learning model will make predictions. This data can be used in the future to continuously improve our ML model. 

In Django, we can do it simply by making a model. A model is a class in python where we will create the necessary fields to take data from the users. With the specified fields in the model, a similar table will be created in your database. The fields will be the names of the features of our dataset. To build a model identical to our dataset, write the following code in the model.py file of your app- 

from django.db import models
class Customer(models.Model ): 
    GENDER_CHOICES = (('Male','Male'),('Female', 'Female') ) 
    gender = models.CharField(max_length=6, choices=GENDER_CHOICES) 
    age = models.IntegerField() 
    salary = models.IntegerField()

Here, the Customer is the required model to make our database where gender, age, and salary will represent the features of our dataset. 

Now, we need to migrate this model as a table in our dataset. SQLite is the default database in Django. But it supports other databases such as PostgresSQL, MongoDB, MariaDB, Oracle, and so on. You can use any of these databases for your project. 

You need to write two different commands to migrate the tables. Type the following commands for that- 

python manage.py makemigrations 
python manage.py migrate

This will create a table named Customers into your database.

You need to register this model to the admin.py file to make it work.

from django.contrib import admin
from .models import Customer


Update url.py File

Django comes with a default url.py file in the project. This file keeps the URLs you  need to access the different web pages or applications you build under the project. Django does not provide a url.py file for apps, you need to create that file for every application you under your project. In the app-specific url.py file, the URLs to access different parts/web pages of an app are listed. 

Remember, you need to update both the url.py file. 

In projects url.py file, write the following-

from django.contrib import admin 
from django.urls import path, include 

urlpatterns = [ 
                path('admin/', admin.site.urls) 
                path('', include('DjangoAPI.urls')), 

Create a Superuser in Django 

Now, we need to create a user account as an admin to access and control our databases and other pages. In Django, it is made easier with the following command-

python manage.py createsuperuser

This will require you to give your email address and set a password. Do exactly what it says and create a superuser account in your web application. 

After creating a superuser account, you can now check the table and edit it through the admin site. 

Create a Form in Django

In our project, we need to collect information from the users, run the ML model into the collected data, and show the output to the user. If we want to collect data from the users, we need to build a form structure in HTML. In Django, the process of creating a form can be done simply with the Form class. This class is much similar to the structure of a Django model. It will simplify all the complicated tasks of managing forms manually by yourself. With this class, you can prepare the HTML template for display the form, render the data, return data to the server, validate and clean up the data and then save or pass the data on for further processing. 

Now, we will build a simple form to collect data for our project. Create a  forms.py file into the DjangoAPI app directory and write the following-

from django import forms 

class CustomerForm(forms.Form): 
    gender = forms.CharField(choices=[('Male', 'Male'), ('Female', 'Female')])
    age = forms.IntegerField() 
    salary = forms.IntegerField()

This code will create a form that you can use further for different purposes. 

We need to create a simple HTML file to show our form to the user. In your templates folder, create a form.html file for showing the form.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <form method='post'>
    <button type="submit">Submit</button> 

This HTML form will be used to collect information.

Build The REST API 

As we have discussed earlier, we will use a REST API to transfer data between the model and the database. Using the Django-REST framework we can build an API in no time!

In order to allow our model to understand and work with the data, we need to first convert them into native Python datatypes that we can easily render into JSON or XML. In Django, we can use serializers to convert complex data like querysets and model instances to convert into native Python data types and vice versa. It is similar to model and form class provided by the framework.

Django-REST framework provides a class named Serializers to build your own serializers. Create a file name serializer.py and start editing like the following. 

from rest_framework import serializers 
from .models import Customer 

class CustomerSerializers(serializers.ModelSerializer): 
    class meta: 

This will be do all the tasks regarding data conversions. We need to set the URL for the API. This will be done later when we will update the app's url.py file

Update View in Django

So far we have built most of the necessary things to make our model work. Now, it's time to do the most crucial part of our project, updating the views.

In Django, view is a python function that takes all the web requests of the site and returns web responses. The responses can be anything, in the project we need to redirect the user to the form, collect the data from, process it, and show the result to the users. All these things will be done in the view.

Go to the views.py file and update it like the following-

from .forms import CustomerForm 
from rest_framework import viewsets 
from rest_framework.decorators import api_view 
from django.core import serializers 
from rest_framework.response import Response 
from rest_framework import status 
from django.http import JsonResponse 
from rest_framework.parsers import JSONParser 
from .models import Customer 
from .serializer import CustomerSerializers 
import pickle import json 
import numpy as np 
from sklearn import preprocessing 
import pandas as pd 
from django.shortcuts import render 

class CustomerView(viewsets.ModelViewSet): 
    queryset = Customer.objects.all() 
    serializer_class = CustomerSerializers 

def ShowStatus(request): 
        encoder=pickle.load("/Users/Hannah/Desktop/ML Deploy/Encoder.sav") 
        scaler=pickle.load("/Users/Hannah/Desktop/ML Deploy/Scaler.sav") 
        model=pickle.load("/Users/Hannah/Desktop/ML Deploy/Prediction.sav") 
        newdf=pd.DataFrame(y_pred, columns=['Status']) 
        newdf=newdf.replace({True:'Yes', False:'No'}) 
        return JsonResponse('Your Status is {}'.format(newdf), safe=False) 
        return (newdf.values[0][0],X[0]) 
    except ValueError as e: 
        return Response(e.args[0], status.HTTP_400_BAD_REQUEST) 

def FormView(request): 
    if request.method=='POST': 
        form=CustomerForm(request.POST or None) 
        if form.is_valid(): 
            Gender = form.cleaned_data['gender'] 
            Age = form.cleaned_data['age'] 
            EstimatedSalary = form.cleaned_data['salary'] 
            myDict = (request.POST).dict() 
            df=pd.DataFrame(myDict, index=[0]) 
            messages.success(request,'Application Status: {}'.format(answer)) 
    return render(request, 'templates/form.html', {'form':form})

Update the App's URL

To access all the different parts of our Django app, we need to specify the URLs of the app. First, create a url.py file under the app and update the URLs like the following-

from django.contrib import admin
from django.urls import path,include
from . import viewsfrom rest_framework import routers

router = routers.DefaultRouter()
urlpatterns = [ 
                path('api/', include(router.urls)), 
                path('status/', views.ShowStatus), 
                path('form/', views.FormView, name='form'),

Now, we are all set to collect data from the user, pass them to the model by the REST API, and process them using the model we pickled earlier.

Final Thoughts

If you have come this far, congratulations to you! Now, you have learned how to deploy a machine learning model using Django and REST API. This tutorial showed you the basic steps for making a machine learning model run on the web. Try to make changes in the tutorials code for your own project. This will help you to understand the process of deployment better.

Hope this tutorial helped you to understand the aspects of deploying machine learning models in Django. If you have any ideas to make this tutorial better, let me know in the comments.

Happy Machine Learning!

© aionlinecourse.com All rights reserved.