🚀 GoEmotions-RoBERTa-XAI
Production-grade monorepo for GoEmotions-RoBERTa-XAI: Fine-Tuned Sentiment Classifier with Token-Level Attribution Heatmaps.
The system classifies text into 7 emotion groups, generates token-level Integrated Gradients heatmaps, and exposes a chatbot-style API through a NestJS gateway and Next.js UI.
Architecture
flowchart LR
User[Browser / Client] --> Frontend[packages/frontend\nNext.js 16 UI]
Frontend --> API[packages/api\nNestJS Gateway :4000]
API --> Model[packages/model\nFastAPI + RoBERTa :8000]
Model --> Weights[saved_emotion_model/]
| Service | Path | Port | Responsibility |
|---|---|---|---|
| Frontend UI | packages/frontend | 3000 | Input form, prediction panel, token heatmap rendering |
| API Gateway | packages/api | 4000 | Proxy /predict, /explain, /chat, /grok/classify |
| Model Service | packages/model | 8000 | RoBERTa inference + Captum Integrated Gradients |
Classification Categories
| ID | Label |
|---|---|
| 0 | Normal (neutral) |
| 1 | Sadness (sadness, grief) |
| 2 | Joy (joy, amusement, excitement, optimism) |
| 3 | Hate / Anger (anger, annoyance, disapproval, disgust) |
| 4 | Sexual / Desire (desire) |
| 5 | Fear / Anxiety (fear, nervousness) |
| 6 | Love / ভালোবাসা (love) |
Monorepo Directory Tree
All application code lives under packages/ — three packages only: frontend, api, and model.
.
├── README.md
├── LICENSE.md
├── package.json # root pnpm scripts
├── pnpm-workspace.yaml # packages/*
├── pnpm-lock.yaml
├── .npmrc
├── .env.example
├── docker-compose.yml # orchestrates model + api + frontend
├── docker-compose.override.yml # dev hot-reload overrides
├── .dockerignore
├── .github/
│ └── workflows/
│ └── ci.yml
├── .eslintrc.json
├── .gitignore
├── data/
│ └── raw/
│ └── goemotions/ # GoEmotions dataset
├── notebooks/ # ML training pipeline
│ ├── scripts/run_pipeline.py # main CLI (Stages 0–7)
│ ├── src/ # data, training, xai modules
│ ├── config/train_config.yaml
│ ├── docs/IEEE_METHODOLOGY.md
│ └── kaggle/run_training.py # Kaggle GPU entry point
├── scripts/ # utility scripts
├── project-plan/ # gitignored production checklist
└── packages/
├── frontend/ # Next.js 16 UI (:3000)
│ ├── app/
│ │ ├── layout.tsx
│ │ ├── page.tsx
│ │ └── globals.css
│ ├── components/
│ │ └── HeatmapText.tsx
│ ├── lib/
│ │ └── api.ts
│ ├── public/
│ ├── Dockerfile
│ ├── next.config.ts
│ ├── package.json
│ └── tsconfig.json
├── api/ # NestJS API Gateway (:4000)
│ ├── src/
│ │ ├── main.ts
│ │ ├── app.module.ts
│ │ ├── app.controller.ts
│ │ ├── model.service.ts
│ │ └── dto/
│ │ └── text.dto.ts
│ ├── Dockerfile
│ ├── package.json
│ └── tsconfig.json
└── model/ # FastAPI RoBERTa service (:8000)
├── app/
│ ├── main.py
│ ├── inference.py
│ ├── explainability.py
│ ├── labels.py
│ └── schemas.py
├── saved_emotion_model/
│ ├── label_map.json
│ ├── README.md
│ └── weights/ # drop trained checkpoint here
├── Dockerfile
└── requirements.txt
Technologies
- ML: Python, FastAPI, PyTorch, Hugging Face Transformers, Captum (Integrated Gradients)
- Gateway: NestJS, Axios, class-validator
- UI: Next.js 16, React 18, Tailwind CSS, TypeScript
- DevOps: Docker Compose, GitHub Actions, ESLint
12 Algorithms & Methods
- RoBERTa (Transformer encoder)
- Logistic Regression (baseline)
- SVM (baseline)
- LSTM (alternate deep model)
- CNN for text (alternate)
- DistilRoBERTa (compact transformer)
- AdamW fine-tuning recipe
- Focal Loss / label smoothing
- Integrated Gradients (Captum)
- Layer-wise Relevance Propagation (LRP)
- Grad-CAM token adaptation
- ONNX quantization / pruning
Dataset
- GoEmotions (Google Research) — original source
- Kaggle dataset: shivamb/go-emotions-google-emotions-dataset
- GitHub repo: engrsakib/GoEmotions-RoBERTa-XAI
- Local path:
data/raw/goemotions/
Model Weights
Copy your trained checkpoint into packages/model/saved_emotion_model/:
trainer.save_model("./saved_emotion_model")
tokenizer.save_pretrained("./saved_emotion_model")
If weights are missing, the model service falls back to roberta-base with a 7-class head for local development only.
API Endpoints
Model service (:8000)
| Method | Path | Description |
|---|---|---|
| GET | /health | Service + model status |
| POST | /predict | Emotion probabilities only |
| POST | /explain | Probabilities + token heatmap |
| POST | /chat | Classification + chatbot reply + heatmap |
NestJS gateway (:4000/api)
| Method | Path | Description |
|---|---|---|
| GET | /api/health | Gateway + downstream model health |
| POST | /api/predict | Proxy to model /predict |
| POST | /api/explain | Proxy to model /explain |
| POST | /api/chat | Proxy to model /chat |
| POST | /api/grok/classify | Grok-compatible classify + heatmap |
Package Manager (pnpm)
This monorepo uses pnpm workspaces exclusively.
# pnpm-workspace.yaml
packages:
- 'packages/*'
Node workspaces: packages/frontend, packages/api
Python service (packages/model) is managed via pip + requirements.txt.
Local setup
corepack enable
pnpm install
cp .env.example .env
pnpm dev # runs api + frontend in parallel
Docker Run
Production
pnpm docker:up
# or
docker compose up --build
Services:
- UI: http://localhost:3000
- API Gateway: http://localhost:4000/api
- Model: http://localhost:8000
Development (hot reload)
docker compose up --build
docker-compose.override.yml automatically mounts source folders and enables reload for all services.
Local Development (without Docker)
corepack enable
pnpm install
# Terminal 1 - model (Python)
cd packages/model
pip install -r requirements.txt
uvicorn app.main:app --reload --port 8000
# Terminal 2 + 3 - api + frontend (from repo root)
pnpm dev:api
pnpm dev:frontend
Kaggle / Training Guidelines
Repository: https://github.com/engrsakib/GoEmotions-RoBERTa-XAI.git (branch: main)
Full Kaggle guide: notebooks/docs/03-kaggle-setup.md
Dataset: GoEmotions on Kaggle
Quick start on Kaggle (GPU T4 x2, Internet ON)
- Add dataset:
shivamb/go-emotions-google-emotions-dataset - Paste in a Kaggle notebook cell:
import os, subprocess, sys
REPO = "https://github.com/engrsakib/GoEmotions-RoBERTa-XAI.git"
REPO_DIR = "/kaggle/working/repo"
if not os.path.exists(REPO_DIR):
subprocess.run(["git", "clone", "--branch", "main", "--depth", "1", REPO, REPO_DIR], check=True)
os.chdir(f"{REPO_DIR}/notebooks")
subprocess.run([sys.executable, "-m", "pip", "install", "-q", "-r", "requirements-train.txt"], check=True)
subprocess.run([sys.executable, "kaggle/run_training.py"], check=True)
- Download
artifacts/exports/saved_emotion_model/and copy topackages/model/saved_emotion_model/
Local training
git clone --branch main https://github.com/engrsakib/GoEmotions-RoBERTa-XAI.git
cd GoEmotions-RoBERTa-XAI/notebooks
pip install -r requirements-train.txt
python scripts/run_pipeline.py --deploy
CI
GitHub Actions runs:
- pnpm install + lint across workspaces
- Python import smoke test for model service
docker compose build
License
Apache License 2.0 — Copyright (c) 2026 Md. Nazmus Sakib (engrsakib.com)
