Adnan855570 commited on
Commit
879c038
·
verified ·
1 Parent(s): a5fa5e3

model uploaded

Browse files
Files changed (1) hide show
  1. README.md +90 -139
README.md CHANGED
@@ -9,38 +9,55 @@ tags:
9
  - hate-speech
10
  - sequence-classification
11
  - pytorch
12
- - safety
13
- - moderation
14
- license: other # Update to the correct license for your model/checkpoint
 
 
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 to detect hate speech. It is intended for content moderation, research, and educational uses. Do not use as the sole basis for enforcement or punitive actions.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- ### Intended uses and limitations
 
 
 
 
29
 
30
- - Intended:
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 = "your-username/urdu-roberta-hate" # replace with your repo id
52
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
53
- model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
54
- model.eval()
 
55
 
56
- def predict_label(text: str) -> dict:
57
- inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
58
  with torch.no_grad():
59
- outputs = model(**inputs)
60
- probs = outputs.logits.softmax(dim=-1).squeeze().tolist()
61
- pred_id = int(outputs.logits.argmax(dim=-1).item())
62
- id2label = model.config.id2label
63
- return {
64
- "label_id": pred_id,
65
- "label": id2label.get(str(pred_id), id2label.get(pred_id, str(pred_id))),
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="your-username/urdu-roberta-hate", top_k=None)
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
- - cURL:
99
 
 
100
  ```bash
101
- curl -X POST \
102
- -H "Authorization: Bearer $HF_TOKEN" \
103
- -H "Content-Type: application/json" \
104
- -d '{"inputs":"یہ نفرت انگیز مواد ہے یا نہیں؟"}' \
105
- https://api-inference.huggingface.co/models/your-username/urdu-roberta-hate
106
  ```
107
 
108
- - huggingface_hub client:
109
-
110
  ```python
111
- from huggingface_hub import InferenceClient
112
- client = InferenceClient(model="your-username/urdu-roberta-hate", token=os.environ.get("HF_TOKEN"))
113
- print(client.text_classification("یہ نفرت انگیز مواد ہے یا نہیں؟"))
 
114
  ```
115
 
116
- ### Expected input and output
117
-
118
- - Input: a single Urdu string (short to medium-length, e.g., tweet or comment)
119
- - Output:
120
- - Transformers: logits or labels via pipeline
121
- - Recommended mapping:
122
- - `id2label = {"0": "not_hate", "1": "hate"}`
123
- - `label2id = {"not_hate": 0, "hate": 1}`
124
-
125
- If you want 0/1 numeric outputs in an API, map `label` to `{not_hate: 0, hate: 1}`.
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
- ### Deployment tips
 
 
 
172
 
173
- - Direct load in Python: `from_pretrained("your-username/urdu-roberta-hate")`
174
- - Render/Flask: set `MODEL_ID` to this repo id and load via `AutoTokenizer/AutoModelForSequenceClassification.from_pretrained(MODEL_ID)`
175
- - HF Inference API: use bearer token for private repos or higher rate limits
176
- - HF Space: create a Docker Space exposing `/predict` for a custom API interface
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{urdu_roberta_hate_2025,
188
- title = {Urdu RoBERTa Hate Speech Classifier},
189
- author = {Your Name},
190
  year = {2025},
191
- howpublished = {\url{https://huggingface.co/your-username/urdu-roberta-hate}}
192
  }
193
  ```
194
 
195
  ### Acknowledgements
196
-
197
- - Base model: `urduhack/roberta-urdu-small`
198
- - Libraries: 🤗 Transformers, PyTorch
 
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)