From f3bf89375ffa7efa3bcc268e9b107628e3194e10 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 15 Nov 2023 14:34:20 -0500 Subject: [PATCH 1/3] Dynamic limit costs --- libretranslate/app.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libretranslate/app.py b/libretranslate/app.py index 7e58f9b..5f7cabf 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -205,6 +205,8 @@ def create_app(args): args.req_limit, args.hourly_req_limit, args.daily_req_limit, api_keys_db ), storage_uri=args.req_limit_storage, + default_limits_deduct_when=lambda req: True, # Force cost to be called after the request + default_limits_cost=lambda: getattr(request, 'req_cost', 1) ) else: from .no_limiter import Limiter @@ -515,6 +517,7 @@ def create_app(args): type: string description: Error message """ + print("/translate") if request.is_json: json = get_json_dict(request) q = json.get("q") @@ -550,6 +553,9 @@ def create_app(args): description=_("Invalid request: request (%(size)s) exceeds text limit (%(limit)s)", size=batch_size, limit=args.batch_limit), ) + if batch: + request.req_cost = max(1, len(q)) + if args.char_limit != -1: chars = sum([len(text) for text in q]) if batch else len(q) @@ -755,6 +761,14 @@ def create_app(args): file.save(filepath) + # Not an exact science: take the number of bytes and divide by + # the character limit. Assuming a plain text file, this will + # set the cost of the request to N = bytes / char_limit, which is + # roughly equivalent to a batch process of N batches assuming + # each batch uses all available limits + if args.char_limit != -1: + request.req_cost = max(1, int(os.path.getsize(filepath) / args.char_limit)) + translated_file_path = argostranslatefiles.translate_file(src_lang.get_translation(tgt_lang), filepath) translated_filename = os.path.basename(translated_file_path) From 9bc342a9aec574bfc1018efde669486c7d5670ea Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 15 Nov 2023 14:47:44 -0500 Subject: [PATCH 2/3] Cleanup --- libretranslate/app.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libretranslate/app.py b/libretranslate/app.py index 5f7cabf..2e1d6f6 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -517,7 +517,6 @@ def create_app(args): type: string description: Error message """ - print("/translate") if request.is_json: json = get_json_dict(request) q = json.get("q") @@ -553,20 +552,21 @@ def create_app(args): description=_("Invalid request: request (%(size)s) exceeds text limit (%(limit)s)", size=batch_size, limit=args.batch_limit), ) + src_texts = q if batch else [q] + + if args.char_limit != -1: + for text in src_texts: + if len(text) > args.char_limit: + abort( + 400, + description=_("Invalid request: request (%(size)s) exceeds text limit (%(limit)s)", size=len(text), limit=args.char_limit), + ) + if batch: request.req_cost = max(1, len(q)) - if args.char_limit != -1: - chars = sum([len(text) for text in q]) if batch else len(q) - - if args.char_limit < chars: - abort( - 400, - description=_("Invalid request: request (%(size)s) exceeds text limit (%(limit)s)", size=chars, limit=args.char_limit), - ) - if source_lang == "auto": - candidate_langs = detect_languages(q if batch else [q]) + candidate_langs = detect_languages(src_texts) detected_src_lang = candidate_langs[0] else: detected_src_lang = {"confidence": 100.0, "language": source_lang} From 48464938cbd458400cc6b144aeb8c2a6c4d36e94 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 15 Nov 2023 14:49:53 -0500 Subject: [PATCH 3/3] Fix tab --- libretranslate/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libretranslate/app.py b/libretranslate/app.py index 2e1d6f6..e6875eb 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -767,7 +767,7 @@ def create_app(args): # roughly equivalent to a batch process of N batches assuming # each batch uses all available limits if args.char_limit != -1: - request.req_cost = max(1, int(os.path.getsize(filepath) / args.char_limit)) + request.req_cost = max(1, int(os.path.getsize(filepath) / args.char_limit)) translated_file_path = argostranslatefiles.translate_file(src_lang.get_translation(tgt_lang), filepath) translated_filename = os.path.basename(translated_file_path)