cardiffnlp/twitter-roberta-base-emotion
onnx-community/twitter-roberta-base-emotion-ONNX
text-classification
config.json (0: joy · 1: optimism · 2: anger · 3: sadness)
1. Live 4-Emotion Classifier & CardiffNLP Preprocessing Inspector
@user / http normalization matters: TweetEval trained
cardiffnlp/twitter-roberta-base-emotion with handles replaced by @user and links
replaced by http. Toggle the normalizer below to compare exact Normalized vs
Raw 4-class probabilities, softmax margin, and real Byte-Level BPE token chips.
config.json label bug (auto-corrected to TweetEval mapping.txt):
Upstream commit dff452c4 (2022-03-23) erroneously wrote
{0: "joy", 1: "optimism", 2: "anger", 3: "sadness"} into config.json
(HF Discussion #1),
and the ONNX conversion inherited it — even though the classifier head weights and CardiffNLP's own README
Python script use TweetEval's canonical
mapping.txt
(0: anger · 1: joy · 2: optimism · 3: sadness). Our worker inspects live
model.config.id2label at load time and applies mapping.txt when the scrambled
permutation is present, surfacing both raw config.json and canonical labels below.
Pick a tweet/social preset, edit text, paste anywhere, or drop/upload a .txt, .md, .csv, or .json file:
pipeline("text-classification", "onnx-community/twitter-roberta-base-emotion-ONNX", { device: "wasm", dtype: "q8" })
with { top_k: 4 } via shared Transformers.js 3.7.5 inside a dedicated Web Worker.
Drop a .txt, .md, .csv, or .json file here, paste from clipboard, or choose a file.
Active Run · 4-Class Emotion Distribution
Valence balance (joy + optimism) − (anger + sadness) will appear after inference.
Live model.config.id2label vs TweetEval mapping.txt status will appear after inference.
Preprocessing Side-by-Side (@user / http)
–
–
See Inside · Byte-Level BPE Token Chips (Real pipe.tokenizer Output)
Displays exact <s>, </s>, @user/http anchors, BPE subword pieces, and token IDs.
Run inference to inspect real RoBERTa Byte-Level BPE tokens.
2. Why 4-Class Social Emotion Matters & Use-Case Ladder
Sentiment polarity lumps constructive hope together with sarcasm or lumps grief together with outrage. Separating joy, optimism, anger, and sadness in a 125.43 MB quantized browser encoder unlocks private client-side workflows:
- Support & community triage: Escalate high-arousal anger outages separately from empathetic sadness churn notes or resilient optimism bug reports.
- Social listening & release telemetry: Measure how emojis, hashtags, and
@useranonymization move probability across the 4-emotion space before exporting audit CSVs. - Sarcasm & divergence detection: Pair Emotion with Twitter-RoBERTa Sentiment to catch optimistic-negative complaints or joy-negative irony.
Show the code · emotion-core.mjs — preprocessTwitterText(), resolveEmotionMapping() & normalizeEmotionScores()
Show the code · worker.js — inspectBpeTokens(), clipToMaxBpeTokens(), runSingleClassify() & classify()
3. How the API Works & References
import { pipeline } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.5";
const classifier = await pipeline(
"text-classification",
"onnx-community/twitter-roberta-base-emotion-ONNX",
{ device: "wasm", dtype: "q8" },
);
// Raw ONNX config.json has {0: "joy", 1: "optimism", 2: "anger", 3: "sadness"};
// TweetEval weights & mapping.txt use {0: "anger", 1: "joy", 2: "optimism", 3: "sadness"}.
const TWEETEVAL_FIX = { joy: "anger", optimism: "joy", anger: "optimism", sadness: "sadness" };
const raw = await classifier("Celebrating my promotion 😎", { top_k: 4 });
const scores = raw.map((r) => ({ ...r, label: TWEETEVAL_FIX[r.label] ?? r.label }));
// -> [{ label: "joy", score: ~0.940 }, { label: "optimism", score: ~0.035 }, ...]
References: cardiffnlp/twitter-roberta-base-emotion · onnx-community/twitter-roberta-base-emotion-ONNX · TweetEval emotion/mapping.txt · HF Discussion #1 (config.json id2label bug) · TweetEval Paper (Barbieri et al., EMNLP 2020) · TweetEval GitHub · Browser requirements: WebAssembly + Web Workers + Cache Storage (Baseline Widely Available; no WebGPU required).