Files
Document_Translator/regenerate_with_original_dify.py
2025-09-03 15:07:34 +08:00

160 lines
6.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
使用修復後的邏輯重新生成韓文Excel檔案
預期: 使用原始DIFY翻譯而非手動補充翻譯
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# 設定編碼
sys.stdout.reconfigure(encoding='utf-8')
from pathlib import Path
from app import create_app
def regenerate_with_original_dify():
"""使用原始DIFY翻譯重新生成韓文Excel檔案"""
print("=" * 80)
print("使用修復後的邏輯重新生成韓文Excel檔案")
print("預期: D2應該使用原始DIFY翻譯 (包含 '와이어 본딩')")
print("=" * 80)
# 檔案路徑
prod_dir = Path(r"C:\Users\EGG\WORK\data\user_scrip\TOOL\Document_translator_V2\uploads\98158984-f335-44f5-a0b4-88fb8ccd5d78")
original_file = prod_dir / "original_panjit_98158984.xlsx"
if not original_file.exists():
print(f"❌ 原始文件不存在: {original_file}")
return
print(f"✅ 原始文件: {original_file.name}")
app = create_app()
with app.app_context():
from app.services.translation_service import ExcelParser
from app import db
import openpyxl
try:
print(f"\n1. 重新生成韓文翻譯檔案")
print("-" * 60)
parser = ExcelParser(str(original_file))
# 生成新的翻譯檔案 (會覆蓋舊的)
translated_file_path = parser.generate_translated_document(
translations={},
target_language='ko',
output_dir=prod_dir
)
print(f"✅ 韓文翻譯檔案已重新生成: {Path(translated_file_path).name}")
print(f"\n2. 驗證D2是否使用原始DIFY翻譯")
print("-" * 60)
# 檢查新生成的D2內容
wb_trans = openpyxl.load_workbook(translated_file_path, data_only=False)
d2_value = wb_trans.active['D2'].value
print(f"D2翻譯內容:")
print(f" {repr(d2_value)}")
# 檢查翻譯來源特徵
if isinstance(d2_value, str) and '\n' in d2_value:
lines = d2_value.split('\n')
if len(lines) >= 2:
korean_part = lines[1] # 第二行是韓文翻譯
if "와이어 본딩" in korean_part:
print(f" 🎯 ✅ 使用原始DIFY翻譯")
print(f" 特徵: 包含 '와이어 본딩'")
print(f" 韓文: {korean_part}")
result = "SUCCESS_ORIGINAL"
elif "연결" in korean_part:
print(f" ✋ ❌ 仍在使用手動補充翻譯")
print(f" 特徵: 包含 '연결'")
print(f" 韓文: {korean_part}")
result = "STILL_MANUAL"
else:
print(f" ❓ 無法判斷翻譯來源")
print(f" 韓文: {korean_part}")
result = "UNKNOWN"
else:
print(f" ❌ 格式異常,不是雙行格式")
result = "FORMAT_ERROR"
else:
print(f" ❌ D2沒有翻譯或格式不正確")
result = "NO_TRANSLATION"
wb_trans.close()
# 3. 檢查其他關鍵儲存格
print(f"\n3. 檢查其他關鍵儲存格")
print("-" * 60)
wb_trans = openpyxl.load_workbook(translated_file_path, data_only=False)
test_cells = ['D3', 'D4', 'D5']
translated_cells = 0
for cell_name in test_cells:
cell_value = wb_trans.active[cell_name].value
if isinstance(cell_value, str) and '\n' in cell_value:
lines = cell_value.split('\n')
if len(lines) >= 2:
korean_part = lines[1]
print(f"{cell_name}: 已翻譯")
print(f" 韓文: {korean_part[:30]}...")
translated_cells += 1
else:
print(f"{cell_name}: 格式異常")
else:
print(f"{cell_name}: 未翻譯")
print(f"\n其他儲存格翻譯狀況: {translated_cells}/{len(test_cells)} 成功")
wb_trans.close()
# 4. 最終結果評估
print(f"\n4. 最終結果評估")
print("-" * 60)
if result == "SUCCESS_ORIGINAL":
print(f"🎉 完美!修復成功")
print(f" ✅ D2正確使用原始DIFY翻譯")
print(f" ✅ 翻譯品質: 原始API翻譯 (更準確)")
print(f" ✅ 問題根源已解決: 文字格式不匹配")
elif result == "STILL_MANUAL":
print(f"⚠️ 部分成功")
print(f" ❌ D2仍使用手動翻譯")
print(f" ❓ 可能需要檢查查詢邏輯或重新啟動Celery")
else:
print(f"❌ 修復失敗")
print(f" 需要進一步排查問題")
# 5. 檔案資訊
print(f"\n5. 檔案資訊")
print("-" * 60)
print(f"韓文翻譯檔案:")
print(f" 檔案名稱: {Path(translated_file_path).name}")
print(f" 檔案路徑: {translated_file_path}")
print(f" 檔案大小: {Path(translated_file_path).stat().st_size / 1024:.1f} KB")
except Exception as e:
print(f"❌ 重新生成韓文翻譯檔案時發生錯誤: {str(e)}")
import traceback
print(f"錯誤詳情: {traceback.format_exc()}")
print(f"\n" + "=" * 80)
print("使用原始DIFY翻譯重新生成完成")
print("=" * 80)
if __name__ == "__main__":
regenerate_with_original_dify()