fix: sync eqbench-ja/translate_eqbench.py
Browse files- eqbench-ja/translate_eqbench.py +29 -29
eqbench-ja/translate_eqbench.py
CHANGED
|
@@ -302,25 +302,19 @@ async def translate_scenarios(
|
|
| 302 |
scenarios: list[dict],
|
| 303 |
state: dict,
|
| 304 |
dry_run: bool,
|
|
|
|
| 305 |
) -> dict:
|
| 306 |
-
"""全シナリオを翻訳する。"""
|
| 307 |
translated = state.get("translated_scenarios", {})
|
| 308 |
target_scenarios = scenarios[:2] if dry_run else scenarios
|
|
|
|
| 309 |
|
| 310 |
-
print(f"[translate] シナリオ翻訳開始: {len(target_scenarios)}件")
|
| 311 |
-
pbar = tqdm(target_scenarios, desc="シナリオ翻訳")
|
| 312 |
|
| 313 |
-
|
| 314 |
sid = str(scenario["id"])
|
| 315 |
-
if sid in translated:
|
| 316 |
-
pbar.set_description(f"シナリオ {sid} スキップ(翻訳済み)")
|
| 317 |
-
continue
|
| 318 |
-
|
| 319 |
-
pbar.set_description(f"シナリオ {sid} 翻訳中...")
|
| 320 |
trans_scenario: dict = {"header": scenario["header"], "prompts": {}}
|
| 321 |
|
| 322 |
-
# ヘッダー(カテゴリ名など)は翻訳不要(IDと構造を保持)
|
| 323 |
-
# Prompt本文を個別に翻訳
|
| 324 |
for prompt_key, prompt_text in scenario["prompts"].items():
|
| 325 |
if not prompt_text.strip():
|
| 326 |
trans_scenario["prompts"][prompt_key] = prompt_text
|
|
@@ -330,12 +324,15 @@ async def translate_scenarios(
|
|
| 330 |
trans_scenario["prompts"][prompt_key] = translated_text
|
| 331 |
except Exception as e:
|
| 332 |
print(f"\n[translate] WARNING: シナリオ{sid}/{prompt_key} 翻訳失敗: {e}")
|
| 333 |
-
trans_scenario["prompts"][prompt_key] = prompt_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
|
| 335 |
-
|
| 336 |
-
state["translated_scenarios"] = translated
|
| 337 |
-
save_checkpoint(state)
|
| 338 |
-
pbar.set_description(f"シナリオ {sid} 完了")
|
| 339 |
|
| 340 |
print(f"[translate] シナリオ翻訳完了: {len(translated)}件")
|
| 341 |
return translated
|
|
@@ -346,27 +343,28 @@ async def translate_notes(
|
|
| 346 |
notes: dict[str, str],
|
| 347 |
state: dict,
|
| 348 |
dry_run: bool,
|
|
|
|
| 349 |
) -> dict:
|
| 350 |
-
"""採点ノートを翻訳する。"""
|
| 351 |
translated_notes = state.get("translated_notes", {})
|
| 352 |
target_notes = dict(list(notes.items())[:2]) if dry_run else notes
|
|
|
|
| 353 |
|
| 354 |
-
print(f"[translate] ノート翻訳開始: {len(target_notes)}件")
|
| 355 |
-
pbar = tqdm(target_notes.items(), desc="ノート翻訳")
|
| 356 |
|
| 357 |
-
|
| 358 |
-
if key in translated_notes:
|
| 359 |
-
continue
|
| 360 |
-
pbar.set_description(f"ノート #{key} 翻訳中...")
|
| 361 |
try:
|
| 362 |
translated_text = await client.translate(note)
|
| 363 |
-
translated_notes[key] = translated_text
|
| 364 |
except Exception as e:
|
| 365 |
print(f"\n[translate] WARNING: ノート#{key} 翻訳失敗: {e}")
|
| 366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
-
|
| 369 |
-
save_checkpoint(state)
|
| 370 |
|
| 371 |
print(f"[translate] ノート翻訳完了: {len(translated_notes)}件")
|
| 372 |
return translated_notes
|
|
@@ -411,17 +409,19 @@ async def main(dry_run: bool, target_file: str | None) -> None:
|
|
| 411 |
async with VLLMClient() as client:
|
| 412 |
await client.wait_for_server()
|
| 413 |
|
|
|
|
|
|
|
| 414 |
# ── 翻訳実行 ────────────────────────────────────────────
|
| 415 |
do_scenarios = target_file is None or target_file == "scenario_prompts.txt"
|
| 416 |
do_notes = target_file is None or target_file == "scenario_notes.txt"
|
| 417 |
|
| 418 |
if do_scenarios:
|
| 419 |
-
translated_scenarios = await translate_scenarios(client, scenarios, state, dry_run)
|
| 420 |
else:
|
| 421 |
translated_scenarios = state.get("translated_scenarios", {})
|
| 422 |
|
| 423 |
if do_notes:
|
| 424 |
-
translated_notes = await translate_notes(client, notes, state, dry_run)
|
| 425 |
else:
|
| 426 |
translated_notes = state.get("translated_notes", {})
|
| 427 |
|
|
|
|
| 302 |
scenarios: list[dict],
|
| 303 |
state: dict,
|
| 304 |
dry_run: bool,
|
| 305 |
+
lock: asyncio.Lock,
|
| 306 |
) -> dict:
|
| 307 |
+
"""全シナリオを asyncio.gather で並列翻訳する。"""
|
| 308 |
translated = state.get("translated_scenarios", {})
|
| 309 |
target_scenarios = scenarios[:2] if dry_run else scenarios
|
| 310 |
+
pending = [s for s in target_scenarios if str(s["id"]) not in translated]
|
| 311 |
|
| 312 |
+
print(f"[translate] シナリオ翻訳開始: {len(target_scenarios)}件 (未翻訳: {len(pending)}件)")
|
|
|
|
| 313 |
|
| 314 |
+
async def _translate_one(scenario: dict) -> None:
|
| 315 |
sid = str(scenario["id"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
trans_scenario: dict = {"header": scenario["header"], "prompts": {}}
|
| 317 |
|
|
|
|
|
|
|
| 318 |
for prompt_key, prompt_text in scenario["prompts"].items():
|
| 319 |
if not prompt_text.strip():
|
| 320 |
trans_scenario["prompts"][prompt_key] = prompt_text
|
|
|
|
| 324 |
trans_scenario["prompts"][prompt_key] = translated_text
|
| 325 |
except Exception as e:
|
| 326 |
print(f"\n[translate] WARNING: シナリオ{sid}/{prompt_key} 翻訳失敗: {e}")
|
| 327 |
+
trans_scenario["prompts"][prompt_key] = prompt_text
|
| 328 |
+
|
| 329 |
+
async with lock:
|
| 330 |
+
translated[sid] = trans_scenario
|
| 331 |
+
state["translated_scenarios"] = translated
|
| 332 |
+
save_checkpoint(state)
|
| 333 |
+
print(f"[translate] シナリオ {sid} 完了")
|
| 334 |
|
| 335 |
+
await asyncio.gather(*[_translate_one(s) for s in pending])
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
print(f"[translate] シナリオ翻訳完了: {len(translated)}件")
|
| 338 |
return translated
|
|
|
|
| 343 |
notes: dict[str, str],
|
| 344 |
state: dict,
|
| 345 |
dry_run: bool,
|
| 346 |
+
lock: asyncio.Lock,
|
| 347 |
) -> dict:
|
| 348 |
+
"""採点ノートを asyncio.gather で並列翻訳する。"""
|
| 349 |
translated_notes = state.get("translated_notes", {})
|
| 350 |
target_notes = dict(list(notes.items())[:2]) if dry_run else notes
|
| 351 |
+
pending = {k: v for k, v in target_notes.items() if k not in translated_notes}
|
| 352 |
|
| 353 |
+
print(f"[translate] ノート翻訳開始: {len(target_notes)}件 (未翻訳: {len(pending)}件)")
|
|
|
|
| 354 |
|
| 355 |
+
async def _translate_one(key: str, note: str) -> None:
|
|
|
|
|
|
|
|
|
|
| 356 |
try:
|
| 357 |
translated_text = await client.translate(note)
|
|
|
|
| 358 |
except Exception as e:
|
| 359 |
print(f"\n[translate] WARNING: ノート#{key} 翻訳失敗: {e}")
|
| 360 |
+
translated_text = note
|
| 361 |
+
async with lock:
|
| 362 |
+
translated_notes[key] = translated_text
|
| 363 |
+
state["translated_notes"] = translated_notes
|
| 364 |
+
save_checkpoint(state)
|
| 365 |
+
print(f"[translate] ノート #{key} 完了")
|
| 366 |
|
| 367 |
+
await asyncio.gather(*[_translate_one(k, v) for k, v in pending.items()])
|
|
|
|
| 368 |
|
| 369 |
print(f"[translate] ノート翻訳完了: {len(translated_notes)}件")
|
| 370 |
return translated_notes
|
|
|
|
| 409 |
async with VLLMClient() as client:
|
| 410 |
await client.wait_for_server()
|
| 411 |
|
| 412 |
+
lock = asyncio.Lock() # チェックポイント書き込みの競合を防ぐ
|
| 413 |
+
|
| 414 |
# ── 翻訳実行 ────────────────────────────────────────────
|
| 415 |
do_scenarios = target_file is None or target_file == "scenario_prompts.txt"
|
| 416 |
do_notes = target_file is None or target_file == "scenario_notes.txt"
|
| 417 |
|
| 418 |
if do_scenarios:
|
| 419 |
+
translated_scenarios = await translate_scenarios(client, scenarios, state, dry_run, lock)
|
| 420 |
else:
|
| 421 |
translated_scenarios = state.get("translated_scenarios", {})
|
| 422 |
|
| 423 |
if do_notes:
|
| 424 |
+
translated_notes = await translate_notes(client, notes, state, dry_run, lock)
|
| 425 |
else:
|
| 426 |
translated_notes = state.get("translated_notes", {})
|
| 427 |
|