moyanxinxu commited on
Commit
85aa96a
·
verified ·
1 Parent(s): 2097cc1
Files changed (1) hide show
  1. app.py +69 -54
app.py CHANGED
@@ -3,65 +3,65 @@ import base64
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文件"):
@@ -73,10 +73,10 @@ with gr.Blocks() as demo:
73
  with gr.Row():
74
  with gr.Column():
75
  with gr.Row():
76
- sorce_language = gr.Dropdown(
77
  value="英文", choices=["英文", "中文"], label="源语言"
78
  )
79
- target_language = gr.Dropdown(
80
  value="中文", choices=["中文", "英文"], label="目标语言"
81
  )
82
  with gr.Row():
@@ -84,7 +84,6 @@ with gr.Blocks() as demo:
84
  label="输入",
85
  placeholder="从PDF复制或输入文本以进行翻译",
86
  )
87
-
88
  with gr.Column():
89
  with gr.Row():
90
  translate_btn = gr.Button(value="翻译")
@@ -94,16 +93,32 @@ with gr.Blocks() as demo:
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()
 
3
  import gradio as gr
4
  from gradio_client import Client
5
 
6
+ API_URL = "https://s5k.cn/api/v1/studio/yangbaosong/Qwen_Turbo_MT/gradio/"
7
+ client = Client(API_URL)
8
+
9
 
10
  def pdf2base64(file):
11
+ """将PDF文件转换为base64编码的字符串。"""
12
  if file is None:
13
  return "从上传PDF文件开始..."
14
  else:
15
+ try:
16
+ with open(file, "rb") as f:
17
+ encoded_pdf = base64.b64encode(f.read()).decode("utf-8")
18
+ return f"""
19
+ <iframe src="data:application/pdf;base64,{encoded_pdf}"
20
+ width="100%"
21
+ height="800px">
22
+ </iframe>
23
+ """
24
+ except Exception as e:
25
+ return f"PDF文件加载失败: {e}"
26
+
27
+
28
+ def translate_text(text, source_language, target_language):
29
+ """调用翻译API进行翻译。"""
30
+ try:
31
+ result = client.predict(
32
+ text,
33
+ source_language,
34
+ target_language,
35
+ "",
36
+ "",
37
+ "",
38
+ "",
39
+ "",
40
+ "",
41
+ api_name="/model_chat",
42
+ )
43
+ return result
44
+ except Exception as e:
45
+ return f"翻译失败: {e}"
46
+
47
+
48
+ def update_chat_history(text, translation, chat_history):
49
+ """更新聊天记录。"""
50
+ chat_history.append((text, translation))
51
+ return chat_history
 
 
 
 
 
52
 
53
 
54
  def clear_text(txt2translate, chatbox):
55
+ """清空文本框和聊天记录。"""
56
  return "", [], gr.update(visible=False)
57
 
58
 
59
  with gr.Blocks() as demo:
60
+ source_language = gr.State("English")
61
+ target_language = gr.State("Chinese")
62
+
63
  with gr.Row():
64
+ with gr.Column(scale=2):
65
  output_pdf = gr.HTML("从上传PDF文件开始...")
66
  with gr.Column():
67
  with gr.Tab(label="传递PDF文件"):
 
73
  with gr.Row():
74
  with gr.Column():
75
  with gr.Row():
76
+ source_dropdown = gr.Dropdown(
77
  value="英文", choices=["英文", "中文"], label="源语言"
78
  )
79
+ target_dropdown = gr.Dropdown(
80
  value="中文", choices=["中文", "英文"], label="目标语言"
81
  )
82
  with gr.Row():
 
84
  label="输入",
85
  placeholder="从PDF复制或输入文本以进行翻译",
86
  )
 
87
  with gr.Column():
88
  with gr.Row():
89
  translate_btn = gr.Button(value="翻译")
 
93
  show_copy_button=True,
94
  )
95
 
96
+ def update_source_language(value):
97
+ if value == "英文":
98
+ return "English"
99
+ elif value == "中文":
100
+ return "Chinese"
101
+
102
+ def update_target_language(value):
103
+ if value == "中文":
104
+ return "Chinese"
105
+ elif value == "英文":
106
+ return "English"
107
+
108
  input_pdf.change(pdf2base64, input_pdf, output_pdf)
109
  explorer.change(pdf2base64, explorer, output_pdf)
110
+ source_dropdown.change(update_source_language, source_dropdown, source_language)
111
+ target_dropdown.change(update_target_language, target_dropdown, target_language)
112
+
113
+ def translate_and_update(text, chat_history, source, target):
114
+ translation = translate_text(text, source, target)
115
+ return "", update_chat_history(text, translation, chat_history)
116
+
117
  translate_btn.click(
118
+ translate_and_update,
119
+ [txt2translate, chatbox, source_language, target_language],
120
  [txt2translate, chatbox],
121
  )
122
  clear_btn.click(clear_text, [txt2translate, chatbox], [txt2translate, chatbox])
123
 
 
124
  demo.launch()