Spaces:
Sleeping
Sleeping
Add application file
Browse files
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
6 |
+
|
7 |
+
from utils import ArgumentParser, LOG
|
8 |
+
from translator import PDFTranslator, TranslationConfig
|
9 |
+
|
10 |
+
|
11 |
+
def translation(input_file, source_language, target_language):
|
12 |
+
LOG.debug(f"[翻译任务]\n源文件: {input_file.name}\n源语言: {source_language}\n目标语言: {target_language}")
|
13 |
+
|
14 |
+
output_file_path = Translator.translate_pdf(
|
15 |
+
input_file.name, source_language=source_language, target_language=target_language)
|
16 |
+
|
17 |
+
return output_file_path
|
18 |
+
|
19 |
+
def launch_gradio():
|
20 |
+
|
21 |
+
iface = gr.Interface(
|
22 |
+
fn=translation,
|
23 |
+
title="OpenAI-Translator v2.0(PDF 电子书翻译工具)",
|
24 |
+
inputs=[
|
25 |
+
gr.File(label="上传PDF文件"),
|
26 |
+
gr.Textbox(label="源语言(默认:英文)", placeholder="English", value="English"),
|
27 |
+
gr.Textbox(label="目标语言(默认:中文)", placeholder="Chinese", value="Chinese")
|
28 |
+
],
|
29 |
+
outputs=[
|
30 |
+
gr.File(label="下载翻译文件")
|
31 |
+
],
|
32 |
+
allow_flagging="never"
|
33 |
+
)
|
34 |
+
|
35 |
+
iface.launch(share=True, server_name="127.0.0.1")
|
36 |
+
|
37 |
+
def initialize_translator():
|
38 |
+
# 解析命令行
|
39 |
+
argument_parser = ArgumentParser()
|
40 |
+
args = argument_parser.parse_arguments()
|
41 |
+
|
42 |
+
# 初始化配置单例
|
43 |
+
config = TranslationConfig()
|
44 |
+
config.initialize(args)
|
45 |
+
# 实例化 PDFTranslator 类,并调用 translate_pdf() 方法
|
46 |
+
global Translator
|
47 |
+
Translator = PDFTranslator(config.model_name)
|
48 |
+
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
# 初始化 translator
|
52 |
+
initialize_translator()
|
53 |
+
# 启动 Gradio 服务
|
54 |
+
launch_gradio()
|