Cnass commited on
Commit
cdb09e8
·
verified ·
1 Parent(s): 437a569

Update app/translation.py

Browse files
Files changed (1) hide show
  1. app/translation.py +37 -28
app/translation.py CHANGED
@@ -45,13 +45,16 @@ LANGUAGES = {
45
  "ug": "Uyghur",
46
  }
47
 
48
- # Placeholder styles commonly found in game strings (RPG Maker control codes,
49
- # printf-style format specifiers, ICU/handlebars-style variables) that the
50
- # model must copy through untouched rather than translate.
51
- _PLACEHOLDER_HINT = (
52
- "You must NEVER translate or alter placeholders and control codes such as "
53
- r"\N[..], \V[..], \C[..], \I[..], {variable}, ${variable}, %s, %d, %1, {{var}} "
54
- "or similar tokens. Keep them exactly as they appear, in the same position."
 
 
 
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 = True,
71
  ) -> str:
 
 
 
 
 
72
  target_name = resolve_language(target_lang)
73
 
74
- header = (
75
- f"Translate the following text into {target_name}. "
76
- "Note that you should only output the translated result "
77
- "without any additional explanation"
78
- )
 
 
 
 
 
 
 
 
 
79
  if source_lang:
80
- source_name = resolve_language(source_lang)
81
- header += f". The source text is in {source_name}"
82
- header += "."
83
 
84
- parts = [header]
 
85
 
86
- if style:
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
- parts.insert(
94
- 0,
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}"