Using Django for Machine Learning like a Legend!

Rishi Banerjee
Analytics Vidhya
Published in
4 min readMay 16, 2020

I often find myself in the middle of a situation where people are pondering over one topic. And the topic is quite big.

“We have made this ML model, how do we integrate it with our mobile app?”.

I answer with a rather overpowering voice, “You use REST API’s you doofy nut.”

But is it that simple? NO. I never found any good tutorial to integrate ML with any web server. Figuring it out was a great ordeal for me. I’ve looped through so many possibilities. Using a minimized “trained model” embedded in the device for use. But that comes at a cost. Supporting so many platforms. Doing the same for an iOS device is exponentially difficult.

What is the workaround?

The simplest possible way is to manually hardcode your views with your model parameters. Doing so will enable you to run your model script on user interaction with your website. But it's kind of like 1995. Super slow and boring.

What we are looking here for is the legendary feel. Feel like a developer right out of Google. Duh!

Setting up the ML code

Lets quickly set up a sample machine learning code that trains on a given dataset and stores the model for later use.

We are going to use the evergreen Salary dataset for ease of understanding by our “younger” audience.

You can download the dataset from here.

Lets just quickly get through the ML code since its not our topic of discussion today.

Understood?

The code here is a basic example of splitting the dataset data.csv into training and testing sets, training a simple Linear Regression model, and then storing the trained model inside a file named model.sav using pickle to enable model persistency. We use model persistence so that we don't have to train the model again and again. TRAIN once, USE anytime.

The Interesting Part

Now comes the Django part. We are gonna set up a basic Django project with two apps inside it. ‘predict’ and ‘server’. The predict app holds the training code and a loader class which enables us to do the machine learning stuff. The server app has the API view which takes in a POST request and gives you the output back.

django-admin startproject dj_ml_legend && cd dj_ml_legend
python manage.py startapp predict
python manage.py startapp server

Cool, you should now see two virgin apps in your directory.

Predict App

Let's work with the predict app first. Copy and paste your train.py file and the data.csv file inside your predict/ directory. Then write the following Loader class inside your predict/views.py file. This file will enable you to load and use your saved model.

Ah, the Loader

Server App

Now, let's move towards the server app. We are gonna write a simple API view using the amazing django rest framework. The API view calls the Loader class that we just wrote and uses its output as a response to the POST request that is made.

we use BASE_DIR to track the model.sav file

Great, now we have an API view which, all 20 fingers crossed should work perfectly. Wait, we still have the urls to manage.

the API “Endpoint”

This endpoint will give us the salary for a specific age.

No more code?

Nope, no more code. This is the beauty of django. Made for Machine Learning engineers with deadlines xD. LOL

So let’s put our code to test by making a simple POST request in python. Fire up your python console. Before that python manage.py runserver on your root directory.

>>> import requests as r
>>> v = r.post('http://localhost:8000/server/api/salary/', data={'exp': 5.6})

Done, now the post request should have worked. Let's see the output

>>> v.text
'79153.46992551646'

Fast, dynamic, and legendary.

Now this API can be used anywhere. On your websites, your apps, everything.

To access the API from a different device, you might consider using ngrock, but when going into production, I highly recommend pythonanywhere or Heroku.

So I guess that was it, now you can use Django for Machine Learning like a Legend. You can find the source code in this repository.

Stay tuned for more “Using X like a Legend”

Bye!

--

--