Instructions to use Psycodem/gemma-4-e4b-lora-diacritization with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use Psycodem/gemma-4-e4b-lora-diacritization with PEFT:
Task type is invalid.
- Notebooks
- Google Colab
- Kaggle
Add model card
Browse files
README.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
base_model: google/gemma-4-E4B-it
|
| 3 |
+
library_name: peft
|
| 4 |
+
pipeline_tag: text-generation
|
| 5 |
+
tags:
|
| 6 |
+
- base_model:adapter:google/gemma-4-E4B-it
|
| 7 |
+
- lora
|
| 8 |
+
- arabic
|
| 9 |
+
- diacritization
|
| 10 |
+
- tashkeel
|
| 11 |
+
language:
|
| 12 |
+
- ar
|
| 13 |
+
datasets:
|
| 14 |
+
- Misraj/Sadeed_Tashkeela
|
| 15 |
+
- Misraj/SadeedDiac-25
|
| 16 |
+
license: apache-2.0
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# Gemma 4 E4B-it + LoRA for Arabic Diacritisation
|
| 20 |
+
|
| 21 |
+
LoRA adapters that restore Arabic diacritics (tashkeel) on top of
|
| 22 |
+
[`google/gemma-4-E4B-it`](https://huggingface.co/google/gemma-4-E4B-it). Three adapters are
|
| 23 |
+
included, trained on 10%, 30% and 50% of the corpus, so the effect of training
|
| 24 |
+
data volume can be read directly.
|
| 25 |
+
|
| 26 |
+
Part of **DiacriticS**, a contamination-controlled study of open-weights models on
|
| 27 |
+
this task — [project site](https://diacritics.vercel.app/) · [code](https://github.com/Psycodem/DiacriticS-Language-Model-for-Arabic-Diacritisation).
|
| 28 |
+
|
| 29 |
+
## Which subfolder to use
|
| 30 |
+
|
| 31 |
+
Each fraction is a subfolder of this repo. `50pct` is the strongest.
|
| 32 |
+
|
| 33 |
+
| Subfolder | Training rows | DER (CE) | DER (no CE) | WER (CE) | WER (no CE) |
|
| 34 |
+
|---|---:|---:|---:|---:|---:|
|
| 35 |
+
| `10pct` | 104,270 | **3.15** | 2.68 | 7.28 | 4.47 |
|
| 36 |
+
| `30pct` | 312,809 | **2.98** | 2.54 | 6.82 | 4.12 |
|
| 37 |
+
| `50pct` | 521,349 | **2.81** | 2.38 | 6.54 | 3.96 |
|
| 38 |
+
|
| 39 |
+
Scored on the full 1,200-paragraph **SadeedDiac-25** benchmark with
|
| 40 |
+
[`Evaluation_Functions_Corrected.py`](https://github.com/Psycodem/DiacriticS-Language-Model-for-Arabic-Diacritisation/blob/main/Evaluation_Functions_Corrected.py).
|
| 41 |
+
Percentages; lower is better. CE = sentence-final case ending (*i'rab*).
|
| 42 |
+
|
| 43 |
+
<details>
|
| 44 |
+
<summary>All three splits</summary>
|
| 45 |
+
|
| 46 |
+
DER / WER with case endings.
|
| 47 |
+
|
| 48 |
+
| Subfolder | Train sample (500) | Tashkeela test (2,485) | SadeedDiac-25 (1,200) |
|
| 49 |
+
|---|---|---|---|
|
| 50 |
+
| `10pct` | 1.23 / 3.24 | 12.96 / 16.2 | 3.15 / 7.28 |
|
| 51 |
+
| `30pct` | 1.81 / 3.54 | 12.73 / 15.73 | 2.98 / 6.82 |
|
| 52 |
+
| `50pct` | 1.76 / 3.19 | 12.57 / 15.41 | 2.81 / 6.54 |
|
| 53 |
+
|
| 54 |
+
The Tashkeela test split scores worse than the benchmark because it inherits the
|
| 55 |
+
residual annotation noise of the source corpus, not because the model does worse
|
| 56 |
+
on it. SadeedDiac-25 is expert-reviewed and balanced 50/50 between Modern
|
| 57 |
+
Standard and Classical Arabic.
|
| 58 |
+
|
| 59 |
+
</details>
|
| 60 |
+
|
| 61 |
+
## Usage
|
| 62 |
+
|
| 63 |
+
```python
|
| 64 |
+
import torch
|
| 65 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 66 |
+
from peft import PeftModel
|
| 67 |
+
|
| 68 |
+
BASE = "google/gemma-4-E4B-it"
|
| 69 |
+
ADAPTER = "Psycodem/gemma-4-e4b-lora-diacritization"
|
| 70 |
+
FRACTION = "50pct" # "10pct" | "30pct" | "50pct"
|
| 71 |
+
|
| 72 |
+
tok = AutoTokenizer.from_pretrained(BASE, trust_remote_code=True)
|
| 73 |
+
if tok.pad_token is None:
|
| 74 |
+
tok.pad_token = tok.eos_token
|
| 75 |
+
|
| 76 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 77 |
+
BASE, dtype=torch.bfloat16, device_map="auto", trust_remote_code=True)
|
| 78 |
+
model = PeftModel.from_pretrained(model, ADAPTER, subfolder=FRACTION)
|
| 79 |
+
model = model.merge_and_unload()
|
| 80 |
+
model.eval()
|
| 81 |
+
|
| 82 |
+
SYSTEM_PROMPT = (
|
| 83 |
+
"أنت نظام متخصص في التشكيل الآلي للنصوص العربية. "
|
| 84 |
+
"مهمتك إضافة الحركات (التشكيل) الصحيحة إلى النص العربي المُدخل دون تغيير الكلمات أو ترتيبها، "
|
| 85 |
+
"مع مراعاة السياق النحوي والصرفي الكامل للجملة."
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
def diacritize(text):
|
| 89 |
+
msgs = [{"role": "system", "content": SYSTEM_PROMPT},
|
| 90 |
+
{"role": "user", "content": text}]
|
| 91 |
+
try:
|
| 92 |
+
prompt = tok.apply_chat_template(msgs, tokenize=False,
|
| 93 |
+
add_generation_prompt=True, enable_thinking=False)
|
| 94 |
+
except TypeError:
|
| 95 |
+
prompt = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
|
| 96 |
+
enc = tok(prompt, return_tensors="pt").to(model.device)
|
| 97 |
+
with torch.no_grad():
|
| 98 |
+
out = model.generate(**enc, max_new_tokens=512, do_sample=False,
|
| 99 |
+
pad_token_id=tok.pad_token_id)
|
| 100 |
+
return tok.decode(out[0, enc["input_ids"].shape[1]:], skip_special_tokens=True).strip()
|
| 101 |
+
|
| 102 |
+
print(diacritize("ذهب الطالب إلى المدرسة"))
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
**The prompt must match the one above** — it is what the adapter was trained
|
| 106 |
+
against, and a different instruction degrades output in ways that look like a bad
|
| 107 |
+
model rather than a harness mistake. Decode greedily (`do_sample=False`); the
|
| 108 |
+
reported numbers assume it. If you batch, set `tok.padding_side = "left"`.
|
| 109 |
+
|
| 110 |
+
## Training
|
| 111 |
+
|
| 112 |
+
Trained on nested subsets of
|
| 113 |
+
[`Misraj/Sadeed_Tashkeela`](https://huggingface.co/datasets/Misraj/Sadeed_Tashkeela),
|
| 114 |
+
drawn with a fixed shuffle seed so the 10% subset is contained in the 30%, and
|
| 115 |
+
that in the 50%. The corpus has a measured 0.4% overlap with the benchmark, which
|
| 116 |
+
is what makes the benchmark number meaningful.
|
| 117 |
+
|
| 118 |
+
| | |
|
| 119 |
+
|---|---|
|
| 120 |
+
| Method | LoRA, base weights frozen in bf16 (not QLoRA) |
|
| 121 |
+
| Rank / alpha / dropout | 16 / 32 / 0.05 |
|
| 122 |
+
| Target modules | `q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj` |
|
| 123 |
+
| Adapted matrices | 258 |
|
| 124 |
+
| Effective batch | 96 (4 x 8 x 3 GPUs) |
|
| 125 |
+
| Learning rate | 2e-4, cosine |
|
| 126 |
+
| Warmup | 5% of total steps |
|
| 127 |
+
| Epochs | 1 |
|
| 128 |
+
| Max sequence length | 1024 |
|
| 129 |
+
| Seed | 42 |
|
| 130 |
+
| Hardware | 3× A100 80GB, DDP via `torchrun` |
|
| 131 |
+
|
| 132 |
+
Both base models in the study share this configuration exactly, so differences
|
| 133 |
+
between them reflect architecture and data volume rather than tuning. The warmup
|
| 134 |
+
ratio of every run was verified after the fact from its logged learning-rate
|
| 135 |
+
curve.
|
| 136 |
+
|
| 137 |
+
## Limitations
|
| 138 |
+
|
| 139 |
+
Sentence-final case endings (*i'rab*) and Classical Arabic remain the dominant
|
| 140 |
+
error sources, as they are for every system in the study. These adapters were
|
| 141 |
+
trained for one epoch on a subset of a single corpus and are not expected to
|
| 142 |
+
transfer to dialectal Arabic or to Quranic orthography with its additional
|
| 143 |
+
annotation marks.
|
| 144 |
+
|
| 145 |
+
## Citation
|
| 146 |
+
|
| 147 |
+
If you use these adapters, please cite the benchmark and corpus they build on:
|
| 148 |
+
|
| 149 |
+
```bibtex
|
| 150 |
+
@article{sadeed2025,
|
| 151 |
+
title = {Sadeed: Advancing Arabic Diacritization Through Small Language Model},
|
| 152 |
+
author = {Aldallal, Z. and Chrouf, S. and Hennara, K. and Hamed, M. M. and
|
| 153 |
+
Hreden, M. and AlModhayan, S.},
|
| 154 |
+
journal = {arXiv preprint arXiv:2504.21635},
|
| 155 |
+
year = {2025}
|
| 156 |
+
}
|
| 157 |
+
```
|