Update app/translation.py
Browse files- app/translation.py +37 -28
app/translation.py
CHANGED
|
@@ -45,13 +45,16 @@ LANGUAGES = {
|
|
| 45 |
"ug": "Uyghur",
|
| 46 |
}
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
#
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
|
|
@@ -67,36 +70,42 @@ def build_prompt(
|
|
| 67 |
source_lang: str | None = None,
|
| 68 |
style: str | None = None,
|
| 69 |
glossary: dict[str, str] | None = None,
|
| 70 |
-
preserve_placeholders: bool =
|
| 71 |
) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
target_name = resolve_language(target_lang)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
if source_lang:
|
| 80 |
-
|
| 81 |
-
header += f". The source text is in {source_name}"
|
| 82 |
-
header += "."
|
| 83 |
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
parts.append(
|
| 88 |
-
f"Note that the translation style must strictly conform to [{style}]."
|
| 89 |
-
)
|
| 90 |
|
| 91 |
if glossary:
|
|
|
|
| 92 |
ref_lines = "\n".join(f"{k} translates to {v}" for k, v in glossary.items())
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
"Reference the following translations:\n" + ref_lines,
|
| 96 |
)
|
| 97 |
|
| 98 |
-
if preserve_placeholders:
|
| 99 |
-
parts.append(_PLACEHOLDER_HINT)
|
| 100 |
-
|
| 101 |
-
instruction = "\n".join(parts)
|
| 102 |
return f"{instruction}\n\n{text}"
|
|
|
|
| 45 |
"ug": "Uyghur",
|
| 46 |
}
|
| 47 |
|
| 48 |
+
# Opt-in only. Hy-MT2 already preserves placeholders and control codes
|
| 49 |
+
# (\V[1], %1, {var}, ...) on its own - verified against the 7B Q4_K_M model -
|
| 50 |
+
# so this clause is off by default: it costs prompt tokens without improving
|
| 51 |
+
# the result. Keep it to a single short sentence with no literal examples of
|
| 52 |
+
# the tokens themselves; a longer version listing sample placeholders gets
|
| 53 |
+
# treated as source text and translated instead of followed, which destroys
|
| 54 |
+
# short inputs ("Potion" came back as the translated instruction).
|
| 55 |
+
_PLACEHOLDER_CLAUSE = (
|
| 56 |
+
". You must keep every placeholder, variable and control code exactly as "
|
| 57 |
+
"it appears, and must not translate, escape or reorder them"
|
| 58 |
)
|
| 59 |
|
| 60 |
|
|
|
|
| 70 |
source_lang: str | None = None,
|
| 71 |
style: str | None = None,
|
| 72 |
glossary: dict[str, str] | None = None,
|
| 73 |
+
preserve_placeholders: bool = False,
|
| 74 |
) -> str:
|
| 75 |
+
"""Build a single-turn prompt in one of the instruction shapes documented on
|
| 76 |
+
the Hy-MT2 model card. Everything the model is meant to *follow* has to stay
|
| 77 |
+
inside the one instruction line - anything placed as its own paragraph ahead
|
| 78 |
+
of the source text is read as more text to translate.
|
| 79 |
+
"""
|
| 80 |
target_name = resolve_language(target_lang)
|
| 81 |
|
| 82 |
+
if style:
|
| 83 |
+
# "Style" template from the model card.
|
| 84 |
+
instruction = (
|
| 85 |
+
f"Please translate the following text into {target_name}. "
|
| 86 |
+
f"Note that the translation style must strictly conform to [{style}]"
|
| 87 |
+
)
|
| 88 |
+
else:
|
| 89 |
+
# "Default Translation" template from the model card.
|
| 90 |
+
instruction = (
|
| 91 |
+
f"Translate the following text into {target_name}. "
|
| 92 |
+
"Note that you should only output the translated result "
|
| 93 |
+
"without any additional explanation"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
if source_lang:
|
| 97 |
+
instruction += f". The source text is in {resolve_language(source_lang)}"
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
if preserve_placeholders:
|
| 100 |
+
instruction += _PLACEHOLDER_CLAUSE
|
| 101 |
|
| 102 |
+
instruction += ":"
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
if glossary:
|
| 105 |
+
# "Terminology" template: reference pairs go *before* the task line.
|
| 106 |
ref_lines = "\n".join(f"{k} translates to {v}" for k, v in glossary.items())
|
| 107 |
+
instruction = (
|
| 108 |
+
"Reference the following translations:\n" + ref_lines + "\n\n" + instruction
|
|
|
|
| 109 |
)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
return f"{instruction}\n\n{text}"
|