How to Build a Text Classifier with OpenAI GPT-3 and Django

In this tutorial, we will explore how to build a text classifier using OpenAI GPT-3, one of the most advanced language models available today, and integrate it with Django, a powerful and popular web framework written in Python. By the end of this tutorial, you will be able to create a web application that can classify text into different categories using GPT-3.

Prerequisites

To follow along with this tutorial, you should have the following:

  • Basic knowledge of Python and Django.
  • An OpenAI API key. You can get one from the OpenAI website.
  • Python 3.6 or higher and pip installed on your system.

Setting up the Django Project

Let’s start by setting up a new Django project. Open your terminal or command prompt and follow the steps below:

  1. Create a new directory for your project and navigate into it:
$ mkdir text_classifier
$ cd text_classifier
  1. Create a new Python virtual environment and activate it:
$ python -m venv venv
$ source venv/bin/activate
  1. Install Django using pip:
$ pip install django
  1. Create a new Django project and navigate into it:
$ django-admin startproject text_classifier_project .
$ cd text_classifier_project

Creating the Text Classifier App

Now that we have set up the project, we will create a new Django app to handle the text classification functionality. In your terminal/command prompt, execute the following commands:

  1. Create a new Django app:
$ python manage.py startapp classifier
  1. Open the text_classifier_project/settings.py file and add 'classifier', to the INSTALLED_APPS list.

Creating the Text Classification Model

In this step, we will create a Django model to store the text classification data. Open the classifier/models.py file and add the following code:

from django.db import models

class Category(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

class Text(models.Model):
    content = models.TextField()
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

    def __str__(self):
        return self.content

This code defines two models: Category and Text. The Category model represents the different categories for classification, and the Text model represents individual text instances with their associated category.

Next, run the following commands in the terminal to apply the model changes to the database:

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

Creating the Text Classification Form

Now, we will create a Django form to allow users to enter text for classification. Open the classifier/forms.py file and add the following code:

from django import forms
from .models import Text

class TextForm(forms.ModelForm):
    class Meta:
        model = Text
        fields = ['content', 'category']

The TextForm class is a Django model form that is automatically created based on the Text model. It includes fields for the content and category of the text.

Creating the Text Classification View

In this step, we will create a Django view to handle the text classification functionality. Open the classifier/views.py file and add the following code:

from django.shortcuts import render
from .forms import TextForm

def classify_text(request):
    if request.method == 'POST':
        form = TextForm(request.POST)
        if form.is_valid():
            text = form.save(commit=False)
            # TODO: Implement text classification with GPT-3
            text.save()
            return render(request, 'classifier/classify_text.html', {'form': form, 'classification_result': text.category})
    else:
        form = TextForm()
    return render(request, 'classifier/classify_text.html', {'form': form})

The classify_text function is a Django view that handles the text classification functionality. Currently, it just saves the submitted text to the database without performing any classification.

Integrating OpenAI GPT-3 for Text Classification

Now comes the exciting part โ€“ integrating OpenAI GPT-3 into our Django application for text classification. To communicate with GPT-3, we will use the OpenAI Python library, which can be installed using pip:

$ pip install openai

Once installed, you can use the following code to integrate GPT-3 text classification into your classifier/views.py file:

import openai

openai.api_key = 'YOUR_OPENAI_API_KEY'

def classify_text(request):
    if request.method == 'POST':
        form = TextForm(request.POST)
        if form.is_valid():
            text = form.save(commit=False)
            classification_result = classify_with_gpt3(text.content)
            text.category = classification_result
            text.save()
            return render(request, 'classifier/classify_text.html', {'form': form, 'classification_result': classification_result})
    else:
        form = TextForm()
    return render(request, 'classifier/classify_text.html', {'form': form})

def classify_with_gpt3(text):
    response = openai.Completion.create(
        engine='davinci',
        prompt=text,
        max_tokens=1,
        n=1,
        stop=None,
        temperature=0.7,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
    )
    category = response.choices[0].text.strip().lower()
    return category

Make sure to replace 'YOUR_OPENAI_API_KEY' with your actual OpenAI API key. The classify_with_gpt3 function makes a POST request to the OpenAI API with the GPT-3 engine and your prompt text. It returns the generated text from GPT-3.

Now, when a user submits text for classification, it will be sent to GPT-3, and the generated category will be saved in the database. The generated classification result is passed to the template to display to the user.

Updating the Text Classification Template

Next, we will create the template that displays the text classification form and the classification result to the user. Create a new directory called templates inside the classifier app directory, and inside that directory, create a new file named classify_text.html. Add the following code to the file:

{% extends 'base.html' %}

{% block content %}
  <h2>Classify Text</h2>
  <form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Classify</button>
  </form>
  {% if classification_result %}
    <div>
      <h3>Classification Result:</h3>
      <p>{{ classification_result }}</p>
    </div>
  {% endif %}
{% endblock %}

This template extends a base template called base.html (which we will create shortly) and displays the text classification form. If a classification result exists, it will be displayed to the user.

Creating the Base Template

We will now create the base template that our classify_text.html template extends. Create a new file named base.html inside the templates directory in the root folder of your project. Add the following code to the file:

<!DOCTYPE html>
<html>
<head>
  <title>Text Classifier</title>
</head>
<body>
  <header>
    <h1>Text Classifier</h1>
  </header>
  <main>
    {% block content %}
    {% endblock %}
  </main>
</body>
</html>

The base template sets up the basic structure for our web application and provides a header and a main content section. The main content section will be populated by the child templates.

Configuring the URL Routing

In the final step, we need to configure the URL routing to connect our views with the appropriate URLs. Open the text_classifier_project/urls.py file and modify it as follows:

from django.contrib import admin
from django.urls import path
from classifier.views import classify_text

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', classify_text, name='classify_text'),
]

This configuration uses the classify_text function from our classifier/views.py file as the view for the root URL of our application.

Running the Development Server

That’s it! Your text classifier application is now ready to run. In your terminal or command prompt, navigate to the root folder of your project (text_classifier_project) and run the following command to start the development server:

$ python manage.py runserver

You should now see output similar to the following:

Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Open your web browser and visit `http://127.0.0.1:8000/` to see your text classifier application in action. You can enter some text into the form and click the “Classify” button to see the classification result.

Conclusion

In this tutorial, we explored how to build a text classifier using OpenAI GPT-3 and Django. We created a Django app with models, forms, views, and templates to handle text classification. We integrated OpenAI GPT-3 using the OpenAI Python library to generate classifications for input text. By following this tutorial, you should now have a solid foundation for building your own text classification applications using GPT-3 and Django.

Related Post