Updated the README
Browse files
README.md
CHANGED
@@ -1,199 +1,268 @@
|
|
1 |
---
|
2 |
library_name: transformers
|
3 |
-
tags:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
---
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
-
|
21 |
-
-
|
22 |
-
-
|
23 |
-
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
-
|
33 |
-
-
|
34 |
-
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
library_name: transformers
|
3 |
+
tags:
|
4 |
+
- text-classification
|
5 |
+
- content-moderation
|
6 |
+
- comment-moderation
|
7 |
+
- text-moderation
|
8 |
+
license: openrail
|
9 |
+
language:
|
10 |
+
- en
|
11 |
+
base_model:
|
12 |
+
- distilbert/distilbert-base-uncased
|
13 |
---
|
14 |
+
# π‘οΈ Comment Moderation Model
|
15 |
+
|
16 |
+
[](https://huggingface.co/Vrandan/Comment-Moderation)
|
17 |
+
[](https://www.python.org/downloads/release/python-370/)
|
18 |
+
[](https://opensource.org/licenses/MIT)
|
19 |
+
|
20 |
+
|
21 |
+
A powerful, multi-label content moderation system built on **DistilBERT** architecture, designed to detect and classify potentially harmful content in user-generated comments with high accuracy. This model stands out as currently the best in terms of performance based on the provided dataset for text moderation. Additionally, it has the smallest footprint, making it ideal for deployment on edge devices. Currently, it is the only model trained to achieve such high performance while maintaining a minimal size relative to the training data on Hugging Face.
|
22 |
+
|
23 |
+
## π₯οΈ Training Details
|
24 |
+
|
25 |
+
The model was trained on an **NVIDIA RTX 3080** GPU in a home setup, demonstrating that effective content moderation models can be developed with consumer-grade hardware. This makes the model development process more accessible to individual developers and smaller organizations.
|
26 |
+
|
27 |
+
Key Training Specifications:
|
28 |
+
- Hardware: NVIDIA RTX 3080
|
29 |
+
- Base Model: DistilBERT
|
30 |
+
- Model Size: 67M parameters (optimized for efficient deployment)
|
31 |
+
- Training Environment: Local workstation
|
32 |
+
- Training Type: Fine-tuning
|
33 |
+
|
34 |
+
Despite its relatively compact size **(67M parameters)**, this model achieves impressive performance metrics, making it suitable for deployment across various devices and environments. The model's efficiency-to-performance ratio demonstrates that effective content moderation is possible without requiring extensive computational resources.
|
35 |
+
|
36 |
+
## π― Key Features
|
37 |
+
|
38 |
+
- Multi-label classification
|
39 |
+
- Real-time content analysis
|
40 |
+
- 95.4% accuracy rate
|
41 |
+
- 9 distinct content categories
|
42 |
+
- Easy integration via API or local implementation
|
43 |
+
- Lightweight deployment footprint
|
44 |
+
- Suitable for **edge devices and mobile applications**
|
45 |
+
- Low latency inference
|
46 |
+
- Resource-efficient while maintaining high accuracy
|
47 |
+
- Can run on consumer-grade hardware
|
48 |
+
|
49 |
+
## π Content Categories
|
50 |
+
|
51 |
+
The model identifies the following types of potentially harmful content:
|
52 |
+
|
53 |
+
| Category | Label | Definition |
|
54 |
+
|----------|--------|------------|
|
55 |
+
| Sexual | `S` | Adult content including explicit sexual references, nudity discussions, or suggestive material |
|
56 |
+
| Hate | `H` | Discriminatory or prejudiced content targeting individuals or groups based on protected characteristics |
|
57 |
+
| Violence | `V` | Content depicting or promoting physical harm, aggression, or cruel behavior |
|
58 |
+
| Harassment | `HR` | Bullying, stalking, or targeted hostile behavior towards individuals or groups |
|
59 |
+
| Self-Harm | `SH` | Content related to suicide, self-injury, or dangerous behavior that could lead to personal harm |
|
60 |
+
| Sexual/Minors | `S3` | Any sexual content involving or targeting minors - strictly prohibited |
|
61 |
+
| Hate/Threat | `H2` | Hate speech combined with explicit threats or incitement to violence |
|
62 |
+
| Violence/Graphic | `V2` | Extreme violence, gore, or disturbing graphic content |
|
63 |
+
| Safe Content | `OK` | Appropriate content that doesn't violate any guidelines |
|
64 |
+
|
65 |
+
## π Performance Metrics
|
66 |
+
|
67 |
+
```
|
68 |
+
Accuracy: 95.4%
|
69 |
+
Mean ROC AUC: 0.912
|
70 |
+
Macro F1 Score: 0.407
|
71 |
+
Micro F1 Score: 0.802
|
72 |
+
```
|
73 |
+
|
74 |
+
[View detailed performance metrics](#model-performance)
|
75 |
+
|
76 |
+
## π Quick Start
|
77 |
+
|
78 |
+
### Python Implementation (Local)
|
79 |
+
|
80 |
+
```python
|
81 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
82 |
+
|
83 |
+
# Initialize model and tokenizer
|
84 |
+
model = AutoModelForSequenceClassification.from_pretrained("Vrandan/Comment-Moderation")
|
85 |
+
tokenizer = AutoTokenizer.from_pretrained("Vrandan/Comment-Moderation")
|
86 |
+
|
87 |
+
def analyze_text(text):
|
88 |
+
inputs = tokenizer(text, return_tensors="pt")
|
89 |
+
outputs = model(**inputs)
|
90 |
+
probabilities = outputs.logits.softmax(dim=-1).squeeze()
|
91 |
+
|
92 |
+
# Get predictions
|
93 |
+
labels = [model.config.id2label[i] for i in range(len(probabilities))]
|
94 |
+
predictions = sorted(zip(labels, probabilities), key=lambda x: x[1], reverse=True)
|
95 |
+
|
96 |
+
return predictions
|
97 |
+
|
98 |
+
# Example usage
|
99 |
+
text = "Your text here"
|
100 |
+
results = analyze_text(text)
|
101 |
+
for label, prob in results:
|
102 |
+
print(f"{label}: {prob:.4f}")
|
103 |
+
```
|
104 |
+
|
105 |
+
#### Example Output:
|
106 |
+
```
|
107 |
+
Label: OK - Probability: 0.9840
|
108 |
+
Label: H - Probability: 0.0043
|
109 |
+
Label: SH - Probability: 0.0039
|
110 |
+
Label: V - Probability: 0.0019
|
111 |
+
Label: S - Probability: 0.0018
|
112 |
+
Label: HR - Probability: 0.0015
|
113 |
+
Label: V2 - Probability: 0.0011
|
114 |
+
Label: S3 - Probability: 0.0010
|
115 |
+
Label: H2 - Probability: 0.0006
|
116 |
+
```
|
117 |
+
|
118 |
+
### Python Implementation (Serverless)
|
119 |
+
|
120 |
+
```python
|
121 |
+
import requests
|
122 |
+
|
123 |
+
API_URL = "https://api-inference.huggingface.co/models/Vrandan/Comment-Moderation"
|
124 |
+
headers = {"Authorization": "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
|
125 |
+
|
126 |
+
def query(payload):
|
127 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
128 |
+
return response.json()
|
129 |
+
|
130 |
+
output = query({
|
131 |
+
"inputs": "Your text here",
|
132 |
+
})
|
133 |
+
```
|
134 |
+
|
135 |
+
### JavaScript Implementation (Node.js)
|
136 |
+
|
137 |
+
```javascript
|
138 |
+
require('dotenv').config();
|
139 |
+
const { HfInference } = require('@huggingface/inference');
|
140 |
+
const readline = require('readline');
|
141 |
+
|
142 |
+
// Initialize the Hugging Face client
|
143 |
+
// To use this, follow these steps:
|
144 |
+
// 1. Create a `.env` file in the root directory of your project.
|
145 |
+
// 2. Visit https://huggingface.co/settings/tokens to generate your access token (you may need to create an account if you haven't already).
|
146 |
+
// 3. Add the token to your `.env` file like this:
|
147 |
+
// HUGGING_FACE_ACCESS_TOKEN=your_token_here
|
148 |
+
// 4. Install dotenv & huggingface/inference package (`npm install dotenv` & `npm install @huggingface/inference`) and load it in your project.
|
149 |
+
const hf = new HfInference(process.env.HUGGING_FACE_ACCESS_TOKEN);
|
150 |
+
|
151 |
+
// Create readline interface
|
152 |
+
const rl = readline.createInterface({
|
153 |
+
input: process.stdin,
|
154 |
+
output: process.stdout
|
155 |
+
});
|
156 |
+
|
157 |
+
async function analyzeText(text) {
|
158 |
+
try {
|
159 |
+
const result = await hf.textClassification({
|
160 |
+
model: 'Vrandan/Comment-Moderation',
|
161 |
+
inputs: text
|
162 |
+
});
|
163 |
+
|
164 |
+
console.log('\nResults:');
|
165 |
+
result.forEach(pred => {
|
166 |
+
console.log(`Label: ${pred.label} - Probability: ${pred.score.toFixed(4)}`);
|
167 |
+
});
|
168 |
+
} catch (error) {
|
169 |
+
console.error('Error analyzing text:', error.message);
|
170 |
+
}
|
171 |
+
}
|
172 |
+
|
173 |
+
async function main() {
|
174 |
+
while (true) {
|
175 |
+
try {
|
176 |
+
const text = await new Promise(resolve => {
|
177 |
+
rl.question('\nEnter text to analyze (or "quit" to exit): ', resolve);
|
178 |
+
});
|
179 |
+
|
180 |
+
if (text.toLowerCase() === 'quit') break;
|
181 |
+
if (text.trim()) await analyzeText(text);
|
182 |
+
} catch (error) {
|
183 |
+
console.error('Error:', error.message);
|
184 |
+
}
|
185 |
+
}
|
186 |
+
rl.close();
|
187 |
+
}
|
188 |
+
|
189 |
+
main().catch(console.error);
|
190 |
+
```
|
191 |
+
|
192 |
+
### JavaScript Implementation (Serverless)
|
193 |
+
|
194 |
+
```javascript
|
195 |
+
async function query(data) {
|
196 |
+
const response = await fetch(
|
197 |
+
"https://api-inference.huggingface.co/models/Vrandan/Comment-Moderation",
|
198 |
+
{
|
199 |
+
headers: {
|
200 |
+
Authorization: "Bearer hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
201 |
+
"Content-Type": "application/json",
|
202 |
+
},
|
203 |
+
method: "POST",
|
204 |
+
body: JSON.stringify(data),
|
205 |
+
}
|
206 |
+
);
|
207 |
+
const result = await response.json();
|
208 |
+
return result;
|
209 |
+
}
|
210 |
+
|
211 |
+
query({"inputs": "Your text here"}).then((response) => {
|
212 |
+
console.log(JSON.stringify(response));
|
213 |
+
});
|
214 |
+
```
|
215 |
+
|
216 |
+
## π Detailed Model Performance <a name="model-performance"></a>
|
217 |
+
|
218 |
+
The model has been extensively evaluated using standard classification metrics:
|
219 |
+
|
220 |
+
- **Loss:** 0.641
|
221 |
+
- **Accuracy:** 0.954 (95.4%)
|
222 |
+
- **Macro F1 Score:** 0.407
|
223 |
+
- **Micro F1 Score:** 0.802
|
224 |
+
- **Weighted F1 Score:** 0.763
|
225 |
+
- **Macro Precision:** 0.653
|
226 |
+
- **Micro Precision:** 0.875
|
227 |
+
- **Weighted Precision:** 0.838
|
228 |
+
- **Macro Recall:** 0.349
|
229 |
+
- **Micro Recall:** 0.740
|
230 |
+
- **Weighted Recall:** 0.740
|
231 |
+
- **Mean ROC AUC:** 0.912
|
232 |
+
|
233 |
+
## β οΈ Important Considerations
|
234 |
+
|
235 |
+
### Ethical Usage
|
236 |
+
- Regular bias monitoring
|
237 |
+
- Context-aware implementation
|
238 |
+
- Privacy-first approach
|
239 |
+
|
240 |
+
### Limitations
|
241 |
+
- May miss contextual nuances
|
242 |
+
- Potential for false positives
|
243 |
+
- Cultural context variations
|
244 |
+
|
245 |
+
## π Dataset Information
|
246 |
+
|
247 |
+
This model was trained on the dataset released by OpenAI, as described in their paper ["A Holistic Approach to Undesired Content Detection"](https://arxiv.org/abs/2208.03274).
|
248 |
+
|
249 |
+
### Dataset Source
|
250 |
+
- π [Original Paper (PDF)](https://arxiv.org/pdf/2208.03274)
|
251 |
+
- πΎ [Dataset Repository](https://github.com/openai/moderation-api-release)
|
252 |
+
|
253 |
+
### Citation
|
254 |
+
If you use this model or dataset in your research, please cite:
|
255 |
+
```bibtex
|
256 |
+
@article{openai2022moderation,
|
257 |
+
title={A Holistic Approach to Undesired Content Detection},
|
258 |
+
author={Todor Markov and Chong Zhang and Sandhini Agarwal and Tyna Eloundou and Teddy Lee and Steven Adler and Angela Jiang and Lilian Weng},
|
259 |
+
journal={arXiv preprint arXiv:2208.03274},
|
260 |
+
year={2022}
|
261 |
+
}
|
262 |
+
```
|
263 |
+
|
264 |
+
## π§ Contact
|
265 |
+
|
266 |
+
For support or queries, please [open an issue](https://github.com/Vrandan/Comment-Moderation/issues).
|
267 |
+
|
268 |
+
---
|