Type something to search...
LibreTranslate

LibreTranslate

LibreTranslate

6.7k 635
01 May, 2024
  Python

What is LibreTranslate ?

LibreTranslate is a Free and Open Source Machine Translation API, entirely self-hosted. Unlike other APIs, it doesn’t rely on proprietary providers such as Google or Azure to perform translations. Instead, its translation engine is powered by the open source Argos Translate library.


LibreTranslate Demo

Try it online! | API Docs | Community Forum

image


API Examples

Simple

Request:

const res = await fetch("https://libretranslate.com/translate", {
method: "POST",
body: JSON.stringify({
q: "Hello!",
source: "en",
target: "es"
}),
headers: { "Content-Type": "application/json" }
});
console.log(await res.json());

Response:

{
"translatedText": "¡Hola!"
}

Auto Detect Language

Request:

const res = await fetch("https://libretranslate.com/translate", {
method: "POST",
body: JSON.stringify({
q: "Ciao!",
source: "auto",
target: "en"
}),
headers: { "Content-Type": "application/json" }
});
console.log(await res.json());

Response:

{
"detectedLanguage": {
"confidence": 83,
"language": "it"
},
"translatedText": "Bye!"
}

HTML (beta)

Request:

const res = await fetch("https://libretranslate.com/translate", {
method: "POST",
body: JSON.stringify({
q: '<p class="green">Hello!</p>',
source: "en",
target: "es",
format: "html"
}),
headers: { "Content-Type": "application/json" }
});
console.log(await res.json());

Response:

{
"translatedText": "<p class=\"green\">¡Hola!</p>"
}

Install and Run

You can run your own API server with just a few lines of setup!

Make sure you have Python installed (3.8 or higher is recommended), then simply run:

Terminal window
pip install libretranslate
libretranslate [args]

Then open a web browser to http://localhost:5000

On Ubuntu 20.04 you can also use the install script available at https://github.com/argosopentech/LibreTranslate-init


Run with Docker

You can also run the application with docker:

Linux/macOS

Terminal window
./run.sh [args]

Windows

Terminal window
run.bat [args]

Build and Run

See CONTIRBUTING.md for information on how to build and run the project yourself.

CUDA

You can use hardware acceleration to speed up translations on a GPU machine with CUDA 11.2 and nvidia-docker installed.

Run this version with:

Terminal window
docker compose -f docker-compose.cuda.yml up -d --build

Arguments

Arguments passed to the process or set via environment variables are split into two kinds.

  • Settings or runtime flags used to toggle specific runmodes or disable parts of the application. These act as toggle when added or removed.

  • Configuration parameters to set various limits and configure the application. These require a parameter to be passed to function, if removed the default parameters are used.

Settings / Flags

ArgumentDescriptionDefault SettingEnv. name
—debugEnable debug environmentDisabledLT_DEBUG
—sslWhether to enable SSLDisabledLT_SSL
—api-keysEnable API keys database for per-client rate limits when —req-limit is reachedDon't use API keysLT_API_KEYS
—require-api-key-originRequire use of an API key for programmatic access to the API, unless the request origin matches this domainNo restrictions on domain originLT_REQUIRE_API_KEY_ORIGIN
—require-api-key-secretRequire use of an API key for programmatic access to the API, unless the client also sends a secret matchNo secrets requiredLT_REQUIRE_API_KEY_SECRET
—suggestionsAllow user suggestionsDisabledLT_SUGGESTIONS
—disable-files-translationDisable files translationFile translation allowedLT_DISABLE_FILES_TRANSLATION
—disable-web-uiDisable web uiWeb Ui enabledLT_DISABLE_WEB_UI
—update-modelsUpdate language models at startupOnly on if no models foundLT_UPDATE_MODELS
—metricsEnable the /metrics endpoint for exporting Prometheus usage metricsDisabledLT_METRICS

Configuration Parameters

ArgumentDescriptionDefault ParameterEnv. name
—hostSet host to bind the server to127.0.0.1LT_HOST
—portSet port to bind the server to5000LT_PORT
—char-limitSet character limitNo limitLT_CHAR_LIMIT
—req-limitSet maximum number of requests per minute per client (outside of limits set by api keys)No limitLT_REQ_LIMIT
—req-limit-storageStorage URI to use for request limit data storage. See Flask Limitermemory://LT_REQ_LIMIT_STORAGE
—batch-limitSet maximum number of texts to translate in a batch requestNo limitLT_BATCH_LIMIT
—ga-idEnable Google Analytics on the API client page by providing an IDEmpty (no tracking)LT_GA_ID
—frontend-language-sourceSet frontend default language - sourceautoLT_FRONTEND_LANGUAGE_SOURCE
—frontend-language-targetSet frontend default language - targetlocale (match site’s locale)LT_FRONTEND_LANGUAGE_TARGET
—frontend-timeoutSet frontend translation timeout500LT_FRONTEND_TIMEOUT
—api-keys-db-pathUse a specific path inside the container for the local database. Can be absolute or relativedb/api_keys.dbLT_API_KEYS_DB_PATH
—api-keys-remoteUse this remote endpoint to query for valid API keys instead of using the local databaseEmpty (use local db instead)LT_API_KEYS_REMOTE
—get-api-key-linkShow a link in the UI where to direct users to get an API keyEmpty (no link shown on web ui)LT_GET_API_KEY_LINK
—shared-storageShared storage URI to use for multi-process data sharing (e.g. when using gunicorn)memory://LT_SHARED_STORAGE
—load-onlySet available languagesEmpty (use all from argostranslate)LT_LOAD_ONLY
—threadsSet number of threads4LT_THREADS
—metrics-auth-tokenProtect the /metrics endpoint by allowing only clients that have a valid Authorization Bearer tokenEmpty (no auth required)LT_METRICS_AUTH_TOKEN
—url-prefixAdd prefix to URL: example.com:5000/url-prefix//LT_URL_PREFIX

Notes:

  • Each argument has an equivalent environment variable that can be used instead. The env. variables overwrite the default values but have lower priority than the command arguments and are particularly useful if used with Docker. The environment variable names are the upper-snake-case of the equivalent command argument’s name with a LT prefix.

  • To configure requirement for api key to use, set --req-limit to 0 and add the --api-keys flag. Requests made without a proper api key will be rejected.

  • Setting --update-models will update models regardless of whether updates are available or not.


Update

Software

If you installed with pip:

pip install -U libretranslate

If you’re using docker:

docker pull libretranslate/libretranslate

Language Models

Start the program with the --update-models argument. For example: libretranslate --update-models or ./run.sh --update-models.

Alternatively you can also run the scripts/install_models.py script.


Run with WSGI and Gunicorn

Terminal window
pip install gunicorn
gunicorn --bind 0.0.0.0:5000 'wsgi:app'

You can pass application arguments directly to Gunicorn via:

Terminal window
gunicorn --bind 0.0.0.0:5000 'wsgi:app(api_keys=True)'

Kubernetes Deployment

See Medium article by JM Robles and the improved k8s.yaml by @rasos.

Helm Chart

Based on @rasos work you can now install LibreTranslate on Kubernetes using Helm.

A Helm chart is now available in the helm-chart repository where you can find more details.

You can quickly install LibreTranslate on Kubernetes using Helm with the following command:

Terminal window
helm repo add libretranslate https://libretranslate.github.io/helm-chart/
helm repo update
helm search repo libretranslate
helm install libretranslate libretranslate/libretranslate --namespace libretranslate --create-namespace

Manage API Keys

LibreTranslate supports per-user limit quotas, e.g. you can issue API keys to users so that they can enjoy higher requests limits per minute (if you also set --req-limit). By default all users are rate-limited based on --req-limit, but passing an optional api_key parameter to the REST endpoints allows a user to enjoy higher request limits. You can also specify different character limits that bypass the default --char-limit value on a per-key basis.

To use API keys simply start LibreTranslate with the --api-keys option. If you modified the API keys database path with the option --api-keys-db-path, you must specify the path with the same argument flag when using the ltmanage keys command.

Add New Keys

To issue a new API key with 120 requests per minute limits:

Terminal window
ltmanage keys add 120

To issue a new API key with 120 requests per minute and a maximum of 5,000 characters per request:

Terminal window
ltmanage keys add 120 --char-limit 5000

If you changed the API keys database path:

Terminal window
ltmanage keys --api-keys-db-path path/to/db/dbName.db add 120

Remove Keys

Terminal window
ltmanage keys remove <api-key>

View Keys

Terminal window
ltmanage keys

Prometheus Metrics

LibreTranslate has Prometheus exporter capabilities when you pass the --metrics argument at startup (disabled by default). When metrics are enabled, a /metrics endpoint is mounted on the instance:

http://localhost:5000/metrics

# HELP libretranslate_http_requests_in_flight Multiprocess metric
# TYPE libretranslate_http_requests_in_flight gauge
libretranslate_http_requests_in_flight{api_key="",endpoint="/translate",request_ip="127.0.0.1"} 0.0
# HELP libretranslate_http_request_duration_seconds Multiprocess metric
# TYPE libretranslate_http_request_duration_seconds summary
libretranslate_http_request_duration_seconds_count{api_key="",endpoint="/translate",request_ip="127.0.0.1",status="200"} 0.0
libretranslate_http_request_duration_seconds_sum{api_key="",endpoint="/translate",request_ip="127.0.0.1",status="200"} 0.0

You can then configure prometheus.yml to read the metrics:

scrape_configs:
- job_name: "libretranslate"
# Needed only if you use --metrics-auth-token
#authorization:
#credentials: "mytoken"
static_configs:
- targets: ["localhost:5000"]

To secure the /metrics endpoint you can also use --metrics-auth-token mytoken.

If you use Gunicorn, make sure to create a directory for storing multiprocess data metrics and set PROMETHEUS_MULTIPROC_DIR:

Terminal window
mkdir -p /tmp/prometheus_data
rm /tmp/prometheus_data/*
export PROMETHEUS_MULTIPROC_DIR=/tmp/prometheus_data
gunicorn -c scripts/gunicorn_conf.py --bind 0.0.0.0:5000 'wsgi:app(metrics=True)'

Language Bindings

You can use the LibreTranslate API using the following bindings:


Discourse Plugin

You can use the official discourse translator plugin to translate Discourse topics with LibreTranslate. To install it simply modify /var/discourse/containers/app.yml:

## Plugins go here
## see https://meta.discourse.org/t/19157 for details
hooks:
after_code:
- exec:
cd: $home/plugins
cmd:
- git clone https://github.com/discourse/docker_manager.git
- git clone https://github.com/discourse/discourse-translator
...

Then issue ./launcher rebuild app. From the Discourse’s admin panel then select “LibreTranslate” as a translation provider and set the relevant endpoint configurations.

See it in action on this page.


Mobile Apps


Web browser