From f3bf89375ffa7efa3bcc268e9b107628e3194e10 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 15 Nov 2023 14:34:20 -0500 Subject: [PATCH] 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)