Skip to content

Small CLI tool that translates between Japanese and English using the LiquidAI `LFM2-350M-ENJP-MT` model via Hugging Face Transformers.

License

Notifications You must be signed in to change notification settings

uchkw/lfm2-translate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LFM2-350M EN↔JP Translator

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.

Installation

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

Requirements

  • Python 3.11+
  • Packages:
    • transformers
    • torch
    • sentencepiece

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.

Files

  • src/lfm2_translate/: Installable package (CLI + core)
  • requirements.txt: Python dependencies
  • pyproject.toml: Packaging configuration (src layout)
  • tests/: Minimal tests

Usage

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)

How it works

  • 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.

Model

Troubleshooting

  • 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_template is 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-code to enable it. Only enable this for repositories you trust.

License

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.

About

Small CLI tool that translates between Japanese and English using the LiquidAI `LFM2-350M-ENJP-MT` model via Hugging Face Transformers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages