Finetuned from
oddadmix/50M-2048-Emhotob,
a tiny Arabic base model trained from scratch.
Scores are much higher than the dialectal (Egyptian) siblings of this model family:
MSA is highly standardized, so a single reference captures most of the valid output
space in both directions. The saved weights are the best checkpoint by validation
loss (eval_loss=0.4945, epoch 2 of 3).
A larger set of 20 examples per direction (with references) is in
eval_bidirectional.json.
ChatML format. Pick the system prompt for the direction you want:
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_id = "oddadmix/50M-English-MSA-v1"
5tok = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16).to("cuda").eval()
7
8SYS_TO_MSA = "أنت مترجم محترف. ترجم النص الإنجليزي إلى اللغة العربية الفصحى."
9SYS_TO_EN = "You are a professional translator. Translate the Modern Standard Arabic text into English."
10
11def translate(text: str, system: str) -> str:
12 prompt = (
13 f"<|im_start|>system\n{system}<|im_end|>\n"
14 f"<|im_start|>user\n{text.strip()}<|im_end|>\n"
15 f"<|im_start|>assistant\n"
16 )
17 ids = tok(prompt, return_tensors="pt", add_special_tokens=False).to(model.device)
18 if tok.bos_token_id is not None: # training prepends BOS
19 bos = torch.tensor([[tok.bos_token_id]], device=model.device)
20 ids["input_ids"] = torch.cat([bos, ids["input_ids"]], dim=1)
21 ids["attention_mask"] = torch.cat([torch.ones_like(bos), ids["attention_mask"]], dim=1)
22 out = model.generate(**ids, max_new_tokens=256, do_sample=False,
23 eos_token_id=tok.eos_token_id, pad_token_id=tok.pad_token_id)
24 return tok.decode(out[0, ids["input_ids"].size(1):], skip_special_tokens=True).strip()
25
26print(translate("Thank you very much, you are so kind.", SYS_TO_MSA))
27# → شكرًا جزيلًا لك، أنت لطيف جدًا.
28print(translate("أنا لست خائفًا من أي شيء حقًا.", SYS_TO_EN))
29# → I'm not really afraid of anything.
Apache-2.0, inherited from the base model.