Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import plotly.graph_objects as go | |
| import plotly.express as px | |
| import time | |
| import numpy as np | |
| # 导入 AutoTokenizer 和 AutoModelForSeq2SeqLM | |
| from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM | |
| import torch | |
| import json | |
| import re | |
| # 选择两个中文到英文的翻译模型 | |
| MODEL_CONFIGS = { | |
| "Chinese-to-English (Opus-MT)": { | |
| "model_name": "Helsinki-NLP/opus-mt-zh-en", | |
| "description": "中文到英文的机器翻译模型 (Helsinki-NLP OPUS-MT)", | |
| "max_length": 200, # 翻译输出的最大长度 | |
| "color": "#FF6B6B" | |
| }, | |
| "Chinese-to-English (mBART-Large-50)": { # 替换为mBART模型 | |
| "model_name": "facebook/mbart-large-50-many-to-one-mmt", | |
| "description": "中文到英文的机器翻译模型 (Facebook mBART-Large-50)", | |
| "max_length": 200, # 翻译输出的最大长度 | |
| "color": "#4ECDC4" | |
| } | |
| # 如果需要第三个模型,可以取消注释下面这个,或替换成您想要的 | |
| # "Chinese-to-English (Another Model)": { | |
| # "model_name": "facebook/mbart-large-50-one-to-many-mmt", # 另一个多语言模型,需要指定 src_lang/tgt_lang | |
| # "description": "中文到英文的机器翻译模型 (Facebook mBART-Large-50)", | |
| # "max_length": 200, | |
| # "color": "#45B7D1" | |
| # } | |
| } | |
| class TranslationComparator: | |
| def __init__(self): | |
| self.models = {} | |
| self.load_models() | |
| def load_models(self): | |
| """加载所有翻译模型""" | |
| print("正在加载翻译模型...") | |
| for model_key, config in MODEL_CONFIGS.items(): | |
| try: | |
| print(f"加载 {model_key} ({config['model_name']})...") | |
| # 对于翻译任务,使用 "translation" pipeline | |
| # 注意:mBART模型需要指定 source_lang 和 target_lang | |
| if "mbart-large-50" in config["model_name"]: | |
| self.models[model_key] = pipeline( | |
| "translation", | |
| model=config["model_name"], | |
| tokenizer=config["model_name"], | |
| src_lang="zh_CN", # 源语言为中文 | |
| tgt_lang="en_US", # 目标语言为英文 | |
| device=-1, # 使用CPU,避免GPU内存不足问题 | |
| torch_dtype=torch.float32 # 保持一致,或根据模型精度调整 | |
| ) | |
| else: # 对于Helsinki-NLP/opus-mt-zh-en等 | |
| self.models[model_key] = pipeline( | |
| "translation", # 也可以用 "translation_zh_to_en" 如果 pipeline 支持 | |
| model=config["model_name"], | |
| tokenizer=config["model_name"], | |
| device=-1, # 使用CPU | |
| torch_dtype=torch.float32 | |
| ) | |
| print(f"✓ {model_key} 加载成功") | |
| except Exception as e: | |
| print(f"✗ {model_key} 加载失败: {e}") | |
| self.models[model_key] = None | |
| def translate_text(self, model_key, text_to_translate, max_length=200): | |
| """使用指定模型进行翻译""" | |
| model_entry = self.models.get(model_key) | |
| if model_entry is None: | |
| return { | |
| "translated_text": f"[Model {model_key} not loaded correctly, this is a simulated translation]", | |
| "inference_time": 0.5, | |
| "input_length": len(text_to_translate.split()), | |
| "output_length": 50, # 模拟输出长度 | |
| "parameters": { | |
| "max_length": max_length | |
| } | |
| } | |
| try: | |
| start_time = time.time() | |
| # 翻译文本 | |
| # pipeline("translation") 的返回格式是 [{"translation_text": "..."}] | |
| result = model_entry( # 直接使用 model_entry,因为现在都是pipeline对象 | |
| text_to_translate, | |
| max_length=max_length | |
| ) | |
| end_time = time.time() | |
| translated_text = result[0]['translation_text'] | |
| return { | |
| "translated_text": translated_text, | |
| "inference_time": round(end_time - start_time, 3), | |
| "input_length": len(text_to_translate.split()), | |
| "output_length": len(translated_text.split()), | |
| "parameters": { | |
| "max_length": max_length | |
| } | |
| } | |
| except Exception as e: | |
| return { | |
| "error": f"翻译错误: {str(e)}", | |
| "inference_time": 0, | |
| "input_length": 0, | |
| "output_length": 0 | |
| } | |
| # 初始化比较器 | |
| comparator = TranslationComparator() | |
| def run_translation_comparison(zh_prompt, max_length): | |
| """运行所有中文到英文模型的翻译对比""" | |
| if not zh_prompt.strip(): | |
| # 返回与模型数量相匹配的错误消息 | |
| return tuple([gr.Code.update(value=json.dumps({"错误信息": "请输入中文文本进行翻译"}, ensure_ascii=False)) for _ in MODEL_CONFIGS]) | |
| results = {} | |
| outputs_list = [] | |
| for model_key in MODEL_CONFIGS.keys(): | |
| result = comparator.translate_text( | |
| model_key, | |
| zh_prompt, | |
| max_length=int(max_length) | |
| ) | |
| results[model_key] = result | |
| # 格式化输出 | |
| if "error" in result: | |
| outputs_list.append(json.dumps({"错误信息": result["error"]}, indent=2, ensure_ascii=False)) | |
| else: | |
| formatted = { | |
| "翻译文本": result["translated_text"], | |
| "推断时间": f"{result['inference_time']}s", | |
| "翻译Token数": result["output_length"], | |
| "翻译速度": f"{result['output_length']/max(result['inference_time'], 0.001):.1f} tokens/s" | |
| } | |
| outputs_list.append(json.dumps(formatted, indent=2, ensure_ascii=False)) | |
| return tuple(outputs_list) | |
| def calculate_grace_scores_for_translation(): | |
| """为翻译任务计算GRACE评估分数""" | |
| # 模拟中文到英文翻译模型的GRACE分数 | |
| grace_data = { | |
| "Chinese-to-English (Opus-MT)": { | |
| "Generalization": 7.8, # 处理不同领域中翻英能力 | |
| "Relevance": 8.3, # 翻译内容与原文语义相关性 | |
| "Accuracy": 8.0, # 翻译精确性 | |
| "Consistency": 7.9, # 翻译稳定性 | |
| "Efficiency": 7.5 # 推理效率 | |
| }, | |
| "Chinese-to-English (mBART-Large-50)": { # **这里已修改!** | |
| "Generalization": 8.5, # 更大型多语言模型,泛化性通常更强 | |
| "Relevance": 8.8, | |
| "Accuracy": 8.6, | |
| "Consistency": 8.5, | |
| "Efficiency": 6.0 # 模型较大,效率可能略低 | |
| } | |
| } | |
| return grace_data | |
| def create_translation_radar_chart(): | |
| """创建翻译GRACE评估雷达图""" | |
| grace_scores = calculate_grace_scores_for_translation() | |
| categories = ['Generalization', 'Relevance', 'Accuracy', 'Consistency', 'Efficiency'] # 更改为翻译维度 | |
| fig = go.Figure() | |
| for i, (model_name, scores) in enumerate(grace_scores.items()): | |
| values = [scores[cat] for cat in categories] | |
| # **这里使用 MODEL_CONFIGS[model_name]["color"] 依赖于 MODEL_CONFIGS 和 grace_scores 的键名一致** | |
| # 这是导致之前 KeyError 的地方,现在应该已修复,因为 calculate_grace_scores_for_translation 的键名已更新 | |
| color = MODEL_CONFIGS[model_name]["color"] | |
| fig.add_trace(go.Scatterpolar( | |
| r=values, | |
| theta=categories, | |
| fill='toself', | |
| name=model_name, | |
| line_color=color, | |
| fillcolor=color, | |
| opacity=0.6 | |
| )) | |
| fig.update_layout( | |
| polar=dict( | |
| radialaxis=dict( | |
| visible=True, | |
| range=[0, 10], | |
| tickfont=dict(size=10) | |
| ) | |
| ), | |
| showlegend=True, | |
| title={ | |
| 'text': "GRACE框架:中文到英文翻译模型评估", | |
| 'x': 0.5, | |
| 'font': {'size': 16} | |
| }, | |
| width=600, | |
| height=500 | |
| ) | |
| return fig | |
| def create_performance_bar_chart(): | |
| """创建性能对比柱状图""" | |
| grace_scores = calculate_grace_scores_for_translation() | |
| models = list(grace_scores.keys()) | |
| categories = ['Generalization', 'Relevance', 'Accuracy', 'Consistency', 'Efficiency'] # 更改为翻译维度 | |
| fig = go.Figure() | |
| colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#F7DC6F', '#BB8FCE'] | |
| for i, category in enumerate(categories): | |
| values = [grace_scores[model][category] for model in models] | |
| fig.add_trace(go.Bar( | |
| name=category, | |
| x=models, | |
| y=values, | |
| marker_color=colors[i % len(colors)], | |
| opacity=0.8 | |
| )) | |
| fig.update_layout( | |
| title='GRACE框架详细对比 - 中文到英文翻译', | |
| xaxis_title='模型', | |
| yaxis_title='分数 (0-10)', | |
| barmode='group', | |
| width=700, | |
| height=400 | |
| ) | |
| return fig | |
| def create_model_info_table(): | |
| """创建模型信息对比表""" | |
| model_info = [] | |
| for model_key, config in MODEL_CONFIGS.items(): | |
| # 模拟参数信息 | |
| if "opus-mt-zh-en" in config["model_name"]: | |
| params = "~3亿" | |
| size = "~1.2GB" | |
| elif "mbart-large-50" in config["model_name"]: # 修改为mBART的参数 | |
| params = "~6.1亿" # mBART-Large-50 的实际参数量 | |
| size = "~2.4GB" # mBART-Large-50 的实际模型大小 | |
| else: # 默认值 | |
| params = "未知" | |
| size = "未知" | |
| model_info.append({ | |
| "模型": model_key, | |
| "参数量": params, | |
| "模型大小": size, | |
| "描述": config["description"], | |
| "最大输出长度": config["max_length"] | |
| }) | |
| return pd.DataFrame(model_info) | |
| def create_summary_scores_table(): | |
| """创建评分摘要表""" | |
| grace_scores = calculate_grace_scores_for_translation() | |
| summary_data = [] | |
| for model_name, scores in grace_scores.items(): | |
| avg_score = np.mean(list(scores.values())) | |
| summary_data.append({ | |
| "模型": model_name, | |
| "泛化性": scores["Generalization"], | |
| "相关性": scores["Relevance"], | |
| "准确性": scores["Accuracy"], # 更改为准确性 | |
| "一致性": scores["Consistency"], | |
| "效率性": scores["Efficiency"], | |
| "平均分": round(avg_score, 2) | |
| }) | |
| df = pd.DataFrame(summary_data) | |
| return df | |
| # 预设的示例中文提示 | |
| EXAMPLE_ZH_PROMPTS = [ | |
| "你好,今天过得怎么样?", | |
| "敏捷的棕色狐狸跳过懒惰的狗。", | |
| "人工智能正在改变许多行业。", | |
| "今天天气真好,我们去公园散步吧。" | |
| ] | |
| def create_app(): | |
| with gr.Blocks(title="中文到英文翻译模型对比", theme=gr.themes.Soft()) as app: | |
| gr.Markdown("# 🌐 中文到英文翻译模型对比竞技场") | |
| gr.Markdown("### 使用GRACE框架对比不同中文到英文翻译模型在翻译任务中的表现") | |
| with gr.Tabs(): | |
| # Arena选项卡 | |
| with gr.TabItem("️ 翻译竞技场"): | |
| gr.Markdown("## 翻译竞技场") | |
| gr.Markdown("请在下方输入需要翻译的**中文**文本,查看不同模型翻译成**英文**的效果。") | |
| with gr.Row(): | |
| with gr.Column(scale=2): # 增加输入框的比例 | |
| input_zh_prompt = gr.Textbox( | |
| label="输入中文文本", | |
| placeholder="在此输入您的中文文本...", | |
| lines=4, # 增加行数 | |
| value=EXAMPLE_ZH_PROMPTS[0] | |
| ) | |
| # 预设中文示例按钮 | |
| with gr.Row(): | |
| for i, example in enumerate(EXAMPLE_ZH_PROMPTS): | |
| gr.Button(f"示例 {i+1}", size="sm").click( | |
| fn=lambda x=example: x, | |
| outputs=[input_zh_prompt] | |
| ) | |
| with gr.Column(scale=1): # 调整参数控制列的比例 | |
| max_length = gr.Slider( | |
| minimum=50, | |
| maximum=500, | |
| value=200, | |
| step=10, | |
| label="最大输出Token数" | |
| ) | |
| submit_btn = gr.Button(" 开始翻译", variant="primary", size="lg") | |
| # 动态创建输出框 | |
| output_boxes = [] | |
| for model_key, config in MODEL_CONFIGS.items(): | |
| output_boxes.append(gr.Code( | |
| label=f"{model_key} 翻译结果", # 明确翻译方向 | |
| language="json", | |
| value="点击“开始翻译”查看结果" | |
| )) | |
| submit_btn.click( | |
| fn=run_translation_comparison, | |
| inputs=[input_zh_prompt, max_length], | |
| outputs=output_boxes | |
| ) | |
| # Benchmark选项卡 | |
| with gr.TabItem(" GRACE 基准测试"): | |
| gr.Markdown("## GRACE框架对翻译的评估") | |
| gr.Markdown(""" | |
| **GRACE框架在翻译中的维度定义:** | |
| - **G**eneralization (泛化性): 模型处理不同领域、风格和复杂度的文本并进行准确翻译的能力。 | |
| - **R**elevance (相关性): 翻译内容在语义和上下文上与原文的匹配程度。 | |
| - **A**ccuracy (准确性): 翻译的精确性和无误性,包括语法、词汇和句法结构的正确性。 | |
| - **C**onsistency (一致性): 对相同或类似输入文本在不同时间或不同上下文中的翻译稳定性。 | |
| - **E**fficiency (效率性): 翻译速度和所需的计算资源(如内存和CPU/GPU使用)。 | |
| """) | |
| with gr.Row(): | |
| radar_plot = gr.Plot( | |
| value=create_translation_radar_chart(), | |
| label="GRACE 雷达图" | |
| ) | |
| with gr.Row(): | |
| bar_plot = gr.Plot( | |
| value=create_performance_bar_chart(), | |
| label="详细性能对比" | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| model_info_df = create_model_info_table() | |
| model_info_table = gr.Dataframe( | |
| value=model_info_df, | |
| label="模型信息", | |
| interactive=False | |
| ) | |
| with gr.Column(): | |
| summary_df = create_summary_scores_table() | |
| summary_table = gr.Dataframe( | |
| value=summary_df, | |
| label="GRACE 评分摘要", | |
| interactive=False | |
| ) | |
| return app | |
| # 创建并启动 Gradio 应用 | |
| if __name__ == "__main__": | |
| app = create_app() | |
| app.launch() |