engrsakib
5,433 hrs
Live WakaTime Coding Metric

Repository Workspace

GoEmotions-RoBERTa-XAI

Explore source files, configuration surfaces, README documentation, and commit history.

★ 0 stars⑂ 0 forksDefault · main

Source viewer

Select a file from the tree

Configuration files and source modules stream through the secure API proxy and render with the active theme accent.

README.md

🚀 GoEmotions-RoBERTa-XAI

Python FastAPI PyTorch Transformers NestJS Next.js React Node.js pnpm Docker GitHub Actions ESLint Tailwind CSS Captum ONNX

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/]
ServicePathPortResponsibility
Frontend UIpackages/frontend3000Input form, prediction panel, token heatmap rendering
API Gatewaypackages/api4000Proxy /predict, /explain, /chat, /grok/classify
Model Servicepackages/model8000RoBERTa inference + Captum Integrated Gradients

Classification Categories

IDLabel
0Normal (neutral)
1Sadness (sadness, grief)
2Joy (joy, amusement, excitement, optimism)
3Hate / Anger (anger, annoyance, disapproval, disgust)
4Sexual / Desire (desire)
5Fear / Anxiety (fear, nervousness)
6Love / ভালোবাসা (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

  1. RoBERTa (Transformer encoder)
  2. Logistic Regression (baseline)
  3. SVM (baseline)
  4. LSTM (alternate deep model)
  5. CNN for text (alternate)
  6. DistilRoBERTa (compact transformer)
  7. AdamW fine-tuning recipe
  8. Focal Loss / label smoothing
  9. Integrated Gradients (Captum)
  10. Layer-wise Relevance Propagation (LRP)
  11. Grad-CAM token adaptation
  12. ONNX quantization / pruning

Dataset

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)

MethodPathDescription
GET/healthService + model status
POST/predictEmotion probabilities only
POST/explainProbabilities + token heatmap
POST/chatClassification + chatbot reply + heatmap

NestJS gateway (:4000/api)

MethodPathDescription
GET/api/healthGateway + downstream model health
POST/api/predictProxy to model /predict
POST/api/explainProxy to model /explain
POST/api/chatProxy to model /chat
POST/api/grok/classifyGrok-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:

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)

  1. Add dataset: shivamb/go-emotions-google-emotions-dataset
  2. 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)
  1. Download artifacts/exports/saved_emotion_model/ and copy to packages/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)

Back online.