File size: 2,849 Bytes
243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc 243c3bd bd0aecc | 1 2 3 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 | ---
language:
- en
license: mit
tags:
- hate-speech-detection
- severity-prediction
- text-classification
- bert
- explainable-ai
datasets:
- Hate-speech-CNERG/hatexplain
metrics:
- accuracy
- f1
---
# Hate Speech Severity Predictor — BERT
## Model Description
This is a fine-tuned BERT model (bert-base-uncased) for hate speech severity prediction,
developed as part of an MSc research project at the University of Moratuwa, Sri Lanka.
The model predicts hate speech severity across three levels:
- Level 0 — Non-hate Speech
- Level 1 — Mild / Offensive
- Level 2 — Severe Hate Speech
It also produces a continuous severity score S in [0,1]:
S = 0.0 x P(Level 0) + 0.5 x P(Level 1) + 1.0 x P(Level 2)
## Model Details
- Developed by: J.A.U.S. Jayakody (239817M), University of Moratuwa
- Supervised by: Dr. Supunmali Ahangama
- Base model: bert-base-uncased
- Language: English
- License: MIT
## Dataset
Fine-tuned on HateXplain (Mathew et al., 2021):
- 20,148 posts from Twitter and Gab
- Stratified 70-15-15 train-validation-test split
## Training Details
- Epochs: 3 (best checkpoint: Epoch 2)
- Batch size: 16
- Learning rate: 2e-5
- Max sequence length: 128 tokens
- Class weighting: Balanced
- Hardware: Tesla T4 GPU
## Evaluation Results
| Metric | SVM | BERT |
|--------|-----|------|
| Accuracy | 0.629 | 0.684 |
| Macro F1 | 0.615 | 0.679 |
Severity Prediction Metrics:
- Spearman Correlation: 0.714
- Pearson Correlation: 0.720
- Mean Absolute Error: 0.212
- RMSE: 0.292
## How to Use
```python
from transformers import BertForSequenceClassification, BertTokenizer
import torch
import torch.nn.functional as F
import numpy as np
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained('UdaniSJ/hate-speech-severity-bert')
model.eval()
def predict_severity(text):
inputs = tokenizer(text, return_tensors='pt',
truncation=True, max_length=128)
with torch.no_grad():
outputs = model(**inputs)
probs = F.softmax(outputs.logits, dim=1).numpy()[0]
score = 0.0*probs[0] + 0.5*probs[1] + 1.0*probs[2]
level = int(np.argmax(probs))
names = {0:'Non-hate', 1:'Mild', 2:'Severe'}
return {'level': names[level], 'score': round(float(score),3)}
print(predict_severity("I love all people regardless of background"))
```
## Live Demo
https://huggingface.co/spaces/UdaniSJ/hate-speech-severity-predictor
## Limitations
- Trained on English social media content only
- May exhibit lexical over-reliance on identity terms
- Context-aware adjustment partially mitigates reclaimed language misclassification
## References
- Mathew et al. (2021). HateXplain. AAAI 2021.
- Devlin et al. (2019). BERT. NAACL 2019.
- Ribeiro et al. (2016). LIME. KDD 2016.
- Lundberg and Lee (2017). SHAP. NeurIPS 2017.
|