How to deploy a scalable Scikit-Learn machine learning model in Production with Kubernetes (Part 2)

Soumyadip Dutta
3 min readMay 20, 2021

--

In this chapter we’ll use per-trained Sc kit-Learn model for inference. We’ll use Django Rest Framework to create API endpoints for serving.

Setup a Django Rest Project:

  • Before begin install djangorestframework python package.
  1. Initialize project first in the root of existing repository: (Notice the trailing dot)
$ django-admin startproject serving .

2. Create the Django app:

$ cd serving
$ django-admin startapp api

3. Initialize the database:

$ cd ..
$ python manage.py migrate && python manage.py makemigrations

4. If you face any problem setting up a Django Project, step by step can be found here: https://www.django-rest-framework.org/tutorial/quickstart/
If you want to create a superuser you can do so.

5. Add this rest framework as a Django module app in serving/settings.py:

INSTALLED_APPS = [
...
'rest_framework',
]

6. After setting up the project the whole project structure will look like this:

Create a prediction module:

  • This module will handle all the data preprocessing work and will serve prediction.
  1. Create a file serving/api/predict.py .
  2. Create a prediction class PredictionServer() which handles all the prediction related activity. This class has methods to parse the incoming JSON data, loading the trained model and providing prediction.

3. Now we have to work on API side. Create a file serving/api/views.py .

4. Create a Django REST Viewset called PredictionViewSet() . Viewset is an interface between incoming HTTP requests and prediction module. To know more details about Django REST Viewsets click here: https://www.django-rest-framework.org/api-guide/viewsets/

5. Add this viewset to Django routing in serving/urls.py

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

from serving.api.views import PredictionViewSet

urlpatterns = [
path('admin/', admin.site.urls),
path('predict/', PredictionViewSet.as_view({'post': 'post'}))
]

6. Now we are good to run our prediction service. First do a database migration and then run the app.

$ python manage.py makemigrations && python manage.py migrate

7. After this stage start the app by running:

$ python manage.py runserver

If everything works fine then you can make a POST request at endpoint http://127.0.0.1:8000/predict/ Use Postman or alternatively you can navigate to the same URL in your browser.

8. Send some content to get prediction:

9. In case any field is missing defined in MANDATORY_INPUT_FIELDS list you will get an error.

{"status": "error","message": "Missing mandatory fields"}
  • At this stage we are successfully able to deliver the prediction to the end users via RESTful API services. In the next chapter we’ll discuss how to scale our model prediction services so that we can serve thousands-millions of request/sec.

To get the source code of this tutorial please visit this link: https://github.com/dsoumyadip/scikit-learn-deployment-demo

--

--

No responses yet