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

In this tutorial, we will walk through the steps to build a text summarizer using OpenAI GPT-3 and Django, a popular Python web framework. We will utilize the power of GPT-3 to generate concise summaries of long texts.

Prerequisites

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

  1. Basic knowledge of Python and Django.
  2. OpenAI GPT-3 API key. You can obtain the API key from OpenAI’s website.
  3. Python 3.x installed on your machine.

Setting Up the Django Project

To get started, let’s create a new Django project and set up the basic structure. Open your terminal or command prompt and run the following commands:

$ django-admin startproject text_summarizer
$ cd text_summarizer
$ python manage.py startapp summarizer

This will create a new Django project called “text_summarizer” and a new app called “summarizer” within the project.

Next, let’s update the project settings. Open the settings.py file located in the text_summarizer directory and add ‘summarizer’ to the INSTALLED_APPS list:

INSTALLED_APPS = [
    ...
    'summarizer',
]

In the same file, locate the TEMPLATES variable and add the following 'DIRS' option:

TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        ...
    },
]

We have added the directory where our templates will be stored.

Next, let’s create a template for our summarizer. Create a new directory called ‘templates’ in the project’s root directory, and then create a new file named ‘index.html’ inside the templates directory.

Creating the Text Summarization Form

Open the index.html file you just created and add the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Text Summarization</title>
</head>
<body>
    <h1>Text Summarization</h1>
    <form method="POST" action="{% url 'summarizer:summarize' %}">
        {% csrf_token %}
        <textarea name="text" rows="10" cols="50"></textarea>
        <button type="submit">Summarize</button>
    </form>
    {% if summary %}
        <h2>Summary:</h2>
        <p>{{ summary }}</p>
    {% endif %}
</body>
</html>

This HTML code creates a basic form where users can input the text they want to summarize. When the form is submitted, it will make a POST request to the summarize URL.

Adding Views and URLs

Now, let’s define the views and URLs for our summarizer app. Open the views.py file located inside the summarizer app directory and replace the existing code with the following:

from django.shortcuts import render
import openai

def summarize(request):
    if request.method == 'POST':
        text = request.POST.get('text')
        api_key = 'YOUR_OPENAI_API_KEY'  # Replace with your actual API key
        openai.api_key = api_key

        response = openai.Completion.create(
            engine='text-davinci-003',
            prompt=text,
            max_tokens=100,
            temperature=0.3,
            n=1,
            stop=None
        )

        summary = response.choices[0].text

        return render(request, 'index.html', {'summary': summary})

    return render(request, 'index.html')

In the summarize view, we first check if the request method is POST. If it is, we retrieve the text entered by the user using request.POST.get('text').

Next, we specify our OpenAI API key by replacing 'YOUR_OPENAI_API_KEY' with the actual key you obtained.

We then use the OpenAI Python package to make an API call to GPT-3 for text summarization. The openai.Completion.create method takes various parameters such as the engine, prompt, max_tokens, temperature, n, and stop.

The summary variable contains the generated text summary obtained from the API response. Finally, we render the index.html template, passing the summary as a context variable.

Next, open the urls.py file located in the summarizer app directory and add the following code:

from django.urls import path
from . import views

app_name = 'summarizer'

urlpatterns = [
    path('', views.summarize, name='summarize'),
]

This code sets up the URL patterns for our summarizer app. We map the root URL to the summarize view.

Testing the Text Summarizer

Now that we have set up our Django project, let’s test our text summarizer. Start the development server by running the following command in your terminal or command prompt:

$ python manage.py runserver

Open your web browser and navigate to `http://localhost:8000` to access the text summarizer form.

Enter a piece of text in the input field and click on the “Summarize” button. The form will be submitted, and the summary will be displayed on the same page.

Deploying the Text Summarizer

Once you are satisfied with the functionality of your text summarizer, you can deploy it to a hosting platform to make it publicly accessible. There are several options to choose from, such as Heroku, AWS, or PythonAnywhere. Below, we will cover a simple deployment using Heroku as an example.

Deploying to Heroku

To deploy your Django app on Heroku, follow these steps:

  1. Create a new Heroku account if you don’t have one already.
  2. Install the Heroku CLI by following the instructions on the Heroku Dev Center.

  3. Login to the Heroku CLI using the following command:

    $ heroku login
    
  4. Create a new Heroku app by running the following command:
    $ heroku create your-app-name
    

    Replace “your-app-name” with your desired app name.

  5. Add the following buildpacks for Python and Node.js:

    $ heroku buildpacks:add heroku/python
    $ heroku buildpacks:add heroku/nodejs
    
  6. Commit any uncommitted changes to your Git repository.
  7. Push your code to the Heroku remote:

    $ git push heroku master
    
  8. Run the following command to scale your Heroku app to one web dyno:
    $ heroku ps:scale web=1
    
  9. Set your OpenAI API key as an environment variable on Heroku using the following command:
    $ heroku config:set OPENAI_API_KEY=YOUR_OPENAI_API_KEY
    

    Replace “YOUR_OPENAI_API_KEY” with your actual OpenAI API key.

  10. Run the following command to open your deployed Django app in a web browser:

    $ heroku open
    

Congratulations! You have successfully deployed your text summarizer app to Heroku.

Conclusion

In this tutorial, we explored how to build a text summarizer using OpenAI GPT-3 and Django. We covered the necessary steps to set up a Django project, wrote the views, and created the necessary templates. We also discussed how to deploy the app to Heroku for public access.

With this text summarizer, you can now generate concise summaries of long texts using the power of OpenAI GPT-3. The possibilities for this type of technology are endless, and you can further enhance and optimize the summarizer based on your specific use case and requirements.

Related Post