How to beginner · 3 min read

How to use text generation pipeline Hugging Face

Quick answer
Use the Hugging Face transformers library's pipeline function with the task set to text-generation. Initialize it with a pretrained model like gpt2, then call it with your prompt to generate text.

PREREQUISITES

  • Python 3.8+
  • pip install transformers>=4.30.0
  • pip install torch (or tensorflow)
  • Internet connection to download pretrained models

Setup

Install the transformers library and a backend like torch for model inference. Set up your Python environment accordingly.

bash
pip install transformers torch

Step by step

Import the pipeline from transformers, create a text generation pipeline with a pretrained model, and generate text by passing a prompt string.

python
from transformers import pipeline

# Initialize text generation pipeline with GPT-2
text_generator = pipeline('text-generation', model='gpt2')

# Generate text from prompt
prompt = "In a distant future,"
outputs = text_generator(prompt, max_length=50, num_return_sequences=1)

print(outputs[0]['generated_text'])
output
In a distant future, humanity has evolved beyond the need for physical bodies, existing as pure consciousness within a vast digital network.

Common variations

  • Use different models like gpt2-medium, distilgpt2, or larger models from Hugging Face Hub.
  • Adjust generation parameters such as max_length, temperature, top_k, and top_p for more creative or focused outputs.
  • Use the pipeline asynchronously with asyncio for integration in async apps.
python
from transformers import pipeline
import asyncio

async def async_generate():
    text_generator = pipeline('text-generation', model='gpt2')
    prompt = "Once upon a time"
    outputs = await asyncio.to_thread(text_generator, prompt, max_length=40)
    print(outputs[0]['generated_text'])

asyncio.run(async_generate())
output
Once upon a time, in a land far away, there lived a wise old wizard who guarded the secrets of the ancient world.

Troubleshooting

  • If you get OSError: Model name 'gpt2' was not found, ensure you have internet access to download the model or have cached it locally.
  • For CUDA errors, verify your PyTorch installation matches your GPU and CUDA version.
  • If generation is slow, try smaller models like distilgpt2 or enable model quantization.

Key Takeaways

  • Use Hugging Face's pipeline with task text-generation for easy text generation.
  • Customize generation with parameters like max_length and temperature.
  • Switch models to balance speed and quality, e.g., gpt2 vs distilgpt2.
  • Async generation is possible using asyncio and asyncio.to_thread.
  • Troubleshoot model loading and environment issues by checking internet, dependencies, and hardware compatibility.
Verified 2026-04 · gpt2, distilgpt2
Verify ↗