MITB Banner

Write an essay in 5 lines of code using GPT-Neo

GPT Neo is an open-source alternative to GPT 3. It is an open-source model trained like GPT 3, an autoregressive transformer using the mesh library.

Share

The text generator has risen in the writers’ industry because who doesn’t need an ‘assistant’ that can handle your work efficiently and professionally. Especially with the rise of Generative Pretrained Transformer 3 (GPT 3) created by Open AI which is a powerful AI writing tool. The unique thing about GPT-3 is that it is the first-ever program that can learn how to write like a human without needing to be taught by a computer scientist. Such cutting edge technology doesn’t come for free. So, Eleuther AI came up with a solution GPT-Neo and GPT-J. In this article, we will be talking about GPT-Neo and will show how an essay can be written with it in just 5 lines of code. Following are the topics to be covered.

Table of contents

  1. About GPT-Neo
  2. Generating Text with GPT-Neo

Let’s start with talking about GPT-Neo

About GPT-Neo

GPT Neo is an open-source alternative to GPT 3. It is an open-source model trained like GPT 3, an autoregressive transformer using the mesh library. GPT-Neo has 3 versions.

  • With 125 million parameters, 
  • With 1.3 billion parameters which are equivalent to GPT-3 Babbage 
  • With 2.7 billion parameters 

Eleuther AI has also developed another alternative GPT-J which is the most powerful open source text generator with 6 billion parameters.

CPU and GPU usage

GPT-J needs around 25GB of GPU VRAM as well as many CPU threads to run. On CPUs, GPT-J runs slowly, so it is simpler to run it on a GPU. This does not fit in most of the existing NVIDIA GPUs that come with 8GB or 16GB of VRAM. In addition to these requirements, it is impossible to test GPT-J and GPT-Neo, let alone use them reliably for inference in production with high availability and scalability in mind. But thanks to cloud computing we can run these NLP models smoothly.

Let’s generate some text using GPT-Neo in python.

Are you looking for a complete repository of Python libraries used in data science, check out here.

Generating Text with GPT-Neo

In this article, we will be using a predefined pipeline that will make it easier to access the outcomes. This pipeline is built by hugging face using the Eleuther AI GPT-Neo and with help of this, we can generate a paragraph just in three lines of code.

Installing the required packages:

Need to install the PyTorch library for multidimensional tensors and mathematical operators.

  • PyTorch is an open-source python library based on Torch which is developed on C. Pytorch is developed and maintained by Facebook AI. In simple words, it is a NumPy with GPU.
! pip install torch torchvision torchaudio

Need to install the transformers 

  • A transformer in NLP is an architecture that can handle long-range dependencies while solving sequence to sequence tasks without using sequence-aligned RNNs or convolutions.
! pip install transformers

Download the pipeline for text generation:

from transformers import pipeline
generator = pipeline('text-generation', model='EleutherAI/gpt-neo-1.3B')

Here using GTP-Neo 1.3 billion parameters since the 2.7 billion parameter version needed 20GB of VRAM and it takes much more time for text generation.

Parameters for model:

prompt = 'The rise of Natural Language Processing'
res = generator(prompt, max_length=300, do_sample=True, temperature=0.5)
res1= generator(prompt, max_length=300, do_sample=True, temperature=0.7)
res2= generator(prompt, max_length=300, do_sample=True, temperature=0.9)

Parameters description:

  • Prompt is where you need to input the title for the paragraph or essay.
  • Max_length is the total number of words to be in the paragraph.
  • Temperature is the randomness of words. It varies between 0 to 1.

As in the above line of code, I used three different generators with different randomness to see the efficiency of the transformer.

Output:

The output is in a dictionary format so extract the text from this dictionary using the below line of code.

print(res[0]['generated_text'])
print("-------------------------------------------------------------------------")
print(res1[0]['generated_text'])
print("-------------------------------------------------------------------------")
print(res2[0]['generated_text'])

Output from the generator with the randomness of 0.5.

In this result, we can observe that the same line is being repeated multiple times.

Output from the generator with the randomness of 0.7.

In this result, some sentences were not making any sense. 

Final generator with randomness 0.9.

This is preferably the best one out of all these and the sentences were making sense when read.

Final Words

GPT-Neo is a powerful alternative to GPT 3, and with the implementation, got a glimpse of its great potential. It is a fabulous assistant to kick start the writing process. With the help of hugging face pipeline, the experience of GPT-Neo becomes better and in just three lines of code get a jump start on the topic. With a hands-on implementation of this concept in this article, we could write an essay with the help of artificial intelligence.

References

Share
Picture of Sourabh Mehta

Sourabh Mehta

Sourabh has worked as a full-time data scientist for an ISP organisation, experienced in analysing patterns and their implementation in product development. He has a keen interest in developing solutions for real-time problems with the help of data both in this universe and metaverse.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India