Small CLI tool that translates between Japanese and English using the LiquidAI LFM2-350M-ENJP-MT model via Hugging Face Transformers.
The script detects the input language (JA or EN) and translates to the other side. It uses the model’s recommended chat template with the required system prompt ("Translate to Japanese." or "Translate to English.") and greedy decoding.
Install as a package (provides the lfm2-translate command):
pip install .Or for development:
pip install -e .Then use the CLI:
lfm2-translate "こんにちは"
echo "hello" | lfm2-translate- Python 3.11+
- Packages:
transformerstorchsentencepiece
Install them with:
pip install -r requirements.txt
Notes:
- Depending on your hardware (CPU/GPU), you may need a specific PyTorch build from https://pytorch.org. The requirement here is generic (
torch>=2.1.0). - The first run will download the model from the Hugging Face Hub, which requires network access.
src/lfm2_translate/: Installable package (CLI + core)requirements.txt: Python dependenciespyproject.toml: Packaging configuration (src layout)tests/: Minimal tests
You can run the script either with a positional argument or via stdin piping. The script prints only the translation.
Examples (argument):
lfm2-translate "こんにちは"
# => Hello.
lfm2-translate "hello"
# => もしもし
Examples (stdin):
echo "こんにちは" | lfm2-translate
# => Hello.
echo "hello" | lfm2-translate
# => もしもしCLI options:
lfm2-translate --help
usage: lfm2-translate [-h] [--model MODEL] [--trust-remote-code] [text]
JA↔EN translator using LFM2-350M-ENJP-MT
positional arguments:
text Input text to translate (English or Japanese). If omitted, reads from stdin.
options:
-h, --help show this help message and exit
--model MODEL Hugging Face model id to use (default: LiquidAI/LFM2-350M-ENJP-MT)
--trust-remote-code Allow executing custom code from the model repository (default: disabled)- Detects Japanese by Unicode ranges (hiragana/katakana/kanji). JA→EN if detected, otherwise EN→JA.
- Applies the model’s ChatML-like template with the required system prompt.
- Uses greedy decoding (no sampling,
num_beams=1). - Cleans common prefixes like "Translation:" and bullets from the output and prints the first non-empty line.
- Hugging Face: https://huggingface.co/LiquidAI/LFM2-350M-ENJP-MT
- The model card recommends the system prompts above and greedy decoding. This script follows those recommendations.
- For better EN→JA outputs for very short inputs like "hello", adding punctuation (e.g., "Hello!"), or a slightly longer phrase may yield "こんにちは" instead of the telephone greeting "もしもし".
- If you need GPU acceleration, install a CUDA-enabled PyTorch from https://pytorch.org and ensure your drivers/toolkit are compatible.
- If
apply_chat_templateis unavailable in your transformers version, upgrade to a recent version (e.g.,pip install -U transformers). - Security note: By default, this CLI loads models with
trust_remote_code=False. If the model repository requires custom code, add--trust-remote-codeto enable it. Only enable this for repositories you trust.
This software is released under the MIT License. See LICENSE.
However, the model used by this software, "LFM2-350M-ENJP-MT," is subject to the LFM Open License v1.0 (Apache 2.0-based, includes a revenue-based limitation on commercial use).
Please refer to https://www.liquid.ai/lfm-license for the model’s usage terms.