forked from modelscope/evalscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz.py
398 lines (331 loc) · 11.6 KB
/
viz.py
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
# Copyright (c) Alibaba, Inc. and its affiliates.
# flake8: noqa
import argparse
import os
import re
import json
import pandas as pd
import plotly.graph_objects as go
import seaborn as sns
import streamlit as st
import yaml
def generate_color_palette(n):
palette = sns.color_palette('hls', n)
return palette.as_hex()
def read_yaml(yaml_file) -> dict:
"""
Read yaml file to dict.
"""
with open(yaml_file, 'r') as f:
try:
stream = yaml.safe_load(f)
except yaml.YAMLError as e:
print(e)
raise e
return stream
def read_jsonl(input_file):
all_data = []
with open(input_file, 'r') as input_jsonl:
for line in input_jsonl:
data = json.loads(line)
all_data.append(data)
return all_data
def cat_view(df, category, models):
cat_df = df[df['category'] == category] if category != 'all' else df
model_scores = {}
for model in models:
model_a_scores = (
cat_df[cat_df['model_a'] == model]['scores'].apply(
lambda x: x[0]).sum())
model_b_scores = (
cat_df[cat_df['model_b'] == model]['scores'].apply(
lambda x: x[1]).sum())
# calculate count of occurrences for model_a and model_b
model_a_counts = cat_df[cat_df['model_a'] == model].shape[0]
model_b_counts = cat_df[cat_df['model_b'] == model].shape[0]
# calculate average scores for each model
model_scores[model] = (model_a_scores + model_b_scores) / (
model_a_counts + model_b_counts)
return dict(
category=category,
count=cat_df['question_id'].nunique(),
**dict(model_scores),
)
def get_color(value):
good_thresholds = [0.2, 0.1, 0.05]
bad_thresholds = [-0.2, -0.1, -0.05]
good_colors = ['#32CD32', '#98FF98', '#D0F0C0']
bad_colors = ['#FF6347', '#FA8072', '#ffcccb']
color = ''
for i in range(len(good_thresholds)):
if value > good_thresholds[i]:
color = good_colors[i]
break
for i in range(len(bad_thresholds)):
if value < bad_thresholds[i]:
color = bad_colors[i]
break
return 'background-color: %s' % color if color else ''
def get_category_map(category_file):
if not category_file or not os.path.exists(category_file):
return dict()
category_mapping = read_yaml(category_file)
return category_mapping
def get_category_group(category_map, cat):
for key, value in category_map.items():
if cat in value or '*' in value:
return key
return cat
def show_table_view(df):
models = df['model_a'].unique().tolist()
for model in df['model_b'].unique().tolist():
if model not in models:
models.append(model)
catogories = df['category'].unique().tolist()
# Calculate the average score for each category
cat_data = [cat_view(df, category, models) for category in catogories]
cat_df = pd.DataFrame(cat_data)
cat_df.sort_values(
by=models[0], ascending=False, inplace=True, ignore_index=True)
# Add total data
total_data = cat_view(df, 'all', models)
cat_df.loc[cat_df.shape[0]] = [
'总计',
total_data['count'],
*[total_data[model] for model in models],
]
# Render as link for each category
cat_df['category'] = cat_df['category'].apply(
lambda x:
'<a href="?category={}&model_a={}&model_b={}" target="_self">{}</a>'.
format(x, models[0], models[1], x), )
# Format the table
if len(models) == 2:
cat_df['score diff'] = cat_df.apply(
lambda x: (x[models[1]] - x[models[0]]) / x[models[0]], axis=1)
cat_df.rename(
columns={
'category': '类别',
'count': 'Case数量',
'score diff': '分差百分比',
},
inplace=True,
)
style_df = cat_df.style.format({
**dict(zip(models, ['{:.2f}' for model in models])),
'分差百分比':
'{:.2%}',
}).applymap(
get_color, subset=['分差百分比'])
else:
cat_df.rename(
columns={
'category': '类别',
'count': 'Case数量',
},
inplace=True,
)
def format(row):
p = '{:.2%}'.format(
(row[model] - row[baseline_model]) / row[baseline_model])
p = '+' + p if p[0] != '-' else p
return f'{row[model]:.2f} ({p})'
def color(value):
match = re.search(r'\((.*?)\)', value)
return get_color(float(match.group(1).strip('%'))
/ 100) if match else ''
baseline_model = models[0]
for model in models[1:]:
cat_df[model] = cat_df.apply(
format,
axis=1,
)
cat_df[baseline_model] = cat_df[baseline_model].apply(
lambda x: '{:.2f}'.format(x))
style_df = cat_df.style.applymap(color, subset=models[1:])
# df_html = style_df.to_html(escape=False, index=False) # TODO
df_html = style_df.to_html()
st.markdown(df_html, unsafe_allow_html=True)
def show_radar_chart(df):
score_list = []
for index, row in df.iterrows():
score_list.append(
dict(
model=row['model_a'],
category=row['category'],
score=row['scores'][0]))
score_list.append(
dict(
model=row['model_b'],
category=row['category'],
score=row['scores'][1]))
score_df = pd.DataFrame(score_list)
df_agg = score_df.groupby(['model',
'category'])['score'].mean().reset_index()
pivot_df = df_agg.pivot(
index='model', columns='category', values='score').fillna(0)
categories = pivot_df.columns.tolist()
fig = go.Figure()
num_models = len(pivot_df.index.tolist())
color_palette = generate_color_palette(num_models)
color_dict = dict(zip(pivot_df.index.tolist(), color_palette))
for model in pivot_df.index.tolist():
model_values = pivot_df[pivot_df.index == model].values.tolist()[0]
model_values.append(model_values[0]) # Make the data cyclic
fig.add_trace(
go.Scatterpolar(
r=model_values,
theta=categories
+ [categories[0]], # Make the categories cyclic
fill='none',
name=model,
line=dict(color=color_dict[model]),
))
fig.update_layout(
polar=dict(radialaxis=dict(visible=True, range=[0, 10])),
showlegend=True)
st.plotly_chart(fig)
def show_single_result(df, category, model_a, model_b):
categories = df['category'].unique().tolist()
model_names = df['model_a'].unique().tolist()
col1, col2 = st.columns([1, 3])
with col1:
category = st.selectbox(
'选择类别',
categories,
index=categories.index(category) if category in categories else 0,
)
df = df[df['category'] == category]
with col2:
ques = st.selectbox('选择问题', df['question'].unique().tolist())
col1, col2 = st.columns(2)
with col1:
model_a_options = [model_a]
model_a = st.selectbox(
'选择模型A',
model_a_options,
index=model_a_options.index(model_a),
)
with col2:
model_b = st.selectbox('选择模型B',
[m for m in model_names if m != model_a])
with st.container():
st.markdown(
"""
<div style="background-color:#DEEBF7;padding:10px;margin:10px 0;border-radius:8px">
<b>问题:</b>
{ques}
</div>""".format(ques=ques),
unsafe_allow_html=True,
)
col1, col2 = st.columns(2)
with col1:
st.write(
"""
<div style="background-color:#E2F0D9;padding:10px;border-radius:8px">
<p><b>{model_a} 回答</b></p>
{output_a}
</div>""".format(
model_a=model_a,
output_a=df[(df['question'] == ques) & (
df['model_a'] == model_a)].iloc[0]['output_a'],
),
unsafe_allow_html=True,
)
with col2:
st.write(
"""
<div style="background-color:#E2F0D9;padding:10px;border-radius:8px">
<p><b>{model_b} 回答</b></p>
{output_b}
</div>""".format(
model_b=model_b,
output_b=df[(df['question'] == ques) & (
df['model_b'] == model_b)].iloc[0]['output_b'],
),
unsafe_allow_html=True,
)
score_1 = df[(df['question'] == ques)
& (df['model_a'] == model_a)].iloc[0]['scores']
score_2 = df[(df['question'] == ques)
& (df['model_b'] == model_a)].iloc[0]['scores']
scores = [
{
'round': '第一轮',
model_a: score_1[0],
model_b: score_1[1]
},
{
'round': '第二轮',
model_a: score_2[1],
model_b: score_2[0]
},
]
score_df = pd.DataFrame(scores)
styled_df = score_df.style.highlight_max(
axis=1,
subset=[model_a, model_b],
color='lightgreen',
)
styled_df.format({
model_a: '{:.1f}',
model_b: '{:.1f}',
})
# score_html = styled_df.to_html(index=False) # TODO
score_html = styled_df.to_html()
st.markdown(
"""
<div style="background-color:#FBE5D6;padding:10px;margin:10px 0;border-radius:8px">
<b>GPT-4 评分:</b>
{score_html}
</div>""".format(score_html=score_html),
unsafe_allow_html=True,
)
def run_app(review_file, category_file):
category_map = get_category_map(category_file)
review_file = os.path.abspath(review_file)
data = read_jsonl(review_file)
df = pd.DataFrame(data)
df = df[[
'model_a',
'model_b',
'scores',
'category',
'question_id',
'question',
'output_a',
'output_b',
]]
df['category'] = df['category'].apply(
lambda x: get_category_group(category_map, x))
query_params = st.experimental_get_query_params()
if 'category' in query_params:
st.set_page_config(layout='wide')
st.write("<a href='/' target='_self'>返回</a>", unsafe_allow_html=True)
show_single_result(
df,
query_params['category'][0],
query_params['model_a'][0],
query_params['model_b'][0],
)
st.write("<a href='/' target='_self'>返回</a>", unsafe_allow_html=True)
else:
st.set_page_config(layout='centered')
st.write('### 评测结果展示(Arena 模式)')
st.write('#### 模型分类别得分')
show_table_view(df)
st.write('#### 模型得分雷达图')
show_radar_chart(df)
def parse_args():
parser = argparse.ArgumentParser(description='Run visualization on a evaluation.')
parser.add_argument('--review-file', type=str, default='evalscope/registry/data/qa_browser/battle.jsonl', required=True)
parser.add_argument('--category-file', type=str, default='evalscope/registry/data/qa_browser/category_mapping.yaml', required=True)
args = parser.parse_args()
return args
def main():
args = parse_args()
print(args)
run_app(args.review_file, args.category_file)
if __name__ == '__main__':
print('**Usage:\n streamlit run viz.py -- --review-file evalscope/registry/data/qa_browser/battle.jsonl --category-file evalscope/registry/data/qa_browser/category_mapping.yaml')
main()