1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-11-14 19:50:11 +01:00

Dynamic limit costs

This commit is contained in:
Piero Toffanin 2023-11-15 14:34:20 -05:00
parent 0aa4bcac7f
commit f3bf89375f

View File

@ -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)