lakresi commited on
Commit
f76c8ea
·
verified ·
1 Parent(s): f635345

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +139 -107
README.md CHANGED
@@ -1,199 +1,231 @@
1
  ---
2
  library_name: transformers
3
- tags: []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  ---
5
 
6
- # Model Card for Model ID
7
-
8
- <!-- Provide a quick summary of what the model is/does. -->
9
-
10
 
 
11
 
12
  ## Model Details
13
 
14
  ### Model Description
15
 
16
- <!-- Provide a longer summary of what this model is. -->
17
 
18
- This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
19
 
20
- - **Developed by:** [More Information Needed]
21
- - **Funded by [optional]:** [More Information Needed]
22
- - **Shared by [optional]:** [More Information Needed]
23
- - **Model type:** [More Information Needed]
24
- - **Language(s) (NLP):** [More Information Needed]
25
- - **License:** [More Information Needed]
26
- - **Finetuned from model [optional]:** [More Information Needed]
27
 
28
- ### Model Sources [optional]
29
 
30
- <!-- Provide the basic links for the model. -->
31
-
32
- - **Repository:** [More Information Needed]
33
- - **Paper [optional]:** [More Information Needed]
34
- - **Demo [optional]:** [More Information Needed]
35
 
36
  ## Uses
37
 
38
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
39
-
40
  ### Direct Use
41
 
42
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
 
44
- [More Information Needed]
45
 
46
- ### Downstream Use [optional]
47
 
48
- <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
 
50
- [More Information Needed]
 
 
51
 
52
- ### Out-of-Scope Use
53
 
54
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
55
 
56
- [More Information Needed]
57
 
58
- ## Bias, Risks, and Limitations
59
 
60
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
61
 
62
- [More Information Needed]
 
 
63
 
64
  ### Recommendations
65
 
66
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
67
-
68
- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
69
 
70
  ## How to Get Started with the Model
71
 
72
- Use the code below to get started with the model.
73
-
74
- [More Information Needed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  ## Training Details
77
 
78
  ### Training Data
79
 
80
- <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
 
82
- [More Information Needed]
83
 
84
  ### Training Procedure
85
 
86
- <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
-
88
- #### Preprocessing [optional]
89
-
90
- [More Information Needed]
91
 
 
92
 
93
  #### Training Hyperparameters
94
 
95
- - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
 
 
 
 
 
96
 
97
- #### Speeds, Sizes, Times [optional]
98
 
99
- <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
100
-
101
- [More Information Needed]
102
 
103
  ## Evaluation
104
 
105
- <!-- This section describes the evaluation protocols and provides the results. -->
106
-
107
- ### Testing Data, Factors & Metrics
108
 
109
  #### Testing Data
110
 
111
- <!-- This should link to a Dataset Card if possible. -->
112
-
113
- [More Information Needed]
114
 
115
  #### Factors
116
 
117
- <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
-
119
- [More Information Needed]
120
 
121
  #### Metrics
122
 
123
- <!-- These are the evaluation metrics being used, ideally with a description of why. -->
124
-
125
- [More Information Needed]
126
 
127
  ### Results
128
 
129
- [More Information Needed]
130
-
131
- #### Summary
132
 
 
 
 
 
 
 
 
133
 
 
134
 
135
- ## Model Examination [optional]
 
 
 
 
 
 
 
 
136
 
137
- <!-- Relevant interpretability work for the model goes here -->
138
 
139
- [More Information Needed]
140
 
141
  ## Environmental Impact
142
 
143
- <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
-
145
- Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
 
 
146
 
147
- - **Hardware Type:** [More Information Needed]
148
- - **Hours used:** [More Information Needed]
149
- - **Cloud Provider:** [More Information Needed]
150
- - **Compute Region:** [More Information Needed]
151
- - **Carbon Emitted:** [More Information Needed]
152
-
153
- ## Technical Specifications [optional]
154
 
155
  ### Model Architecture and Objective
156
 
157
- [More Information Needed]
158
 
159
  ### Compute Infrastructure
160
 
161
- [More Information Needed]
162
-
163
  #### Hardware
164
 
165
- [More Information Needed]
166
 
167
  #### Software
168
 
169
- [More Information Needed]
170
-
171
- ## Citation [optional]
172
-
173
- <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
-
175
- **BibTeX:**
176
-
177
- [More Information Needed]
178
-
179
- **APA:**
180
-
181
- [More Information Needed]
182
-
183
- ## Glossary [optional]
184
-
185
- <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
-
187
- [More Information Needed]
188
 
189
- ## More Information [optional]
190
 
191
- [More Information Needed]
192
 
193
- ## Model Card Authors [optional]
194
 
195
- [More Information Needed]
196
 
197
  ## Model Card Contact
198
 
199
- [More Information Needed]
 
1
  ---
2
  library_name: transformers
3
+ language:
4
+ - en
5
+ license: apache-2.0
6
+ base_model: distilbert-base-uncased
7
+ datasets:
8
+ - jigsaw-toxic-comment-classification-challenge
9
+ tags:
10
+ - text-classification
11
+ - multi-label-classification
12
+ - content-moderation
13
+ - toxic-comment-detection
14
+ - distilbert
15
+ metrics:
16
+ - f1
17
+ - roc_auc
18
  ---
19
 
20
+ # lakresi/toxic-comment-moderator
 
 
 
21
 
22
+ A DistilBERT model fine tuned for multi label toxic comment classification across six categories. Trained on the Jigsaw Toxic Comment Classification dataset. Deployed as a production REST API.
23
 
24
  ## Model Details
25
 
26
  ### Model Description
27
 
28
+ distilbert-base-uncased fine tuned for multi label classification of toxic content. A linear classification head maps the CLS token representation to six independent output logits, one per toxicity category. Sigmoid activation produces per label probabilities between 0 and 1. Each label is classified independently so a comment can be flagged for multiple categories simultaneously.
29
 
30
+ Trained with BCEWithLogitsLoss and per label pos_weight to handle significant class imbalance. Per label decision thresholds were tuned on the validation set post training and stored in thresholds.json.
31
 
32
+ - **Developed by:** Lloyd Akresi
33
+ - **Model type:** Text classification, multi label
34
+ - **Language:** English
35
+ - **License:** Apache 2.0
36
+ - **Finetuned from:** distilbert-base-uncased
 
 
37
 
38
+ ### Model Sources
39
 
40
+ - **Repository:** [GitHub](https://github.com/lloydakresi/toxic_comment_moderator_api)
41
+ - **Demo:** [Live API](https://lakresi-toxic-comment-moderator.hf.space) | [Interactive Docs](https://lakresi-toxic-comment-moderator.hf.space/docs)
 
 
 
42
 
43
  ## Uses
44
 
 
 
45
  ### Direct Use
46
 
47
+ Classifying text across six toxicity categories for content moderation pipelines. The model accepts raw English text and returns per label probabilities and binary flags based on tuned decision thresholds.
48
 
49
+ ### Downstream Use
50
 
51
+ Can be used as a baseline for further fine tuning on domain specific moderation tasks. The model weights and tokenizer can be loaded directly via HuggingFace Transformers as shown in the How to Get Started section below.
52
 
53
+ ### Out of Scope Use
54
 
55
+ - Automated content removal without human review for borderline or LGBTQ+ identity related sentences. See the bias section below.
56
+ - Languages other than English.
57
+ - Deployment without awareness of the known bias issues documented below.
58
 
59
+ ## Bias, Risks, and Limitations
60
 
61
+ The model was evaluated against a synthetic probe set of 1,400 sentences spanning 25 demographic groups across religion, race and ethnicity, gender and sexuality, and nationality.
62
 
63
+ 12 of 1,400 sentences (0.9%) were flagged. All flags were on the toxic label only and concentrated entirely in three groups: Gay, Lesbian, and Transgender. No sentence from any of the other 22 groups tested was flagged in any category.
64
 
65
+ The effect is driven by a copula construction stating identity directly. "The new manager is Gay" scored 0.818 toxic probability. The structurally identical "The new manager is Muslim" scored 0.007. This is a documented artifact of the Jigsaw training corpus where constructions like "that's so gay" are disproportionately represented as insults, causing the model to associate the is/are + LGBTQ+ identity term pattern with toxicity independent of surrounding sentiment or context.
66
 
67
+ Additional limitations:
68
 
69
+ - Comments exceeding 512 tokens are truncated and content beyond the limit is not considered
70
+ - Trained on Wikipedia comments so performance may vary on platform specific language or slang
71
+ - Rare categories threat and severe_toxic have lower F1 scores due to data scarcity and should be treated as indicative rather than definitive
72
 
73
  ### Recommendations
74
 
75
+ Do not use this model for automated content removal on sentences that state an LGBTQ+ identity using an is/are construction. Route these to human review instead. A post hoc calibration layer or allowlist rule for this specific construction is a reasonable short term mitigation before any automated action is taken.
 
 
76
 
77
  ## How to Get Started with the Model
78
 
79
+ ```python
80
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
81
+ from huggingface_hub import hf_hub_download
82
+ import torch
83
+ import json
84
+
85
+ model_name = "lakresi/toxic-comment-moderator"
86
+
87
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
88
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
89
+ model.eval()
90
+
91
+ file_path = hf_hub_download(repo_id=model_name, filename="thresholds.json")
92
+ with open(file_path) as f:
93
+ thresholds = json.load(f)
94
+
95
+ label_cols = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
96
+
97
+ def classify(text: str):
98
+ inputs = tokenizer(
99
+ text,
100
+ return_tensors="pt",
101
+ truncation=True,
102
+ padding="max_length",
103
+ max_length=512
104
+ )
105
+ with torch.no_grad():
106
+ logits = model(**inputs).logits
107
+ probs = torch.sigmoid(logits).squeeze().tolist()
108
+ return {
109
+ label: {
110
+ "probability": round(prob, 4),
111
+ "flagged": prob >= thresholds[label]
112
+ }
113
+ for label, prob in zip(label_cols, probs)
114
+ }
115
+
116
+ result = classify("You are absolutely worthless and should be ashamed of yourself")
117
+ print(result)
118
+ ```
119
 
120
  ## Training Details
121
 
122
  ### Training Data
123
 
124
+ Jigsaw Toxic Comment Classification Challenge dataset. Approximately 145k training examples and 16k validation examples. Wikipedia comments labeled across six binary toxicity categories.
125
 
126
+ Class distribution is highly imbalanced: toxic appears in ~10.2% of comments, obscene in ~5.9%, insult in ~4.9%, severe_toxic in ~1.0%, identity_hate in ~0.9%, and threat in ~0.3%. Per label pos_weight was applied inversely proportional to each label's positive frequency to address this.
127
 
128
  ### Training Procedure
129
 
130
+ #### Preprocessing
 
 
 
 
131
 
132
+ Text tokenized with the distilbert-base-uncased tokenizer. Maximum sequence length of 512 tokens with truncation and padding. Comments exceeding 512 tokens are truncated from the right.
133
 
134
  #### Training Hyperparameters
135
 
136
+ - **Training regime:** fp32
137
+ - **Epochs:** 5
138
+ - **Batch size:** 256 effective (128 per device across 2x RTX 3090)
139
+ - **Optimizer:** AdamW
140
+ - **Loss function:** BCEWithLogitsLoss with per label pos_weight
141
+ - **Primary metric for best model:** F1 Macro
142
 
143
+ #### Speeds, Sizes, Times
144
 
145
+ - **Hardware:** 2x NVIDIA RTX 3090 (24GB VRAM each) via Vast.ai
146
+ - **Training time:** ~34 minutes (2,845 steps at ~1.57 iterations per second)
147
+ - **Evaluation speed:** ~410 samples per second on validation set
148
 
149
  ## Evaluation
150
 
151
+ ### Testing Data, Factors and Metrics
 
 
152
 
153
  #### Testing Data
154
 
155
+ 16,180 held out examples from the Jigsaw Toxic Comment Classification dataset, stratified by label to preserve class distribution.
 
 
156
 
157
  #### Factors
158
 
159
+ Performance disaggregated by toxicity category. Rare labels evaluated separately given their low positive frequency.
 
 
160
 
161
  #### Metrics
162
 
163
+ F1 score per label and overall macro F1. ROC AUC to measure discriminative capability independent of threshold. Both metrics reported because the model can have high ROC AUC (strong ranking ability) while having lower F1 macro (threshold sensitivity), which is exactly what was observed here.
 
 
164
 
165
  ### Results
166
 
167
+ **Epoch by epoch training dynamics:**
 
 
168
 
169
+ | Epoch | Train Loss | Eval Loss | F1 Macro | ROC AUC |
170
+ |---|---|---|---|---|
171
+ | 1 | 3.917 | 1.598 | 0.632 | 0.989 |
172
+ | 2 | 1.438 | 1.546 | 0.670 | 0.990 |
173
+ | 3 | 1.116 | 1.687 | 0.671 | 0.989 |
174
+ | 4 | 0.857 | 1.880 | 0.684 | 0.987 |
175
+ | 5 | 0.697 | 1.934 | 0.687 | 0.987 |
176
 
177
+ **Per label results after threshold tuning:**
178
 
179
+ | Category | Threshold | F1 Score | Class Frequency |
180
+ |---|---|---|---|
181
+ | obscene | 0.44 | 0.849 | ~5.9% |
182
+ | toxic | 0.52 | 0.840 | ~10.2% |
183
+ | insult | 0.52 | 0.765 | ~4.9% |
184
+ | identity_hate | 0.26 | 0.589 | ~0.9% |
185
+ | threat | 0.36 | 0.561 | ~0.3% |
186
+ | severe_toxic | 0.46 | 0.558 | ~1.0% |
187
+ | **Overall** | | **0.687 macro / 0.987 ROC AUC** | |
188
 
189
+ #### Summary
190
 
191
+ Strong performance on frequent labels (toxic, obscene, insult) with F1 above 0.76. Rare labels (threat, severe_toxic, identity_hate) underperform due to data scarcity rather than modeling limitations, as evidenced by the consistently high ROC AUC of 0.987 across all labels. Per label threshold tuning meaningfully improved F1 over the default 0.5 cutoff, particularly for rare labels where lower thresholds improve recall at acceptable precision cost.
192
 
193
  ## Environmental Impact
194
 
195
+ - **Hardware type:** 2x NVIDIA RTX 3090 via Vast.ai
196
+ - **Hours used:** ~0.57 hours (34 minutes)
197
+ - **Cloud provider:** Vast.ai
198
+ - **Compute region:** Not specified
199
+ - **Carbon emitted:** Estimated minimal given short training duration
200
 
201
+ ## Technical Specifications
 
 
 
 
 
 
202
 
203
  ### Model Architecture and Objective
204
 
205
+ distilbert-base-uncased with a single linear classification head. Input text is tokenized and passed through 6 transformer layers. The CLS token representation from the final layer is passed through the linear head to produce 6 logits. Sigmoid activation converts logits to independent per label probabilities. Binary cross entropy loss with logits and per label positive weighting.
206
 
207
  ### Compute Infrastructure
208
 
 
 
209
  #### Hardware
210
 
211
+ 2x NVIDIA RTX 3090 (24GB VRAM each) via Vast.ai. Multi GPU training managed by HuggingFace Accelerate.
212
 
213
  #### Software
214
 
215
+ - Python 3.10
216
+ - PyTorch
217
+ - HuggingFace Transformers
218
+ - HuggingFace Accelerate
219
+ - HuggingFace Trainer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
 
221
+ ## More Information
222
 
223
+ Full technical breakdown including training code, bias analysis methodology, API documentation, and Docker deployment instructions are available in the [GitHub repository](https://github.com/lloydakresi/toxic_comment_moderator_api).
224
 
225
+ ## Model Card Authors
226
 
227
+ Lloyd Akresi
228
 
229
  ## Model Card Contact
230
 
231
+ [GitHub](https://github.com/lloydakresi)