Skip to content

nabin2004/ChessMania

Repository files navigation

β™ŸοΈ chessMania

Chess MLOps Pipeline

From raw PGN ingestion to Transformer-powered next-move prediction, with full MLOps lifecycle coverage.

Python 3.10+ License: MIT


πŸ“‹ Overview

chessMania is an end-to-end MLOps project that demonstrates every stage of the machine learning lifecycle using chess game data. It starts with a tabular ML baseline (XGBoost) and scales into Transformer-based sequence modelling, showcasing research engineering rigor and modern ML operations practices.

Component Implementation
Storage MinIO (PGN / JSON / Parquet)
Orchestration Apache Airflow (PGN parsing β†’ Feature & Sequence extraction)
Data Validation Great Expectations
Data Warehouse PostgreSQL
ML Framework XGBoost β†’ GPT-style Transformer with LoRA/QLoRA
Experiment Tracking MLflow (Accuracy, F1, AUC, Perplexity, MFU)
Serving FastAPI + Uvicorn (Next-move prediction, Win probability)
Monitoring Evidently AI (Feature drift + Structural/Move-sequence drift)
Containerisation Docker + Docker Compose

πŸ—οΈ Project Structure

chessMania/
β”‚
β”œβ”€β”€ README.md                          # ← You are here
β”œβ”€β”€ pyproject.toml                     # Dependencies & build config
β”œβ”€β”€ Makefile                           # Developer shortcuts
β”œβ”€β”€ Dockerfile                         # API container image
β”œβ”€β”€ .dockerignore
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .env.example                       # Environment variable template
β”œβ”€β”€ .pre-commit-config.yaml            # Linting & formatting hooks
β”œβ”€β”€ alembic.ini                        # Database migration config
β”œβ”€β”€ LICENSE
β”‚
β”œβ”€β”€ src/                               # ═══ APPLICATION CODE ═══
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚
β”‚   β”œβ”€β”€ config/                        # Centralised configuration
β”‚   β”‚   β”œβ”€β”€ __init__.py                #   Hydra/OmegaConf loader
β”‚   β”‚   └── config.yaml               #   All settings (storage, models, serving…)
β”‚   β”‚
β”‚   β”œβ”€β”€ ingestion/                     # ── Stage 1: Data Ingestion ──
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ minio_client.py            #   MinIO upload / download helpers
β”‚   β”‚   β”œβ”€β”€ pgn_parser.py             #   Parse PGN β†’ structured dicts
β”‚   β”‚   β”œβ”€β”€ db.py                      #   SQLAlchemy models (PostgreSQL)
β”‚   β”‚   β”œβ”€β”€ ingest_pgn.py             #   End-to-end ingestion entry-point
β”‚   β”‚   └── validate.py               #   Great Expectations quality checks
β”‚   β”‚
β”‚   β”œβ”€β”€ preprocessing/                 # ── Stage 2: Preprocessing ──
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ tabular_features.py       #   XGBoost feature extraction
β”‚   β”‚   β”œβ”€β”€ sequence_tokenizer.py     #   SAN β†’ integer token IDs
β”‚   β”‚   β”œβ”€β”€ dataset.py                #   PyTorch Dataset / DataLoader
β”‚   β”‚   └── splits.py                 #   Train / Val / Test splitting
β”‚   β”‚
β”‚   β”œβ”€β”€ models/                        # ── Stage 3: Model Development ──
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ mlflow_utils.py           #   MLflow tracking helpers
β”‚   β”‚   β”œβ”€β”€ xgboost_trainer.py        #   XGBoost classifier training
β”‚   β”‚   β”œβ”€β”€ transformer_model.py      #   GPT-style causal LM architecture
β”‚   β”‚   β”œβ”€β”€ transformer_trainer.py    #   Training loop with LoRA/QLoRA
β”‚   β”‚   └── registry.py              #   Save / load model artefacts
β”‚   β”‚
β”‚   β”œβ”€β”€ serving/                       # ── Stage 4: Deployment & Serving ──
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ app.py                    #   FastAPI application & routes
β”‚   β”‚   β”œβ”€β”€ schemas.py                #   Pydantic request / response models
β”‚   β”‚   └── inference.py              #   Inference helpers
β”‚   β”‚
β”‚   └── monitoring/                    # ── Stage 5: Model Monitoring ──
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ generate_report.py        #   Evidently drift report generator
β”‚       └── drift_detectors.py        #   Custom sequence-level drift checks
β”‚
β”œβ”€β”€ airflow/                           # ═══ ORCHESTRATION ═══
β”‚   └── dags/
β”‚       β”œβ”€β”€ chess_ingestion_dag.py     #   Daily: ingest β†’ validate β†’ features
β”‚       └── chess_training_dag.py      #   Manual: train models β†’ monitor
β”‚
β”œβ”€β”€ infra/                             # ═══ INFRASTRUCTURE ═══
β”‚   └── docker-compose.yml             #   MinIO, PostgreSQL, MLflow, Airflow, API
β”‚
β”œβ”€β”€ tests/                             # ═══ TEST SUITE ═══
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ test_pgn_parser.py
β”‚   β”œβ”€β”€ test_tokenizer.py
β”‚   β”œβ”€β”€ test_features.py
β”‚   β”œβ”€β”€ test_transformer.py
β”‚   β”œβ”€β”€ test_api.py
β”‚   └── test_drift.py
β”‚
β”œβ”€β”€ notebooks/                         # ═══ EXPLORATION ═══
β”‚   └── .gitkeep
β”‚
β”œβ”€β”€ data/                              # ═══ DATA (git-ignored) ═══
β”‚   β”œβ”€β”€ raw/                           #   Raw PGN files
β”‚   β”œβ”€β”€ interim/                       #   Intermediate artefacts
β”‚   └── processed/                     #   Model-ready features & splits
β”‚
β”œβ”€β”€ artefacts/                         # ═══ MODEL ARTEFACTS (git-ignored) ═══
β”‚   β”œβ”€β”€ models/                        #   Trained model checkpoints
β”‚   └── tokenizers/                    #   Fitted tokenizer JSON
β”‚
└── reports/                           # ═══ MONITORING REPORTS (git-ignored) ═══
    └── .gitkeep

πŸš€ Quick Start

1. Clone & Install

git clone https://github.com/nabin2004/chessMania.git
cd chessMania

# Create virtual environment
python -m venv .venv && source .venv/bin/activate

# Install with dev extras
make dev

2. Configure Environment

cp .env.example .env
# Edit .env with your credentials

3. Start Infrastructure

make infra-up    # Starts MinIO, PostgreSQL, MLflow, Airflow
Service URL
MinIO Console http://localhost:9001
PostgreSQL localhost:5432
MLflow UI http://localhost:5000
Airflow UI http://localhost:8080
API http://localhost:8000

4. Ingest Data

Place Lichess PGN files in data/raw/, then:

make ingest      # Parse PGNs β†’ PostgreSQL + MinIO
make validate    # Run Great Expectations checks

5. Train Models

make train-xgb          # XGBoost baseline
make train-transformer  # Transformer sequence model

6. Serve

make serve   # Start FastAPI at localhost:8000

7. Monitor

make monitor  # Generate Evidently drift reports

πŸ”Œ API Endpoints

GET /health

{ "status": "ok", "models_loaded": ["xgboost", "transformer"] }

POST /predict/win

Predict game outcome probabilities from tabular features.

{
  "white_elo": 1500,
  "black_elo": 1450,
  "time_control": "300+3",
  "eco": "B20",
  "moves_played": 10
}

β†’ { "white_win": 0.52, "draw": 0.28, "black_win": 0.20, "predicted_result": "1-0" }

POST /predict/next-move

Suggest next moves from a partial game sequence.

{
  "moves": ["e4", "e5", "Nf3"],
  "num_suggestions": 3,
  "temperature": 1.0
}

β†’ { "suggestions": [{"move": "Nc6", "probability": 0.35}, ...] }


πŸ§ͺ Testing

make test    # pytest with coverage
make lint    # ruff + mypy

πŸ“Š MLOps Lifecycle Mapping

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         DATA LAYER                                β”‚
β”‚  Lichess PGNs ──► MinIO ──► Airflow ETL ──► PostgreSQL            β”‚
β”‚                              β”‚                                    β”‚
β”‚                    Great Expectations (validation)                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     PREPROCESSING                                 β”‚
β”‚  Tabular Features (ELO, ECO, TC)    β”‚   Sequence Tokenizer (SAN)  β”‚
β”‚  ──► XGBoost feature matrix         β”‚   ──► Integer token IDs     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   MODEL DEVELOPMENT                               β”‚
β”‚  XGBoost Classifier                 β”‚   GPT-style Transformer     β”‚
β”‚  (Accuracy, F1, AUC)               β”‚   (Perplexity, MFU, Acc)    β”‚
β”‚                                     β”‚   + LoRA / QLoRA adapters   β”‚
β”‚                   MLflow Tracking                                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      SERVING                                      β”‚
β”‚  FastAPI + Uvicorn + Docker                                       β”‚
β”‚  /predict/win  (XGBoost)    β”‚   /predict/next-move  (Transformer) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     MONITORING                                    β”‚
β”‚  Evidently AI                                                     β”‚
β”‚  β€’ Tabular drift (ELO distributions, accuracy)                    β”‚
β”‚  β€’ Sequence drift (invalid tokens, structural anomalies)          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“œ License

MIT β€” see LICENSE.

About

From raw PGN ingestion to Transformer-powered next-move prediction, with full MLOps lifecycle coverage.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages