Civo ML provides an excellent platform for the training and deployment of machine learning models, amongst other things.

Django is one of Pythons' most powerful web frameworks. It follows the Model-View-Controller (MVC) architectural design pattern and is used to build scalable web applications. We will be using it in this article to create a simple front end for our sample Civo ML model.

Civo KfaaS (Kubeflow as a Service) refers to a serverless platform offered by Civo that aids in running and deployment of serverless functions using Kubernetes. Training your machine learning models using Civo KfaaS eradicates the backlog of individually setting up and maintaining your development environment. This in turn enables you to focus mostly on the data analysis process. Effective resource allocation also comes in handy when we choose Civo KfaaS to train our machine learning models. For example, while using Civo KfaaS, we are allocated sufficient CPU, memory and GPU, according to our dataset and model size, making it the best for training large scale models. This ensures our models train faster and a bit more efficiently.

In this article, I will take you through how to use Civo KfaaS to train a sample machine learning model, then connect it to a simple Django frontend.

You can learn more about Civo’s machine learning offering here:


  • A Civo account
  • Basic Django framework knowledge
  • Access to Civo KfaaS
  • Accessing Civo ML

    In order to access Civo KfaaS, follow these steps:

    Log into your Civo account and navigate to the products sub-menu > select “Machine Learning” > click the “Launch your CPU ML project” button found at the center of the page.

    Once you have completed the previous steps, go ahead and create a KfaaS cluster by clicking on the “Create a KfaaS cluster” button.

    Fill in the specifications for your cluster, then click on the “create KfaaS” button.

    From here, the cluster takes a few minutes to build before it is ready.

    When your KfaaS cluster is finally ready, you can access your Kubeflow dashboard (via the Kubeflow dashboard button at the bottom of the page) by login in.

    Your Kubeflow dashboard should look like the image below:

    Kubeflow dashboard On the left side menu, click on “Notebooks' ' which will successfully launch a Jupyter Notebook server built on top of the Kubeflow environment.

    For you to efficiently run a notebook server in a Kubeflow environment, CPU, memory, storage, and networking are some of the basic requirements that need to be met. Their specifications depend on the type of notebook server you need to run.

    For the CPU, a minimum of 2vCPUs is highly recommended, but it could be adjusted based on the workload intended. For the memory specification, it is advisable to have a minimum of 4GB so that your notebook server can run efficiently. However, just like the CPU specification, the memory specification can be adjusted according to your project needs.

    Training a sample model

    In this example, we are going to train a simple language translation machine learning model that will help us translate English to French. For this, we will upload a local dataset to our Notebook server.

    To upload datasets to our Kubeflow Notebooks, go to the interface's file browser section and click on the "upload" button.

    Once our dataset has been uploaded, you will see the code below on the Notebook:

    # modules import
    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sklearn.feature_extraction.text import CountVectorizer
    from sklearn.linear_model import LogisticRegression
    # Load the dataset
    df = pd.read_csv('translation_dataset.csv')
    # Split the dataset into input (English) and target (French) texts
    X = df['English_Text']
    y = df['French_Text']
    # Split the dataset into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    # Create a CountVectorizer to convert text into numerical features
    vectorizer = CountVectorizer()
    X_train_vec = vectorizer.fit_transform(X_train)
    X_test_vec = vectorizer.transform(X_test)
    # Train a logistic regression model
    model = LogisticRegression(), y_train)
    # Evaluate the model
    accuracy = model.score(X_test_vec, y_test)
    print("Accuracy:", accuracy)

    Once we are satisfied with our model, we are going to package it so that we use it in our Django front end.

    Add the code below on the last cell of your Notebook instance:

    import joblib
    # model = LogisticRegression(), y_train)
    # Save the model to a file
    joblib.dump(model, 'model.pkl')

    In the code we used above to train our model, we made use of a logistic regression algorithm. A logistic regression algorithm is a statistical model used to predict and give the highest probability between one or more outcomes based on an input feature. We mostly use logistic regression algorithms to help solve binary classification issues.

    For example, we can use logistic regression to predict the probability of customers clicking on links/adverts on a certain site. Assuming we have a customer's dataset, we could use a logistic regression algorithm to predict a binary outcome by fitting a linear model, using the sigmoid function, to the data provided. Once the model is trained to our satisfaction, we can then go ahead and predict the highest probability of a customer clicking on a link/advert.

    You can read more on Logistic regression algorithms here.

    In our trained model above, we used the joblib.dump function to save our trained model. Our saved model is now stored in a file named model.pkl. This method of saving our model into a file is referred to as model pickling. By making use of model pickling, it becomes easier to export and use in other applications of your choice, eradicating the need to re-train our model every time we need to use it. After model pickling, our model is also able to load faster because our interpreter quickly interprets and reconstructs our model architecture from our file and uses it as needed.

    We can now download our model from the Kubeflow Notebook environment to our local machine, where we are going to develop a simple Django front end.

    Select "Download" from the Notebook interface to begin the download process. Once this is complete, a copy of the trained model should be available on the local machine.

    Building the Django frontend

    Now that we have our model trained to our satisfaction and saved, let us begin setting up our front end, which will be used to display and test out the new model.

    Creating Django project and app

    Before the development of every Python project, you need to create a virtual environment for it. To create a virtual environment, check out Python documentation here..

    Once we are done creating our virtual environment, run the below command into your terminal to create a new Django project named djangoProject:

    django-admin startproject djangoProject

    Then run the following command to install Django into your project:

    pip3 install django==3.*

    Once we are done with the Django installation, you need to change directories for the new project by running the command:

    cd djangoProject

    The project directory should have the following structure:


    Inside our djangoProject directory, run the command below to create a new app named djangoApp, which we will then use to develop our front end.

    python3 startapp djangoApp 

    The app directory should now look like the one below:


    To properly configure and connect the djangoProject and djangoApp, there is a couple of steps you’ll need to do:

    • Navigate to djangoProject and into the settings file,and add the app name, djangoApp into the Installed_App list.
    • Create a new URL file, in the djangoApp: The URL file helps us define the patterns of our app by mapping each view to a specific URL pattern.
    • In the project’s URL file, import the include function from django.urls then add the app’s URL as a new URL pattern using the include function.

    After following these steps, you have successfully connected djangoProject and djangoApp.

    Loading saved model to the Django project

    Now that the Django project is set up, we can create a new directory called savedModel at our project root, where we will store our saved Civo KfaaS model. Then you can export the model.pkl file to the savedModel directory, resulting in the trained Civo KfaaS model properly placed in your Django project.

    From here, we are going to load the model in our views file.

    from django.shortcuts import render
    from django.http import JsonResponse
    from sklearn.externals import joblib
    def translation_view(request):
        if request.method == 'POST':
            # Load the saved model
            model_path = 'djangoProject/djangoApp/savedModel/model.pkl'
            model = joblib.load(model_path)
            # Get the input text from the request
            input_text = request.POST.get('text', '')
            # Perform translation using the loaded model
            translated_text = model.predict([input_text])[0]
            # Return the translated text as a JSON response
            return JsonResponse({'translated_text': translated_text})
        return render(request, 'translation.html')

    Next, you’ll need to have a translation_view function that takes a request object as a parameter and checks if the HTTP request method is a POST request before executing the function. The next line then loads the model into the view file. After this, you’ll have an input_text variable that is assigned to the value of text in POST request data.

    Next, our model calls the predict method to our [input_text], then the result is assigned to the translated_text variable. A JSON response object is then returned with translated_text as content. A HTML template is rendered after all the functions' conditions are met.

    In the URLs, we are going to define a URL pattern that will map to the above translation view:

    from django.urls import path
    from . import views
    urlpatterns = [
        path('translate/', views.translation_view, name='translate'),

    The final thing we are going to do is create a HTML template that is going to display our Django frontend:

    • In the djangoApp directory, create a new directory named templates
    • Inside the templates directory, create a new file named index.html

    To generate a simple form that lets users enter sample texts and sentences for English-French translation, enter the following code in the index.html file:

    <!DOCTYPE html>  
    <html lang="en">  
    <meta charset="UTF-8">  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title> Django </title>  
    <h1>Language translation</h1>  
      <form method="POST" action="{% url 'translate' %}">
      {% csrf_token %}
      <input type="text" name="text" placeholder="Enter English text">
      <button type="submit">Translate</button>

    Finally, run the command below in the terminal to test the application on the local server:

    python3 runserver

    In your browser, you should be able to see a layout similar to the one below:

    language translation

    Once satisfied with the application, go ahead and publicly deploy it on different platforms!


    Following the instruction above, we successfully used Civo ML and Civo KfaaS to train a sample translation model, save it using model pickling, and connect it to a minimal Django frontend. From this, developers can leverage Civo ML with Django's flexibility to create faster and more impactful machine learning apps by following the methods outlined above.

    By exploring more Kubeflow and Django documentation, you can transform your serverless machine learning journey!