Ismail Ben Alla

Bilingual Net

English ↔ Darija Translation

Generative AI · Apr 2025 · Completed

A from-scratch neural translator between English and Moroccan Darija.

Built withPyTorchHugging Face TransformersStreamlitPoetry

Bilingual Net — overview

Overview

Moroccan Darija is spoken by millions of people but has very little parallel text, so most translation tools handle it poorly. I wanted to know whether a Transformer I wrote myself, end to end, could learn usable translations from a small English–Darija corpus.

I built BIALinguaNet in PyTorch: a full encoder–decoder Transformer with residual pre-LayerNorm attention blocks, a label-smoothed loss, and a decoder that supports temperature, top-k, top-p, beam search, and length normalization. I trained one checkpoint per direction (Darija to English and English to Darija) and wrapped both in a Streamlit app that shows ranked translation hypotheses.

Writing every piece myself — masking, batching, decoding — showed me how much of the final output quality comes from decoding choices rather than the network alone, especially on a low-resource language.

The question

Can a from-scratch seq2seq Transformer translate between English and Moroccan Darija, a low-resource dialect, with usable quality?

Key features

How it works

  1. Tokenize and batch

    Sentence pairs are tokenized with a GPT-2 tokenizer extended with pad, start, and end tokens. Batches are grouped by sequence length under a fixed token budget so little compute is wasted on padding.

  2. Encode the source

    The encoder runs pre-LayerNorm residual self-attention and feed-forward blocks over the source sentence, masking out padding positions.

  3. Decode with cross-attention

    The decoder applies causally masked self-attention, then cross-attention to the encoder output. Its token embeddings and output projection share weights with the encoder embedding.

  4. Train with label smoothing

    Training uses a label-smoothed cross-entropy loss, Adam with a warmup learning-rate schedule, and gradient clipping.

  5. Search for the best translation

    At inference, beam search combined with top-k/top-p sampling and length normalization produces ranked hypotheses, and the best one is shown first.

Examples

Demo video of the translation app. (watch on YouTube)
Darija to English: the model's translations of 20 everyday Darija sentences (beam size 4, top-k 50, top-p 0.95).
Darija to English: the model's translations of 20 everyday Darija sentences (beam size 4, top-k 50, top-p 0.95).
English to Darija: short English phrases on the left and the model's Darija output on the right. The print labels are reused from the other direction.
English to Darija: short English phrases on the left and the model's Darija output on the right. The print labels are reused from the other direction.

Input

bghit nji lmaghrib

Output

i want to come to morocco

One Darija-to-English pair taken from the repository's translation screenshot.

Tech stack

What I learned

Limitations