A.I Text Generation GPT using Hugging Face Transformers Library Python (no OpenAI API Keys needed)

David Oluyale
2 min readSep 17, 2023

--

There are several models available for text generation and natural language processing in the Hugging Face Transformers library, including various sizes of the GPT-2 models, GPT-3 Models, GPT-4 Models, T5 Models, BERT Models, XLNet Models, RoBERTa Models, DistilBERT Models, Turing-NLG Models, BART Models, and many more models available in the Hugging Face Transformers library.

Each with its own characteristics and use cases. You can choose a model that best suits your specific text generation or NLP task based on factors like model size, task compatibility, and available computational resources.

You can implement any of the above using the Library and make accessible to your frontend application using Python Flask without need OpenAI API keys.

In this lesson, we will use the GPT2 to generate text based on a prompt or context.

Install Required Libraries: Install libraries such as transformers (for accessing pretrained models), torch, and numpy .

!pip install transformers torch numpy

Choose a pretrained language model suitable for text generation. For example, Hugging Face’s Transformers library provides access to various models. For this tutorial, I will use the gpt-2 model.


from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch

model_name = "gpt2" # You can choose a different model on hugging face or fine-tune a model
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)

Finally , we use the pre-trained model to generate text based on a prompt or context.

prompt = "What is a car?"

# Tokenize input
input_ids = tokenizer.encode(prompt, return_tensors="pt", add_special_tokens=True)

# Create an attention mask
attention_mask = torch.ones(input_ids.shape, dtype=torch.long)

# Generate text with attention mask
output = model.generate(input_ids, attention_mask=attention_mask, max_length=100, num_return_sequences=1, no_repeat_ngram_size=2, top_k=50, top_p=0.95, temperature=0.7)

generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print(generated_text)
Result output

This can be extended into an API interface using Python Flask and integrated into a chat UI to allow users interact better.

Colab: https://colab.research.google.com/drive/1EPqyna4VEn6Ui9gKNTByt9HeOfJ2keBx?usp=sharing

--

--

David Oluyale

Over 8 years experience in Software Engineering | Data Scientist @ UNDP ExpertRos