model uploaded
Browse files
README.md
CHANGED
|
@@ -9,38 +9,55 @@ tags:
|
|
| 9 |
- hate-speech
|
| 10 |
- sequence-classification
|
| 11 |
- pytorch
|
| 12 |
-
-
|
| 13 |
-
-
|
| 14 |
-
license: other #
|
|
|
|
|
|
|
| 15 |
---
|
| 16 |
|
| 17 |
-
## Urdu RoBERTa Hate Speech Classifier
|
| 18 |
|
| 19 |
- **Base model**: `urduhack/roberta-urdu-small`
|
| 20 |
- **Task**: Binary text classification (hate vs. not_hate)
|
| 21 |
- **Language**: Urdu (ur)
|
| 22 |
-
- **Labels
|
| 23 |
- 0 → `not_hate`
|
| 24 |
- 1 → `hate`
|
| 25 |
|
| 26 |
-
This model fine-tunes a small RoBERTa for Urdu
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
- Flagging potentially hateful content in Urdu text (e.g., tweets, comments)
|
| 32 |
-
- Assisting human moderators and analysts
|
| 33 |
-
- Research and educational demos
|
| 34 |
-
- Limitations:
|
| 35 |
-
- May misclassify satire, reclaimed slurs, or dialectal expressions
|
| 36 |
-
- Sensitive to domain shift (platform/topic/user community)
|
| 37 |
-
- Biases may reflect the data it was trained on
|
| 38 |
-
- Risks:
|
| 39 |
-
- False positives can suppress legitimate speech
|
| 40 |
-
- False negatives can miss harmful content
|
| 41 |
-
- Mitigations:
|
| 42 |
-
- Use with a human-in-the-loop
|
| 43 |
-
- Monitor performance and update thresholds per deployment domain
|
| 44 |
|
| 45 |
### How to use (Transformers)
|
| 46 |
|
|
@@ -48,151 +65,85 @@ This model fine-tunes a small RoBERTa for Urdu to detect hate speech. It is inte
|
|
| 48 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 49 |
import torch
|
| 50 |
|
| 51 |
-
MODEL_ID = "
|
| 52 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 53 |
-
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
def
|
| 57 |
-
|
| 58 |
with torch.no_grad():
|
| 59 |
-
|
| 60 |
-
probs =
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
"scores": {"not_hate": probs[0], "hate": probs[1]},
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
print(predict_label("یہ نفرت انگیز مواد ہے یا نہیں؟"))
|
| 70 |
```
|
| 71 |
|
| 72 |
Or with a pipeline:
|
| 73 |
|
| 74 |
```python
|
| 75 |
from transformers import pipeline
|
| 76 |
-
clf = pipeline("text-classification", model="
|
| 77 |
-
print(clf("یہ نفرت انگیز
|
| 78 |
-
```
|
| 79 |
-
|
| 80 |
-
### Inference API (no code download)
|
| 81 |
-
|
| 82 |
-
- Python (requests):
|
| 83 |
-
|
| 84 |
-
```python
|
| 85 |
-
import os, requests
|
| 86 |
-
|
| 87 |
-
API_URL = "https://api-inference.huggingface.co/models/your-username/urdu-roberta-hate"
|
| 88 |
-
HEADERS = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN', '')}"}
|
| 89 |
-
|
| 90 |
-
def infer(text: str):
|
| 91 |
-
r = requests.post(API_URL, headers=HEADERS, json={"inputs": text}, timeout=30)
|
| 92 |
-
r.raise_for_status()
|
| 93 |
-
return r.json() # [{label, score}, ...] OR [[{label, score}, ...]] depending on config
|
| 94 |
-
|
| 95 |
-
print(infer("یہ نفرت انگیز مواد ہے یا نہیں؟"))
|
| 96 |
```
|
| 97 |
|
| 98 |
-
|
| 99 |
|
|
|
|
| 100 |
```bash
|
| 101 |
-
curl -X POST \
|
| 102 |
-
-
|
| 103 |
-
-
|
| 104 |
-
-d '{"inputs":"یہ نفرت انگیز مواد ہے یا نہیں؟"}' \
|
| 105 |
-
https://api-inference.huggingface.co/models/your-username/urdu-roberta-hate
|
| 106 |
```
|
| 107 |
|
| 108 |
-
-
|
| 109 |
-
|
| 110 |
```python
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 114 |
```
|
| 115 |
|
| 116 |
-
###
|
| 117 |
-
|
| 118 |
-
-
|
| 119 |
-
-
|
| 120 |
-
|
| 121 |
-
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
### Preprocessing
|
| 128 |
-
|
| 129 |
-
- Standard RoBERTa tokenization (`AutoTokenizer` for the base model).
|
| 130 |
-
- Truncation and padding to the model max length (e.g., 128/256). Adjust as needed.
|
| 131 |
-
|
| 132 |
-
### Training details
|
| 133 |
-
|
| 134 |
-
- Base: `urduhack/roberta-urdu-small`
|
| 135 |
-
- Objective: Cross-entropy, 2 classes
|
| 136 |
-
- Hardware: CPU or single GPU
|
| 137 |
-
- Hyperparameters (example; update with your actual settings):
|
| 138 |
-
- lr: 2e-5
|
| 139 |
-
- batch_size: 16
|
| 140 |
-
- epochs: 3–5
|
| 141 |
-
- max_length: 128–256
|
| 142 |
-
- weight_decay: 0.01
|
| 143 |
-
- warmup_ratio: 0.1
|
| 144 |
-
|
| 145 |
-
### Data
|
| 146 |
-
|
| 147 |
-
- Source: Custom Urdu hate speech dataset (e.g., tweets/comments)
|
| 148 |
-
- Class balance: Please document distribution if available (helps threshold setting)
|
| 149 |
-
- Cleaning: Standard text normalization as applicable
|
| 150 |
-
|
| 151 |
-
### Evaluation
|
| 152 |
-
|
| 153 |
-
- Metrics to report (fill in your numbers):
|
| 154 |
-
- Accuracy: TBD
|
| 155 |
-
- F1 (macro): TBD
|
| 156 |
-
- Precision/Recall (hate class): TBD
|
| 157 |
-
- Suggested threshold: argmax for 2-class; for imbalanced data, consider probability threshold tuning on a validation set.
|
| 158 |
-
|
| 159 |
-
### Limitations and bias
|
| 160 |
-
|
| 161 |
-
- May misinterpret context, irony, or reclaimed language
|
| 162 |
-
- Potential domain and demographic bias
|
| 163 |
-
- Performance can degrade on long-form or code-mixed content
|
| 164 |
-
|
| 165 |
-
### Responsible AI and safety
|
| 166 |
-
|
| 167 |
-
- Use as an assistive tool with human review
|
| 168 |
-
- Provide user appeals and error reporting
|
| 169 |
-
- Regularly audit for disparities
|
| 170 |
|
| 171 |
-
###
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
|
| 174 |
-
-
|
| 175 |
-
-
|
| 176 |
-
-
|
| 177 |
|
| 178 |
### License
|
| 179 |
-
|
| 180 |
-
- The license must be compatible with the base model and your data usage. Update the `license:` field above and add details here.
|
| 181 |
|
| 182 |
### Citation
|
| 183 |
-
|
| 184 |
-
If you use this model, please cite the base model and your fine-tuning work.
|
| 185 |
-
|
| 186 |
```bibtex
|
| 187 |
-
@misc{
|
| 188 |
-
title = {Urdu RoBERTa Hate Speech Classifier},
|
| 189 |
-
author = {
|
| 190 |
year = {2025},
|
| 191 |
-
howpublished = {\url{https://huggingface.co/
|
| 192 |
}
|
| 193 |
```
|
| 194 |
|
| 195 |
### Acknowledgements
|
| 196 |
-
|
| 197 |
-
-
|
| 198 |
-
-
|
|
|
|
| 9 |
- hate-speech
|
| 10 |
- sequence-classification
|
| 11 |
- pytorch
|
| 12 |
+
- smote
|
| 13 |
+
- tf-idf
|
| 14 |
+
license: other # inherit/align with base model's license
|
| 15 |
+
datasets:
|
| 16 |
+
- Adnan855570/urdu-hate-speech
|
| 17 |
---
|
| 18 |
|
| 19 |
+
## Urdu RoBERTa Hate Speech Classifier (Balanced)
|
| 20 |
|
| 21 |
- **Base model**: `urduhack/roberta-urdu-small`
|
| 22 |
- **Task**: Binary text classification (hate vs. not_hate)
|
| 23 |
- **Language**: Urdu (ur)
|
| 24 |
+
- **Labels**
|
| 25 |
- 0 → `not_hate`
|
| 26 |
- 1 → `hate`
|
| 27 |
|
| 28 |
+
This model fine-tunes a small RoBERTa for Urdu hate-speech detection. Class imbalance was addressed by oversampling with SMOTE at the feature level (TF–IDF) prior to tokenization-based training.
|
| 29 |
+
|
| 30 |
+
### Training data and preprocessing
|
| 31 |
+
- Source dataset: `Adnan855570/urdu-hate-speech` (Excel files: `preprocessed_combined_file (1).xlsx`, `Urdu_Hate_Speech.xlsx`)
|
| 32 |
+
- Columns used in notebook: `Tweet` (text), `Tag` (label in {0,1})
|
| 33 |
+
- Steps:
|
| 34 |
+
- TF–IDF featurization (max_features=10000)
|
| 35 |
+
- SMOTE oversampling (random_state=42) to balance classes
|
| 36 |
+
- Train/test split: 80/20 (random_state=42)
|
| 37 |
+
- Tokenization: `AutoTokenizer.from_pretrained("urduhack/roberta-urdu-small")` with `truncation=True`, `padding=True`
|
| 38 |
+
|
| 39 |
+
### Training setup
|
| 40 |
+
- Model: `AutoModelForSequenceClassification` with `num_labels=2`
|
| 41 |
+
- Device: GPU if available
|
| 42 |
+
- Hyperparameters:
|
| 43 |
+
- epochs: 3
|
| 44 |
+
- per_device_train_batch_size: 8
|
| 45 |
+
- per_device_eval_batch_size: 8
|
| 46 |
+
- warmup_steps: 500
|
| 47 |
+
- weight_decay: 0.01
|
| 48 |
+
- evaluation_strategy: epoch
|
| 49 |
+
- save_strategy: epoch
|
| 50 |
+
- load_best_model_at_end: true
|
| 51 |
+
- Metrics:
|
| 52 |
+
- Accuracy, Precision, Recall, F1 (binary)
|
| 53 |
|
| 54 |
+
### Evaluation results (test split)
|
| 55 |
+
- accuracy: 0.7891
|
| 56 |
+
- f1: 0.7854
|
| 57 |
+
- precision: 0.8208
|
| 58 |
+
- recall: 0.7529
|
| 59 |
|
| 60 |
+
Note: Results derive from the balanced (SMOTE) dataset and the 80/20 split used in the notebook.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
### How to use (Transformers)
|
| 63 |
|
|
|
|
| 65 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 66 |
import torch
|
| 67 |
|
| 68 |
+
MODEL_ID = "Adnan855570/urdu-roberta-hate" # replace if different
|
| 69 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 70 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID).eval()
|
| 71 |
+
|
| 72 |
+
id2label = model.config.id2label or {"0":"not_hate","1":"hate"}
|
| 73 |
|
| 74 |
+
def predict(text: str):
|
| 75 |
+
enc = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 76 |
with torch.no_grad():
|
| 77 |
+
logits = model(**enc).logits
|
| 78 |
+
probs = logits.softmax(dim=-1).squeeze().tolist()
|
| 79 |
+
pred = int(logits.argmax(dim=-1).item())
|
| 80 |
+
return {"label_id": pred, "label": id2label.get(str(pred), str(pred)),
|
| 81 |
+
"scores": {"not_hate": probs[0], "hate": probs[1]}}
|
| 82 |
+
|
| 83 |
+
print(predict("یہ نفرت انگیز ہے یا نہیں؟"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
```
|
| 85 |
|
| 86 |
Or with a pipeline:
|
| 87 |
|
| 88 |
```python
|
| 89 |
from transformers import pipeline
|
| 90 |
+
clf = pipeline("text-classification", model="Adnan855570/urdu-roberta-hate", top_k=None)
|
| 91 |
+
print(clf("یہ نفرت انگیز ہے یا نہیں؟"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
```
|
| 93 |
|
| 94 |
+
### Inference API
|
| 95 |
|
| 96 |
+
- cURL
|
| 97 |
```bash
|
| 98 |
+
curl -X POST -H "Authorization: Bearer $HF_TOKEN" -H "Content-Type: application/json" \
|
| 99 |
+
-d '{"inputs":"یہ نفرت انگیز ہے یا نہیں؟"}' \
|
| 100 |
+
https://api-inference.huggingface.co/models/Adnan855570/urdu-roberta-hate
|
|
|
|
|
|
|
| 101 |
```
|
| 102 |
|
| 103 |
+
- Python
|
|
|
|
| 104 |
```python
|
| 105 |
+
import os, requests
|
| 106 |
+
API_URL = "https://api-inference.huggingface.co/models/Adnan855570/urdu-roberta-hate"
|
| 107 |
+
HEADERS = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN','')}"}
|
| 108 |
+
print(requests.post(API_URL, headers=HEADERS, json={"inputs":"..."}, timeout=30).json())
|
| 109 |
```
|
| 110 |
|
| 111 |
+
### Intended uses and limitations
|
| 112 |
+
- Intended:
|
| 113 |
+
- Flagging potentially hateful Urdu content
|
| 114 |
+
- Assisting human moderation and research
|
| 115 |
+
- Limitations:
|
| 116 |
+
- May misclassify satire, reclaimed slurs, or code-mixed content
|
| 117 |
+
- Domain shift sensitivity (platform/community/topic)
|
| 118 |
+
- Risks:
|
| 119 |
+
- False positives/negatives; do not use as the sole basis for punitive actions
|
| 120 |
+
- Recommendation:
|
| 121 |
+
- Use with human-in-the-loop; periodically audit outcomes and bias
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
### Label mapping
|
| 124 |
+
Ensure the config includes:
|
| 125 |
+
- `id2label = {"0":"not_hate","1":"hate"}`
|
| 126 |
+
- `label2id = {"not_hate":0,"hate":1}`
|
| 127 |
|
| 128 |
+
### Reproducibility notes
|
| 129 |
+
- SMOTE and split seeds: `random_state=42`
|
| 130 |
+
- Tokenization: truncation and padding enabled (no explicit max_length set in notebook)
|
| 131 |
+
- Hardware: single GPU (e.g., Colab)
|
| 132 |
|
| 133 |
### License
|
| 134 |
+
- The model derivation should comply with the base model’s license (`urduhack/roberta-urdu-small`). Set a compatible license here once confirmed.
|
|
|
|
| 135 |
|
| 136 |
### Citation
|
|
|
|
|
|
|
|
|
|
| 137 |
```bibtex
|
| 138 |
+
@misc{urdu_roberta_hate_balanced_2025,
|
| 139 |
+
title = {Urdu RoBERTa Hate Speech Classifier (Balanced)},
|
| 140 |
+
author = {Adnan},
|
| 141 |
year = {2025},
|
| 142 |
+
howpublished = {\url{https://huggingface.co/Adnan855570/urdu-roberta-hate}}
|
| 143 |
}
|
| 144 |
```
|
| 145 |
|
| 146 |
### Acknowledgements
|
| 147 |
+
- Base: `urduhack/roberta-urdu-small`
|
| 148 |
+
- Libraries: 🤗 Transformers, Datasets, PyTorch
|
| 149 |
+
- Oversampling: SMOTE (imblearn)
|