Spaces:
Running on Zero
Running on Zero
Update pipeline/clients.py
Browse files- pipeline/clients.py +61 -5
pipeline/clients.py
CHANGED
|
@@ -272,9 +272,7 @@ def restore_placeholders(source: str, translated: str) -> str:
|
|
| 272 |
return result
|
| 273 |
|
| 274 |
|
| 275 |
-
|
| 276 |
-
def translate(text: str) -> str:
|
| 277 |
-
"""Translate one string with TranslateGemma (greedy)."""
|
| 278 |
messages = [
|
| 279 |
{
|
| 280 |
"role": "user",
|
|
@@ -297,8 +295,7 @@ def translate(text: str) -> str:
|
|
| 297 |
return restore_placeholders(text, reply)
|
| 298 |
|
| 299 |
|
| 300 |
-
|
| 301 |
-
def adjust_tone(
|
| 302 |
source: str,
|
| 303 |
draft: str,
|
| 304 |
wiki: str,
|
|
@@ -338,6 +335,65 @@ def adjust_tone(
|
|
| 338 |
return restore_placeholders(source, reply) if reply else draft
|
| 339 |
|
| 340 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
@_gpu
|
| 342 |
def update_character_wiki(system_prompt: str, user_msg: str) -> str:
|
| 343 |
"""Update a character wiki via the same in-process Gemma runtime as tone pass."""
|
|
|
|
| 272 |
return result
|
| 273 |
|
| 274 |
|
| 275 |
+
def _translate_text(text: str) -> str:
|
|
|
|
|
|
|
| 276 |
messages = [
|
| 277 |
{
|
| 278 |
"role": "user",
|
|
|
|
| 295 |
return restore_placeholders(text, reply)
|
| 296 |
|
| 297 |
|
| 298 |
+
def _adjust_tone_text(
|
|
|
|
| 299 |
source: str,
|
| 300 |
draft: str,
|
| 301 |
wiki: str,
|
|
|
|
| 335 |
return restore_placeholders(source, reply) if reply else draft
|
| 336 |
|
| 337 |
|
| 338 |
+
@_gpu
|
| 339 |
+
def translate(text: str) -> str:
|
| 340 |
+
"""Translate one string with TranslateGemma (greedy)."""
|
| 341 |
+
return _translate_text(text)
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
@_gpu
|
| 345 |
+
def adjust_tone(
|
| 346 |
+
source: str,
|
| 347 |
+
draft: str,
|
| 348 |
+
wiki: str,
|
| 349 |
+
char_name: str,
|
| 350 |
+
context_lines: list[str] | None = None,
|
| 351 |
+
) -> str:
|
| 352 |
+
"""Rewrite a draft translation in the character's voice via Gemma 4."""
|
| 353 |
+
return _adjust_tone_text(source, draft, wiki, char_name, context_lines)
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
@_gpu
|
| 357 |
+
def translate_and_tone_items(
|
| 358 |
+
items: list[dict],
|
| 359 |
+
cache: dict,
|
| 360 |
+
wiki: str | None,
|
| 361 |
+
char_name: str | None,
|
| 362 |
+
context_window: int = 2,
|
| 363 |
+
) -> tuple[list[dict], dict, dict]:
|
| 364 |
+
"""Translate and tone a full review record in one GPU allocation."""
|
| 365 |
+
cache = dict(cache)
|
| 366 |
+
translated = 0
|
| 367 |
+
toned = 0
|
| 368 |
+
|
| 369 |
+
for item in items:
|
| 370 |
+
if item.get("mt") is not None:
|
| 371 |
+
continue
|
| 372 |
+
source = item["source"]
|
| 373 |
+
cached = cache.get(source)
|
| 374 |
+
if cached is not None:
|
| 375 |
+
item["mt"] = cached
|
| 376 |
+
else:
|
| 377 |
+
item["mt"] = _translate_text(source)
|
| 378 |
+
cache[source] = item["mt"]
|
| 379 |
+
translated += 1
|
| 380 |
+
|
| 381 |
+
if wiki and char_name:
|
| 382 |
+
sources = [item["source"] for item in items]
|
| 383 |
+
for i, item in enumerate(items):
|
| 384 |
+
if item.get("kind") != "dialogue" or item.get("toned") is not None:
|
| 385 |
+
continue
|
| 386 |
+
if item.get("mt") is None:
|
| 387 |
+
continue
|
| 388 |
+
context = sources[max(0, i - context_window) : i]
|
| 389 |
+
item["toned"] = _adjust_tone_text(
|
| 390 |
+
item["source"], item["mt"], wiki, char_name, context
|
| 391 |
+
)
|
| 392 |
+
toned += 1
|
| 393 |
+
|
| 394 |
+
return items, cache, {"translated": translated, "toned": toned}
|
| 395 |
+
|
| 396 |
+
|
| 397 |
@_gpu
|
| 398 |
def update_character_wiki(system_prompt: str, user_msg: str) -> str:
|
| 399 |
"""Update a character wiki via the same in-process Gemma runtime as tone pass."""
|