Upload 11 files
Browse files- app.py +1 -0
- translation.py +164 -107
app.py
CHANGED
|
@@ -1274,6 +1274,7 @@ def _step5_translate() -> None:
|
|
| 1274 |
provider=prov, model=model, api_key=key,
|
| 1275 |
fmt=fmt, ssafile=subs, filename=fname,
|
| 1276 |
system_prompt=sys_prompt,
|
|
|
|
| 1277 |
)
|
| 1278 |
# Stash checkpoint into job so thread can resume
|
| 1279 |
if cp_data:
|
|
|
|
| 1274 |
provider=prov, model=model, api_key=key,
|
| 1275 |
fmt=fmt, ssafile=subs, filename=fname,
|
| 1276 |
system_prompt=sys_prompt,
|
| 1277 |
+
preset_key=st.session_state.get("prompt_preset", "adult_18"),
|
| 1278 |
)
|
| 1279 |
# Stash checkpoint into job so thread can resume
|
| 1280 |
if cp_data:
|
translation.py
CHANGED
|
@@ -207,131 +207,140 @@ def _chunk(lst: list, size: int) -> list:
|
|
| 207 |
|
| 208 |
|
| 209 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 210 |
-
# Prompts โ Multi-Preset System (
|
| 211 |
#
|
| 212 |
# Architecture: build_system_prompt() assembles 4 non-overlapping layers:
|
| 213 |
# 1. STYLE โ tone / register / swearing (preset-specific)
|
| 214 |
# 2. TERMS โ special terms to preserve (culture-specific)
|
| 215 |
# 3. NOUNS โ proper noun handling (noun_style ร culture, generated once)
|
| 216 |
-
# 4. TECH โ count rule / tags / glossary / output (always identical)
|
| 217 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 218 |
|
| 219 |
-
# โโ
|
|
|
|
| 220 |
|
| 221 |
-
#
|
| 222 |
-
#
|
| 223 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
_STYLE_STANDARD = """\
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
โข แ
|
| 231 |
-
โข
|
| 232 |
-
โข
|
| 233 |
-
|
|
|
|
| 234 |
"""
|
| 235 |
|
| 236 |
_STYLE_ADULT = """\
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
โข แ
|
| 242 |
-
โข
|
| 243 |
-
โข
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
| 247 |
"""
|
| 248 |
|
| 249 |
_STYLE_ANIME = """\
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
โข
|
| 255 |
-
โข
|
| 256 |
-
โข
|
| 257 |
-
โข
|
| 258 |
-
|
|
|
|
| 259 |
"""
|
| 260 |
|
| 261 |
_STYLE_CDRAMA = """\
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
โข
|
| 267 |
-
โข Villain / schemer โ แแผแแบแธแแแบแธ
|
| 268 |
-
โข
|
|
|
|
|
|
|
| 269 |
"""
|
| 270 |
|
| 271 |
_STYLE_KDRAMA = """\
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
โข Young adult
|
| 277 |
-
โข
|
| 278 |
-
โข
|
| 279 |
-
|
|
|
|
| 280 |
"""
|
| 281 |
|
| 282 |
_STYLE_FORMAL = """\
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
Register
|
| 286 |
-
โข Narration โ
|
| 287 |
-
โข Expert
|
| 288 |
-
โข
|
| 289 |
-
|
|
|
|
| 290 |
"""
|
| 291 |
|
| 292 |
# โโ Layer 2: TERMS blocks (culture-specific special terms) โโโโโโโโโโโโโโโโโโโโ
|
| 293 |
-
# Only stated here โ never repeated in preset STYLE or NOUNS layer.
|
| 294 |
|
| 295 |
-
_TERMS_WESTERN = ""
|
| 296 |
|
| 297 |
_TERMS_JAPANESE = """\
|
| 298 |
-
Japanese
|
| 299 |
-
โข
|
| 300 |
-
โข Anime
|
| 301 |
-
โข Game
|
| 302 |
-
โข Cultural
|
| 303 |
"""
|
| 304 |
|
| 305 |
_TERMS_CHINESE = """\
|
| 306 |
-
Chinese
|
| 307 |
-
โข Relationship /
|
| 308 |
-
|
| 309 |
-
โข
|
| 310 |
-
Golden Core, Nascent Soul, cultivation, qi, meridians, sect, immortal, spiritual root
|
| 311 |
-
โข Historical titles โ English: Emperor, Empress, Crown Prince, Noble Consort, Imperial Guard\
|
| 312 |
"""
|
| 313 |
|
| 314 |
_TERMS_KOREAN = """\
|
| 315 |
-
Korean
|
| 316 |
-
โข Relationship
|
| 317 |
-
|
| 318 |
-
โข
|
| 319 |
-
โข Cultural terms โ Romanized as-is: Ramyeon, Tteokbokki, Hanbok, Jjimjilbang, Aegyo\
|
| 320 |
"""
|
| 321 |
|
| 322 |
_TERMS_THAI = """\
|
| 323 |
-
Thai
|
| 324 |
-
โข Honorifics
|
| 325 |
-
โข Cultural
|
| 326 |
"""
|
| 327 |
|
| 328 |
-
_TERMS_UNIVERSAL = ""
|
| 329 |
|
| 330 |
# โโ Layer 3: NOUNS block (generated from noun_style ร culture) โโโโโโโโโโโโโโโโ
|
| 331 |
-
# Generated once by _noun_block() โ never appears in STYLE or TERMS.
|
| 332 |
|
| 333 |
def _noun_block(style: str, culture_key: str) -> str:
|
| 334 |
-
"""Single source of truth for proper noun handling rule."""
|
| 335 |
script = {
|
| 336 |
"japanese": "Romaji",
|
| 337 |
"chinese": "Pinyin",
|
|
@@ -342,47 +351,51 @@ def _noun_block(style: str, culture_key: str) -> str:
|
|
| 342 |
}.get(culture_key, "romanized")
|
| 343 |
|
| 344 |
examples = {
|
| 345 |
-
"western": [("Victor","แแ
แบแแฌ"),
|
| 346 |
-
"japanese": [("Sasuke","แแแฐแแฑ"),
|
| 347 |
-
"chinese": [("Wei Wuxian","แแฑแแฐแแพแปแฌแธ"),
|
| 348 |
-
"korean": [("Park Seo-jun","แแซแทแแฑแแปแฝแแบ"),
|
| 349 |
-
"thai": [("Prayuth","แแผแฌแแแบ"),
|
| 350 |
-
"universal": [("Victor","แแ
แบแแฌ"),
|
| 351 |
}.get(culture_key, [("Victor","แแ
แบแแฌ"), ("Hope Street","แแฏแแบแแบแ
แแแญแแบ"), ("London","แแแบแแแบ")])
|
| 352 |
|
| 353 |
if style == "phonetic":
|
| 354 |
-
ex_lines = "\n".join(f
|
| 355 |
return (
|
| 356 |
-
f"PROPER NOUNS โ
|
| 357 |
-
f"
|
| 358 |
f"{ex_lines}\n"
|
| 359 |
-
f"Glossary แแซ โ Glossary
|
| 360 |
)
|
| 361 |
else:
|
| 362 |
-
ex_lines = "\n".join(f' "{e}"
|
| 363 |
return (
|
| 364 |
f"PROPER NOUNS โ {script} spelling:\n"
|
| 365 |
-
f"
|
| 366 |
f"{ex_lines}\n"
|
| 367 |
-
f"Glossary แแซ โ Glossary
|
| 368 |
)
|
| 369 |
|
| 370 |
# โโ Layer 4: TECH block (always identical) ๏ฟฝ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 371 |
|
| 372 |
_TECH = """\
|
| 373 |
-
|
| 374 |
-
1.
|
| 375 |
-
2. แแผแแบแแฌ
|
| 376 |
-
3.
|
| 377 |
-
4. ASS tags ({\\an8}
|
| 378 |
-
5. Glossary term โ Glossary
|
| 379 |
-
|
| 380 |
-
COUNT: Input N
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
โ "
|
| 384 |
-
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
["แแฌแแฌแแผแแบ แ", "แแฌแแฌแแผแแบ แ", ..., "แแฌแแฌแแผแแบ n"]\
|
| 387 |
"""
|
| 388 |
|
|
@@ -508,6 +521,42 @@ def _strip_html(text: str) -> str:
|
|
| 508 |
return re.sub(r'<[^>]+>', '', text)
|
| 509 |
|
| 510 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
# Thread-local override for count-mismatch retry feedback
|
| 512 |
# _translate_chunk_safe sets [0] = error message; _call_chunk_once injects it
|
| 513 |
_chunk_once_override: list = [None]
|
|
@@ -774,7 +823,8 @@ def translate_all(
|
|
| 774 |
session_state: dict | None = None,
|
| 775 |
resume: bool = False,
|
| 776 |
cooldown: float | None = None,
|
| 777 |
-
system_prompt: str | None = None,
|
|
|
|
| 778 |
) -> list:
|
| 779 |
"""
|
| 780 |
Translate subtitle text lines in chunks.
|
|
@@ -936,6 +986,11 @@ def translate_all(
|
|
| 936 |
+ (f" | {fallbacks} chunks original" if fallbacks else "")
|
| 937 |
)
|
| 938 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 939 |
# Final length guarantee
|
| 940 |
assert len(result) == len(lines), \
|
| 941 |
f"Length invariant broken: {len(result)} != {len(lines)}"
|
|
@@ -955,7 +1010,8 @@ def run_translation_bg(
|
|
| 955 |
fmt: str,
|
| 956 |
ssafile,
|
| 957 |
filename: str,
|
| 958 |
-
system_prompt: str | None = None,
|
|
|
|
| 959 |
) -> threading.Thread:
|
| 960 |
"""
|
| 961 |
Spawn background thread for translation.
|
|
@@ -991,6 +1047,7 @@ def run_translation_bg(
|
|
| 991 |
session_state=session_st,
|
| 992 |
resume=bool(cp_init),
|
| 993 |
system_prompt=system_prompt,
|
|
|
|
| 994 |
)
|
| 995 |
|
| 996 |
# Build output file
|
|
|
|
| 207 |
|
| 208 |
|
| 209 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 210 |
+
# Prompts โ Multi-Preset System (zero redundancy, each rule said once)
|
| 211 |
#
|
| 212 |
# Architecture: build_system_prompt() assembles 4 non-overlapping layers:
|
| 213 |
# 1. STYLE โ tone / register / swearing (preset-specific)
|
| 214 |
# 2. TERMS โ special terms to preserve (culture-specific)
|
| 215 |
# 3. NOUNS โ proper noun handling (noun_style ร culture, generated once)
|
| 216 |
+
# 4. TECH โ count rule / tags / glossary / output / examples (always identical)
|
| 217 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 218 |
|
| 219 |
+
# โโ Core philosophy (shared, referenced only by TECH) โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 220 |
+
# Written once here for maintainability โ injected into _TECH below.
|
| 221 |
|
| 222 |
+
# โโ Layer 1: STYLE blocks (preset-specific) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 223 |
+
# Covers: who is speaking, what register, how to handle strong language.
|
| 224 |
+
# Does NOT repeat: "don't add/remove" (that's TECH), nouns (that's NOUNS).
|
| 225 |
+
#
|
| 226 |
+
# DEFAULT PRONOUN RULE (applies to all presets):
|
| 227 |
+
# แแซ/แแแบแธ is the natural default for most dialogue.
|
| 228 |
+
# แแปแฝแแบแแฑแฌแบ/แแปแฝแแบแ only when the character is genuinely being servile/formal
|
| 229 |
+
# (e.g. servant to emperor, soldier to commanding officer, student to professor).
|
| 230 |
+
# Unknown gender โ แแซ/แแแบแธ. Ambiguous context โ แแซ/แแแบแธ.
|
| 231 |
+
# แแแฏแถแธแแผแแบแแญแฏแแบแแแบ แแซ/แแแบแธ แแฏแถแธ โ แแปแฝแแบแแฑแฌแบ/แแปแฝแแบแ แแแฏแถแธแ
|
| 232 |
|
| 233 |
_STYLE_STANDARD = """\
|
| 234 |
+
You are a Myanmar subtitle translator. Output natural spoken Myanmar โ the way a real person talks, not the way a textbook translates.
|
| 235 |
+
|
| 236 |
+
VOICE: Reproduce the character's actual voice. Ask yourself: "แแผแแบแแฌแแฐแแ
แบแแฑแฌแแบ แแฎ situation แแพแฌ แแแแบ แแฌแแผแฑแฌแแแฒ?"
|
| 237 |
+
DEFAULT pronouns โ แแซ/แแแบแธ for most characters. Only use แแปแฝแแบแแฑแฌแบ/แแปแฝแแบแ when the character is genuinely being servile or very formal (servant โ master, cadet โ commander). Unknown gender โ แแซ/แแแบแธ.
|
| 238 |
+
โข Rough / villain โ แแซ/แแแบแธ/แแฑแฌแแบ + แแฝ/แแป/แแฝแฌ (แแแฒแ โ family-safe)
|
| 239 |
+
โข Female casual โ แแซ/แแปแ + แแฝแฌ/แแฑแท/แแฑแฌแบ
|
| 240 |
+
โข Gentle / romantic โ แแซ/แแแบแธ + แแฐแธแแถแทแแฑแฌแแแบ
|
| 241 |
+
โข Comic โ แแผแฑแฌแแผแฑแฌแแญแฏแแญแฏ แแฑแซแทแแฑแซแทแแซแธแแซแธ
|
| 242 |
+
Strong language โ source intensity แแญแฏ family-safe แแผแแบแแฌ equivalent แแผแแทแบ แแญแแบแธแแญแแบแธ\
|
| 243 |
"""
|
| 244 |
|
| 245 |
_STYLE_ADULT = """\
|
| 246 |
+
You are a Myanmar subtitle translator. Output natural spoken Myanmar โ the way a real person talks, not the way a textbook translates.
|
| 247 |
+
|
| 248 |
+
VOICE: Reproduce the character's actual voice. Ask yourself: "แแผแแบแแฌแแฐแแ
แบแแฑแฌแแบ แแฎ situation แแพแฌ แแแแบ แแฌแแผแฑแฌแแแฒ?"
|
| 249 |
+
DEFAULT pronouns โ แแซ/แแแบแธ for most characters. Only use แแปแฝแแบแแฑแฌแบ/แแปแฝแแบแ when the character is genuinely being servile or formally subordinate. Unknown gender โ แแซ/แแแบแธ.
|
| 250 |
+
โข Rough / gang / villain โ แแซ/แแแบแธ/แแฑแฌแแบ/แแฑแทแแฑแฌแแบ + แแฝ/แแป/แแฝแฌ
|
| 251 |
+
โข Female casual / hostile โ แแซ/แแแบ + แแฝแฌ/แแฑแท/แ
|
| 252 |
+
โข Dominant / boss โ แแซ + แแผแแบแธแแแบแแแบ
|
| 253 |
+
โข Light / comic โ แแผแฑแฌแแผแฑแฌแแญแฏแแญแฏ แแฑแซแทแแฑแซแทแแซแธแแซแธ
|
| 254 |
+
Strong language โ keep the exact weight of the source:
|
| 255 |
+
English source โ English as-is (Fuck / Shit / Bitch / Bastard / Asshole)
|
| 256 |
+
Other language โ nearest English equivalent\
|
| 257 |
"""
|
| 258 |
|
| 259 |
_STYLE_ANIME = """\
|
| 260 |
+
You are a Myanmar subtitle translator. Output natural spoken Myanmar โ the way a real person talks, not the way a textbook translates.
|
| 261 |
+
|
| 262 |
+
VOICE: Reproduce the character's vocal personality. Ask yourself: "แแผแแบแแฌแแฐแแ
แบแแฑแฌแแบ แแฎ character แแญแฏ แแฌแแผแฑแฌแแแฒ?"
|
| 263 |
+
DEFAULT pronouns โ แแซ/แแแบแธ. Only use แแปแฝแแบแแฑแฌแบ/แแปแฝแแบแ for genuinely servile/formal moments (student โ sensei in apology, servant to lord).
|
| 264 |
+
โข Energetic / shonen โ แแซ/แแแบแธ + แแฝ/แแป แกแแฑแทแแป
|
| 265 |
+
โข Tsundere โ แแผแแบแแฌแธแแผแฎแธ แแฑแซแแแปแฑแฌแแบแแแบ โ แแซ/แแแบแธ แแแบแธแแแบแธ
|
| 266 |
+
โข Kuudere / cold โ แแญแฏแแญแฏ แแผแแบแแฌแธ แแญแฏแธแแญแฏแธ
|
| 267 |
+
โข Cute / moe โ แแซ/แแปแ + particle: แแฑแฌแบ/แแพแแบ + แแถแแปแญแฏ
|
| 268 |
+
โข Villain โ แแผแแบแธ แแซ/แแแบแธ/แแฑแฌแแบ
|
| 269 |
+
Strong language โ English as-is (Fuck / Shit / Bitch)\
|
| 270 |
"""
|
| 271 |
|
| 272 |
_STYLE_CDRAMA = """\
|
| 273 |
+
You are a Myanmar subtitle translator. Output natural spoken Myanmar โ the way a real person talks, not the way a textbook translates.
|
| 274 |
+
|
| 275 |
+
VOICE: Preserve the dramatic weight and emotional layering of Chinese drama. Ask yourself: "แแผแแบแแฌแแฐแแ
แบแแฑแฌแแบ แแฎ scene แแพแฌ แแฌแแผแฑแฌแแแฒ?"
|
| 276 |
+
DEFAULT pronouns โ แแซ/แแแบแธ. แแปแฝแแบแแฑแฌแบ/แแปแฝแแบแ only for genuine servile situations (servant before emperor, disciple before master in formal address).
|
| 277 |
+
โข Commoner / young โ แแซ/แแแบแธ colloquial
|
| 278 |
+
โข Villain / schemer โ แแซ/แแแบแธ + แแผแแบแธแแแบแธแแแบ
|
| 279 |
+
โข Noble in private โ แแซ/แแแบแธ + dignified
|
| 280 |
+
โข Noble in court ceremony โ แแปแฝแแบแแฑแฌแบ (genuine formal ritual only)
|
| 281 |
+
Poetic / dramatic lines โ preserve emotional weight in natural Myanmar; แแแบแแแบแแแบ formal Myanmar แแแฏแถแธ\
|
| 282 |
"""
|
| 283 |
|
| 284 |
_STYLE_KDRAMA = """\
|
| 285 |
+
You are a Myanmar subtitle translator. Output natural spoken Myanmar โ the way a real person talks, not the way a textbook translates.
|
| 286 |
+
|
| 287 |
+
VOICE: Preserve K-drama emotional intensity and sincerity. Ask yourself: "แแผแแบแแฌแแฐแแ
แบแแฑแฌแแบ แแฎ feeling แแพแฌ แแฌแแผแฑแฌแแแฒ?"
|
| 288 |
+
DEFAULT pronouns โ แแซ/แแแบแธ. แแปแฝแแบแแฑแฌแบ/แแปแฝแแบแ only for genuine formal subordinate moments (employee to CEO in official setting, child formal apology to elder).
|
| 289 |
+
โข Young adult / peers โ แแซ/แแแบแธ + แแฝแฌ/แแฑแท/แแป
|
| 290 |
+
โข Emotional / romantic โ แแซ/แแแบแธ + weight แแญแฏแแญแแบแธ
|
| 291 |
+
โข Chaebol / CEO (private) โ แแซ/แแแบแธ + measured
|
| 292 |
+
โข Elder / authority (official) โ แแปแฝแแบแแฑแฌแบ/แแปแ (genuine only)
|
| 293 |
+
Strong language โ English as-is\
|
| 294 |
"""
|
| 295 |
|
| 296 |
_STYLE_FORMAL = """\
|
| 297 |
+
You are a Myanmar subtitle translator for documentary, news, and educational content. Output clear, objective, educated standard Myanmar.
|
| 298 |
+
|
| 299 |
+
Register: Formal written Myanmar โ แแแฌแแแบ standard. No colloquial แแซ/แแแบแธ โ use แแแบ/แแปแฝแแบแฏแแบ or avoid pronouns where natural.
|
| 300 |
+
โข Narration โ formal, neutral, third-person where possible
|
| 301 |
+
โข Expert dialogue โ semi-formal educated register
|
| 302 |
+
โข Interview subject โ natural but measured
|
| 303 |
+
Technical terms โ precise Myanmar equivalent; if none exists, keep (English term) in parentheses.
|
| 304 |
+
Numbers / dates / statistics โ exact.\
|
| 305 |
"""
|
| 306 |
|
| 307 |
# โโ Layer 2: TERMS blocks (culture-specific special terms) โโโโโโโโโโโโโโโโโโโโ
|
|
|
|
| 308 |
|
| 309 |
+
_TERMS_WESTERN = ""
|
| 310 |
|
| 311 |
_TERMS_JAPANESE = """\
|
| 312 |
+
Japanese terms (keep as-is โ these are NOT proper nouns, noun-style rule does not apply):
|
| 313 |
+
โข Honorific suffixes: -san / -kun / -chan / -senpai / -sensei / -sama / -dono / -nii / -nee โ keep or use natural Myanmar equivalent (Glossary wins)
|
| 314 |
+
โข Anime/otaku: baka, kawaii, nakama, sugoi, yare yare, ara ara โ English as-is
|
| 315 |
+
โข Game/isekai system: HP, MP, Level, Skill, Quest, Guild, Status, Rank โ English as-is
|
| 316 |
+
โข Cultural: onsen, matsuri, bento, senpai-kouhai โ English/Romaji as-is\
|
| 317 |
"""
|
| 318 |
|
| 319 |
_TERMS_CHINESE = """\
|
| 320 |
+
Chinese terms (keep as-is โ these are NOT proper nouns, noun-style rule does not apply):
|
| 321 |
+
โข Relationship/title: Shifu, Shidi/Shixiong/Shijie, Gege/Jiejie/Meimei/Didi, A'niang, Wangye, Gongzi, Guniang โ Pinyin as-is
|
| 322 |
+
โข Wuxia/Xianxia: Golden Core, Nascent Soul, cultivation, qi, meridians, sect, immortal, spiritual root โ English/Pinyin as-is
|
| 323 |
+
โข Court titles: Emperor, Empress, Crown Prince, Noble Consort, Imperial Guard โ English as-is\
|
|
|
|
|
|
|
| 324 |
"""
|
| 325 |
|
| 326 |
_TERMS_KOREAN = """\
|
| 327 |
+
Korean terms (keep as-is โ these are NOT proper nouns, noun-style rule does not apply):
|
| 328 |
+
โข Relationship/honorifics: Oppa, Unnie, Hyung, Noona, Sunbae, Hoobae, Sajangnim, Seonsaengnim โ Romanized as-is
|
| 329 |
+
โข Speech level: ์กด๋๋ง โ formal Myanmar register | ๋ฐ๋ง โ colloquial Myanmar register
|
| 330 |
+
โข Cultural: Ramyeon, Tteokbokki, Hanbok, Jjimjilbang, Aegyo โ Romanized as-is\
|
|
|
|
| 331 |
"""
|
| 332 |
|
| 333 |
_TERMS_THAI = """\
|
| 334 |
+
Thai/SE Asian terms (keep as-is โ these are NOT proper nouns, noun-style rule does not apply):
|
| 335 |
+
โข Honorifics: Khun, Phi, Nong โ Romanized as-is or natural Myanmar equivalent
|
| 336 |
+
โข Cultural: Wai, Songkran, Muay Thai, Tuk-tuk โ Romanized as-is\
|
| 337 |
"""
|
| 338 |
|
| 339 |
+
_TERMS_UNIVERSAL = ""
|
| 340 |
|
| 341 |
# โโ Layer 3: NOUNS block (generated from noun_style ร culture) โโโโโโโโโโโโโโโโ
|
|
|
|
| 342 |
|
| 343 |
def _noun_block(style: str, culture_key: str) -> str:
|
|
|
|
| 344 |
script = {
|
| 345 |
"japanese": "Romaji",
|
| 346 |
"chinese": "Pinyin",
|
|
|
|
| 351 |
}.get(culture_key, "romanized")
|
| 352 |
|
| 353 |
examples = {
|
| 354 |
+
"western": [("Victor","แแ
แบแแฌ"), ("Hope Street","แแฏแแบแแบแ
แแแญแแบ"), ("London","แแแบแแแบ")],
|
| 355 |
+
"japanese": [("Sasuke","แแแฐแแฑ"), ("Tokyo","แแญแฏแแปแญแฏ"), ("Konoha","แแญแฏแแญแฏแแฌ")],
|
| 356 |
+
"chinese": [("Wei Wuxian","แแฑแแฐแแพแปแฌแธ"), ("Beijing","แแฑแแปแแบแธ"), ("Yunmeng","แแฝแแบแแญแแบ")],
|
| 357 |
+
"korean": [("Park Seo-jun","แแซแทแแฑแแปแฝแแบ"), ("Seoul","แแญแฏแธแแบ"), ("Busan","แแฐแแแบ")],
|
| 358 |
+
"thai": [("Prayuth","แแผแฌแแแบ"), ("Chiang Mai","แแปแแบแธแแญแฏแแบ"), ("Bangkok","แแแบแแฑแฌแแบ")],
|
| 359 |
+
"universal": [("Victor","แแ
แบแแฌ"), ("Sakura","แแฌแแฐแแฌ"), ("Madison","แแแบแแ
แบแแแบ")],
|
| 360 |
}.get(culture_key, [("Victor","แแ
แบแแฌ"), ("Hope Street","แแฏแแบแแบแ
แแแญแแบ"), ("London","แแแบแแแบ")])
|
| 361 |
|
| 362 |
if style == "phonetic":
|
| 363 |
+
ex_lines = "\n".join(f" {e} โ {p}" for e, p in examples)
|
| 364 |
return (
|
| 365 |
+
f"PROPER NOUNS โ Phonetic Myanmar:\n"
|
| 366 |
+
f"Names / places / organizations โ Myanmar phonetic transliteration only (meaning-translation เธซเนเธฒเธก)\n"
|
| 367 |
f"{ex_lines}\n"
|
| 368 |
+
f"Glossary แแซ โ Glossary | แแแซ โ phonetic"
|
| 369 |
)
|
| 370 |
else:
|
| 371 |
+
ex_lines = "\n".join(f' "{e}" not "{p}"' for e, p in examples)
|
| 372 |
return (
|
| 373 |
f"PROPER NOUNS โ {script} spelling:\n"
|
| 374 |
+
f"Names / places / organizations โ keep {script} original spelling unchanged\n"
|
| 375 |
f"{ex_lines}\n"
|
| 376 |
+
f"Glossary แแซ โ Glossary | แแแซ โ {script} แแฐแแแบแธ"
|
| 377 |
)
|
| 378 |
|
| 379 |
# โโ Layer 4: TECH block (always identical) ๏ฟฝ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 380 |
|
| 381 |
_TECH = """\
|
| 382 |
+
ABSOLUTE RULES:
|
| 383 |
+
1. แแแญแฏแแแทแบ แแแฏแแบ โ source แแแซแแฑแฌแกแแฌ แแแผแแทแบแแ แแซแแฑแฌแกแแฌ แแแแบแ
|
| 384 |
+
2. แแผแแบแแฌ word order โ source language sentence structure copy แแแฐแธแ
|
| 385 |
+
3. แแญแฏแแบแแฑแธ consistency โ same character แแ
แบแแปแแบแแแบแธ pronoun + style แแฏแถแธ
|
| 386 |
+
4. ASS tags ({\\an8} {\\pos} {\\be5} {\\fad} etc.) + โช + โ โ as-is แแฌแธ
|
| 387 |
+
5. Glossary term แแซ โ Glossary value แแญแฏแแฌ แแฏแถแธแ แแฝแฒแแผแฌแธแแฑแฌแแฌแแฌแแผแแบ แแแฏแถแธแ
|
| 388 |
+
|
| 389 |
+
COUNT: Input N โ output EXACTLY N. Merge / split เธซเนเธฒเธกแ
|
| 390 |
+
|
| 391 |
+
NATURAL vs AI โ แแฎ pattern แแฑแแญแฏ แแพแฑแฌแแบ:
|
| 392 |
+
โ "แแซแแแบ แแฑแฌแแบแธแแฝแแบแแฑแฌแแแแบแแ
แบแแฏแแผแ
แบแแแบ" โ
"แแซแแแฑแฌแท แแฑแฌแแบแธแแฒแท outcome แแฒ"
|
| 393 |
+
โ "แแแบแแแบ แคแแฑแแฌแแญแฏแท แแฌแแผแฑแฌแแทแบแแฑแฌแแบแแฌแแแแบแธ" โ
"แแแบแธ แแฎแแพแฌ แแฌแแฌแแฏแแบแแฌแแฒ"
|
| 394 |
+
โ "แแปแฝแแบแฏแแบแแญแฏ แแฐแแฎแแฑแธแแซ แแปแฑแธแแฐแธแแผแฏแ" โ
"แแฐแแฎแแซ" / "แแฐแแฎแแฝแฌ"
|
| 395 |
+
โ "แแปแฝแแบแแฑแฌแบ แแแทแบแแญแฏ แแปแ
แบแแซแแแบ" โ
"แแซ แแแบแธแแญแฏ แแปแ
แบแแแบ"
|
| 396 |
+
โ "แแญแฏแแแบแธแกแแญแฏแแบแธ แแปแฝแแบแฏแแบแแญแฏแท แแฑแฌแแบแแฝแแบแแแแบ" โ
"แกแฒแทแแญแฏแแญแฏแแแบ แแซแแญแฏแท แแฏแแบแแแพแฌแแฒ"
|
| 397 |
+
|
| 398 |
+
Output: JSON array, EXACTLY n items. No markdown, no preamble.
|
| 399 |
["แแฌแแฌแแผแแบ แ", "แแฌแแฌแแผแแบ แ", ..., "แแฌแแฌแแผแแบ n"]\
|
| 400 |
"""
|
| 401 |
|
|
|
|
| 521 |
return re.sub(r'<[^>]+>', '', text)
|
| 522 |
|
| 523 |
|
| 524 |
+
# Pronoun substitution table โ applied after translation.
|
| 525 |
+
# Each entry: (pattern_string, replacement)
|
| 526 |
+
# Skipped for "formal" / "custom" presets.
|
| 527 |
+
_PRONOUN_SUBS: list[tuple] = [
|
| 528 |
+
# 1st person formal โ natural
|
| 529 |
+
(r'แแปแฝแแบแแฑแฌแบ', 'แแซ'),
|
| 530 |
+
(r'แแปแฝแแบแ', 'แแซ'),
|
| 531 |
+
# 2nd person formal โ natural
|
| 532 |
+
(r'แแแบแแปแฌแธ', 'แแแบแธ'),
|
| 533 |
+
]
|
| 534 |
+
# Compile once
|
| 535 |
+
_PRONOUN_RE: list[tuple] = [
|
| 536 |
+
(re.compile(p), r) for p, r in _PRONOUN_SUBS
|
| 537 |
+
]
|
| 538 |
+
|
| 539 |
+
def _naturalize_pronouns(text: str) -> str:
|
| 540 |
+
"""
|
| 541 |
+
Replace formal pronouns with natural spoken equivalents.
|
| 542 |
+
แแปแฝแแบแแฑแฌแบ โ แแซ | แแปแฝแแบแ โ แแปแ | แแแบแแปแฌแธ โ แแแบแธ
|
| 543 |
+
Applied after translation for all non-formal presets.
|
| 544 |
+
Does NOT touch ASS tags ({...}) or music markers.
|
| 545 |
+
"""
|
| 546 |
+
# Don't touch content inside ASS override tag blocks
|
| 547 |
+
# Split on { } boundaries to protect tags
|
| 548 |
+
parts = re.split(r'(\{[^}]*\})', text)
|
| 549 |
+
out = []
|
| 550 |
+
for part in parts:
|
| 551 |
+
if part.startswith('{'):
|
| 552 |
+
out.append(part) # ASS tag โ untouched
|
| 553 |
+
else:
|
| 554 |
+
for pattern, replacement in _PRONOUN_RE:
|
| 555 |
+
part = pattern.sub(replacement, part)
|
| 556 |
+
out.append(part)
|
| 557 |
+
return ''.join(out)
|
| 558 |
+
|
| 559 |
+
|
| 560 |
# Thread-local override for count-mismatch retry feedback
|
| 561 |
# _translate_chunk_safe sets [0] = error message; _call_chunk_once injects it
|
| 562 |
_chunk_once_override: list = [None]
|
|
|
|
| 823 |
session_state: dict | None = None,
|
| 824 |
resume: bool = False,
|
| 825 |
cooldown: float | None = None,
|
| 826 |
+
system_prompt: str | None = None,
|
| 827 |
+
preset_key: str = "adult_18", # used to decide pronoun post-processing
|
| 828 |
) -> list:
|
| 829 |
"""
|
| 830 |
Translate subtitle text lines in chunks.
|
|
|
|
| 986 |
+ (f" | {fallbacks} chunks original" if fallbacks else "")
|
| 987 |
)
|
| 988 |
|
| 989 |
+
# โโ Post-process: naturalize pronouns (skip for formal/custom presets) โโ
|
| 990 |
+
_SKIP_NATURALIZE = {"formal", "custom"}
|
| 991 |
+
if preset_key not in _SKIP_NATURALIZE:
|
| 992 |
+
result = [_naturalize_pronouns(line) for line in result]
|
| 993 |
+
|
| 994 |
# Final length guarantee
|
| 995 |
assert len(result) == len(lines), \
|
| 996 |
f"Length invariant broken: {len(result)} != {len(lines)}"
|
|
|
|
| 1010 |
fmt: str,
|
| 1011 |
ssafile,
|
| 1012 |
filename: str,
|
| 1013 |
+
system_prompt: str | None = None,
|
| 1014 |
+
preset_key: str = "adult_18",
|
| 1015 |
) -> threading.Thread:
|
| 1016 |
"""
|
| 1017 |
Spawn background thread for translation.
|
|
|
|
| 1047 |
session_state=session_st,
|
| 1048 |
resume=bool(cp_init),
|
| 1049 |
system_prompt=system_prompt,
|
| 1050 |
+
preset_key=preset_key,
|
| 1051 |
)
|
| 1052 |
|
| 1053 |
# Build output file
|