How to Build a Text Generator with OpenAI GPT-3 and Flask

In this tutorial, we will learn how to build a text generator using OpenAI’s GPT-3 language model and Flask web framework. OpenAI’s GPT-3 is a state-of-the-art language model that can generate human-like text based on the given input. Flask is a lightweight web framework that allows us to create web applications easily. By combining these two technologies, we can create an interactive web application where users can generate text using GPT-3.

Prerequisites

Before we begin, make sure you have the following prerequisites:

  • Python 3.6 or above installed on your machine
  • OpenAI Python library installed (openai)

To install the OpenAI library, use the following command:

pip install openai

Setting up OpenAI API

To use OpenAI GPT-3, you need to have an API key. If you don’t have one, you can sign up on OpenAI’s website to get access to the API. Once you have the API key, set it up as an environment variable on your machine. For example, on Linux, you can add the following line to your ~/.bashrc or ~/.bash_profile file:

export OPENAI_API_KEY='YOUR_API_KEY'

Replace 'YOUR_API_KEY' with your actual API key.

Now, let’s start building our text generator!

Creating a Flask Application

To begin, create a new folder for our project and navigate to the project folder using the terminal. Then, create a new Python virtual environment and activate it with the following commands:

mkdir text-generator
cd text-generator
python3 -m venv venv
source venv/bin/activate

Now that we have our virtual environment activated, let’s install Flask:

pip install flask

Next, create a new file called app.py in the project folder and open it in a text editor.

touch app.py

Initializing Flask Application

In app.py, let’s start by importing the necessary modules and initializing the Flask application:

from flask import Flask, render_template, request
import openai

app = Flask(__name__)

Next, we need to configure the OpenAI API client by setting the API key:

openai.api_key = 'YOUR_API_KEY'

Replace 'YOUR_API_KEY' with your actual API key.

Creating the Home Page

Let’s create a simple home page where users can enter the text prompt. Add the following route decorator to define the route for the home page:

@app.route('/', methods=['GET'])
def home():
    return render_template('index.html')

This route renders an HTML template called index.html which we will create shortly. In the template, users will be able to enter the text prompt.

Before we create the index.html template, let’s create a new folder called templates in the project folder to store all our HTML templates:

mkdir templates

Create a new file called index.html in the templates folder and open it in a text editor:

touch templates/index.html

In index.html, add the following content:

<!DOCTYPE html>
<html>
<head>
    <title>Text Generator</title>
</head>
<body>
    <h1>Text Generator with GPT-3</h1>
    <form method="POST" action="/generate">
        <textarea name="prompt" rows="10" cols="50"></textarea><br>
        <input type="submit" value="Generate">
    </form>
</body>
</html>

Save the file. Now, when users visit the home page, they will see a simple form with a text area where they can enter the text prompt and a submit button to generate the text.

Generating Text with GPT-3

Let’s create a new route to handle the text generation logic. Add the following route decorator below the home route decorator:

@app.route('/generate', methods=['POST'])
def generate():
    prompt = request.form['prompt']
    response = openai.Completion.create(
        engine='text-davinci-003',
        prompt=prompt,
        max_tokens=100,
        n=1,
        stop=None,
        temperature=0.7
    )
    completion = response.choices[0].text.strip()
    return render_template('result.html', prompt=prompt, completion=completion)

Here, we retrieve the text prompt from the form data, generate the completion using the OpenAI API, and render the result.html template with the prompt and completion values.

Before we create the result.html template, let’s create a new folder called static in the project folder to store all our static files:

mkdir static

Create a new file called result.html in the templates folder and open it in a text editor:

touch templates/result.html

In result.html, add the following content:

<!DOCTYPE html>
<html>
<head>
    <title>Text Generator - Result</title>
</head>
<body>
    <h1>Generated Text</h1>
    <p><strong>Prompt:</strong> {{ prompt }}</p>
    <p><strong>Completion:</strong> {{ completion }}</p>
    <a href="/">Back to home</a>
</body>
</html>

Save the file. Now, when users submit the form to generate the text, they will be redirected to a new page that displays the prompt and the generated completion.

Running the Application

To run the Flask application, go back to the terminal and execute the following command:

flask run

By default, the application should start running on `http://127.0.0.1:5000`. Open your web browser and visit this URL to see the text generator in action.

Enter a text prompt in the form and click the “Generate” button. You should be redirected to a new page that displays the generated completion along with the entered prompt.

Conclusion

In this tutorial, we learned how to build a text generator using OpenAI’s GPT-3 language model and Flask web framework. We created a simple web application that allows users to enter a text prompt and generates human-like text based on the prompt using the GPT-3 model. Flask made it easy to create the routes and templates required for our application. By combining the power of GPT-3 with Flask, we can build a wide variety of text generation applications. Happy coding!

Related Post