Skip to main content

NLLB Serve

This project offers a web interface and REST API to Meta's No Language Left Behind (NLLB) models that can translate across 200 languages.

Setup

# Option 1: install as read only; recommended to use as is
pip install git+https://github.com/thammegowda/nllb-serve

# Option 2: install for editable mode; recommended if you'd like to modify code
git clone  https://github.com/thammegowda/nllb-serve
cd nllb-serve
pip install -e .


# Entry point: either one of these options should work
nllb-serve -h
python -m nllb_serve -h

Start server

# Either one of these should work
nllb-serve
# or
python -m nllb_serve

# Use CPU, ignore GPUs even if they exist
CUDA_VISIBLE_DEVICES= nllb-serve

# Use GPU device 0
CUDA_VISIBLE_DEVICES=0 nllb-serve

This starts a service on http://localhost:6060 by default.

Cuda and Torch Compatibility Issues

# check 1:
$ python -c 'import torch; print(torch.cuda.is_available())'
True

# Check 2: match the version of installed cudatookit with the version for which torch bins were compiled
# if exactly matching the versions is not possible/difficult, try getting the versions as close as possible
$ python -c 'import torch; print(torch.version.cuda)'
11.7
$  nvidia-smi  | grep -o 'CUDA.*'
CUDA Version: 12.0

CLI options:

$ nllb-serve -h
usage: nllb-serve [-h] [-d] [-p PORT] [-ho HOST] [-b BASE] [-mi MODEL_ID] [-msl MAX_SRC_LEN]

Deploy NLLB model to a RESTful server

optional arguments:
  -h, --help            show this help message and exit
  -d, --debug           Run Flask server in debug mode (default: False)
  -p PORT, --port PORT  port to run server on (default: 6060)
  -ho HOST, --host HOST
                        Host address to bind. (default: 0.0.0.0)
  -b BASE, --base BASE  Base prefix path for all the URLs. E.g., /v1 (default: None)
  -mi MODEL_ID, --model_id MODEL_ID
                        model ID; see https://huggingface.co/models?other=nllb (default: facebook/nllb-200-distilled-600M)
  -msl MAX_SRC_LEN, --max-src-len MAX_SRC_LEN
                        max source len; longer seqs will be truncated (default: 250)

REST API

  • /translate end point accepts GET and POST requests with the following args:
    • source -- source text. Can be a single string or a batch (i.e., list of strings)
    • src_lang -- source language ID, e.g., eng_Latn
    • tgt_lang -- target language ID, e.g., eng_Latn

HTTP Clients may send these parameters in three ways:

  1. Query parameters (GET)
    For example:

  2. URL encoded form (POST)

 curl --data "source=Comment allez-vous?" --data "source=Bonne journée" \
 --data "src_lang=fra_Latn" --data "tgt_lang=eng_Latn" \
  http://localhost:6060/translate
  1. JSON body (POST)
$ curl -H "Content-Type: application/json" -X POST \
    http://localhost:6060/translate \
   --data '{"source": ["Comment allez-vous?"], "src_lang": "fra_Latn", "tgt_lang": "kan_Knda"}'

List of language codes: https://huggingface.co/facebook/nllb-200-distilled-600M/blob/main/special_tokens_map.json

NLLB-Batch

This CLI tool is for decoding batches of data. While the REST API is optimized for translating single translation at once, nllb-batch is optmized for decoding large files.

$ nllb-batch  --help
INFO:root:torch device=cuda
usage: nllb-batch [-h] [-mi MODEL_ID] -sl SRC_LANG -tl TGT_LANG [-i INP] [-o OUT] [-msl MAX_SRC_CHARS] [-b BATCH_SIZE]

Serve NLLB model via command line

options:
  -h, --help            show this help message and exit
  -mi MODEL_ID, --model-id MODEL_ID
                        model ID; see https://huggingface.co/models?other=nllb (default: facebook/nllb-200-distilled-600M)
  -sl SRC_LANG, --src-lang SRC_LANG
                        source language identifier; eg: eng_Latn (default: None)
  -tl TGT_LANG, --tgt-lang TGT_LANG
                        Target language identifier; eg: eng_Latn (default: None)
  -i INP, --inp INP     Input file (default: <_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>)
  -o OUT, --out OUT     Output file (default: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)
  -msl MAX_SRC_CHARS, --max-src-chars MAX_SRC_CHARS
                        max source chars len; longer seqs will be truncated (default: 512)
  -b BATCH_SIZE, --batch-size BATCH_SIZE
                        Batch size; number of sentences (default: 10)

License

The code and model weights carry different licenses. The code in this repository is distributed via Apache License 2.0.
But the model weights are accessed from Huggingface Hub and the original license of model weigts are applicable. At the time of writing, authors of NLLB model distributed weights via CC-BY-NC-4.0 license. Read more at LICENSE.model.md and Creative Commons License

References

Metadata

Release files for nllb-serve 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nllb-serve 0.2.1
File Size Uploaded Provenance Actions
nllb-serve-0.2.1.tar.gz 1.2 MB Without provenance Details for nllb-serve-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nllb-serve 0.2.1
File Interpreter ABI Platform Actions
nllb_serve-0.2.1-py3-none-any.whl Python 3 none any Details for nllb_serve-0.2.1-py3-none-any.whl

Total release size: 2.4 MB

Release files / nllb-serve-0.2.1.tar.gz

Download URL nllb-serve-0.2.1.tar.gz
Size 1.2 MB
Tags Source
SHA-256 checksum
How to use checksums
64199f7b0fbf7e164ef6aa637be1f091639f61aa4a7fd2d9dcb71bf01dc216ba
BLAKE2b-256 checksum
How to use checksums
3af178da70fee468d1417ca29ef96c2ace64680e3da7f3aa7a8ed999ad4a658c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release files / nllb_serve-0.2.1-py3-none-any.whl

Download URL nllb_serve-0.2.1-py3-none-any.whl
Size 1.2 MB
Tags Python 3
SHA-256 checksum
How to use checksums
dbb564780ea1ec66b2a0f92964d1eb5d4d97cea9c759faa3fe6db89b3bb6ff9d
BLAKE2b-256 checksum
How to use checksums
2717722d1195c406899c4935fd074ca5b8a3e22537911e8137237783775a273e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Security for nllb-serve 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page