Fix auto batch translation

This commit is contained in:
Piero Toffanin 2023-11-08 21:49:56 -05:00
parent 3ad487f4bf
commit cbe1c5db71

View file

@ -555,18 +555,14 @@ def create_app(args):
if source_lang == "auto": if source_lang == "auto":
candidate_langs = detect_languages(q if batch else [q]) candidate_langs = detect_languages(q if batch else [q])
source_langs = [candidate_langs[0]] detected_src_lang = candidate_langs[0]
else: else:
if batch: detected_src_lang = {"confidence": 100.0, "language": source_lang}
source_langs = [ {"confidence": 100.0, "language": source_lang} for text in q]
else:
source_langs = [ {"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 src_lang is None:
if lang is None: abort(400, description=_("%(lang)s is not supported", lang=source_lang))
abort(400, description=_("%(lang)s is not supported", lang=source_langs[idx]))
tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None) tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None)
@ -582,10 +578,10 @@ def create_app(args):
try: try:
if batch: if batch:
results = [] results = []
for idx, text in enumerate(q): for _idx, text in enumerate(q):
translator = src_langs[idx].get_translation(tgt_lang) translator = src_lang.get_translation(tgt_lang)
if translator is None: 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": if text_format == "html":
translated_text = str(translate_html(translator, text)) translated_text = str(translate_html(translator, text))
@ -597,7 +593,7 @@ def create_app(args):
return jsonify( return jsonify(
{ {
"translatedText": results, "translatedText": results,
"detectedLanguage": source_langs "detectedLanguage": [detected_src_lang] * len(q)
} }
) )
else: else:
@ -607,9 +603,9 @@ def create_app(args):
} }
) )
else: else:
translator = src_langs[0].get_translation(tgt_lang) translator = src_lang.get_translation(tgt_lang)
if translator is None: 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": if text_format == "html":
translated_text = str(translate_html(translator, q)) translated_text = str(translate_html(translator, q))
@ -620,7 +616,7 @@ def create_app(args):
return jsonify( return jsonify(
{ {
"translatedText": unescape(translated_text), "translatedText": unescape(translated_text),
"detectedLanguage": source_langs[0] "detectedLanguage": detected_src_lang
} }
) )
else: else: