Initial commit
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
EPUB 翻译器使用示例
|
||||
演示如何使用程序进行翻译
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# 添加 src 目录到路径
|
||||
sys.path.insert(0, str(Path(__file__).parent / "src"))
|
||||
|
||||
from src.translator import EPUBTranslator
|
||||
from src.utils import load_config, setup_logging
|
||||
from rich.console import Console
|
||||
|
||||
|
||||
async def example_usage():
|
||||
"""使用示例"""
|
||||
console = Console()
|
||||
|
||||
console.print("[bold blue]EPUB 翻译器使用示例[/bold blue]")
|
||||
|
||||
try:
|
||||
# 加载配置
|
||||
config = load_config('config/config.json')
|
||||
setup_logging(config)
|
||||
|
||||
# 初始化翻译器
|
||||
translator = EPUBTranslator(config)
|
||||
|
||||
# 示例 EPUB 文件路径(请替换为实际文件)
|
||||
epub_file = "sample_book.epub"
|
||||
|
||||
if not Path(epub_file).exists():
|
||||
console.print(f"[yellow]示例文件 {epub_file} 不存在[/yellow]")
|
||||
console.print("请将你的 EPUB 文件放在当前目录并重命名为 sample_book.epub")
|
||||
return
|
||||
|
||||
# 1. 估算翻译成本
|
||||
console.print("\n[cyan]1. 估算翻译成本...[/cyan]")
|
||||
estimate = await translator.get_translation_estimate(epub_file)
|
||||
|
||||
if estimate:
|
||||
console.print(f"总段落数: {estimate['total_paragraphs']}")
|
||||
console.print(f"估算时间: {estimate['estimated_time_minutes']:.1f} 分钟")
|
||||
console.print(f"估算请求数: {estimate['estimated_requests']}")
|
||||
|
||||
# 2. 测试翻译
|
||||
console.print("\n[cyan]2. 运行测试翻译...[/cyan]")
|
||||
test_result = await translator.translate_epub(epub_file, test_mode=True)
|
||||
|
||||
if test_result.get('status') == 'success':
|
||||
console.print("[green]测试翻译成功![/green]")
|
||||
else:
|
||||
console.print("[red]测试翻译失败[/red]")
|
||||
return
|
||||
|
||||
# 3. 询问是否继续完整翻译
|
||||
console.print("\n[yellow]是否继续完整翻译?这可能需要一些时间和费用。[/yellow]")
|
||||
response = input("输入 'yes' 继续,其他任意键退出: ")
|
||||
|
||||
if response.lower() == 'yes':
|
||||
console.print("\n[cyan]3. 开始完整翻译...[/cyan]")
|
||||
output_file = await translator.translate_epub(epub_file, test_mode=False)
|
||||
console.print(f"[green]翻译完成!输出文件: {output_file}[/green]")
|
||||
else:
|
||||
console.print("[yellow]已取消完整翻译[/yellow]")
|
||||
|
||||
except Exception as e:
|
||||
console.print(f"[red]示例运行失败: {e}[/red]")
|
||||
|
||||
finally:
|
||||
await translator.llm_client.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(example_usage())
|
||||
Reference in New Issue
Block a user