From 10f82d9a3e236e8cf6432c970a86e5614ab6578f Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Tue, 20 Dec 2022 10:33:15 -0500 Subject: [PATCH 1/8] lt.sh helper script --- Dockerfile | 2 +- README.md | 7 ++-- lt.sh | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 6 deletions(-) create mode 100755 lt.sh diff --git a/Dockerfile b/Dockerfile index a51883d..0c8034b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,7 @@ FROM python:3.8.14-slim-bullseye ARG with_models=false ARG models= -RUN addgroup --system --gid 1032 libretranslate && adduser --system --uid 1032 libretranslate +RUN addgroup --system --gid 1032 libretranslate && adduser --system --uid 1032 libretranslate && mkdir -p /home/libretranslate/.local && chown -R libretranslate:libretranslate /home/libretranslate/.local USER libretranslate COPY --from=builder --chown=1032:1032 /app /app diff --git a/README.md b/README.md index 884e641..eae4215 100644 --- a/README.md +++ b/README.md @@ -130,11 +130,8 @@ Then open a web browser to http://localhost:5000 ### Run with Docker -Simply run: - -```bash -docker run -ti --rm -p 5000:5000 libretranslate/libretranslate -``` +Linux/MacOS: `./lt.sh` +Windows: double-click `lt.bat` Then open a web browser to http://localhost:5000 diff --git a/lt.sh b/lt.sh new file mode 100755 index 0000000..96526e4 --- /dev/null +++ b/lt.sh @@ -0,0 +1,95 @@ +#!/bin/bash +set -eo pipefail +__dirname=$(cd "$(dirname "$0")"; pwd -P) +cd "${__dirname}" + +platform="Linux" # Assumed +uname=$(uname) +case $uname in + "Darwin") + platform="MacOS / OSX" + ;; + MINGW*) + platform="Windows" + ;; +esac + +if [[ $platform = "Windows" ]]; then + export COMPOSE_CONVERT_WINDOWS_PATHS=1 +fi + +# define realpath replacement function +if [[ $platform = "MacOS / OSX" ]]; then + realpath() { + [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}" + } +fi + +usage(){ + echo "Usage: $0 [--port N]" + echo + echo "Run LibreTranslate using docker." + echo + exit +} + +export LT_PORT=5000 + +# Parse args for overrides +ARGS=() +while [[ $# -gt 0 ]] +do +key="$1" +case $key in + --port) + export LT_PORT="$2" + ARGS+=("$1") + ARGS+=("$2") # save it in an array for later + shift # past argument + shift # past value + ;; + --debug) + export LT_DEBUG=YES + ARGS+=("$1") + shift # past argument + ;; + --help) + usage + ;; + *) # unknown option + ARGS+=("$1") + shift # past argument + ;; +esac +done + +# $1 = command | $2 = help_text | $3 = install_command (optional) +check_command(){ + hash "$1" 2>/dev/null || not_found=true + if [[ $not_found ]]; then + check_msg_prefix="Checking for $1... " + + # Can we attempt to install it? + if [[ -n "$3" ]]; then + echo -e "$check_msg_prefix \033[93mnot found, we'll attempt to install\033[39m" + $3 || sudo $3 + + # Recurse, but don't pass the install command + check_command "$1" "$2" + else + check_msg_result="\033[91m can't find $1! Check that the program is installed and that you have added the proper path to the program to your PATH environment variable before launching WebODM. If you change your PATH environment variable, remember to close and reopen your terminal. $2\033[39m" + fi + fi + + echo -e "$check_msg_prefix $check_msg_result" + if [[ $not_found ]]; then + return 1 + fi +} + +environment_check(){ + check_command "docker" "https://www.docker.com/" +} + +environment_check +docker run -ti --rm --entrypoint bash -p $LT_PORT:$LT_PORT -v lt-share:/home/libretranslate/.local/share libretranslate/libretranslate #${ARGS[@]} \ No newline at end of file From 528569720341bd7f186f5ebf6fc9e340f4ee858f Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Tue, 20 Dec 2022 10:36:31 -0500 Subject: [PATCH 2/8] Add db --- README.md | 4 ++-- db/.gitignore | 0 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 db/.gitignore diff --git a/README.md b/README.md index eae4215..b63f1f3 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ Then open a web browser to http://localhost:5000 ### Build with Docker ```bash -docker build [--build-arg with_models=true] -t libretranslate . +docker build [--build-arg with_models=true] -t libretranslate/libretranslate . ``` If you want to run the Docker image in a complete offline environment, you need to add the `--build-arg with_models=true` parameter. Then the language models are downloaded during the build process of the image. Otherwise these models get downloaded on the first run of the image/container. @@ -146,7 +146,7 @@ If you want to run the Docker image in a complete offline environment, you need Run the built image: ```bash -docker run -it -p 5000:5000 libretranslate [args] +./lt.sh [args] ``` Or build and run using `docker-compose`: diff --git a/db/.gitignore b/db/.gitignore new file mode 100644 index 0000000..e69de29 From 959f4638cc9bbccf70764e78e5364a7aaf7d7b00 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Tue, 20 Dec 2022 11:13:56 -0500 Subject: [PATCH 3/8] Named volume for db, --update-models arg --- README.md | 5 +++-- app/app.py | 2 +- app/default_values.py | 5 +++++ app/init.py | 4 ++-- app/main.py | 4 +++- lt.sh | 18 ++++++------------ 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b63f1f3..852b265 100644 --- a/README.md +++ b/README.md @@ -130,8 +130,8 @@ Then open a web browser to http://localhost:5000 ### Run with Docker -Linux/MacOS: `./lt.sh` -Windows: double-click `lt.bat` +Linux/MacOS: `./lt.sh [args]` +Windows: `lt.bat [args]` Then open a web browser to http://localhost:5000 @@ -195,6 +195,7 @@ docker-compose -f docker-compose.cuda.yml up -d --build | --suggestions | Allow user suggestions | `false` | LT_SUGGESTIONS | | --disable-files-translation | Disable files translation | `false` | LT_DISABLE_FILES_TRANSLATION | | --disable-web-ui | Disable web ui | `false` | LT_DISABLE_WEB_UI | +| --update-models | Update models at startup | `false` | LT_UPDATE_MODELS | Note that 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. diff --git a/app/app.py b/app/app.py index 9976ac0..f4c5019 100644 --- a/app/app.py +++ b/app/app.py @@ -100,7 +100,7 @@ def get_routes_limits(default_req_limit, daily_req_limit, api_keys_db): def create_app(args): from app.init import boot - boot(args.load_only) + boot(args.load_only, args.update_models) from app.language import load_languages diff --git a/app/default_values.py b/app/default_values.py index 343f22c..263d4ad 100644 --- a/app/default_values.py +++ b/app/default_values.py @@ -156,6 +156,11 @@ _default_options_objects = [ 'default_value': False, 'value_type': 'bool' }, + { + 'name': 'UPDATE_MODELS', + 'default_value': False, + 'value_type': 'bool' + }, ] diff --git a/app/init.py b/app/init.py index 4b081ac..d0c4de4 100644 --- a/app/init.py +++ b/app/init.py @@ -5,9 +5,9 @@ from argostranslate import package, translate import app.language -def boot(load_only=None): +def boot(load_only=None, update_models=False): try: - check_and_install_models(load_only_lang_codes=load_only) + check_and_install_models(force=update_models, load_only_lang_codes=load_only) except Exception as e: print("Cannot update models (normal if you're offline): %s" % str(e)) diff --git a/app/main.py b/app/main.py index 6e24158..52aa06e 100644 --- a/app/main.py +++ b/app/main.py @@ -144,7 +144,9 @@ def get_args(): parser.add_argument( "--disable-web-ui", default=DEFARGS['DISABLE_WEB_UI'], action="store_true", help="Disable web ui" ) - + parser.add_argument( + "--update-models", default=DEFARGS['UPDATE_MODELS'], action="store_true", help="Update language models at startup" + ) return parser.parse_args() diff --git a/lt.sh b/lt.sh index 96526e4..ee43d30 100755 --- a/lt.sh +++ b/lt.sh @@ -14,17 +14,6 @@ case $uname in ;; esac -if [[ $platform = "Windows" ]]; then - export COMPOSE_CONVERT_WINDOWS_PATHS=1 -fi - -# define realpath replacement function -if [[ $platform = "MacOS / OSX" ]]; then - realpath() { - [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}" - } -fi - usage(){ echo "Usage: $0 [--port N]" echo @@ -53,6 +42,11 @@ case $key in ARGS+=("$1") shift # past argument ;; + --api-keys) + export DB_VOLUME="-v lt-db:/app/db" + ARGS+=("$1") + shift # past argument + ;; --help) usage ;; @@ -92,4 +86,4 @@ environment_check(){ } environment_check -docker run -ti --rm --entrypoint bash -p $LT_PORT:$LT_PORT -v lt-share:/home/libretranslate/.local/share libretranslate/libretranslate #${ARGS[@]} \ No newline at end of file +docker run -ti --rm -p $LT_PORT:$LT_PORT $DB_VOLUME -v lt-local:/home/libretranslate/.local libretranslate/libretranslate ${ARGS[@]} \ No newline at end of file From f2d6800f4c080e84d1f83bdacf86138772301dac Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Tue, 20 Dec 2022 11:17:34 -0500 Subject: [PATCH 4/8] Readme tweaks --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 852b265..997b6e0 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ Then open a web browser to http://localhost:5000 ### Build with Docker ```bash -docker build [--build-arg with_models=true] -t libretranslate/libretranslate . +docker build [--build-arg with_models=true] -t libretranslate . ``` If you want to run the Docker image in a complete offline environment, you need to add the `--build-arg with_models=true` parameter. Then the language models are downloaded during the build process of the image. Otherwise these models get downloaded on the first run of the image/container. @@ -146,7 +146,7 @@ If you want to run the Docker image in a complete offline environment, you need Run the built image: ```bash -./lt.sh [args] +docker run -it -p 5000:5000 libretranslate [args] ``` Or build and run using `docker-compose`: @@ -157,7 +157,7 @@ docker-compose up -d --build > Feel free to change the [`docker-compose.yml`](https://github.com/LibreTranslate/LibreTranslate/blob/main/docker-compose.yml) file to adapt it to your deployment needs, or use an extra `docker-compose.prod.yml` file for your deployment configuration. -> The models are stored inside the container under `/root/.local/share` and `/root/.local/cache`. Feel free to use volumes if you do not want to redownload the models when the container is destroyed. Be aware that this will prevent the models from being updated! +> The models are stored inside the container under `/home/libretranslate/.local/share` and `/home/libretranslate/.local/cache`. Feel free to use volumes if you do not want to redownload the models when the container is destroyed. To update the models, use the `--update-models` argument. ### CUDA @@ -195,7 +195,7 @@ docker-compose -f docker-compose.cuda.yml up -d --build | --suggestions | Allow user suggestions | `false` | LT_SUGGESTIONS | | --disable-files-translation | Disable files translation | `false` | LT_DISABLE_FILES_TRANSLATION | | --disable-web-ui | Disable web ui | `false` | LT_DISABLE_WEB_UI | -| --update-models | Update models at startup | `false` | LT_UPDATE_MODELS | +| --update-models | Update language models at startup | `false` | LT_UPDATE_MODELS | Note that 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. From b407ffee3caaab95f73c3159357101f3631c6cd5 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Tue, 20 Dec 2022 11:29:51 -0500 Subject: [PATCH 5/8] lt.sh --> run.sh --- README.md | 4 +-- lt.sh | 89 ------------------------------------------------------- 2 files changed, 2 insertions(+), 91 deletions(-) delete mode 100755 lt.sh diff --git a/README.md b/README.md index 997b6e0..f349a50 100644 --- a/README.md +++ b/README.md @@ -130,8 +130,8 @@ Then open a web browser to http://localhost:5000 ### Run with Docker -Linux/MacOS: `./lt.sh [args]` -Windows: `lt.bat [args]` +Linux/MacOS: `./run.sh [args]` +Windows: `run.bat [args]` Then open a web browser to http://localhost:5000 diff --git a/lt.sh b/lt.sh deleted file mode 100755 index ee43d30..0000000 --- a/lt.sh +++ /dev/null @@ -1,89 +0,0 @@ -#!/bin/bash -set -eo pipefail -__dirname=$(cd "$(dirname "$0")"; pwd -P) -cd "${__dirname}" - -platform="Linux" # Assumed -uname=$(uname) -case $uname in - "Darwin") - platform="MacOS / OSX" - ;; - MINGW*) - platform="Windows" - ;; -esac - -usage(){ - echo "Usage: $0 [--port N]" - echo - echo "Run LibreTranslate using docker." - echo - exit -} - -export LT_PORT=5000 - -# Parse args for overrides -ARGS=() -while [[ $# -gt 0 ]] -do -key="$1" -case $key in - --port) - export LT_PORT="$2" - ARGS+=("$1") - ARGS+=("$2") # save it in an array for later - shift # past argument - shift # past value - ;; - --debug) - export LT_DEBUG=YES - ARGS+=("$1") - shift # past argument - ;; - --api-keys) - export DB_VOLUME="-v lt-db:/app/db" - ARGS+=("$1") - shift # past argument - ;; - --help) - usage - ;; - *) # unknown option - ARGS+=("$1") - shift # past argument - ;; -esac -done - -# $1 = command | $2 = help_text | $3 = install_command (optional) -check_command(){ - hash "$1" 2>/dev/null || not_found=true - if [[ $not_found ]]; then - check_msg_prefix="Checking for $1... " - - # Can we attempt to install it? - if [[ -n "$3" ]]; then - echo -e "$check_msg_prefix \033[93mnot found, we'll attempt to install\033[39m" - $3 || sudo $3 - - # Recurse, but don't pass the install command - check_command "$1" "$2" - else - check_msg_result="\033[91m can't find $1! Check that the program is installed and that you have added the proper path to the program to your PATH environment variable before launching WebODM. If you change your PATH environment variable, remember to close and reopen your terminal. $2\033[39m" - fi - fi - - echo -e "$check_msg_prefix $check_msg_result" - if [[ $not_found ]]; then - return 1 - fi -} - -environment_check(){ - check_command "docker" "https://www.docker.com/" -} - -environment_check -docker run -ti --rm -p $LT_PORT:$LT_PORT $DB_VOLUME -v lt-local:/home/libretranslate/.local libretranslate/libretranslate ${ARGS[@]} \ No newline at end of file From 72aaa41d8f06fb77b6f5ab67bcc1d46a03d9bfde Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Tue, 20 Dec 2022 11:30:14 -0500 Subject: [PATCH 6/8] Add run.sh --- run.sh | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 run.sh diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..ee43d30 --- /dev/null +++ b/run.sh @@ -0,0 +1,89 @@ +#!/bin/bash +set -eo pipefail +__dirname=$(cd "$(dirname "$0")"; pwd -P) +cd "${__dirname}" + +platform="Linux" # Assumed +uname=$(uname) +case $uname in + "Darwin") + platform="MacOS / OSX" + ;; + MINGW*) + platform="Windows" + ;; +esac + +usage(){ + echo "Usage: $0 [--port N]" + echo + echo "Run LibreTranslate using docker." + echo + exit +} + +export LT_PORT=5000 + +# Parse args for overrides +ARGS=() +while [[ $# -gt 0 ]] +do +key="$1" +case $key in + --port) + export LT_PORT="$2" + ARGS+=("$1") + ARGS+=("$2") # save it in an array for later + shift # past argument + shift # past value + ;; + --debug) + export LT_DEBUG=YES + ARGS+=("$1") + shift # past argument + ;; + --api-keys) + export DB_VOLUME="-v lt-db:/app/db" + ARGS+=("$1") + shift # past argument + ;; + --help) + usage + ;; + *) # unknown option + ARGS+=("$1") + shift # past argument + ;; +esac +done + +# $1 = command | $2 = help_text | $3 = install_command (optional) +check_command(){ + hash "$1" 2>/dev/null || not_found=true + if [[ $not_found ]]; then + check_msg_prefix="Checking for $1... " + + # Can we attempt to install it? + if [[ -n "$3" ]]; then + echo -e "$check_msg_prefix \033[93mnot found, we'll attempt to install\033[39m" + $3 || sudo $3 + + # Recurse, but don't pass the install command + check_command "$1" "$2" + else + check_msg_result="\033[91m can't find $1! Check that the program is installed and that you have added the proper path to the program to your PATH environment variable before launching WebODM. If you change your PATH environment variable, remember to close and reopen your terminal. $2\033[39m" + fi + fi + + echo -e "$check_msg_prefix $check_msg_result" + if [[ $not_found ]]; then + return 1 + fi +} + +environment_check(){ + check_command "docker" "https://www.docker.com/" +} + +environment_check +docker run -ti --rm -p $LT_PORT:$LT_PORT $DB_VOLUME -v lt-local:/home/libretranslate/.local libretranslate/libretranslate ${ARGS[@]} \ No newline at end of file From 91ae57ad6cf796ac6c5412910c5d1a8502752d10 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Tue, 20 Dec 2022 11:48:03 -0500 Subject: [PATCH 7/8] Add run.bat --- run.bat | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 run.bat diff --git a/run.bat b/run.bat new file mode 100644 index 0000000..d83c78b --- /dev/null +++ b/run.bat @@ -0,0 +1,41 @@ +@ECHO OFF + +SETLOCAL + +SET LT_PORT=5000 + +:loop +IF NOT "%1"=="" ( + IF "%1"=="--port" ( + SET LT_PORT=%2 + SHIFT + ) + IF "%1"=="--help" ( + echo Usage: run.bat [--port N] + echo: + echo Run LibreTranslate using docker. + echo: + GOTO :done + ) + IF "%1"=="--api-keys" ( + SET DB_VOLUME=-v lt-db:/app/db + SHIFT + ) + SHIFT + GOTO :loop +) + +WHERE /Q docker +IF %ERRORLEVEL% NEQ 0 GOTO :install_docker + +docker run -ti --rm -p %LT_PORT%:%LT_PORT% %DB_VOLUME% -v lt-local:/home/libretranslate/.local libretranslate/libretranslate %* + +GOTO :done + +:install_docker +ECHO Cannot find docker! Go to https://docs.docker.com/desktop/install/windows-install/ and install docker before running this script (pressing Enter will open the page) +pause +start "" https://docs.docker.com/desktop/install/windows-install/ +GOTO :done + +:done \ No newline at end of file From 11324ab1f5c46e360f41c0d929f5f37e88d3d365 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Tue, 20 Dec 2022 11:48:25 -0500 Subject: [PATCH 8/8] Bump version --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 31e5c84..d0149fe 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.3 +1.3.4