2021-02-09 02:24:34 +02:00
import argparse
2021-03-28 16:19:19 +02:00
import operator
2022-02-20 10:09:02 +02:00
import sys
2021-05-18 05:51:33 +02:00
2022-12-30 01:44:53 +02:00
from libretranslate . app import create_app
from libretranslate . default_values import DEFAULT_ARGUMENTS as DEFARGS
2021-02-09 02:24:34 +02:00
2021-05-18 05:41:02 +02:00
2022-04-02 22:26:37 +02:00
def get_args ( ) :
2021-05-18 05:41:02 +02:00
parser = argparse . ArgumentParser (
description = " LibreTranslate - Free and Open Source Translation API "
)
parser . add_argument (
2021-07-12 22:43:06 +02:00
" --host " , type = str , help = " Hostname ( %(default)s ) " , default = DEFARGS [ ' HOST ' ]
2021-05-18 05:41:02 +02:00
)
2021-07-12 22:43:06 +02:00
parser . add_argument ( " --port " , type = int , help = " Port ( %(default)s ) " , default = DEFARGS [ ' PORT ' ] )
2021-05-18 05:41:02 +02:00
parser . add_argument (
" --char-limit " ,
2021-07-12 22:43:06 +02:00
default = DEFARGS [ ' CHAR_LIMIT ' ] ,
2021-05-18 05:41:02 +02:00
type = int ,
metavar = " <number of characters> " ,
help = " Set character limit ( %(default)s ) " ,
)
parser . add_argument (
" --req-limit " ,
2021-07-12 22:43:06 +02:00
default = DEFARGS [ ' REQ_LIMIT ' ] ,
2021-05-18 05:41:02 +02:00
type = int ,
metavar = " <number> " ,
help = " Set the default maximum number of requests per minute per client ( %(default)s ) " ,
)
2022-10-14 19:27:34 +02:00
parser . add_argument (
" --req-limit-storage " ,
default = DEFARGS [ ' REQ_LIMIT_STORAGE ' ] ,
type = str ,
metavar = " <Storage URI> " ,
help = " Storage URI to use for request limit data storage. See https://flask-limiter.readthedocs.io/en/stable/configuration.html. ( %(default)s ) " ,
)
2023-11-10 21:56:52 +02:00
parser . add_argument (
" --hourly-req-limit " ,
default = DEFARGS [ ' HOURLY_REQ_LIMIT ' ] ,
type = int ,
metavar = " <number> " ,
help = " Set the default maximum number of requests per hour per client, in addition to req-limit. ( %(default)s ) " ,
)
2023-11-16 22:43:47 +02:00
parser . add_argument (
" --hourly-req-limit-decay " ,
default = DEFARGS [ ' HOURLY_REQ_LIMIT_DECAY ' ] ,
type = int ,
metavar = " <number> " ,
help = " When used in combination with hourly-req-limit, adds additional hourly restrictions that logaritmically decrease for each additional hour. ( %(default)s ) " ,
)
2021-05-18 05:41:02 +02:00
parser . add_argument (
" --daily-req-limit " ,
2021-07-12 23:41:03 +02:00
default = DEFARGS [ ' DAILY_REQ_LIMIT ' ] ,
2021-05-18 05:41:02 +02:00
type = int ,
metavar = " <number> " ,
help = " Set the default maximum number of requests per day per client, in addition to req-limit. ( %(default)s ) " ,
)
parser . add_argument (
" --req-flood-threshold " ,
2021-07-12 22:43:06 +02:00
default = DEFARGS [ ' REQ_FLOOD_THRESHOLD ' ] ,
2021-05-18 05:41:02 +02:00
type = int ,
metavar = " <number> " ,
2021-11-24 19:49:07 +02:00
help = " Set the maximum number of request limit offences that a client can exceed before being banned. ( %(default)s ) " ,
2021-05-18 05:41:02 +02:00
)
2024-02-05 22:03:27 +02:00
parser . add_argument (
" --req-time-cost " ,
default = DEFARGS [ ' REQ_TIME_COST ' ] ,
type = int ,
metavar = " <number> " ,
help = " Considers a time cost (in seconds) for request limiting purposes. If a request takes 10 seconds and this value is set to 5, the request cost is either 2 or the actual request cost (whichever is greater). ( %(default)s ) " ,
)
2021-05-18 05:41:02 +02:00
parser . add_argument (
" --batch-limit " ,
2021-07-12 22:43:06 +02:00
default = DEFARGS [ ' BATCH_LIMIT ' ] ,
2021-05-18 05:41:02 +02:00
type = int ,
metavar = " <number of texts> " ,
help = " Set maximum number of texts to translate in a batch request ( %(default)s ) " ,
)
parser . add_argument (
" --ga-id " ,
type = str ,
2021-07-12 22:43:06 +02:00
default = DEFARGS [ ' GA_ID ' ] ,
2021-05-18 05:41:02 +02:00
metavar = " <GA ID> " ,
help = " Enable Google Analytics on the API client page by providing an ID ( %(default)s ) " ,
)
parser . add_argument (
2021-07-12 22:43:06 +02:00
" --debug " , default = DEFARGS [ ' DEBUG ' ] , action = " store_true " , help = " Enable debug environment "
2021-05-18 05:41:02 +02:00
)
parser . add_argument (
2021-07-12 22:43:06 +02:00
" --ssl " , default = DEFARGS [ ' SSL ' ] , action = " store_true " , help = " Whether to enable SSL "
2021-05-18 05:41:02 +02:00
)
parser . add_argument (
" --frontend-language-source " ,
type = str ,
2021-07-12 22:43:06 +02:00
default = DEFARGS [ ' FRONTEND_LANGUAGE_SOURCE ' ] ,
2021-05-18 05:41:02 +02:00
metavar = " <language code> " ,
help = " Set frontend default language - source ( %(default)s ) " ,
)
parser . add_argument (
" --frontend-language-target " ,
type = str ,
2021-07-12 22:43:06 +02:00
default = DEFARGS [ ' FRONTEND_LANGUAGE_TARGET ' ] ,
2021-05-18 05:41:02 +02:00
metavar = " <language code> " ,
help = " Set frontend default language - target ( %(default)s ) " ,
)
parser . add_argument (
" --frontend-timeout " ,
type = int ,
2021-07-12 22:43:06 +02:00
default = DEFARGS [ ' FRONTEND_TIMEOUT ' ] ,
2021-05-18 05:41:02 +02:00
metavar = " <milliseconds> " ,
help = " Set frontend translation timeout ( %(default)s ) " ,
)
parser . add_argument (
" --api-keys " ,
2021-07-12 22:43:06 +02:00
default = DEFARGS [ ' API_KEYS ' ] ,
2021-05-18 05:41:02 +02:00
action = " store_true " ,
help = " Enable API keys database for per-user rate limits lookup " ,
2022-07-15 13:22:04 +02:00
)
parser . add_argument (
" --api-keys-db-path " ,
default = DEFARGS [ ' API_KEYS_DB_PATH ' ] ,
type = str ,
help = " Use a specific path inside the container for the local database. Can be absolute or relative ( %(default)s ) " ,
2021-05-18 05:41:02 +02:00
)
2022-06-21 20:57:32 +02:00
parser . add_argument (
" --api-keys-remote " ,
default = DEFARGS [ ' API_KEYS_REMOTE ' ] ,
type = str ,
help = " Use this remote endpoint to query for valid API keys instead of using the local database " ,
)
2022-06-21 21:17:42 +02:00
parser . add_argument (
" --get-api-key-link " ,
default = DEFARGS [ ' GET_API_KEY_LINK ' ] ,
type = str ,
help = " Show a link in the UI where to direct users to get an API key " ,
)
2021-05-18 05:41:02 +02:00
parser . add_argument (
" --require-api-key-origin " ,
type = str ,
2021-07-12 22:43:06 +02:00
default = DEFARGS [ ' REQUIRE_API_KEY_ORIGIN ' ] ,
2021-05-18 05:41:02 +02:00
help = " Require use of an API key for programmatic access to the API, unless the request origin matches this domain " ,
)
2023-03-09 20:59:25 +02:00
parser . add_argument (
" --require-api-key-secret " ,
default = DEFARGS [ ' REQUIRE_API_KEY_SECRET ' ] ,
action = " store_true " ,
help = " Require use of an API key for programmatic access to the API, unless the client also sends a secret match " ,
)
2023-03-09 23:09:04 +02:00
parser . add_argument (
" --shared-storage " ,
type = str ,
default = DEFARGS [ ' SHARED_STORAGE ' ] ,
metavar = " <Storage URI> " ,
help = " Shared storage URI to use for multi-process data sharing (e.g. via gunicorn) " ,
)
2021-05-18 05:41:02 +02:00
parser . add_argument (
" --load-only " ,
type = operator . methodcaller ( " split " , " , " ) ,
2021-07-12 22:43:06 +02:00
default = DEFARGS [ ' LOAD_ONLY ' ] ,
2021-05-18 05:41:02 +02:00
metavar = " <comma-separated language codes> " ,
help = " Set available languages (ar,de,en,es,fr,ga,hi,it,ja,ko,pt,ru,zh) " ,
)
2024-06-03 18:42:32 +02:00
parser . add_argument (
" --alternatives-limit " ,
default = DEFARGS [ ' ALTERNATIVES_LIMIT ' ] ,
type = int ,
metavar = " <maximum number of alternatives translations> " ,
help = " Set the maximum number of supported alternative translations ( %(default)s ) " ,
)
2022-07-15 18:12:20 +02:00
parser . add_argument (
" --threads " ,
default = DEFARGS [ ' THREADS ' ] ,
type = int ,
metavar = " <number of threads> " ,
help = " Set number of threads ( %(default)s ) " ,
)
2021-10-09 15:45:58 +02:00
parser . add_argument (
" --suggestions " , default = DEFARGS [ ' SUGGESTIONS ' ] , action = " store_true " , help = " Allow user suggestions "
)
2021-10-25 17:00:44 +02:00
parser . add_argument (
2022-04-02 22:26:37 +02:00
" --disable-files-translation " , default = DEFARGS [ ' DISABLE_FILES_TRANSLATION ' ] , action = " store_true " ,
help = " Disable files translation "
2021-10-25 17:00:44 +02:00
)
2022-02-07 12:02:32 +02:00
parser . add_argument (
" --disable-web-ui " , default = DEFARGS [ ' DISABLE_WEB_UI ' ] , action = " store_true " , help = " Disable web ui "
)
2022-12-20 18:13:56 +02:00
parser . add_argument (
" --update-models " , default = DEFARGS [ ' UPDATE_MODELS ' ] , action = " store_true " , help = " Update language models at startup "
)
2023-09-29 22:43:59 +02:00
parser . add_argument (
2023-09-30 00:31:33 +02:00
" --force-update-models " , default = DEFARGS [ ' FORCE_UPDATE_MODELS ' ] , action = " store_true " , help = " Install/Reinstall language models at startup "
2023-09-29 22:43:59 +02:00
)
2022-12-26 23:10:43 +02:00
parser . add_argument (
" --metrics " ,
default = DEFARGS [ ' METRICS ' ] ,
action = " store_true " ,
help = " Enable the /metrics endpoint for exporting Prometheus usage metrics " ,
)
parser . add_argument (
" --metrics-auth-token " ,
default = DEFARGS [ ' METRICS_AUTH_TOKEN ' ] ,
type = str ,
help = " Protect the /metrics endpoint by allowing only clients that have a valid Authorization Bearer token ( %(default)s ) " ,
)
2022-12-31 06:23:50 +02:00
parser . add_argument (
" --url-prefix " ,
default = DEFARGS [ ' URL_PREFIX ' ] ,
type = str ,
help = " Add prefix to URL: example.com:5000/url-prefix/ " ,
)
args = parser . parse_args ( )
if args . url_prefix and not args . url_prefix . startswith ( ' / ' ) :
args . url_prefix = ' / ' + args . url_prefix
return args
2022-04-02 22:26:37 +02:00
def main ( ) :
args = get_args ( )
2021-02-15 20:30:28 +02:00
app = create_app ( args )
2021-02-09 02:24:34 +02:00
2022-12-31 06:23:50 +02:00
if ' --wsgi ' in sys . argv :
2021-07-07 02:26:06 +02:00
return app
2021-02-09 02:24:34 +02:00
else :
2024-03-06 16:46:29 +02:00
if args . debug and args . host == " * " :
# '::' will listen on both ipv6 and ipv4
args . host = " :: "
2021-07-07 02:26:06 +02:00
if args . debug :
app . run ( host = args . host , port = args . port )
else :
from waitress import serve
2021-05-18 05:41:02 +02:00
2022-06-21 22:56:48 +02:00
url_scheme = " https " if args . ssl else " http "
2023-07-09 12:29:11 +02:00
print ( f " Running on { url_scheme } :// { args . host } : { args . port } { args . url_prefix } " )
2022-06-21 22:56:48 +02:00
2021-07-07 02:26:06 +02:00
serve (
app ,
host = args . host ,
port = args . port ,
2022-06-21 22:56:48 +02:00
url_scheme = url_scheme ,
2022-07-15 18:12:20 +02:00
threads = args . threads
2021-07-07 02:26:06 +02:00
)
2021-05-18 05:41:02 +02:00
2021-02-09 02:24:34 +02:00
if __name__ == " __main__ " :
main ( )