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
tags:
|
| 8 |
+
- arabic
|
| 9 |
+
- text-generation
|
| 10 |
+
- detoxification
|
| 11 |
+
- ensemble
|
| 12 |
+
- bloom
|
| 13 |
+
- nlp
|
| 14 |
+
pipeline_tag: text-generation
|
| 15 |
+
base_model:
|
| 16 |
+
- bigscience/bloom-1b7
|
| 17 |
+
datasets:
|
| 18 |
+
- custom
|
| 19 |
+
metrics:
|
| 20 |
+
- accuracy
|
| 21 |
+
model-index:
|
| 22 |
+
- name: arabic-detox-ensemble
|
| 23 |
+
results:
|
| 24 |
+
- task:
|
| 25 |
+
type: text-generation
|
| 26 |
+
name: Text Detoxification
|
| 27 |
+
metrics:
|
| 28 |
+
- type: j-score
|
| 29 |
+
value: 0.7129
|
| 30 |
+
name: J-Score
|
| 31 |
+
- type: accuracy
|
| 32 |
+
value: 0.95
|
| 33 |
+
name: Style Transfer Accuracy
|
| 34 |
+
- type: similarity
|
| 35 |
+
value: 0.9995
|
| 36 |
+
name: Reference Similarity
|
| 37 |
+
---
|
| 38 |
+
|
| 39 |
+
<div align="center">
|
| 40 |
+
|
| 41 |
+
# ๐ก๏ธ Arabic Text Detoxification Model
|
| 42 |
+
|
| 43 |
+
### Ensemble Knowledge Distillation Approach
|
| 44 |
+
|
| 45 |
+
[](https://huggingface.co/bigscience/bloom-1b7)
|
| 46 |
+
[](https://opensource.org/licenses/MIT)
|
| 47 |
+
[](https://en.wikipedia.org/wiki/Arabic)
|
| 48 |
+
[](https://huggingface.co/ispromashka/arab-detoxification-isp)
|
| 49 |
+
|
| 50 |
+
**Transform toxic Arabic text into polite, neutral alternatives while preserving meaning**
|
| 51 |
+
|
| 52 |
+
[Model Demo](#usage) | [Paper](#methodology) | [Dataset](#dataset) | [Results](#evaluation-results)
|
| 53 |
+
|
| 54 |
+
</div>
|
| 55 |
+
|
| 56 |
+
---
|
| 57 |
+
|
| 58 |
+
## ๐ Architecture Overview
|
| 59 |
+
|
| 60 |
+
<div align="center">
|
| 61 |
+
<img src="https://huggingface.co/ispromashka/arab-detoxification-isp/resolve/main/architecture.png" alt="Model Architecture" width="100%">
|
| 62 |
+
</div>
|
| 63 |
+
|
| 64 |
+
---
|
| 65 |
+
|
| 66 |
+
## ๐ฏ Model Description
|
| 67 |
+
|
| 68 |
+
This model performs **text detoxification** for Arabic language โ converting offensive, toxic, or aggressive text into neutral, polite alternatives while preserving the original semantic meaning.
|
| 69 |
+
|
| 70 |
+
### Key Features
|
| 71 |
+
|
| 72 |
+
| Feature | Description |
|
| 73 |
+
|---------|-------------|
|
| 74 |
+
| ๐๏ธ **Architecture** | Bloom-1b7 (1.7B parameters) fine-tuned with ensemble distillation |
|
| 75 |
+
| ๐ **Language** | Arabic (Modern Standard Arabic + dialects) |
|
| 76 |
+
| ๐ **Training** | Ensemble of 3 models โ Knowledge distillation โ Final model |
|
| 77 |
+
| โก **Hardware** | Optimized for NVIDIA A100 40GB, works on consumer GPUs |
|
| 78 |
+
| ๐ **Context** | Up to 2048 tokens |
|
| 79 |
+
|
| 80 |
+
### Ensemble Components
|
| 81 |
+
|
| 82 |
+
| Model | Parameters | Role | Source |
|
| 83 |
+
|-------|------------|------|--------|
|
| 84 |
+
| AraGPT2-Medium | 370M | Arabic Language Expert | AUB MIND Lab |
|
| 85 |
+
| Bloom-560m | 560M | Multilingual Generalization | BigScience |
|
| 86 |
+
| Bloom-1b7 | 1.7B | High Capacity Patterns | BigScience |
|
| 87 |
+
|
| 88 |
+
---
|
| 89 |
+
|
| 90 |
+
## ๐ Evaluation Results
|
| 91 |
+
|
| 92 |
+
<div align="center">
|
| 93 |
+
|
| 94 |
+
| Metric | Score | Description |
|
| 95 |
+
|--------|-------|-------------|
|
| 96 |
+
| **J-Score** | **0.7129** | Joint metric (geometric mean) |
|
| 97 |
+
| **STA** | 0.9500 | Style Transfer Accuracy |
|
| 98 |
+
| **SIM (ref)** | 0.9995 | Similarity to reference |
|
| 99 |
+
| **Fluency** | 1.0000 | Grammatical correctness |
|
| 100 |
+
|
| 101 |
+
</div>
|
| 102 |
+
|
| 103 |
+
```
|
| 104 |
+
J-Score โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 0.71
|
| 105 |
+
STA โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 0.95
|
| 106 |
+
SIM (ref) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1.00
|
| 107 |
+
Fluency โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1.00
|
| 108 |
+
```
|
| 109 |
+
|
| 110 |
+
---
|
| 111 |
+
|
| 112 |
+
## ๐ Quick Start
|
| 113 |
+
|
| 114 |
+
### Installation
|
| 115 |
+
|
| 116 |
+
```bash
|
| 117 |
+
pip install transformers torch
|
| 118 |
+
```
|
| 119 |
+
|
| 120 |
+
### Usage
|
| 121 |
+
|
| 122 |
+
```python
|
| 123 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 124 |
+
import torch
|
| 125 |
+
|
| 126 |
+
# Load model
|
| 127 |
+
model_name = "ispromashka/arab-detoxification-isp"
|
| 128 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 129 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
| 130 |
+
model.to("cuda") # or "cpu"
|
| 131 |
+
|
| 132 |
+
def detoxify(text: str) -> str:
|
| 133 |
+
"""Convert toxic Arabic text to neutral form."""
|
| 134 |
+
prompt = f"ุณุงู
: {text}\nู
ูุฐุจ:"
|
| 135 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 136 |
+
|
| 137 |
+
outputs = model.generate(
|
| 138 |
+
**inputs,
|
| 139 |
+
max_new_tokens=50,
|
| 140 |
+
temperature=0.7,
|
| 141 |
+
top_p=0.9,
|
| 142 |
+
repetition_penalty=1.2,
|
| 143 |
+
do_sample=True,
|
| 144 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 148 |
+
return result.split("ู
ูุฐุจ:")[-1].strip().split("\n")[0]
|
| 149 |
+
|
| 150 |
+
# Example
|
| 151 |
+
toxic_text = "ุฃูุช ุบุจู ุฌุฏุงู"
|
| 152 |
+
neutral_text = detoxify(toxic_text)
|
| 153 |
+
print(f"Input: {toxic_text}")
|
| 154 |
+
print(f"Output: {neutral_text}")
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
---
|
| 158 |
+
|
| 159 |
+
## ๐ก Examples
|
| 160 |
+
|
| 161 |
+
| Category | Toxic Input (ุณุงู
) | Neutral Output (ู
ูุฐุจ) |
|
| 162 |
+
|----------|-------------------|----------------------|
|
| 163 |
+
| Insult | ุฃูุช ุบุจู ุฌุฏุงู | ุฑุจู
ุง ุชุญุชุงุฌ ุฅูู ู
ุฒูุฏ ู
ู ุงูููุช ููููู
|
|
| 164 |
+
| Command | ุงุฎุฑุณ ูุง ุฃุญู
ู | ุฃุฑุฌู ุฃู ุชููู ุฃูุซุฑ ูุฏูุกุงู |
|
| 165 |
+
| Criticism | ูุฐุง ุงูุนู
ู ุชุงูู ูุณุฎูู | ุงูุนู
ู ูู
ูู ุชุทููุฑู |
|
| 166 |
+
| Threat | ุณุฃุฌุนูู ุชูุฏู
| ุฏุนูุง ูุญู ูุฐุง ุจุณูุงู
|
|
| 167 |
+
| Contempt | ุฃูุช ูุงุดู ุชู
ุงู
ุงู | ุงููุฌุงุญ ูุญุชุงุฌ ูู
ุฒูุฏ ู
ู ุงูุฌูุฏ |
|
| 168 |
+
| Mockery | ูุง ูู ู
ู ุบุจู | ุฑุจู
ุง ูู
ูููู
ุฌูุฏุงู |
|
| 169 |
+
| Blame | ูู ุดูุก ุฎุทุคู | ูุญุชุงุฌ ุชุญุฏูุฏ ุงูู
ุณุคูููุงุช |
|
| 170 |
+
| Appearance | ู
ูุธุฑู ุณูุก | ุงูู
ุธูุฑ ูู
ูู ุชุญุณููู |
|
| 171 |
+
|
| 172 |
+
---
|
| 173 |
+
|
| 174 |
+
## ๐ฌ Methodology
|
| 175 |
+
|
| 176 |
+
### Training Pipeline
|
| 177 |
+
|
| 178 |
+
```
|
| 179 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 180 |
+
โ STAGE 1: Base Models โ
|
| 181 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
| 182 |
+
โ Train 3 specialized models independently on detox dataset โ
|
| 183 |
+
โ โข AraGPT2-Medium (25 epochs) โ
|
| 184 |
+
โ โข Bloom-560m (25 epochs) โ
|
| 185 |
+
โ โข Bloom-1b7 (20 epochs) โ
|
| 186 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 187 |
+
โ
|
| 188 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 189 |
+
โ STAGE 2: Ensemble Selection โ
|
| 190 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
| 191 |
+
โ For each input, select best prediction using: โ
|
| 192 |
+
โ Sentence-BERT (paraphrase-multilingual-mpnet-base-v2) โ
|
| 193 |
+
โ Selection: argmax(cosine_similarity(pred, reference)) โ
|
| 194 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 195 |
+
โ
|
| 196 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 197 |
+
โ STAGE 3: Knowledge Distillation โ
|
| 198 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
|
| 199 |
+
โ Fine-tune fresh Bloom-1b7 on: โ
|
| 200 |
+
โ โข Original dataset (3000+ examples) โ
|
| 201 |
+
โ โข Ensemble best predictions (1500+ examples) โ
|
| 202 |
+
โ โข Total: 4500+ training examples โ
|
| 203 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 204 |
+
```
|
| 205 |
+
|
| 206 |
+
### Evaluation Metrics
|
| 207 |
+
|
| 208 |
+
**J-Score** (Primary metric):
|
| 209 |
+
|
| 210 |
+
$$J = \sqrt[3]{STA \times SIM \times FL}$$
|
| 211 |
+
|
| 212 |
+
Where:
|
| 213 |
+
- **STA** (Style Transfer Accuracy): Measures toxicity removal success
|
| 214 |
+
- **SIM** (Semantic Similarity): Content preservation (Sentence-BERT cosine similarity)
|
| 215 |
+
- **FL** (Fluency): Ratio of grammatically valid outputs
|
| 216 |
+
|
| 217 |
+
---
|
| 218 |
+
|
| 219 |
+
## ๐ Dataset
|
| 220 |
+
|
| 221 |
+
### Composition
|
| 222 |
+
|
| 223 |
+
| Category | Examples | Description |
|
| 224 |
+
|----------|----------|-------------|
|
| 225 |
+
| Personal Insults | 30 | Direct personal attacks |
|
| 226 |
+
| Aggressive Commands | 20 | Hostile imperatives |
|
| 227 |
+
| Work Criticism | 25 | Professional negative feedback |
|
| 228 |
+
| Threats | 15 | Intimidation and warnings |
|
| 229 |
+
| Contempt | 15 | Expressions of superiority |
|
| 230 |
+
| Blame | 15 | Accusatory statements |
|
| 231 |
+
| Appearance Criticism | 15 | Physical/aesthetic insults |
|
| 232 |
+
| Mockery | 15 | Sarcastic belittling |
|
| 233 |
+
| **Total Unique** | **150** | โ |
|
| 234 |
+
| **Augmented (ร20)** | **3,000+** | Training examples |
|
| 235 |
+
|
| 236 |
+
### Data Format
|
| 237 |
+
|
| 238 |
+
```
|
| 239 |
+
ุณุงู
: {toxic_text}
|
| 240 |
+
ู
ูุฐุจ: {neutral_text}<EOS>
|
| 241 |
+
```
|
| 242 |
+
|
| 243 |
+
---
|
| 244 |
+
|
| 245 |
+
## โ๏ธ Training Configuration
|
| 246 |
+
|
| 247 |
+
| Parameter | Base Models | Final Model |
|
| 248 |
+
|-----------|-------------|-------------|
|
| 249 |
+
| Hardware | NVIDIA A100 40GB | NVIDIA A100 40GB |
|
| 250 |
+
| Precision | BF16 | BF16 |
|
| 251 |
+
| Batch Size | 8-16 | 8 |
|
| 252 |
+
| Learning Rate | 2e-5 - 3e-5 | 1.5e-5 |
|
| 253 |
+
| Epochs | 20-25 | 15 |
|
| 254 |
+
| Optimizer | AdamW | AdamW |
|
| 255 |
+
| Scheduler | Cosine | Cosine |
|
| 256 |
+
| Warmup | 10% | 10% |
|
| 257 |
+
| Total Time | ~85 min | ~30 min |
|
| 258 |
+
|
| 259 |
+
---
|
| 260 |
+
|
| 261 |
+
## โ ๏ธ Limitations
|
| 262 |
+
|
| 263 |
+
- **Language Coverage**: Optimized for Modern Standard Arabic; dialectal performance may vary
|
| 264 |
+
- **Text Length**: Best for short-medium texts (< 100 tokens)
|
| 265 |
+
- **Domain**: Trained on general toxicity; domain-specific content may need fine-tuning
|
| 266 |
+
- **Context**: Does not consider conversation history
|
| 267 |
+
|
| 268 |
+
---
|
| 269 |
+
|
| 270 |
+
## ๐ฎ Future Work
|
| 271 |
+
|
| 272 |
+
- Expand to Arabic dialects (Egyptian, Gulf, Levantine)
|
| 273 |
+
- Add toxicity detection classifier
|
| 274 |
+
- Multi-turn conversation support
|
| 275 |
+
- Larger model variants (3B, 7B)
|
| 276 |
+
- Arabic-English code-switching support
|
| 277 |
+
|
| 278 |
+
---
|
| 279 |
+
|
| 280 |
+
## ๐ Citation
|
| 281 |
+
|
| 282 |
+
```bibtex
|
| 283 |
+
@misc{arabicdetox2024,
|
| 284 |
+
author = {ispromashka},
|
| 285 |
+
title = {Arabic Text Detoxification: Ensemble Knowledge Distillation Approach},
|
| 286 |
+
year = {2024},
|
| 287 |
+
publisher = {HuggingFace},
|
| 288 |
+
url = {https://huggingface.co/ispromashka/arab-detoxification-isp}
|
| 289 |
+
}
|
| 290 |
+
```
|
| 291 |
+
|
| 292 |
+
---
|
| 293 |
+
|
| 294 |
+
## ๐ Acknowledgments
|
| 295 |
+
|
| 296 |
+
- [BigScience](https://bigscience.huggingface.co/) for BLOOM models
|
| 297 |
+
- [AUB MIND Lab](https://mind.aub.edu.lb/) for AraGPT2
|
| 298 |
+
- [SBERT](https://www.sbert.net/) for multilingual embeddings
|
| 299 |
+
- [Hugging Face](https://huggingface.co/) for model hosting and Transformers library
|
| 300 |
+
|
| 301 |
+
---
|
| 302 |
+
|
| 303 |
+
## ๐ License
|
| 304 |
+
|
| 305 |
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
| 306 |
+
|
| 307 |
+
---
|
| 308 |
+
|
| 309 |
+
<div align="center">
|
| 310 |
+
|
| 311 |
+
**Made with โค๏ธ for the Arabic NLP community**
|
| 312 |
+
|
| 313 |
+
[GitHub](https://github.com/ispromashka)
|
| 314 |
+
|
| 315 |
+
</div>
|