⚡️ Saturday AI Sparks 🤖 - 📝 Generate Text with GPT-2
Posted On: August 30, 2025
Description:
Text generation is one of the most exciting applications of modern AI. With models like GPT-2, you can provide a short prompt, and the model will generate realistic text continuations.
The best part? Using Hugging Face’s transformers library, you can try this in just a few lines of Python — no API key required.
Why GPT-2 for Text Generation?
- Language fluency: GPT-2 was trained on massive internet data and can generate coherent sentences.
- Fast & lightweight: Variants like distilgpt2 are optimized for speed, perfect for local experiments.
- Creative flexibility: By adjusting decoding parameters, you control randomness and style of the output.
Installing Required Packages
We need Hugging Face’s transformers and PyTorch as the backend.
pip install transformers torch
Setting Up the Text Generator
We use the Hugging Face pipeline for text-generation. For efficiency, we’ll use the smaller distilgpt2 model.
from transformers import pipeline
generator = pipeline("text-generation", model="distilgpt2")
Providing a Prompt
The model needs a starting point. You can experiment with any short phrase or question.
prompt = "In the next five years, Python developers will"
Generating Text
We configure decoding parameters:
- max_length → total length of output.
- temperature → controls creativity.
- top_k / top_p → control diversity of words.
outputs = generator(
prompt,
max_length=60,
temperature=0.9,
top_k=50,
top_p=0.95,
do_sample=True,
num_return_sequences=2,
truncation=True,
pad_token_id=50256
)
Key Takeaways
- GPT-2 is a powerful local model for text generation.
- By adjusting parameters like temperature and top_p, you can make the text more focused or creative.
- This script forms the foundation for chatbots, story generators, or AI writing tools.
Code Snippet:
# Import the pipeline utility from Transformers
from transformers import pipeline, set_seed
# Create a text-generation pipeline with a small, fast model
# You can switch to "gpt2" for the full model if your machine can handle it.
generator = pipeline("text-generation", model="distilgpt2")
# Set a seed for reproducibility (same outputs across runs)
set_seed(42)
# Your starting text
prompt = "In the next five years, Python developers will"
print("Prompt:", prompt)
# Generation configuration
gen_kwargs = {
"max_length": 60, # total tokens including prompt
"temperature": 0.9, # creativity; try 0.7–1.1
"top_k": 50, # sample from top-k tokens
"top_p": 0.95, # or nucleus sampling
"do_sample": True, # enable sampling (not greedy)
"num_return_sequences": 2, # generate multiple candidates
}
# Generate multiple completions
outputs = generator(prompt,
truncation=True, # handle long prompts explicitly
pad_token_id=50256, # set EOS token as padding
**gen_kwargs)
# Display results
for i, out in enumerate(outputs, 1):
print(f"\n=== Completion {i} ===")
print(out["generated_text"].strip())
Link copied!
Comments
Add Your Comment
Comment Added!
No comments yet. Be the first to comment!