DarijaDZ_Transliteration_ByT5
ByT5-small fine-tuned to write Algerian Darija in Arabizi. It takes a sentence in Arabic script and returns the same sentence in Arabizi (Latin letters and digits), one Arabizi word for each Arabic word. French and English words are written in their original spelling.
شفت الفيديو تاعك ميرسي بزاف -> chft lvideo ta3k merci bzaf
Try it in the demo Space.
Usage
import torch
from transformers import AutoTokenizer, T5ForConditionalGeneration
tok = AutoTokenizer.from_pretrained("google/byt5-small")
model = T5ForConditionalGeneration.from_pretrained("nasrellahkharroubi/DarijaDZ_Transliteration_ByT5").eval()
enc = tok(["واش راك خويا كيفاش حالك"], return_tensors="pt")
with torch.no_grad():
out = model.generate(**enc, max_new_tokens=384, num_beams=1, do_sample=False, use_cache=True)
print(tok.batch_decode(out, skip_special_tokens=True)[0])
Run it in FP32: ByT5 overflows in half precision (we measured NaN losses in FP16). To get several spellings of the same sentence, use sampling (do_sample=True, temperature, top_k, top_p, num_return_sequences) or beam search with num_return_sequences.
Training
- Data: two stages. v1 was trained on 11,560 sentence pairs from DarijaDZ_Transliteration. This version (v2) continues from v1 on 22,750 pairs (the verified and reviewed pairs available when v2 training started), with the sentences that contain restored French/English words shown twice per epoch. Validation: 2,414 pairs. Test: 2,434 pairs. The dataset has since grown to 26,416 training pairs; the 3,666 newest were not used to train this model.
- Method: full fine-tuning of
google/byt5-small(300M parameters), sentence level, FP32, 8-bit AdamW, batch size 16, gradient checkpointing. v1: learning rate 2e-4 with warm-up and cosine decay, 10 epochs with early stopping on validation character error rate. v2: initialised from v1, learning rate 5e-5, up to 3 epochs with early stopping on validation character error rate (the best checkpoint, after epoch 1, is the one published).
Results
Measured on the 2,434 held-out test sentences of the current dataset release, greedy decoding, FP32. The v1 column is the previous version of this model on the same test set. (The v1 model card reported 0.123 CER and 54.8% exact word match on an older, smaller 1,445-sentence test set; those numbers are not comparable with the table below.)
| Metric | v2 (this model) | v1 (previous version) | Rule-based transliterator |
|---|---|---|---|
| Character error rate (lower is better) | 0.117 | 0.119 | 0.219 |
| Exact word match | 56.6% | 56.6% | 34.5% |
| Word match, accepting any spelling the training data uses for that Arabic word | 84.7% | 84.1% | 57.3% |
| Output has the same number of words as the input | 99.7% | 99.7% | 100% |
| Words restored to French/English spelling: exact match | 54.8% | 51.7% | 2.7% |
"Accepting any spelling the training data uses" is computed against the spellings found in the current 26,416 training pairs.
Smaller versions
The same model in smaller downloads, stored in subfolders of this repo. Measured on the first 300 test sentences (greedy decoding):
| Version | Size | CER | Exact word match |
|---|---|---|---|
| Full precision (this repo's root) | 1,143 MB | 0.1179 | 55.9% |
fp16/ (works on any GPU) | 572 MB | 0.1177 | 55.9% |
nf4/ (4-bit, needs bitsandbytes and a GPU) | 441 MB | 0.1224 | 55.5% |
repo = "nasrellahkharroubi/DarijaDZ_Transliteration_ByT5"
# fp16
model = T5ForConditionalGeneration.from_pretrained(repo, subfolder="fp16", dtype=torch.float16).to("cuda").eval()
# 4-bit NF4
model = T5ForConditionalGeneration.from_pretrained(repo, subfolder="nf4", device_map={"": 0}).eval()