J-Score ████████████████████████████░░░░░░░░░░ 0.71
STA ██████████████████████████████████████ 0.95
SIM (ref) ██████████████████████████████████████ 1.00
Fluency ██████████████████████████████████████ 1.00
1 from transformers import AutoTokenizer , AutoModelForCausalLM
2 import torch
3
4 # Load model
5 model_name = "ispromashka/arab-detoxification-isp"
6 tokenizer = AutoTokenizer . from_pretrained ( model_name )
7 model = AutoModelForCausalLM . from_pretrained ( model_name , torch_dtype = torch . float16 )
8 model . to ( "cuda" ) # or "cpu"
9
10 def detoxify ( text : str ) - > str :
11 """Convert toxic Arabic text to neutral form."""
12 prompt = f"سام: { text } \nمهذب:"
13 inputs = tokenizer ( prompt , return_tensors = "pt" ) . to ( model . device )
14
15 outputs = model . generate (
16 ** inputs ,
17 max_new_tokens = 50 ,
18 temperature = 0.7 ,
19 top_p = 0.9 ,
20 repetition_penalty = 1.2 ,
21 do_sample = True ,
22 pad_token_id = tokenizer . pad_token_id ,
23 )
24
25 result = tokenizer . decode ( outputs [ 0 ] , skip_special_tokens = True )
26 return result . split ( "مهذب:" ) [ - 1 ] . strip ( ) . split ( "\n" ) [ 0 ]
27
28 # Example
29 toxic_text = "أنت غبي جداً"
30 neutral_text = detoxify ( toxic_text )
31 print ( f"Input: { toxic_text } " )
32 print ( f"Output: { neutral_text } " )
┌─────────────────────────────────────────────────────────────┐
│ STAGE 1: Base Models │
├─────────────────────────────────────────────────────────────┤
│ Train 3 specialized models independently on detox dataset │
│ • AraGPT2-Medium (25 epochs) │
│ • Bloom-560m (25 epochs) │
│ • Bloom-1b7 (20 epochs) │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ STAGE 2: Ensemble Selection │
├─────────────────────────────────────────────────────────────┤
│ For each input, select best prediction using: │
│ Sentence-BERT (paraphrase-multilingual-mpnet-base-v2) │
│ Selection: argmax(cosine_similarity(pred, reference)) │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ STAGE 3: Knowledge Distillation │
├─────────────────────────────────────────────────────────────┤
│ Fine-tune fresh Bloom-1b7 on: │
│ • Original dataset (3000+ examples) │
│ • Ensemble best predictions (1500+ examples) │
│ • Total: 4500+ training examples │
└─────────────────────────────────────────────────────────────┘
Dataset used for training and evaluation:
ispromashka/arabic-detox-dataset
سام: {toxic_text}
مهذب: {neutral_text}<EOS>
1 @misc{arabicdetox2024,
2 author = {ispromashka},
3 title = {Arabic Text Detoxification: Ensemble Knowledge Distillation Approach},
4 year = {2024},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/ispromashka/arab-detoxification-isp}
7 }
This project is licensed under the MIT License - see the
LICENSE file for details.