A Quick Guide to Build and Deploy Machine Leaning Models with IBM Watson and Django

Written by- Aionlinecourse2562 times views

If you are a data scientist and have so far worked with machine learning models locally, applying ML algorithms on data sets in a Jupyter notebook, you should know that there are many ways you can deploy them on the web.

So, how can you deploy an ML model on the web?

Today,  I will walk you through different steps to build and deploy machine learning models on IBM Watson Studio and save the model as an API. Finally, we will develop a Django web application to make use of the API.

The tutorial is divided into three parts-

  • Build a Machine Learning model in IBM Watson Studio

  • Deploy the model as a web service 

  • Develop a Django web app to serve the model and get predictions

Let’s dive into the tutorial!

A Brief Intro to IBM Watson

IBM Watson is a cloud service provided by IBM Cloud infrastructure. This is an intuitive and powerful platform to build, train, manage, and deploy machine learning models. 

With IBM Watson, you can use any open-source framework including PyTorch, TensorFlow, and scikit-learn. Model development tools such as Jupyter notebooks, JupterLap, and CLIs are also available. You can choose any language such as Python, R, and Scala and bring together the development tools and frameworks to build and deploy amazing machine learning models.

If you have an IBM Cloud account, you already have access to IBM Watson and other machine learning services.

If you do not have an IBM Cloud account, click here to make one.

For our task, we will need the following services from IBM Cloud

  • Watson Studio The platform where we will build the model.

  • Watson Machine Learning This service is required to train and run the model

  • Object Storage This is where we will keep the required files like data sets and model results.

We will get introduced to these services shortly in the latter part of this tutorial.

Part 1: Build a Machine Learning model in IBM Watson Studio

Now, let's get to work. In this part, we will develop a simple machine learning model on Watson Studio.

First, log in to your IBM Cloud dashboard and get to the catalog section. There you will find all the services. For our project, add the following services one by one.

Step 1: Add a Watson Studio service

For this, select the Watson studio from the catalog and add a plan. See the following snapshot for reference.


Step 2: Add a Machine Learning service

Machine learning service is the key service for our project. Find it from the catalog menu and add it to your services. It would look like the below image-


 Step 3: Add the object storage instance

We need storage to save our data sets and models. IBM provides many storage services. For our project Cloud Object Storage is suffice. Create one like the following.


We have created all the required services for our projects. Now it's time to create a project and start building our model. 

Go to your Watson Studio dashboard and create a project. There will be two options- you can create an empty project where you need to do all the things from scratch or you can start with a demo project.

As we know how machine learning works, start with an empty project would give us more freedom to create the thing we imagined.


If you don't want to build the model from scratch, try the sample project.

In this tutorial, we will create an empty one like the following-


Note: The storage of the projects would set to our cloud object instance. If not set automatically, you should manually add it from the services and come back to Watson studio. After reloading the tab one time, the storage should be added to the projects.

Part 3: Develop a Django web app to Use the model

We have built and deployed the model as a web service, more precisely an API that we can integrate into any web application to get predictions from the model. We will a Django web app to integrate and use the API we built. 

A Brief Intro to 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 that you can reuse in other projects without making any major changes. This is the greatest advantage of using Django for building web applications.

As the growing need for machine learning applications, Django is the best option to build an application. You can also use Flask or Pyramid, but with Django, you can get more functionalities and power.

Now, we will develop a web app in Django. Let's get started!

Install Django and Other Dependencies

First of all, we need to install Django on our local computer. We can do it in IBM Cloud. But this will lead us to more complexity. For simplicity's sake, we will build the application locally.

I assume you have installed Python already on your computer. If not, go here and get a Python distribution suitable to your machine.

From the command prompt create a virtual environment. It is a best practice to create a virtual environment for any development task. This will let you keep all the things related to a project in one place. In your command prompt write the following-

Then install Django. In command prompt write the following-

Create a New Django Project

Before diving into any development, we need to create a Django project first. In Django, the project is the main place where all the other app is managed. Under a project, you can create multiple apps.

Create a New Django App 

As I said earlier, Django lets you create multiple apps under a project. This app is a complete web application containing all the necessary codes and files to run independently from other apps. 

In Django, it is quite simple to create an app. Just write the following in the command prompt.

Now, register the app in settings.py file of the main project.

Update App's URL

Django has no default url.py file for apps. You need to create the URLs to access the different parts/web pages of your app. For our specific app, create a new url.py file.

This file will contain the URL for our API where we can send data and get predictions. In the file, write the following-

Update Project URL 

In Django, you need to register your app's URL to the project main url.py file. The Django app will look for URL from this file.

Create a Form

Now, we need to create a form for the user. In this form, the user will provide data. This data will be sent to our model through the API we built earlier. The API will fetch the data to the model and in return will come back with the prediction.

Create a Simple View 

In Django, view is the part where the main logic is coded. The view section controls what a user will get from our web application.

Create a Minimal HTML Frontend

We need to make a frontend where the user can give the data. For simplicity's sake, we will build a simple HTML form template. You can use CSS or Bootstrap to make the frontend more beautiful. You can also use Javascript to make the template more responsive and user-friendly. 

As our primary concern is to run the model. We are happy with a simple HTML frontend for now.