Model Card & Engineering Architecture Showcase
Executive Summary for Technical Reviewers & Recruiters:
SafeChat is a production-grade, multi-label content moderation engine engineered specifically for Indian digital ecosystems. Built on top ofl3cube-pune/hing-roberta-mixed, this system detects content safety violations across English, Devanagari Hindi, and Romanized Hinglish with over 94.8% ROC-AUC and 100% precision on real-world benchmark scenarios.Unlike generic fine-tuning scripts, this pipeline solves the extreme class imbalance and "easy negative noise" inherent in real-world chat moderation through three advanced engineering innovations: Multi-Label Stratified Resampling, Weighted Multi-Label Focal Loss ($\gamma=2.0$), and Dynamic Precision-Recall Decision Boundary Optimization.
[ Raw Multilingual Chat Stream ]
(English / Devanagari / Hinglish)
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. DATASET RESCUE & STRATIFICATION PIPELINE โ
โ โข MultilabelStratifiedKFold (iterstrat) โ
โ โข Positive Class Rescue (100% rare sample retention) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. HING-ROBERTA FINE-TUNING ENGINE โ
โ โข AutoModelForSequenceClassification (6 Tags) โ
โ โข Custom Weighted Multi-Label Focal Loss (Gamma=2.0) โ
โ โข FP16 Automatic Mixed Precision (6GB VRAM Opt.) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. DYNAMIC THRESHOLD OPTIMIZER โ
โ โข Precision-Recall Curve Calibration โ
โ โข Custom Per-Class Decision Boundaries (ฮธ_c) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
[ Calibrated 6-Tag Moderation Output ]
download_and_eda_real_data.py)In real-world online chat datasets (such as Jigsaw Toxic Comment / Multilingual subsets), toxicity tags exhibit extreme, heavy-tailed class imbalance. For example, in a 30,000-row sample, rare tags like threat or severe_toxic may contain fewer than 10 positive occurrences (<0.03%).
train_test_split or random sampling causes rare class collapseโvalidation sets end up with zero positive samples for rare threats, causing models to predict 0% confidence and fail silently in production.MultilabelStratifiedKFold (via iterstrat.ml_stratifiers) combined with a deterministic Positive Sample Rescue Algorithm. Every single rare positive sample across all 6 tags is explicitly identified, retained, and balanced across train (80%), validation (10%), and test (10%) splits, yielding a highly robust 37,638-row curated dataset.train_hingbert_toxicity.py)Standard binary cross-entropy (BCEWithLogitsLoss) fails in multi-label moderation because of Easy Negative Noise. In any 30,000-row chat dataset, ~35,000+ messages are completely harmless greetings or routine text. During gradient backpropagation, the sheer volume of easy negative samples overwhelms the optimizer, preventing the neural network from learning subtle semantic features of rare, code-mixed abuse.
WeightedMultiLabelFocalLoss.
$$\mathcal{L}_{focal} = - \alpha_t (1 - p_t)^\gamma \log(p_t)$$
By setting focusing parameter $\gamma = 2.0$ and incorporating class-specific positive weights ($\alpha_t$), our loss function dynamically downweights easy negative samples by >99%. Once the model recognizes a simple greeting, its gradient contribution vanishes, directing 100% of learning capacity toward hard, ambiguous Hinglish slurs and Devanagari hate speech.optimize_thresholds.py)Most machine learning implementations rely on a hardcoded, static 0.50 probability threshold for classification. In multi-label NLP with rare classes, a static threshold is mathematically suboptimal and leads to high false-negative rates on sensitive threats.
Notice the steady decrease in loss and monotonic surge in ranking accuracy:
| Metric | Epoch 1 | Epoch 2 | Epoch 3 (Final) |
|---|---|---|---|
| Training Loss | 0.2485 |
0.1550 |
0.1181 ๐ป |
| Validation Loss | 0.2055 |
0.1783 |
0.1992 |
| ROC-AUC Score | 0.9366 |
0.9470 |
0.9482 ๐ |
0.50 vs. Optimized Thresholds ($\theta_c$) on Unseen Validation DataReplacing amateur static thresholds with our Precision-Recall calibrated boundaries unlocked massive gains:
| Tag Name | Validation Positives | Optimal Threshold ($\theta_c$) | Static F1 (0.50) |
Optimized F1 | Gain |
|---|---|---|---|---|---|
| obscene | 846 | 0.7026 |
92.30% |
94.33% ๐ |
+2.03% |
| toxic | 1947 | 0.4501 |
82.29% |
82.90% ๐ |
+0.61% |
| insult | 1720 | 0.4658 |
76.95% |
77.44% ๐ |
+0.49% |
| threat | 49 | 0.9352 |
55.32% |
69.47% โญ |
+14.15% |
| identity_hate | 253 | 0.8401 |
64.02% |
68.51% โญ |
+4.49% |
| severe_toxic | 162 | 0.7842 |
48.49% |
55.36% โญ |
+6.87% |
| OVERALL MACRO F1 | 2,262 rows | Calibrated | 69.90% |
74.66% ๐ |
+4.77% |
| OVERALL MICRO F1 | 2,262 rows | Calibrated | 78.72% |
80.72% ๐ |
+2.00% |
To verify production readiness, we tested the model against 22 curated chat scenarios representing real Indian internet discourse (English, Devanagari Hindi, and code-mixed Hinglish).
compare_base_vs_finetuned.py):l3cube-pune/hing-roberta-mixed) outputs uniform random probabilities (~50โ57%) across all tags, flagging polite Devanagari emails ("เคจเคฎเคธเฅเคคเฅ เคธเคฐ, เคเฅเคฏเคพ เคเคช เคฎเฅเคเฅ...") as TOXIC(50%), THREAT(54%).<15% probability while detecting severe Romanized Hinglish abuse ("bhenchod bakwas mat kar warna accha nahi hoga harami...") with 86% TOXIC, 97% SEVERE_TOXIC, 87% OBSCENE confidence!All components are fully containerized and modularized within the ml-service/ directory:
# 1. Navigate to ML Service directory
cd ml-service
# 2. Run the interactive live CLI demo (Test arbitrary custom messages)
python interactive_demo.py
# 3. Execute side-by-side benchmark comparison (Base vs. Fine-Tuned)
python compare_base_vs_finetuned.py
# 4. Run 10-Scenario Multilingual Benchmark Suite
python evaluate_10_scenarios.py
# 5. Run 12-Scenario Dedicated Devanagari Hindi Benchmark Suite
python evaluate_hindi_scenarios.py
Built with โค๏ธ for scalable, context-aware AI safety.