A production-ready Bengali Language Model pipeline built with Python, Hugging Face Transformers, and PyTorch.
This pipeline helps you:
- Load & Clean Bengali text data
- Tokenize text (convert to numbers)
- Train a Bengali language model
- Fine-tune on your custom data
- Generate Bengali text from prompts
- Instruction-tune for Q&A tasks
Think of it like creating your own Bengali ChatGPT, starting from scratch!
sleekAI/
├── data/ # Data storage
│ ├── raw/ # Raw Bengali text
│ └── processed/ # Cleaned data (JSONL format)
├── models/ # Trained models
│ ├── tokenizer/ # Tokenizer files
│ ├── checkpoints/ # Training checkpoints
│ └── final_model/ # Final trained model
├── scripts/ # Python scripts
│ ├── prepare_data.py # Data cleaning
│ ├── setup_tokenizer.py # Tokenizer setup
│ └── instruction_tuning.py # Instruction tuning
├── notebooks/ # Jupyter notebooks (optional)
├── config.py # Configuration (ONE PLACE TO CHANGE SETTINGS!)
├── train.py # Main training script
├── inference.py # Generate Bengali text
├── requirements.txt # Python dependencies
└── README.md # This file
pip install -r requirements.txtpython scripts/prepare_data.pyOutput: data/processed/bengali_texts.jsonl
python scripts/setup_tokenizer.pypython train.py⏱️ On CPU: ~15-30 minutes
⏱️ On GPU: ~2-5 minutes
python inference.py| ML Concept | Web Analogy |
|---|---|
| Dataset | Database with millions of rows |
| Tokenizer | URL encoder (text → numbers) |
| Model | Cache + prediction engine |
| Training | Learning patterns from data |
| Inference | Serving predictions via API |
| config.py | .env file (all settings) |
Raw Text → Clean → Tokenize → Train Model → Save → Generate
All settings in config.py:
BATCH_SIZE = 8 # Adjust for memory
NUM_EPOCHS = 3 # More training
LEARNING_RATE = 2e-5 # Learning speed
MAX_SEQ_LENGTH = 512 # Context length
USE_FP16 = False # GPU accelerationCleans Bengali text:
- Removes URLs, emails
- Fixes Unicode encoding
- Removes extra spaces
- Filters invalid texts
- Saves as JSONL
Input: Raw Bengali text
Output: bengali_texts.jsonl
Converts text to numbers:
- Input: "বাংলাদেশ"
- Output:
[1234, 5678, 9012, 3456]
Options:
- Pretrained (recommended): AI4Bharat's Indic-BERT
- Custom: Train on your data
Teaches model to predict Bengali:
- Load pretrained model
- Fine-tune on your data
- Save checkpoints
- Evaluate on validation set
Optimization:
- Gradient accumulation
- FP16 (on GPU)
- Gradient checkpointing
Generate Bengali text:
- Load trained model
- Take Bengali prompt
- Generate token-by-token
- Output Bengali text
Teach Q&A capabilities:
Input: "প্রশ্ন: বাংলাদেশ কোথায় অবস্থিত?"
Output: "উত্তর: বাংলাদেশ দক্ষিণ এশিয়ায় অবস্থিত।"
- Works fine
- 15-30 minutes per epoch
- 8GB+ RAM needed
- Much faster
- 2-5 minutes per epoch
- 4GB+ VRAM needed
Check GPU availability:
python -c "import torch; print(torch.cuda.is_available())"# 1. Put Bengali texts in data/raw/my_texts.txt
# 2. Edit prepare_data.py to load from your file
# 3. Run pipeline
python scripts/prepare_data.py
python train.py# In config.py
BASE_MODEL = "xlm-roberta-base" # Multilingual# In config.py
NUM_EPOCHS = 10
LEARNING_RATE = 1e-5# config.py
BATCH_SIZE = 4
GRADIENT_ACCUMULATION_STEPS = 2
GRADIENT_CHECKPOINTING = TrueThat's normal. Options:
- Use GPU
- Reduce
MAX_SEQ_LENGTHto 256 - Process less data
- Get more data
- Train longer (
NUM_EPOCHS = 10) - Use better base model
BASE_MODEL = "ai4bharat/indic-bert"Fast, Bengali-optimized, 110M parameters
BASE_MODEL = "xlm-roberta-base"Works for many languages, 270M parameters
BASE_MODEL = "distilgpt2"Very small, 82M parameters
Q: Production-ready?
A: Yes! Add error handling and monitoring for production.
Q: How to get more data?
A: OSCAR, Wikipedia, web scraping, HuggingFace datasets
Q: How to improve results?
A: More data, longer training, better base model, task-specific fine-tuning
Q: Can I use as a library?
A: Yes!
from inference import BengaliLLMInference
model = BengaliLLMInference()
output = model.generate("বাংলাদেশ")Happy training! 🚀