Skip to content

arafat-web/sleekAI

Repository files navigation

Bengali LLM Pipeline - Complete Guide

A production-ready Bengali Language Model pipeline built with Python, Hugging Face Transformers, and PyTorch.

🎯 What This Project Does

This pipeline helps you:

  1. Load & Clean Bengali text data
  2. Tokenize text (convert to numbers)
  3. Train a Bengali language model
  4. Fine-tune on your custom data
  5. Generate Bengali text from prompts
  6. Instruction-tune for Q&A tasks

Think of it like creating your own Bengali ChatGPT, starting from scratch!


📁 Project Structure

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

🚀 Quick Start (5 minutes)

1. Install Dependencies

pip install -r requirements.txt

2. Prepare Data

python scripts/prepare_data.py

Output: data/processed/bengali_texts.jsonl

3. Setup Tokenizer

python scripts/setup_tokenizer.py

4. Train the Model

python train.py

⏱️ On CPU: ~15-30 minutes
⏱️ On GPU: ~2-5 minutes

5. Generate Bengali Text

python inference.py

🎓 Understanding the Pipeline

For Backend Developers (PHP/Laravel Background)

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)

Data Flow

Raw Text → Clean → Tokenize → Train Model → Save → Generate

⚙️ Configuration

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 acceleration

📊 Step-by-Step Explanation

Step 1: Data Preparation (prepare_data.py)

Cleans 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

Step 2: Tokenization (setup_tokenizer.py)

Converts text to numbers:

  • Input: "বাংলাদেশ"
  • Output: [1234, 5678, 9012, 3456]

Options:

  • Pretrained (recommended): AI4Bharat's Indic-BERT
  • Custom: Train on your data

Step 3: Model Training (train.py)

Teaches model to predict Bengali:

  1. Load pretrained model
  2. Fine-tune on your data
  3. Save checkpoints
  4. Evaluate on validation set

Optimization:

  • Gradient accumulation
  • FP16 (on GPU)
  • Gradient checkpointing

Step 4: Inference (inference.py)

Generate Bengali text:

  • Load trained model
  • Take Bengali prompt
  • Generate token-by-token
  • Output Bengali text

Bonus: Instruction Tuning

Teach Q&A capabilities:

Input:  "প্রশ্ন: বাংলাদেশ কোথায় অবস্থিত?"
Output: "উত্তর: বাংলাদেশ দক্ষিণ এশিয়ায় অবস্থিত।"

🔌 Hardware Requirements

CPU Only

  • Works fine
  • 15-30 minutes per epoch
  • 8GB+ RAM needed

GPU (CUDA)

  • Much faster
  • 2-5 minutes per epoch
  • 4GB+ VRAM needed

Check GPU availability:

python -c "import torch; print(torch.cuda.is_available())"

📝 Customization Examples

Use Your Own Data

# 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

Different Base Model

# In config.py
BASE_MODEL = "xlm-roberta-base"  # Multilingual

Longer Training

# In config.py
NUM_EPOCHS = 10
LEARNING_RATE = 1e-5

🐛 Troubleshooting

Out of Memory

# config.py
BATCH_SIZE = 4
GRADIENT_ACCUMULATION_STEPS = 2
GRADIENT_CHECKPOINTING = True

Slow on CPU

That's normal. Options:

  • Use GPU
  • Reduce MAX_SEQ_LENGTH to 256
  • Process less data

Poor Results

  • Get more data
  • Train longer (NUM_EPOCHS = 10)
  • Use better base model

🏆 Model Recommendations

For Bengali Only (Best)

BASE_MODEL = "ai4bharat/indic-bert"

Fast, Bengali-optimized, 110M parameters

For Multilingual

BASE_MODEL = "xlm-roberta-base"

Works for many languages, 270M parameters

For Quick Testing

BASE_MODEL = "distilgpt2"

Very small, 82M parameters


📖 Resources


❓ FAQ

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! 🚀

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors