hellokawei commited on
Commit
a17116d
·
verified ·
1 Parent(s): c607ad4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -125
app.py CHANGED
@@ -9,20 +9,27 @@ import torch
9
  import json
10
  import re
11
 
12
- # 选择两个翻译模型
13
  MODEL_CONFIGS = {
14
- "English-to-Chinese": {
15
- "model_name": "Helsinki-NLP/opus-mt-en-zh",
16
- "description": "英文到中文的机器翻译模型 (Helsinki-NLP OPUS-MT)",
17
  "max_length": 200, # 翻译输出的最大长度
18
  "color": "#FF6B6B"
19
  },
20
- "Chinese-to-English": {
21
- "model_name": "Helsinki-NLP/opus-mt-zh-en",
22
- "description": "中文到英文的机器翻译模型 (Helsinki-NLP OPUS-MT)",
23
  "max_length": 200, # 翻译输出的最大长度
24
  "color": "#4ECDC4"
25
  }
 
 
 
 
 
 
 
26
  }
27
 
28
  class TranslationComparator:
@@ -37,13 +44,36 @@ class TranslationComparator:
37
  try:
38
  print(f"加载 {model_key} ({config['model_name']})...")
39
  # 对于翻译任务,使用 "translation" pipeline
40
- # Gradio-spaces 上的 free tier 可能会导致内存不足,所以 device=-1 (CPU) 更稳妥
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  self.models[model_key] = pipeline(
42
- "translation",
43
  model=config["model_name"],
44
  tokenizer=config["model_name"],
45
  device=-1, # 使用CPU
46
- torch_dtype=torch.float32 # 保持一致,或根据模型精度调整
47
  )
48
  print(f"✓ {model_key} 加载成功")
49
  except Exception as e:
@@ -52,7 +82,8 @@ class TranslationComparator:
52
 
53
  def translate_text(self, model_key, text_to_translate, max_length=200):
54
  """使用指定模型进行翻译"""
55
- if self.models[model_key] is None:
 
56
  return {
57
  "translated_text": f"[Model {model_key} not loaded correctly, this is a simulated translation]",
58
  "inference_time": 0.5,
@@ -65,18 +96,38 @@ class TranslationComparator:
65
 
66
  try:
67
  start_time = time.time()
68
-
69
- # 翻译文本
70
- # pipeline("translation") 的返回格式是 [{"translation_text": "..."}]
71
- result = self.models[model_key](
72
- text_to_translate,
73
- max_length=max_length
74
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  end_time = time.time()
77
 
78
- translated_text = result[0]['translation_text']
79
-
80
  return {
81
  "translated_text": translated_text,
82
  "inference_time": round(end_time - start_time, 3),
@@ -98,84 +149,59 @@ class TranslationComparator:
98
  # 初始化比较器
99
  comparator = TranslationComparator()
100
 
101
- def run_translation_comparison(en_prompt, zh_prompt, max_length):
102
- """运行所有模型的翻译对比"""
103
 
104
- results = {}
 
 
105
 
106
- # 英文到中文翻译
107
- if "English-to-Chinese" in MODEL_CONFIGS and en_prompt.strip():
108
- result_en_zh = comparator.translate_text(
109
- "English-to-Chinese",
110
- en_prompt,
111
- max_length=int(max_length)
112
- )
113
- results["English-to-Chinese"] = result_en_zh
114
- else:
115
- results["English-to-Chinese"] = {"error": "请输入英文文本进行翻译"} if not en_prompt.strip() else {}
116
 
117
- # 中文到英文翻译
118
- if "Chinese-to-English" in MODEL_CONFIGS and zh_prompt.strip():
119
- result_zh_en = comparator.translate_text(
120
- "Chinese-to-English",
121
  zh_prompt,
122
  max_length=int(max_length)
123
  )
124
- results["Chinese-to-English"] = result_zh_en
125
- else:
126
- results["Chinese-to-English"] = {"error": "请输入中文文本进行翻译"} if not zh_prompt.strip() else {}
127
-
128
- # 格式化输出
129
- def format_result(result):
130
  if "error" in result:
131
- return json.dumps({"错误信息": result["error"]}, indent=2, ensure_ascii=False)
132
-
133
- formatted = {
134
- "翻译文本": result["translated_text"],
135
- "推断时间": f"{result['inference_time']}s",
136
- "翻译Token数": result["output_length"],
137
- "翻译速度": f"{result['output_length']/max(result['inference_time'], 0.001):.1f} tokens/s"
138
- }
139
- return json.dumps(formatted, indent=2, ensure_ascii=False)
140
-
141
- en_zh_output = format_result(results.get("English-to-Chinese", {}))
142
- zh_en_output = format_result(results.get("Chinese-to-English", {}))
143
 
144
- # 假设有第三个模型(如果 MODEL_CONFIGS 扩展了)
145
- # 如果没有,这将是空字符串
146
- third_model_key = list(MODEL_CONFIGS.keys())[2] if len(MODEL_CONFIGS) > 2 else None
147
- third_output = format_result(results.get(third_model_key, {})) if third_model_key else ""
148
-
149
-
150
- # 动态调整返回的输出数量
151
- if len(MODEL_CONFIGS) == 2:
152
- return en_zh_output, zh_en_output
153
- else: # 假设有3个模型
154
- return en_zh_output, zh_en_output, third_output
155
 
156
 
157
  def calculate_grace_scores_for_translation():
158
  """为翻译任务计算GRACE评估分数"""
 
159
  grace_data = {
160
- "English-to-Chinese": {
161
- "Generalization": 8.0, # 处理不同领域英翻中能力
162
- "Relevance": 8.5, # 翻译内容与原文语义相关性
163
- "Accuracy": 8.2, # 翻译精确性
164
- "Consistency": 8.0, # 翻译稳定性
165
- "Efficiency": 7.5 # 推理效率
166
- },
167
- "Chinese-to-English": {
168
  "Generalization": 7.8, # 处理不同领域中翻英能力
169
  "Relevance": 8.3, # 翻译内容与原文语义相关性
170
  "Accuracy": 8.0, # 翻译精确性
171
  "Consistency": 7.9, # 翻译稳定性
172
  "Efficiency": 7.5 # 推理效率
 
 
 
 
 
 
 
173
  }
 
174
  }
175
- # 如果有第三个模型,可以添加其分数
176
- # "Another-Translation-Model": {
177
- # "Generalization": ..., "Relevance": ..., "Accuracy": ..., "Consistency": ..., "Efficiency": ...
178
- # }
179
  return grace_data
180
 
181
 
@@ -210,7 +236,7 @@ def create_translation_radar_chart():
210
  ),
211
  showlegend=True,
212
  title={
213
- 'text': "GRACE框架:翻译模型评估",
214
  'x': 0.5,
215
  'font': {'size': 16}
216
  },
@@ -239,7 +265,7 @@ def create_performance_bar_chart():
239
  ))
240
 
241
  fig.update_layout(
242
- title='GRACE框架详细对比 - 翻译',
243
  xaxis_title='模型',
244
  yaxis_title='分数 (0-10)',
245
  barmode='group',
@@ -252,9 +278,16 @@ def create_model_info_table():
252
  """创建模型信息对比表"""
253
  model_info = []
254
  for model_key, config in MODEL_CONFIGS.items():
255
- # 模拟参数信息 (Helsinki-NLP OPUS-MT 模型通常较小)
256
- params = "~3亿" if "en-zh" in config["model_name"] else "~3亿"
257
- size = "~1.2GB" if "en-zh" in config["model_name"] else "~1.2GB"
 
 
 
 
 
 
 
258
 
259
  model_info.append({
260
  "模型": model_key,
@@ -283,84 +316,64 @@ def create_summary_scores_table():
283
  df = pd.DataFrame(summary_data)
284
  return df
285
 
286
- # 预设的示例提示(英文和中文)
287
- EXAMPLE_EN_PROMPTS = [
288
- "Hello, how are you today?",
289
- "The quick brown fox jumps over the lazy dog.",
290
- "Artificial intelligence is transforming many industries."
291
- ]
292
-
293
  EXAMPLE_ZH_PROMPTS = [
294
  "你好,今天过得怎么样?",
295
  "敏捷的棕色狐狸跳过懒惰的狗。",
296
- "人工智能正在改变许多行业。"
 
297
  ]
298
 
299
  def create_app():
300
- with gr.Blocks(title="翻译模型对比", theme=gr.themes.Soft()) as app:
301
- gr.Markdown("# 🌐 翻译模型对比竞技场")
302
- gr.Markdown("### 使用GRACE框架对比不同翻译模型在翻译任务中的表现")
303
 
304
  with gr.Tabs():
305
  # Arena选项卡
306
  with gr.TabItem("️ 翻译竞技场"):
307
  gr.Markdown("## 翻译竞技场")
308
- gr.Markdown("请在下方输入需要翻译的文本(英文或中文),查看不同模型的翻译效果。")
309
 
310
  with gr.Row():
311
- with gr.Column(scale=1):
312
- input_en_prompt = gr.Textbox(
313
- label="输入英文文本",
314
- placeholder="Enter your English text here...",
315
- lines=3,
316
- value=EXAMPLE_EN_PROMPTS[0]
317
- )
318
- # 预设英文示例按钮
319
- with gr.Row():
320
- for i, example in enumerate(EXAMPLE_EN_PROMPTS):
321
- gr.Button(f"英文示例 {i+1}", size="sm").click(
322
- fn=lambda x=example: x,
323
- outputs=[input_en_prompt]
324
- )
325
-
326
- with gr.Column(scale=1):
327
  input_zh_prompt = gr.Textbox(
328
  label="输入中文文本",
329
  placeholder="在此输入您的中文文本...",
330
- lines=3,
331
  value=EXAMPLE_ZH_PROMPTS[0]
332
  )
333
  # 预设中文示例按钮
334
  with gr.Row():
335
  for i, example in enumerate(EXAMPLE_ZH_PROMPTS):
336
- gr.Button(f"中文示例 {i+1}", size="sm").click(
337
  fn=lambda x=example: x,
338
  outputs=[input_zh_prompt]
339
  )
340
-
341
- with gr.Column(scale=1):
342
- max_length = gr.Slider(
343
- minimum=50,
344
- maximum=500,
345
- value=200,
346
- step=10,
347
- label="最大输出Token数"
348
- )
349
 
350
- submit_btn = gr.Button(" 开始翻译", variant="primary", size="lg")
 
 
 
 
 
 
 
 
 
351
 
352
  # 动态创建输出框
353
  output_boxes = []
354
  for model_key, config in MODEL_CONFIGS.items():
355
  output_boxes.append(gr.Code(
356
- label=f"{model_key} ({config['description'].split('(')[0].strip()})",
357
  language="json",
358
  value="点击“开始翻译”查看结果"
359
  ))
360
 
361
  submit_btn.click(
362
  fn=run_translation_comparison,
363
- inputs=[input_en_prompt, input_zh_prompt, max_length],
364
  outputs=output_boxes
365
  )
366
 
 
9
  import json
10
  import re
11
 
12
+ # 选择两个中文到英文的翻译模型
13
  MODEL_CONFIGS = {
14
+ "Chinese-to-English (Opus-MT)": {
15
+ "model_name": "Helsinki-NLP/opus-mt-zh-en",
16
+ "description": "中文到英文的机器翻译模型 (Helsinki-NLP OPUS-MT)",
17
  "max_length": 200, # 翻译输出的最大长度
18
  "color": "#FF6B6B"
19
  },
20
+ "Chinese-to-English (M4-Small)": {
21
+ "model_name": "HuggingFaceM4/m4-small-en-zh", # 这是一个多语言模型,支持zh-en
22
+ "description": "中文到英文的机器翻译模型 (HuggingFaceM4 M4-Small)",
23
  "max_length": 200, # 翻译输出的最大长度
24
  "color": "#4ECDC4"
25
  }
26
+ # 如果需要第三个模型,可以取消注释下面这个,或替换成您想要的
27
+ # "Chinese-to-English (Another Model)": {
28
+ # "model_name": "facebook/mbart-large-50-one-to-many-mmt", # 另一个多语言模型,需要指定 src_lang/tgt_lang
29
+ # "description": "中文到英文的机器翻译模型 (Facebook mBART-Large-50)",
30
+ # "max_length": 200,
31
+ # "color": "#45B7D1"
32
+ # }
33
  }
34
 
35
  class TranslationComparator:
 
44
  try:
45
  print(f"加载 {model_key} ({config['model_name']})...")
46
  # 对于翻译任务,使用 "translation" pipeline
47
+ # 注意:某些多语言模型(如 m4-small)可能需要显式指定源语言和目标语言
48
+ # 对于 Helsinki-NLP/opus-mt-zh-en,pipeline会自动处理
49
+ # 对于 HuggingFaceM4/m4-small-en-zh,虽然名字是en-zh,但它内部支持zh-en。
50
+ # 如果遇到问题,可能需要更复杂的tokenizer/model加载方式而非pipeline
51
+ if "opus-mt-zh-en" in config["model_name"]:
52
+ task = "translation_zh_to_en" # 更明确的翻译任务
53
+ elif "m4-small" in config["model_name"]:
54
+ # m4-small是一个多语言模型,需要提供源语言和目标语言。
55
+ # pipeline("translation") 不直接支持 src_lang/tgt_lang 参数
56
+ # 需要手动加载 AutoModelForSeq2SeqLM 和 AutoTokenizer
57
+ print(f"特别加载 {model_key} 及其Tokenizer...")
58
+ tokenizer = AutoTokenizer.from_pretrained(config["model_name"])
59
+ model = AutoModelForSeq2SeqLM.from_pretrained(config["model_name"])
60
+ # 将其包装成一个简单的可调用对象,模拟pipeline的行为
61
+ self.models[model_key] = {
62
+ "tokenizer": tokenizer,
63
+ "model": model,
64
+ "pipeline_type": "custom_translation"
65
+ }
66
+ print(f"✓ {model_key} 加载成功 (自定义翻译模式)")
67
+ continue # 跳过pipeline加载
68
+ else: # 默认翻译任务
69
+ task = "translation"
70
+
71
  self.models[model_key] = pipeline(
72
+ task,
73
  model=config["model_name"],
74
  tokenizer=config["model_name"],
75
  device=-1, # 使用CPU
76
+ torch_dtype=torch.float32
77
  )
78
  print(f"✓ {model_key} 加载成功")
79
  except Exception as e:
 
82
 
83
  def translate_text(self, model_key, text_to_translate, max_length=200):
84
  """使用指定模型进行翻译"""
85
+ model_entry = self.models.get(model_key)
86
+ if model_entry is None:
87
  return {
88
  "translated_text": f"[Model {model_key} not loaded correctly, this is a simulated translation]",
89
  "inference_time": 0.5,
 
96
 
97
  try:
98
  start_time = time.time()
99
+
100
+ if isinstance(model_entry, dict) and model_entry.get("pipeline_type") == "custom_translation":
101
+ # 对于需要自定义处理的模型 (如 HuggingFaceM4/m4-small-en-zh)
102
+ tokenizer = model_entry["tokenizer"]
103
+ model = model_entry["model"]
104
+
105
+ # 对于 m4-small,需要手动设置源语言和目标语言
106
+ # 假设输入是中文
107
+ input_ids = tokenizer(text_to_translate, return_tensors="pt", truncation=True, max_length=512).input_ids
108
+
109
+ # 设置生成参数,特别是强制生成目标语言的 token (en_XX)
110
+ # 对于 m4-small 而言,`en_XX` 是英文的目标语言token
111
+ # 请注意:这可能需要根据具体的m4模型进行微调,因为它可能没有直接的force_bos_token_id
112
+ # 一个更通用的方法是手动构建decoder_input_ids
113
+
114
+ # 尝试一个通用的生成方式,让模型自己识别语言
115
+ # 对于翻译任务,transformers pipeline已经封装了大部分复杂性
116
+ # 如果手动调用generate,需要确保输入格式和语言ID正确
117
+
118
+ # 简单的直接生成(可能不带force_bos_token_id)
119
+ generated_ids = model.generate(input_ids, max_new_tokens=max_length)
120
+ translated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
121
+
122
+ else: # 使用 pipeline
123
+ result = model_entry(
124
+ text_to_translate,
125
+ max_length=max_length
126
+ )
127
+ translated_text = result[0]['translation_text']
128
 
129
  end_time = time.time()
130
 
 
 
131
  return {
132
  "translated_text": translated_text,
133
  "inference_time": round(end_time - start_time, 3),
 
149
  # 初始化比较器
150
  comparator = TranslationComparator()
151
 
152
+ def run_translation_comparison(zh_prompt, max_length):
153
+ """运行所有中文到英文模型的翻译对比"""
154
 
155
+ if not zh_prompt.strip():
156
+ # 返回与模型数量相匹配的错误消息
157
+ return tuple([gr.Code.update(value=json.dumps({"错误信息": "请输入中文文本进行翻译"}, ensure_ascii=False)) for _ in MODEL_CONFIGS])
158
 
159
+ results = {}
160
+ outputs_list = []
 
 
 
 
 
 
 
 
161
 
162
+ for model_key in MODEL_CONFIGS.keys():
163
+ result = comparator.translate_text(
164
+ model_key,
 
165
  zh_prompt,
166
  max_length=int(max_length)
167
  )
168
+ results[model_key] = result
169
+
170
+ # 格式化输出
 
 
 
171
  if "error" in result:
172
+ outputs_list.append(json.dumps({"错误信息": result["error"]}, indent=2, ensure_ascii=False))
173
+ else:
174
+ formatted = {
175
+ "翻译文本": result["translated_text"],
176
+ "推断时间": f"{result['inference_time']}s",
177
+ "翻译Token数": result["output_length"],
178
+ "翻译速度": f"{result['output_length']/max(result['inference_time'], 0.001):.1f} tokens/s"
179
+ }
180
+ outputs_list.append(json.dumps(formatted, indent=2, ensure_ascii=False))
 
 
 
181
 
182
+ return tuple(outputs_list)
 
 
 
 
 
 
 
 
 
 
183
 
184
 
185
  def calculate_grace_scores_for_translation():
186
  """为翻译任务计算GRACE评估分数"""
187
+ # 模拟中文到英文翻译模型的GRACE分数
188
  grace_data = {
189
+ "Chinese-to-English (Opus-MT)": {
 
 
 
 
 
 
 
190
  "Generalization": 7.8, # 处理不同领域中翻英能力
191
  "Relevance": 8.3, # 翻译内容与原文语义相关性
192
  "Accuracy": 8.0, # 翻译精确性
193
  "Consistency": 7.9, # 翻译稳定性
194
  "Efficiency": 7.5 # 推理效率
195
+ },
196
+ "Chinese-to-English (M4-Small)": {
197
+ "Generalization": 7.0, # 多语言模型可能在特定语对上略逊色于专用模型
198
+ "Relevance": 7.5,
199
+ "Accuracy": 7.2,
200
+ "Consistency": 7.0,
201
+ "Efficiency": 8.5 # 通常小模型效率更高
202
  }
203
+ # 如果有第三个模型,在这里添加其分数
204
  }
 
 
 
 
205
  return grace_data
206
 
207
 
 
236
  ),
237
  showlegend=True,
238
  title={
239
+ 'text': "GRACE框架:中文到英文翻译模型评估",
240
  'x': 0.5,
241
  'font': {'size': 16}
242
  },
 
265
  ))
266
 
267
  fig.update_layout(
268
+ title='GRACE框架详细对比 - 中文到英文翻译',
269
  xaxis_title='模型',
270
  yaxis_title='分数 (0-10)',
271
  barmode='group',
 
278
  """创建模型信息对比表"""
279
  model_info = []
280
  for model_key, config in MODEL_CONFIGS.items():
281
+ # 模拟参数信息
282
+ if "opus-mt-zh-en" in config["model_name"]:
283
+ params = "~3亿"
284
+ size = "~1.2GB"
285
+ elif "m4-small" in config["model_name"]:
286
+ params = "~4亿" # m4-small 实际参数量可能更大
287
+ size = "~1.5GB"
288
+ else: # 默认值
289
+ params = "未知"
290
+ size = "未知"
291
 
292
  model_info.append({
293
  "模型": model_key,
 
316
  df = pd.DataFrame(summary_data)
317
  return df
318
 
319
+ # 预设的示例中文提示
 
 
 
 
 
 
320
  EXAMPLE_ZH_PROMPTS = [
321
  "你好,今天过得怎么样?",
322
  "敏捷的棕色狐狸跳过懒惰的狗。",
323
+ "人工智能正在改变许多行业。",
324
+ "今天天气真好,我们去公园散步吧。"
325
  ]
326
 
327
  def create_app():
328
+ with gr.Blocks(title="中文到英文翻译模型对比", theme=gr.themes.Soft()) as app:
329
+ gr.Markdown("# 🌐 中文到英文翻译模型对比竞技场")
330
+ gr.Markdown("### 使用GRACE框架对比不同中文到英文翻译模型在翻译任务中的表现")
331
 
332
  with gr.Tabs():
333
  # Arena选项卡
334
  with gr.TabItem("️ 翻译竞技场"):
335
  gr.Markdown("## 翻译竞技场")
336
+ gr.Markdown("请在下方输入需要翻译的**中文**文本,查看不同模型翻译成**英文**的效果。")
337
 
338
  with gr.Row():
339
+ with gr.Column(scale=2): # 增加输入框的比例
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  input_zh_prompt = gr.Textbox(
341
  label="输入中文文本",
342
  placeholder="在此输入您的中文文本...",
343
+ lines=4, # 增加行数
344
  value=EXAMPLE_ZH_PROMPTS[0]
345
  )
346
  # 预设中文示例按钮
347
  with gr.Row():
348
  for i, example in enumerate(EXAMPLE_ZH_PROMPTS):
349
+ gr.Button(f"示例 {i+1}", size="sm").click(
350
  fn=lambda x=example: x,
351
  outputs=[input_zh_prompt]
352
  )
 
 
 
 
 
 
 
 
 
353
 
354
+ with gr.Column(scale=1): # 调整参数控制列的比例
355
+ max_length = gr.Slider(
356
+ minimum=50,
357
+ maximum=500,
358
+ value=200,
359
+ step=10,
360
+ label="最大输出Token数"
361
+ )
362
+
363
+ submit_btn = gr.Button(" 开始翻译", variant="primary", size="lg")
364
 
365
  # 动态创建输出框
366
  output_boxes = []
367
  for model_key, config in MODEL_CONFIGS.items():
368
  output_boxes.append(gr.Code(
369
+ label=f"{model_key} 翻译结果", # 明确翻译方向
370
  language="json",
371
  value="点击“开始翻译”查看结果"
372
  ))
373
 
374
  submit_btn.click(
375
  fn=run_translation_comparison,
376
+ inputs=[input_zh_prompt, max_length],
377
  outputs=output_boxes
378
  )
379