A beginner-friendly demonstration of text generation using a small language model (DistilGPT-2) via Hugging Face Transformers.
Uses DistilGPT-2 (82M parameters) to generate text from a given prompt. Two scripts are included:
| File | Description |
|---|---|
intro_transformer.py |
Minimal example — loads the model and generates text from a single hardcoded prompt |
intro_transformer_enhanced.py |
Full demo with three modes: batch prompts, interactive input, and a tokenization explainer |
- Python 3.8+
- PyTorch
- Hugging Face Transformers
Install dependencies:
pip install torch transformerspython intro_transformer.pyGenerates a continuation of "Once upon a time" and prints it to the console.
python intro_transformer_enhanced.pyYou'll be prompted to choose a mode:
1. Run basic demonstration # Cycles through 3 example prompts
2. Interactive mode # Enter your own prompts in a loop
3. Explain the process # Shows tokenization step-by-step
Note: The model downloads automatically on first run (~300MB). Subsequent runs use the cached version.
Your text → Tokenizer → Token IDs → DistilGPT-2 → Predicted tokens → Output text
The model predicts the most likely next token repeatedly until it hits the max_length limit. It runs entirely on CPU — no GPU required.