Generating Text using GPT-2 and Python

Daily Dose of Python
3 min readJan 24, 2023

Python is a powerful and versatile programming language that is widely used in various industries. In this blog post, we’ll take a look at generating Text using GPT-2 and Python

Generative Pre-trained Transformer 2 (GPT-2) is a state-of-the-art language model developed by OpenAI. It has the ability to generate human-like text, which can be used for various natural language processing (NLP) tasks such as text generation, language translation, and language summarization. In this blog post, we’ll take a look at how to use GPT-2 in Python for text generation.

The first step in working with GPT-2 is to install the openai library using pip. Once you've installed the library, you can use the openai.Completion.create() function to generate text. For example:

import openai

openai.api_key = "PUT_YOUR_API_KEY_HERE"

prompt = "Once upon a time"

completions = openai.Completion.create(engine="text-davinci-002", prompt=prompt, max_tokens=1024, n=1,stop=None,temperature=0.7)
message = completions.choices[0].text
print(message)

The openai.Completion.create() function takes several parameters such as the engine which is the model to use, the prompt which is the text to generate completions for, max_tokens which is the maximum number of tokens to generate, n which is the number of completions to generate, stop which is a string where the API will stop generating further tokens, temperature which controls the creativity of the response.

In the example above, we first set the openai API key using the openai.api_key variable. Then, we defined the prompt variable as "Once upon a time" which is the text that we want GPT-2 to complete. We set the max_tokens to 1024 which means that GPT-2 will generate up to 1024 tokens of text. We set n to 1, which means that we only want one completion generated. We set stop to None, which means that GPT-2 will not stop generating text until it reaches the max_tokens limit. Finally, we set the temperature to 0.7, which controls the creativity of the response.

Here’s an example of what the output might look like:

Once upon a time, there was a beautiful princess who lived in a grand castle. 
She spent her days wandering the lush gardens and sparkling fountains of the castle grounds, dreaming of the day when she would find true love.
One day, a handsome prince came to the kingdom and asked for her hand in marriage.
The princess was overjoyed and said yes, and they lived happily ever after.

You can also use GPT-2 to generate text for other NLP tasks such as language translation and language summarization. For example, you can use the openai.Completion.create() function to generate a summary of a given text by using the prompt parameter:

prompt = "Please summarize this article: 'The article is about the benefits of exercise'"
completions = openai.Completion.create(engine="text-davinci-002", prompt=prompt, max_tokens=1024, n=1,stop=None,temperature=0.7)
message = completions.choices[0].text
print(message)

Do note that the output in the generated form the code above would vary based on the input to the model and the specific version of the model you are using. But you can copy and paste the code in your local python environment and run it to see the output.

In conclusion, GPT-2 is a powerful language model that can be used for various NLP tasks such as text generation, language translation, and language summarization. The openai library makes it easy to work with GPT-2 in Python and provides a wide range of tools for text generation. With consistent practice, you will become comfortable with these tools and will be able to implement them in your code with ease.

Hope you liked this post :)

--

--

Daily Dose of Python

Come and learn something new and exciting about Python every single day! New Post every Day @ 7 PM IST.