File size: 4,210 Bytes
7fed05d
 
 
 
 
85aa96a
 
 
7fed05d
 
85aa96a
7fed05d
 
 
85aa96a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fed05d
 
 
85aa96a
7fed05d
 
 
 
85aa96a
 
 
7fed05d
85aa96a
7fed05d
 
 
 
 
 
 
 
 
 
 
85aa96a
2097cc1
7fed05d
85aa96a
2097cc1
7fed05d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85aa96a
 
 
 
 
 
 
 
 
 
 
 
7fed05d
 
85aa96a
 
 
 
 
 
 
7fed05d
85aa96a
 
7fed05d
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import base64

import gradio as gr
from gradio_client import Client

API_URL = "https://s5k.cn/api/v1/studio/yangbaosong/Qwen_Turbo_MT/gradio/"
client = Client(API_URL)


def pdf2base64(file):
    """将PDF文件转换为base64编码的字符串。"""
    if file is None:
        return "从上传PDF文件开始..."
    else:
        try:
            with open(file, "rb") as f:
                encoded_pdf = base64.b64encode(f.read()).decode("utf-8")
            return f"""
                        <iframe src="data:application/pdf;base64,{encoded_pdf}"
                                width="100%"
                                height="800px">
                        </iframe>
                        """
        except Exception as e:
            return f"PDF文件加载失败: {e}"


def translate_text(text, source_language, target_language):
    """调用翻译API进行翻译。"""
    try:
        result = client.predict(
            text,
            source_language,
            target_language,
            "",
            "",
            "",
            "",
            "",
            "",
            api_name="/model_chat",
        )
        return result
    except Exception as e:
        return f"翻译失败: {e}"


def update_chat_history(text, translation, chat_history):
    """更新聊天记录。"""
    chat_history.append((text, translation))
    return chat_history


def clear_text(txt2translate, chatbox):
    """清空文本框和聊天记录。"""
    return "", [], gr.update(visible=False)


with gr.Blocks() as demo:
    source_language = gr.State("English")
    target_language = gr.State("Chinese")

    with gr.Row():
        with gr.Column(scale=2):
            output_pdf = gr.HTML("从上传PDF文件开始...")
        with gr.Column():
            with gr.Tab(label="传递PDF文件"):
                input_pdf = gr.File(label="上传PDF文件")
                explorer = gr.FileExplorer(
                    label="选择PDF文件", file_count="single", glob="*.pdf"
                )
            with gr.Tab(label="文本翻译"):
                with gr.Row():
                    with gr.Column():
                        with gr.Row():
                            source_dropdown = gr.Dropdown(
                                value="英文", choices=["英文", "中文"], label="源语言"
                            )
                            target_dropdown = gr.Dropdown(
                                value="中文", choices=["中文", "英文"], label="目标语言"
                            )
                with gr.Row():
                    txt2translate = gr.Textbox(
                        label="输入",
                        placeholder="从PDF复制或输入文本以进行翻译",
                    )
                    with gr.Column():
                        with gr.Row():
                            translate_btn = gr.Button(value="翻译")
                            clear_btn = gr.Button(value="清空")
                with gr.Column():
                    chatbox = gr.Chatbot(
                        show_copy_button=True,
                    )

    def update_source_language(value):
        if value == "英文":
            return "English"
        elif value == "中文":
            return "Chinese"

    def update_target_language(value):
        if value == "中文":
            return "Chinese"
        elif value == "英文":
            return "English"

    input_pdf.change(pdf2base64, input_pdf, output_pdf)
    explorer.change(pdf2base64, explorer, output_pdf)
    source_dropdown.change(update_source_language, source_dropdown, source_language)
    target_dropdown.change(update_target_language, target_dropdown, target_language)

    def translate_and_update(text, chat_history, source, target):
        translation = translate_text(text, source, target)
        return "", update_chat_history(text, translation, chat_history)

    translate_btn.click(
        translate_and_update,
        [txt2translate, chatbox, source_language, target_language],
        [txt2translate, chatbox],
    )
    clear_btn.click(clear_text, [txt2translate, chatbox], [txt2translate, chatbox])

demo.launch()