This project demonstrates a lightweight implementation of a small language model (SLM) using the GPT2 architecture via Hugging Face 🤗 Transformers.
It supports text generation in both Persian and English, making it ideal for educational use, rapid experimentation, and low-resource environments.
- ✅ Based on the compact GPT2 model:
openai-community/gpt2
- 🧠 Suitable as a small language model (SLM) for quick testing and concept validation
- 🗣️ Supports bilingual prompts: Farsi (فارسی) & English
- ⚡ Fast execution with minimal dependencies
- 🔁 Easily extendable for fine-tuning or creative applications (e.g., poetry, Q&A, storytelling)
Install required packages using pip:
pip install transformers accelerate torch
- Load the GPT2 model and tokenizer from Hugging Face Hub
- Define a generation function with parameters like temperature and top-p
- Provide a prompt in Farsi or English
- Generate text continuation using the
generate()
method
# Persian example
prompt = "چرا آسمان آبی است؟"
# English example
prompt = "Write a short story about a brave knight and a dragon."
Sample output:
Prompt (فارسی): چرا آسمان آبی است؟
Generated: به دلیل پراکندگی نور خورشید توسط جو زمین، بیشتر نور آبی دیده میشود...
Prompt (English): Write a short story about a brave knight...
Generated: ...who ventured into the mountains to protect his village from the dragon...
slm.py # Main Python script for bilingual text generation
README.md # Project description and instructions
- Bilingual prompt testing
- Educational demos for language modeling
- Creative writing, poetry, or storytelling
- Exploring small LLM behavior in controlled setups