Spaces:
Running
Running
File size: 17,755 Bytes
f887c3b b8a0ce6 f887c3b b8a0ce6 1537fc2 f887c3b b8a0ce6 1537fc2 b8a0ce6 b58538e 1537fc2 b58538e 1537fc2 b58538e f887c3b b8a0ce6 f887c3b b8a0ce6 1537fc2 b8a0ce6 f887c3b b8a0ce6 f887c3b b8a0ce6 f887c3b 1537fc2 b8a0ce6 f887c3b b8a0ce6 1537fc2 b8a0ce6 b58538e b8a0ce6 b58538e b8a0ce6 f887c3b b8a0ce6 b58538e b8a0ce6 1537fc2 f887c3b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
import gradio as gr
import pandas as pd
import pandas as pd
import json
import plotly.express as px
def on_confirm(dataset_radio, num_parts_dropdown, perspective_radio, division_method_radio):
# 根据用户选择的参数构建文件路径
num_parts = num_parts_dropdown
if dataset_radio == "HumanEval":
base_path = "./dividing_into_different_subsets"
else: # MBPP
base_path = "./dividing_into_different_subsets_mbpp"
method = "QS" if division_method_radio == "Equal Frequency Partitioning" else "EI"
# 根据perspective选择读取对应的文件
if "Tokens" in perspective_radio:
df = pd.read_csv(f"{base_path}/{num_parts}/{method}/token_counts_{method}.csv")
elif "Lines" in perspective_radio:
df = pd.read_csv(f"{base_path}/{num_parts}/{method}/line_counts_{method}.csv")
elif "Complexity" in perspective_radio:
df = pd.read_csv(f"{base_path}/{num_parts}/{method}/CC_{method}.csv")
elif "Problem Types" in perspective_radio:
df = pd.read_csv(f"{base_path}/cata_result.csv")
# 加载分析报告
analysis_result,_ = load_analysis_report(dataset_radio, num_parts_dropdown, perspective_radio, division_method_radio)
# AI分析列
df["Analysis"] = df["Model"].map(lambda m: analysis_result.get(m, "No analysis provided."))
return df
# 生成 CSS 样式
def generate_css(line_counts, token_counts, cyclomatic_complexity, problem_type, show_high, show_medium, show_low):
css = """
#dataframe th {
background-color: #f2f2f2
}
"""
colors = ["#e6f7ff", "#ffeecc", "#e6ffe6", "#ffe6e6"]
categories = [line_counts, token_counts, cyclomatic_complexity]
category_index = 0
column_index = 1
for category in categories:
if category:
if show_high:
css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {colors[category_index]}; }}\n"
column_index += 1
if show_medium:
css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {colors[category_index]}; }}\n"
column_index += 1
if show_low:
css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {colors[category_index]}; }}\n"
column_index += 1
category_index += 1
# 为 Problem Type 相关的三个子列设置固定颜色
if problem_type:
problem_type_color = "#d4f0fc" # 你可以选择任何你喜欢的颜色
css += f"#dataframe td:nth-child({column_index + 1}) {{ background-color: {problem_type_color}; }}\n"
css += f"#dataframe td:nth-child({column_index + 2}) {{ background-color: {problem_type_color}; }}\n"
css += f"#dataframe td:nth-child({column_index + 3}) {{ background-color: {problem_type_color}; }}\n"
# 隐藏 "data" 标识
css += """
.gradio-container .dataframe-container::before {
content: none !important;
}
"""
return css
# AI分析
def load_analysis_report(dataset_radio, num_parts_dropdown, perspective_radio, division_method_radio):
num_parts = num_parts_dropdown
method = "QS" if division_method_radio == "Equal Frequency Partitioning" else "EI"
# 根据perspective确定文件路径
if "Tokens" in perspective_radio:
perspective = "token_counts"
elif "Lines" in perspective_radio:
perspective = "line_counts"
elif "Complexity" in perspective_radio:
perspective = "CC"
else:
perspective = "problem_type"
base_path = "./llm_insight"
if perspective == "problem_type":
report_file = f"{base_path}/{dataset_radio}/{perspective}_report.json"
recommendation_file = f"{base_path}/{dataset_radio}/{perspective}_recommendation.json"
else:
report_file = f"{base_path}/{dataset_radio}/{num_parts}/{method}/{perspective}_report.json"
recommendation_file = f"{base_path}/{dataset_radio}/{num_parts}/{method}/{perspective}_recommendation.json"
try:
with open(report_file, 'r', encoding='utf-8') as f:
analysis_result = json.load(f)
except Exception as e:
analysis_result = f"[Error] error load analysis report: {e}"
try:
with open(recommendation_file, 'r', encoding='utf-8') as f:
recommendation_result = json.load(f)
except Exception as e:
recommendation_result = f"[Error] error load model recommendation: {e}"
return (analysis_result,recommendation_result)
# 可视化
def plot_visualization(dataset_radio, perspective_radio, num_parts, plot_type):
if dataset_radio == "HumanEval":
base_path = "./dividing_into_different_subsets"
else: # MBPP
base_path = "./dividing_into_different_subsets_mbpp"
if "Tokens" in perspective_radio:
file_path = f'{base_path}/{num_parts}/QS/token_counts_QS.csv'
elif "Lines" in perspective_radio:
file_path = f'{base_path}/{num_parts}/QS/line_counts_QS.csv'
elif "Complexity" in perspective_radio:
file_path = f'{base_path}/{num_parts}/QS/CC_QS.csv'
else: # Problem Types
file_path = f'{base_path}/cata_result.csv'
df = pd.read_csv(file_path)
df.set_index('Model', inplace=True)
df_transposed = df.T
if plot_type == "Line Chart":
fig = px.line(df_transposed,
x=df_transposed.index,
y=df_transposed.columns,
title='Model Performance Across Different Subsets',
labels={'value': 'Evaluation Score', 'index': 'Subsets'},
color_discrete_sequence=px.colors.qualitative.Plotly)
fig.update_traces(hovertemplate='%{y}')
elif plot_type == "Radar Chart": # Radar Chart
# 重新组织数据为雷达图所需格式
radar_data = []
for model in df.index:
for subset, score in df.loc[model].items():
radar_data.append({
'Model': model,
'Subset': subset,
'Score': score
})
radar_df = pd.DataFrame(radar_data)
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
# 创建雷达图
fig = px.line_polar(radar_df,
r='Score',
theta='Subset',
color='Model',
line_close=True,
color_discrete_sequence=colors,
title='Model Performance Radar Chart')
# 自定义每个模型的线条样式
for i, trace in enumerate(fig.data):
trace.update(
fill=None, # 移除填充
line=dict(
width=2,
dash='solid' if i % 2 == 0 else 'dash', # 交替使用实线和虚线
)
)
# 优化雷达图的显示
fig.update_layout(
polar=dict(
radialaxis=dict(
visible=True,
range=[0, 100],
showline=True,
linewidth=1,
gridcolor='lightgrey'
),
angularaxis=dict(
showline=True,
linewidth=1,
gridcolor='lightgrey'
)
),
showlegend=True,
legend=dict(
yanchor="middle", # 垂直居中
y=0.5,
xanchor="left",
x=1.2, # 将图例移到雷达图右侧
bgcolor="rgba(255, 255, 255, 0.8)", # 半透明白色背景
bordercolor="lightgrey", # 添加边框
borderwidth=1
),
margin=dict(r=150), # 增加右侧边距,为图例留出空间
paper_bgcolor='white'
)
else: # Heatmap
# 创建热力图
fig = px.imshow(df_transposed,
labels=dict(x="Model", y="Subset", color="Score"),
color_continuous_scale="RdYlBu_r", # 使用科研风格配色:红-黄-蓝
aspect="auto", # 自动调整宽高比
title="Model Performance Heatmap")
# 优化热力图显示
fig.update_layout(
title=dict(
text='Model Performance Distribution Across Subsets',
x=0.5,
y=0.95,
xanchor='center',
yanchor='top',
font=dict(size=14)
),
xaxis=dict(
title="Model",
tickangle=45, # 斜着显示模型名称
tickfont=dict(size=10),
side="bottom"
),
yaxis=dict(
title="Subset",
tickfont=dict(size=10)
),
coloraxis=dict(
colorbar=dict(
title="Score",
titleside="right",
tickfont=dict(size=10),
titlefont=dict(size=12),
len=0.9, # 色条长度
)
),
margin=dict(t=80, r=100, b=80, l=80), # 调整边距
paper_bgcolor='white',
plot_bgcolor='white'
)
# 添加具体数值标注
annotations = []
for i in range(len(df_transposed.index)):
for j in range(len(df_transposed.columns)):
annotations.append(
dict(
x=j,
y=i,
text=f"{df_transposed.iloc[i, j]:.1f}",
showarrow=False,
font=dict(size=9, color='black')
)
)
fig.update_layout(annotations=annotations)
return fig
# 旭日图
def plot_recommendation_sunburst(dataset_radio, num_parts_dropdown, perspective_radio, division_method_radio):
import plotly.graph_objects as go
_, recommendation_result = load_analysis_report(dataset_radio, num_parts_dropdown, perspective_radio, division_method_radio)
labels = ['Model Recommendation'] # 根节点
parents = ['']
values = []
customdata = ['Choose your preference model']
# 统计每个场景下模型数量
scenario_model_count = {}
total_model_count = 0
for scenario, model_list in recommendation_result.items():
# 处理模型
model_items = []
if isinstance(model_list, dict):
model_items = model_list.items()
elif isinstance(model_list, list):
for d in model_list:
if isinstance(d, dict):
for k, v in d.items():
model_items.append((k, v))
scenario_model_count[scenario] = len(model_items)
total_model_count += len(model_items)
# 根节点 value
values.append(total_model_count)
# 再次遍历,填充 labels/parents/values/customdata
for scenario, model_list in recommendation_result.items():
scenario_words = scenario.split()
short_label = " ".join(scenario_words[:3]) + "..." if len(scenario_words) > 3 else scenario
labels.append(short_label)
parents.append('Model Recommendation')
values.append(scenario_model_count[scenario])
customdata.append(scenario)
# 处理模型
model_items = []
if isinstance(model_list, dict):
model_items = model_list.items()
elif isinstance(model_list, list):
for d in model_list:
if isinstance(d, dict):
for k, v in d.items():
model_items.append((k, v))
for model, reason in model_items:
labels.append(model)
parents.append(short_label)
values.append(1)
customdata.append(reason)
fig = go.Figure(go.Sunburst(
labels=labels,
parents=parents,
values=values,
branchvalues="total",
hovertemplate='%{customdata}<extra></extra>',
customdata=customdata
))
fig.update_layout(margin=dict(t=10, l=10, r=10, b=10), height=500)
return fig
### Gradio代码部分 ###
# 自定义 CSS 样式
custom_css = """
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f9f9f9;
}
.gr-label {
font-size: 15px;
}
.gr-button-primary {
background-color: #4CAF50;
color: white;
border-radius: 8px;
}
.gr-tabs > .tab-nav {
background-color: #e0e0e0;
border-bottom: 2px solid #ccc;
}
.gr-tabs > .tab-nav button.selected {
background-color: #ffffff !important;
border-bottom: 2px solid #4CAF50;
}
.gr-panel {
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
background-color: #fff;
}
.markdown-title {
font-size: 1.5em;
font-weight: bold;
margin-bottom: 10px;
}
.analysis-box {
background-color: #f1f8ff;
padding: 20px;
border-left: 5px solid #4CAF50;
border-radius: 6px;
margin-top: 10px;
}
.recommendation-box {
background-color: #fff3cd;
padding: 20px;
border-left: 5px solid #ff9800;
border-radius: 6px;
margin-top: 10px;
}
</style>
"""
# 构建界面
with gr.Blocks(css=custom_css) as iface:
gr.HTML("""
<div style='text-align:center; padding:15px;'>
<h1>Multi-view Code LLM Leaderboard</h1>
<p>Multi-view Leaderboard: Evaluating Large Language Models From Multiple Views</p>
</div>
""")
with gr.Row():
# 选择配置
with gr.Column(scale=1):
dataset_radio = gr.Radio(
["HumanEval", "MBPP"],
label="Select a dataset",
value="HumanEval"
)
num_parts_slider = gr.Slider(
minimum=3,
maximum=8,
step=1,
label="Choose the Number of Subsets",
value=3
)
# 将多个checkbox改为一个radio
perspective_radio = gr.Radio(
["I - Num of Tokens in Problem Desc",
"II - Num of Lines in Problem Desc",
"III - Complexity of Reference Code",
"IV - Problem Types"],
label="Choose Perspective",
value="I - Num of Tokens in Problem Desc"
)
# 统一的division method radio
division_method_radio = gr.Radio(
["Equal Frequency Partitioning", "Equal Interval Partitioning"],
label="Choose the Division Method",
visible=True
)
confirm_btn = gr.Button("Confirm", variant="primary")
# 核心展示
with gr.Column(scale=2):
with gr.Tabs():
# 表格
with gr.TabItem("Ranking Table"):
data_table = gr.Dataframe(headers=["Model", "Score","Analysis"],interactive=True)
# 可视化
with gr.TabItem("Visualization"):
plot_type = gr.Radio(
choices=["Line Chart", "Radar Chart","Heatmap"],
label="Select Plot Type",
value="Line Chart"
)
chart = gr.Plot()
# AI分析
with gr.TabItem("Model selection suggestions"):
with gr.Column():
gr.Markdown("<h2 class='markdown-title'>🎯 Model Recommendation</h2>")
recommendation_plot = gr.Plot()
scenario_legend = gr.Markdown(value="") # 新增图例
def update_perspective_options(dataset):
if dataset == "MBPP":
return gr.update(choices=[
"I - Num of Tokens in Problem Desc",
"III - Complexity of Reference Code",
"IV - Problem Types"
])
else:
return gr.update(choices=[
"I - Num of Tokens in Problem Desc",
"II - Num of Lines in Problem Desc",
"III - Complexity of Reference Code",
"IV - Problem Types"
])
dataset_radio.change(
fn=update_perspective_options,
inputs=dataset_radio,
outputs=perspective_radio
)
# 绑定事件
confirm_btn.click(
fn=on_confirm,
inputs=[dataset_radio, num_parts_slider, perspective_radio, division_method_radio],
outputs=data_table
).then(
fn=load_analysis_report,
inputs=[dataset_radio, num_parts_slider, perspective_radio, division_method_radio],
outputs=[gr.State()]
).then(
fn=plot_visualization,
inputs=[dataset_radio, perspective_radio, num_parts_slider, plot_type],
outputs=chart
).then(
fn=plot_recommendation_sunburst,
inputs=[dataset_radio, num_parts_slider, perspective_radio, division_method_radio],
outputs=[recommendation_plot] # 注意这里是列表
)
plot_type.change(
fn=plot_visualization,
inputs=[dataset_radio, perspective_radio, num_parts_slider, plot_type],
outputs=chart
)
# 启动界面
iface.launch() |