From cbe1c5db71d2201666b1efa429d68d126315fed2 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 8 Nov 2023 21:49:56 -0500 Subject: [PATCH 1/4] Fix auto batch translation --- libretranslate/app.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/libretranslate/app.py b/libretranslate/app.py index eb4fafe..beb5bb0 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -555,18 +555,14 @@ def create_app(args): if source_lang == "auto": candidate_langs = detect_languages(q if batch else [q]) - source_langs = [candidate_langs[0]] + detected_src_lang = candidate_langs[0] else: - if batch: - source_langs = [ {"confidence": 100.0, "language": source_lang} for text in q] - else: - source_langs = [ {"confidence": 100.0, "language": source_lang} ] + detected_src_lang = {"confidence": 100.0, "language": source_lang} - src_langs = [next(iter([l for l in languages if l.code == source_lang["language"]]), None) for source_lang in source_langs] + src_lang = next(iter([l for l in languages if l.code == detected_src_lang["language"]]), None) - for idx, lang in enumerate(src_langs): - if lang is None: - abort(400, description=_("%(lang)s is not supported", lang=source_langs[idx])) + if src_lang is None: + abort(400, description=_("%(lang)s is not supported", lang=source_lang)) tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None) @@ -582,10 +578,10 @@ def create_app(args): try: if batch: results = [] - for idx, text in enumerate(q): - translator = src_langs[idx].get_translation(tgt_lang) + for _idx, text in enumerate(q): + translator = src_lang.get_translation(tgt_lang) if translator is None: - abort(400, description=_("%(tname)s (%(tcode)s) is not available as a target language from %(sname)s (%(scode)s)", tname=_lazy(tgt_lang.name), tcode=tgt_lang.code, sname=_lazy(src_langs[idx].name), scode=src_langs[idx].code)) + abort(400, description=_("%(tname)s (%(tcode)s) is not available as a target language from %(sname)s (%(scode)s)", tname=_lazy(tgt_lang.name), tcode=tgt_lang.code, sname=_lazy(src_lang.name), scode=src_lang.code)) if text_format == "html": translated_text = str(translate_html(translator, text)) @@ -597,7 +593,7 @@ def create_app(args): return jsonify( { "translatedText": results, - "detectedLanguage": source_langs + "detectedLanguage": [detected_src_lang] * len(q) } ) else: @@ -607,9 +603,9 @@ def create_app(args): } ) else: - translator = src_langs[0].get_translation(tgt_lang) + translator = src_lang.get_translation(tgt_lang) if translator is None: - abort(400, description=_("%(tname)s (%(tcode)s) is not available as a target language from %(sname)s (%(scode)s)", tname=_lazy(tgt_lang.name), tcode=tgt_lang.code, sname=_lazy(src_langs[0].name), scode=src_langs[0].code)) + abort(400, description=_("%(tname)s (%(tcode)s) is not available as a target language from %(sname)s (%(scode)s)", tname=_lazy(tgt_lang.name), tcode=tgt_lang.code, sname=_lazy(src_lang.name), scode=src_lang.code)) if text_format == "html": translated_text = str(translate_html(translator, q)) @@ -620,7 +616,7 @@ def create_app(args): return jsonify( { "translatedText": unescape(translated_text), - "detectedLanguage": source_langs[0] + "detectedLanguage": detected_src_lang } ) else: From dc194db0dda8cbf9e5d454c2468f6f56f9db0070 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 8 Nov 2023 21:55:55 -0500 Subject: [PATCH 2/4] Refactor translate_file endpoint --- libretranslate/app.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/libretranslate/app.py b/libretranslate/app.py index beb5bb0..d4e4354 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -733,12 +733,10 @@ def create_app(args): if os.path.splitext(file.filename)[1] not in frontend_argos_supported_files_format: abort(400, description=_("Invalid request: file format not supported")) - source_langs = [source_lang] - src_langs = [next(iter([l for l in languages if l.code == source_lang]), None) for source_lang in source_langs] + src_lang = next(iter([l for l in languages if l.code == source_lang]), None) - for idx, lang in enumerate(src_langs): - if lang is None: - abort(400, description=_("%(lang)s is not supported", lang=source_langs[idx])) + if src_lang is None: + abort(400, description=_("%(lang)s is not supported", lang=source_lang)) tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None) @@ -751,7 +749,7 @@ def create_app(args): file.save(filepath) - translated_file_path = argostranslatefiles.translate_file(src_langs[0].get_translation(tgt_lang), filepath) + translated_file_path = argostranslatefiles.translate_file(src_lang.get_translation(tgt_lang), filepath) translated_filename = os.path.basename(translated_file_path) return jsonify( From 37bd2365b9012db0cfa1c5a49d6432200f01d32b Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 8 Nov 2023 21:57:14 -0500 Subject: [PATCH 3/4] Bump version --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index bc80560..26ca594 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.5.0 +1.5.1 From 8427f86275b259f99203e64d70cd2edf87f6922c Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 8 Nov 2023 21:58:59 -0500 Subject: [PATCH 4/4] Minor refactor --- libretranslate/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libretranslate/app.py b/libretranslate/app.py index d4e4354..8c34dda 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -578,7 +578,7 @@ def create_app(args): try: if batch: results = [] - for _idx, text in enumerate(q): + for text in q: translator = src_lang.get_translation(tgt_lang) if translator is None: abort(400, description=_("%(tname)s (%(tcode)s) is not available as a target language from %(sname)s (%(scode)s)", tname=_lazy(tgt_lang.name), tcode=tgt_lang.code, sname=_lazy(src_lang.name), scode=src_lang.code))