Interactive English–Arabic translation in your terminal, powered by a locally stored M2M100 model.
A small command-line tool built around Facebook's M2M100 (418M) multilingual translation model, loaded locally through Hugging Face transformers. It presents a simple menu for English → Arabic and Arabic → English translation, and verifies with langdetect that the text you enter actually matches the chosen direction before translating. All inference happens locally — no API keys or network calls at runtime.
- Interactive console menu:
1English → Arabic,2Arabic → English,qto quit - Input-language verification with
langdetect(seeded withDetectorFactory.seed = 0for deterministic results); mismatched input is rejected and the detected language is shown - Fully local inference from
./models/m2m100— works offline once the model is downloaded - Reusable
translate(text, source_lang, target_lang)function that supports any M2M100 language pair (the interactive menu only wires upen↔ar)
- Python 3
- There is no
requirements.txt;transformersandlangdetectare imported directly, whiletorchandsentencepieceare required at runtime by the M2M100 model and tokenizer:
pip install transformers torch sentencepiece langdetectgit clone https://github.com/mkamranr/Multilingual-Translation.git
cd Multilingual-Translation
pip install transformers torch sentencepiece langdetectDownload the model from: https://huggingface.co/facebook/m2m100_418M
Place the model files under the models/m2m100 folder — the script loads both the tokenizer and the model from that exact path (from_pretrained('./models/m2m100')). One convenient way:
git lfs install
git clone https://huggingface.co/facebook/m2m100_418M models/m2m100Run from the repository root (the model path is relative):
python main.pyExample session:
Choose translation direction (1: English to Arabic, 2: Arabic to English, q: quit): 1
Enter English text: Hello, how are you?
Translation (English to Arabic): <Arabic translation>
Choose translation direction (1: English to Arabic, 2: Arabic to English, q: quit): q
If the entered text does not match the selected direction, the tool refuses to translate:
Enter English text: bonjour tout le monde
Input text is not in English. Detected language: fr
At startup, the script loads the M2M100 tokenizer and M2M100ForConditionalGeneration model from ./models/m2m100. It then loops over a text menu: after you pick a direction and enter text, langdetect (seeded for consistency) checks that the input is really English (choice 1) or Arabic (choice 2), and rejects it otherwise. For valid input, translate() sets tokenizer.src_lang to the source language, tokenizes the text (with padding and truncation), and calls model.generate() with forced_bos_token_id=tokenizer.get_lang_id(target_lang) to force output in the target language. The result is decoded with special tokens stripped and printed to the console.
main.py # Entry point: model loading, translate()/detect_language() helpers, interactive menu
models/m2m100/ # Not in the repo — place the downloaded M2M100 model here
LICENSE # GPL-3.0
- Only English ↔ Arabic is exposed in the menu, even though M2M100 (and the generic
translate()function) supports many more languages. - The model directory is not included in the repo and must be downloaded first; the ~418M-parameter checkpoint is roughly 2 GB on disk and is loaded fully at startup.
- The model path is relative (
./models/m2m100), so the script must be run from the repository root. - Inference runs on CPU — the script never moves the model to a GPU.
- Language detection can be unreliable for very short or ambiguous input, in which case the translation is refused even for valid text.
This project is licensed under the GNU General Public License v3.0 — see LICENSE.