1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-11-16 20:40:10 +01:00

Merge pull request #533 from pierotofy/batchfix

Fix batch auto language detection
This commit is contained in:
Piero Toffanin 2023-11-08 22:04:45 -05:00 committed by GitHub
commit 4761ae5c43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 23 deletions

View File

@ -1 +1 @@
1.5.0
1.5.1

View File

@ -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 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_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:
@ -737,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)
@ -755,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(