Generate Text using Python

Text generation involves generating text using machine learning techniques. The purpose of text generation is to automatically generate text that is indistinguishable from a text written by a human. If you want to learn how to generate text with Python, this article is for you. In this article, I will walk you through how to use the popular GPT-2 text generation model to generate text using Python.

What is GPT-2 Model?

GPT-2 stands for Generative Pre-trained Transformer 2. It is an open-source Natural Language Processing model created by OpenAI. It can generate paragraphs of text with state of the art performance on many language benchmarks. It is also used for machine translation, question answering, and text summarization.

To use the GPT-2 model to generate text using Python, you need to install the Transformers library in Python. It can be easily installed using the pip command on your command prompt or terminal as mentioned below:

  • pip install transformers

I hope you now have understood what GPT-2 model is and how you can install it in your Python virtual environment. You can read more about this model here. Now in the section below, I’ll explain how you can use this model for generating text using Python.

Generate Text using Python

Let’s import the GPT-2 model from the transformers library and start with the task of generating text using Python:

from transformers import pipeline
model = pipeline("text-generation", model = "gpt2")
Downloading: 100%
665/665 [00:00<00:00, 8.60kB/s]
Downloading: 100%
523M/523M [00:11<00:00, 43.5MB/s]
Downloading: 100%
0.99M/0.99M [00:00<00:00, 1.74MB/s]
Downloading: 100%
446k/446k [00:00<00:00, 1.74MB/s]
Downloading: 100%
1.29M/1.29M [00:00<00:00, 3.44MB/s]

Here’s how you can generate text using Python by using the GPT-2 model:

sentence = model("Hi, My name is John Cena, I am here", 
                 do_sample=True, top_k=50, 
                 temperature=0.9, max_length=100, 
                 num_return_sentences=2)

for i in sentence:
  print(i["generated_text"])
Hi, My name is John Cena, I am here to see you. I have been here this entire time. I've worked. I've seen all these things. It's just, man, my life has changed because of you guys. You guys get to see everything, including my career, things like that. You guys have, you know, the most amazing stuff about me.

JANUARY 10, 2015:

After the match, the fans were happy.

Summary

The purpose of text generation is to automatically generate text that is indistinguishable from text written by a human. GPT-2 is an open-source Natural Language Processing model created by OpenAI. It can generate paragraphs of text with state of the art performance on many language benchmarks. I hope you liked this article on generating text using Python and the GPT-2 model. Feel free to ask valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

Data Strategist at Statso. My aim is to decode data science for the real world in the most simple words.

Articles: 1617

5 Comments

Leave a Reply

Discover more from thecleverprogrammer

Subscribe now to keep reading and get access to the full archive.

Continue reading