Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,169 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import torch
|
3 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
4 |
-
import pandas as pd
|
5 |
-
import plotly.express as px
|
6 |
-
import os
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
# --- 1. 模型加载 ---
|
13 |
-
# 负责同学: [填写负责这个模型的同学姓名,例如:张三]
|
14 |
-
# 注意:QuantFactory/Apollo2-7B-GGUF 模型通常不直接兼容 pipeline("text-generation", ...)
|
15 |
-
# 除非有额外的llama.cpp或特定的transformers加载配置。
|
16 |
-
# 为了演示和确保运行流畅,这里使用 gpt2-large 作为替代。
|
17 |
-
try:
|
18 |
-
model1_name = "gpt2-large" # 替代 QuantFactory/Apollo2-7B-GGUF 以确保兼容性
|
19 |
-
generator1 = pipeline("text-generation", model=model1_name, device=0 if torch.cuda.is_available() else -1)
|
20 |
-
print(f"✅ 模型 1 (文本生成: {model1_name}) 加载成功!")
|
21 |
-
except Exception as e:
|
22 |
-
print(f"❌ 模型 1 (文本生成: {model1_name}) 加载失败: {e}")
|
23 |
-
generator1 = None
|
24 |
-
|
25 |
-
# 负责同学: [填写负责这个模型的同学姓名,例如:李四]
|
26 |
-
# deepset/roberta-base-squad2 是一个问答模型,需要 context
|
27 |
-
try:
|
28 |
-
model2_name = "deepset/roberta-base-squad2"
|
29 |
-
qa_model = pipeline("question-answering", model=model2_name, device=0 if torch.cuda.is_available() else -1)
|
30 |
-
print(f"✅ 模型 2 (问答: {model2_name}) 加载成功!")
|
31 |
-
except Exception as e:
|
32 |
-
print(f"❌ 模型 2 (问答: {model2_name}) 加载失败: {e}")
|
33 |
-
qa_model = None
|
34 |
-
|
35 |
-
|
36 |
-
# --- 2. 推理函数 ---
|
37 |
-
# 这个函数现在接受一个问题/提示词和一个上下文
|
38 |
-
def get_model_outputs(question_or_prompt, context, max_length=100):
|
39 |
-
output_text_gen = "文本生成模型未加载或生成失败。"
|
40 |
-
output_qa = "问答模型未加载或生成失败。"
|
41 |
-
|
42 |
-
# 模型 1: 文本生成
|
43 |
-
if generator1:
|
44 |
-
try:
|
45 |
-
# 文本生成模型将问题和上下文作为其prompt的一部分
|
46 |
-
full_prompt_for_gen = f"{question_or_prompt}\nContext: {context}" if context else question_or_prompt
|
47 |
-
gen_result = generator1(full_prompt_for_gen, max_new_tokens=max_length, num_return_sequences=1, truncation=True)
|
48 |
-
output_text_gen = gen_result[0]['generated_text']
|
49 |
-
# 清理:移除输入部分,只保留生成内容
|
50 |
-
if output_text_gen.startswith(full_prompt_for_gen):
|
51 |
-
output_text_gen = output_text_gen[len(full_prompt_for_gen):].strip()
|
52 |
-
except Exception as e:
|
53 |
-
output_text_gen = f"文本生成模型 ({model1_name}) 错误: {e}"
|
54 |
-
|
55 |
-
# 模型 2: 问答
|
56 |
-
if qa_model and context: # 问答模型必须有上下文
|
57 |
-
try:
|
58 |
-
qa_result = qa_model(question=question_or_prompt, context=context)
|
59 |
-
output_qa = qa_result['answer']
|
60 |
-
except Exception as e:
|
61 |
-
output_qa = f"问答模型 ({model2_name}) 错误: {e}"
|
62 |
-
elif qa_model and not context:
|
63 |
-
output_qa = "问答模型需要提供上下文才能回答问题。"
|
64 |
-
|
65 |
-
return output_text_gen, output_qa
|
66 |
-
|
67 |
-
|
68 |
-
# --- 3. GRACE 评估数据(示例数据,请根据你们的实际评估结果修改) ---
|
69 |
-
# 请根据 gpt2-large 和 deepset/roberta-base-squad2 的实际表现进行评分
|
70 |
-
grace_data = {
|
71 |
-
"维度": ["Generalization (泛化性)", "Relevance (相关性)", "Artistry (创新表现力)", "Efficiency (效率性)"],
|
72 |
-
# 模型 1: gpt2-large (通用文本生成模型)
|
73 |
-
"GPT2-Large": [
|
74 |
-
4.0, # 泛化性: 能处理多种文本生成任务
|
75 |
-
3.5, # 相关性: 对于特定事实性问题可能不如问答模型精确
|
76 |
-
4.2, # 创新表现力: 生成文本流畅,有一定创造性
|
77 |
-
3.8 # 效率性: 相对 GPT2 较大,但比 Llama-2-7b 小
|
78 |
-
],
|
79 |
-
# 模型 2: deepset/roberta-base-squad2 (问答模型)
|
80 |
-
"RoBERTa-SQuAD2": [
|
81 |
-
3.0, # 泛化性: 专门用于问答,不能生成开放式文本
|
82 |
-
4.8, # 相关性: 从给定上下文中抽取答案,相关性极高
|
83 |
-
2.0, # 创新表现力: 抽取式问答,无创新表现
|
84 |
-
4.5 # 效率性: 推理速度快,效率高
|
85 |
-
]
|
86 |
-
}
|
87 |
-
grace_df = pd.DataFrame(grace_data)
|
88 |
-
|
89 |
-
|
90 |
-
# --- 4. Gradio 界面构建 ---
|
91 |
-
|
92 |
-
# LLM Benchmark 选项卡内容创建函数 (30分)
|
93 |
-
def create_benchmark_tab():
|
94 |
-
# 生成雷达图
|
95 |
-
fig = px.line_polar(grace_df, r=grace_df.columns[1], theta="维度", line_close=True,
|
96 |
-
range_r=[0, 5], title="GRACE 评估:模型横向对比")
|
97 |
-
# 添加其他模型的轨迹
|
98 |
-
for col in grace_df.columns[2:]:
|
99 |
-
fig.add_trace(px.line_polar(grace_df, r=col, theta="维度", line_close=True).data[0])
|
100 |
-
|
101 |
-
fig.update_traces(fill='toself', opacity=0.6) # 填充颜色,增加透明度
|
102 |
-
fig.update_layout(
|
103 |
-
polar=dict(
|
104 |
-
radialaxis=dict(visible=True, range=[0, 5], tickvals=[1,2,3,4,5], ticktext=['1分','2分','3分','4分','5分']) # 显示刻度
|
105 |
-
),
|
106 |
-
showlegend=True, # 显示图例
|
107 |
-
# title_font_size=20 # 标题字体大小
|
108 |
-
)
|
109 |
-
|
110 |
-
return gr.Column(
|
111 |
-
gr.Markdown("## 📊 模型性能对比 (GRACE 评估)"),
|
112 |
-
gr.Markdown("本页展示了我们选用的模型在 GRACE 框架下的评估结果。数据为 1-5 分,分数越高代表表现越好。\n"
|
113 |
-
"**注意**: GPT2-Large 主要用于文本生成,RoBERTa-SQuAD2 主要用于问答,它们的评估维度侧重有所不同。"),
|
114 |
-
gr.Plot(fig, label="GRACE 评估雷达图"),
|
115 |
-
gr.Markdown("### GRACE 评估数据"),
|
116 |
-
gr.DataFrame(grace_df, label="详细评估数据")
|
117 |
-
)
|
118 |
-
|
119 |
-
# Arena 选项卡内容创建函数 (40分)
|
120 |
-
def create_arena_tab():
|
121 |
-
with gr.Blocks() as arena_block:
|
122 |
-
gr.Markdown("## ⚔️ Arena: 模型实时对比")
|
123 |
-
gr.Markdown("在这里,您可以输入一个问题或提示词,并提供一段上下文。文本生成模型将根据问题和上下文生成文本,问答模型将从上下文中抽取答案。")
|
124 |
-
|
125 |
-
with gr.Row():
|
126 |
-
# 统一输入框 1: 问题/提示词
|
127 |
-
question_input = gr.Textbox(label="问题/提示词:", placeholder="请输入您的问题或想让模型生成的提示词...", lines=3)
|
128 |
-
# 统一输入框 2: 上下文 (主要用于问答模型)
|
129 |
-
context_input = gr.Textbox(label="上下文 (Context):", placeholder="请输入问答模型需要从中抽取答案的上下文...", lines=5)
|
130 |
-
|
131 |
-
with gr.Row():
|
132 |
-
# 增加生成长度控制(主要针对文本生成模型)
|
133 |
-
gen_length_slider = gr.Slider(minimum=20, maximum=300, value=100, step=10, label="文本生成最大长度")
|
134 |
-
generate_btn = gr.Button("🚀 生成并对比")
|
135 |
-
|
136 |
-
with gr.Row():
|
137 |
-
# 模型 1 输出 (文本生成)
|
138 |
-
output_text_gen = gr.Textbox(label=f"模型 1 (文本生成: {model1_name}) 输出:", interactive=False, lines=10)
|
139 |
-
# 模型 2 输出 (问答)
|
140 |
-
output_qa = gr.Textbox(label=f"模型 2 (问答: {model2_name}) 输出:", interactive=False, lines=10)
|
141 |
-
|
142 |
-
# 绑定按钮点击事件到推理函数
|
143 |
-
generate_btn.click(
|
144 |
-
fn=get_model_outputs,
|
145 |
-
inputs=[question_input, context_input, gen_length_slider],
|
146 |
-
outputs=[output_text_gen, output_qa]
|
147 |
-
)
|
148 |
-
return arena_block
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
# --- Gradio 应用界面定义 ---
|
153 |
-
with gr.Blocks(title="AI模型对比项目") as demo:
|
154 |
-
gr.Markdown("# 🤖 AI 模型对比与评估平台")
|
155 |
-
gr.Markdown("本平台旨在通过交互式界面,对比分析不同 AI 模型在特定任务上的表现。")
|
156 |
-
|
157 |
-
# 定义选项卡
|
158 |
-
with gr.Tab("⚔️ Arena"):
|
159 |
-
create_arena_tab()
|
160 |
-
|
161 |
-
with gr.Tab("📊 LLM Benchmark"):
|
162 |
-
create_benchmark_tab()
|
163 |
-
|
164 |
-
# with gr.Tab("📝 Report"): # Adding the Report tab
|
165 |
-
# create_report_tab()
|
166 |
-
|
167 |
-
# 启动 Gradio 应用
|
168 |
-
if __name__ == "__main__":
|
169 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|