Spaces:
Running
Running
moyanxinxu
commited on
create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from gradio_client import Client
|
5 |
+
|
6 |
+
client = Client("https://s5k.cn/api/v1/studio/yangbaosong/Qwen_Turbo_MT/gradio/")
|
7 |
+
source = "English"
|
8 |
+
target = "Chinese"
|
9 |
+
|
10 |
+
def pdf2base64(file):
|
11 |
+
if file is None:
|
12 |
+
return "从上传PDF文件开始..."
|
13 |
+
else:
|
14 |
+
with open(file, "rb") as f:
|
15 |
+
encoded_pdf = base64.b64encode(f.read()).decode("utf-8")
|
16 |
+
return f"""
|
17 |
+
<iframe src="data:application/pdf;base64,{encoded_pdf}"
|
18 |
+
width="100%"
|
19 |
+
height="800px">
|
20 |
+
</iframe>
|
21 |
+
"""
|
22 |
+
|
23 |
+
|
24 |
+
def change_source_language(sorce_language):
|
25 |
+
global source
|
26 |
+
if sorce_language == "英文":
|
27 |
+
source = "English"
|
28 |
+
elif sorce_language == "中文":
|
29 |
+
source = "Chinese"
|
30 |
+
|
31 |
+
|
32 |
+
def change_target_language(target_language):
|
33 |
+
global target
|
34 |
+
if target_language == "中文":
|
35 |
+
target = "Chinese"
|
36 |
+
elif target_language == "英文":
|
37 |
+
target = "English"
|
38 |
+
|
39 |
+
|
40 |
+
def translate_and_display(text, chat_history):
|
41 |
+
global source, target
|
42 |
+
result = client.predict(
|
43 |
+
text, # str in 'Input' Textbox component
|
44 |
+
source, # Literal['Chinese', 'English', 'Japanese', 'Korean', 'Thai', 'French', 'German', 'Spanish', 'Arabic', 'Indonesian', 'Vietnamese', 'Portuguese'] in 'Source Language' Dropdown component
|
45 |
+
target, # Literal['Chinese', 'English', 'Japanese', 'Korean', 'Thai', 'French', 'German', 'Spanish', 'Arabic', 'Indonesian', 'Vietnamese', 'Portuguese'] in 'Target Language' Dropdown component
|
46 |
+
"",
|
47 |
+
"",
|
48 |
+
"",
|
49 |
+
"",
|
50 |
+
"",
|
51 |
+
"",
|
52 |
+
api_name="/model_chat",
|
53 |
+
)
|
54 |
+
chat_history.append((text, result))
|
55 |
+
return "", chat_history
|
56 |
+
|
57 |
+
|
58 |
+
def clear_text(txt2translate, chatbox):
|
59 |
+
return "", [], gr.update(visible=False)
|
60 |
+
|
61 |
+
|
62 |
+
with gr.Blocks() as demo:
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column(scale=2): # PDF栏占比更大
|
65 |
+
output_pdf = gr.HTML("从上传PDF文件开始...")
|
66 |
+
with gr.Column():
|
67 |
+
with gr.Tab(label="传递PDF文件"):
|
68 |
+
input_pdf = gr.File(label="上传PDF文件")
|
69 |
+
explorer = gr.FileExplorer(
|
70 |
+
label="选择PDF文件", file_count="single", glob="*.pdf"
|
71 |
+
)
|
72 |
+
with gr.Tab(label="文本翻译"):
|
73 |
+
with gr.Row():
|
74 |
+
with gr.Column():
|
75 |
+
with gr.Row():
|
76 |
+
sorce_language = gr.Dropdown(
|
77 |
+
choices=["英文", "中文"], label="源语言"
|
78 |
+
)
|
79 |
+
target_language = gr.Dropdown(
|
80 |
+
choices=["中文", "英文"], label="目标语言"
|
81 |
+
)
|
82 |
+
with gr.Row():
|
83 |
+
txt2translate = gr.Textbox(
|
84 |
+
label="输入",
|
85 |
+
placeholder="从PDF复制或输入文本以进行翻译",
|
86 |
+
)
|
87 |
+
|
88 |
+
with gr.Column():
|
89 |
+
with gr.Row():
|
90 |
+
translate_btn = gr.Button(value="翻译")
|
91 |
+
clear_btn = gr.Button(value="清空")
|
92 |
+
with gr.Column():
|
93 |
+
chatbox = gr.Chatbot(
|
94 |
+
show_copy_button=True,
|
95 |
+
)
|
96 |
+
|
97 |
+
input_pdf.change(pdf2base64, input_pdf, output_pdf)
|
98 |
+
explorer.change(pdf2base64, explorer, output_pdf)
|
99 |
+
sorce_language.change(change_source_language, sorce_language)
|
100 |
+
target_language.change(change_target_language, target_language)
|
101 |
+
translate_btn.click(
|
102 |
+
translate_and_display,
|
103 |
+
[txt2translate, chatbox],
|
104 |
+
[txt2translate, chatbox],
|
105 |
+
)
|
106 |
+
clear_btn.click(clear_text, [txt2translate, chatbox], [txt2translate, chatbox])
|
107 |
+
|
108 |
+
|
109 |
+
demo.launch()
|