ispromashka commited on
Commit
92b22c7
ยท
verified ยท
1 Parent(s): 537f7dc

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +315 -3
README.md CHANGED
@@ -1,3 +1,315 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ language:
4
+ - ar
5
+ - en
6
+ library_name: transformers
7
+ 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
+ [![Model](https://img.shields.io/badge/Model-Bloom--1b7-blue)](https://huggingface.co/bigscience/bloom-1b7)
46
+ [![License](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
47
+ [![Language](https://img.shields.io/badge/Language-Arabic-red)](https://en.wikipedia.org/wiki/Arabic)
48
+ [![HuggingFace](https://img.shields.io/badge/๐Ÿค—-HuggingFace-yellow)](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>