File size: 10,569 Bytes
01f830e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87a0436
 
 
 
 
 
 
 
 
 
 
 
 
e6cfa34
87a0436
 
 
2a72d1d
 
 
 
 
 
 
 
 
 
 
 
 
 
87a0436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01f830e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac62be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01f830e
ac62be1
87a0436
01f830e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac62be1
 
87a0436
ac62be1
 
 
 
 
 
72b6fa6
ac62be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06c4694
 
 
 
 
 
 
01f830e
 
e6cfa34
 
 
06c4694
 
ac62be1
e6cfa34
01f830e
06c4694
 
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
import gradio as gr
import plotly.graph_objects as go
from datasets import load_dataset
import plotly.graph_objects as go
from plotly.subplots import make_subplots

import os
# ==
import json
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go


# CATEGORIES = ["Writing", "Roleplay", "Reasoning", "Math", "Coding", "Extraction", "STEM", "Humanities"]
CATEGORIES = ["task-solving", "math-reasoning", "general-instruction", "natural-question", "safety"]
LANGS = ['en', 'vi', 'th', 'id', 'km', 'lo', 'ms', 'my', 'tl']


force_download = bool(int(os.environ.get("force_download", "1")))
HF_TOKEN = str(os.environ.get("HF_TOKEN", ""))
DATA_SET_REPO_PATH = str(os.environ.get("DATA_SET_REPO_PATH", ""))
PERFORMANCE_FILENAME = str(os.environ.get("PERFORMANCE_FILENAME", "gpt4_single_json.csv"))

MODEL_DFRAME = None


CATEGORIES = ["task-solving", "math-reasoning", "general-instruction", "natural-question", "safety"]
LANGS = ['en', 'vi', 'th', 'id', 'km', 'lo', 'ms', 'my', 'tl']

FORCE_DOWNLOAD = bool(int(os.environ.get("FORCE_DOWNLOAD", "0")))
HF_TOKEN = str(os.environ.get("HF_TOKEN", ""))
DATA_SET_REPO_PATH = str(os.environ.get("DATA_SET_REPO_PATH", "SeaLLMs/Sea-bench"))

PERFORMANCE_FILENAME = str(os.environ.get("PERFORMANCE_FILENAME", "model_judgment/gpt-4_single.jsonl"))
QUESTION_FILE_NAME = str(os.environ.get("QUESTION_FILE_NAME", "question.jsonl"))

rename_map = {
    "seallm-13b-chat": "SeaLLM-13b",
    # "polylm-13b": "PolyLM-13b",
    "qwen-14b": "Qwen-14b",
    "gpt-3.5-turbo": "GPT-3.5-turbo",
}
rename_map = {
    "SeaLLM-7B-v2.5": "SeaLLM-7B-v2.5",
    # "SeaLLM-7B-v2.5-CUT3": "SeaLLM-7B-v2.5-CUT",
    "SeaLLM-7B-v2": "SeaLLM-7B-v2",
    "Sailor-7B-Chat": "Sailor-7B-Chat",
    # "SeaLLM-13B-v1": "SeaLLM-13B-v1",
    # "llama2-13b": "LLaMA2-13B",
    # "sealion7b_instr": "SeaLion-7b",
    # "polylm": "PolyLM-13b",
    # "qwen": "Qwen-14b",
    "qwen_15_7b_chat": "Qwen1.5-7B-chat",
    "gpt-3.5-turbo": "GPT-3.5-turbo",
    # "gpt-4-1106-preview": "GPT-4-turbo",
}
CATEGORIES = [ "task-solving",  "math-reasoning",  "general-instruction",  "natural-question",  "safety", ]

CATEGORIES_NAMES = {
    "task-solving": 'Task-solving', 
    "math-reasoning": 'Math', 
    "general-instruction": 'General-instruction', 
    "natural-question": 'NaturalQA', 
    "safety": 'Safety',
}

LANGS = ['en', 'vi', 'id', 'ms', 'tl', 'th', 'km', 'lo', 'my']
LANG_NAMES = {
    'en': 'eng', 
    'vi': 'vie', 
    'th': 'tha', 
    'id': 'ind', 
    'km': 'khm', 
    'lo': 'lao', 
    'ms': 'msa', 
    'my': 'mya', 
    'tl': 'tgl',
}



MODEL_DFRAME = None


def read_jsonl_report(question_path, file_path):
    with open(question_path, 'r') as f:
        questions = [
            json.loads(x)
            for x in f
        ]
        questions = {
            q['question_id']: q
            for q in questions
        }

    q2result = []
    fin = open(file_path, "r")
    for line in fin:
        obj = json.loads(line)
        obj["category"] = questions[obj['question_id']]['category']
        obj["lang"] = questions[obj['question_id']]['lang']
        q2result.append(obj)
    df = pd.DataFrame(q2result)
    return df

def get_report_df_from_jsonl():
    from huggingface_hub import hf_hub_download
    assert DATA_SET_REPO_PATH != ''
    assert HF_TOKEN != ''
    repo_id = DATA_SET_REPO_PATH
    question_path = hf_hub_download(
        repo_id=repo_id,
        filename=QUESTION_FILE_NAME,
        force_download=FORCE_DOWNLOAD,
        local_dir='./hf_cache',
        repo_type="dataset",
        token=HF_TOKEN
    )
    file_path = hf_hub_download(
        repo_id=repo_id,
        filename=PERFORMANCE_FILENAME,
        force_download=FORCE_DOWNLOAD,
        local_dir='./hf_cache',
        repo_type="dataset",
        token=HF_TOKEN
    )
    print(f'Downloaded file at {question_path}/ {file_path} from {DATA_SET_REPO_PATH} / {PERFORMANCE_FILENAME}')
    return read_jsonl_report(question_path, file_path)


def get_model_df():
    global MODEL_DFRAME
    if isinstance(MODEL_DFRAME, pd.DataFrame):
        print(f'Load cache data frame')
        return MODEL_DFRAME
    from huggingface_hub import hf_hub_download
    assert DATA_SET_REPO_PATH != ''
    assert HF_TOKEN != ''
    repo_id = DATA_SET_REPO_PATH
    filename = PERFORMANCE_FILENAME

    # data_path = f"{DATA_SET_REPO_PATH}/{PERFORMANCE_FILENAME}"
    file_path = hf_hub_download(
        repo_id=repo_id,
        filename=filename,
        force_download=force_download,
        local_dir='./hf_cache',
        repo_type="dataset",
        token=HF_TOKEN
    )
    print(f'Downloaded file at {file_path} from {DATA_SET_REPO_PATH} / {PERFORMANCE_FILENAME}')
    MODEL_DFRAME = pd.read_csv(file_path)
    return MODEL_DFRAME


def aggregate_df(df, model_dict, category_name, categories):
    scores_all = []
    all_models = df["model"].unique()
    for model in all_models:
        for i, cat in enumerate(categories):
            # filter category/model, and score format error (<1% case)
            res = df[(df[category_name]==cat) & (df["model"]==model) & (df["score"] >= 0)]
            score = res["score"].mean()
            cat_name = cat
            scores_all.append({"model": model, category_name: cat_name, "score": score})
    
    target_models = list(model_dict.keys())
    scores_target = [scores_all[i] for i in range(len(scores_all)) if scores_all[i]["model"] in target_models]
    scores_target = sorted(scores_target, key=lambda x: target_models.index(x["model"]), reverse=True)

    df_score = pd.DataFrame(scores_target)
    df_score = df_score[df_score["model"].isin(target_models)]
    
    rename_map = model_dict

    for k, v in rename_map.items():
        df_score.replace(k, v, inplace=True)
    return df_score


def polar_subplot(fig, dframe, model_names, category_label, category_names, row, col, showlegend=True):

    # cat category
    colors = px.colors.qualitative.Plotly
    for i, (model, model_name) in enumerate(model_names):
        cat_list = dframe[dframe['model'] == model_name][category_label].tolist()
        score_list = dframe[dframe['model'] == model_name]['score'].tolist()
        cat_list += [cat_list[0]]
        cat_list = [category_names[x] for x in cat_list]
        score_list += [score_list[0]]
        polar = go.Scatterpolar(
            name = model_name,
            r = score_list,
            theta = cat_list,
            legendgroup=f'{i}',
            marker=dict(color=colors[i]),
            hovertemplate="""Score: %{r:.2f}""",
            showlegend=showlegend,
        )
        fig.add_trace(polar, row, col)


def plot_agg_fn():
    df = get_report_df_from_jsonl()

    all_models = df["model"].unique()
    model_names = list(rename_map.items())
    colors = px.colors.qualitative.Plotly

    cat_df = aggregate_df(df, rename_map, "category", CATEGORIES, )
    lang_df = aggregate_df(df, rename_map, "lang", LANGS, )

    fig = make_subplots(
        rows=1, cols=2, 
        specs=[[{'type': 'polar'}]*2],
        subplot_titles=("By Category", "By Language"),
    )
    fig.layout.annotations[0].y = 1.05
    fig.layout.annotations[1].y = 1.05

    # cat category
    for i, (model, model_name) in enumerate(model_names):
        cat_list = cat_df[cat_df['model'] == model_name]['category'].tolist()
        score_list = cat_df[cat_df['model'] == model_name]['score'].tolist()
        cat_list += [cat_list[0]]
        cat_list = [CATEGORIES_NAMES[x] for x in cat_list]
        score_list += [score_list[0]]
        polar = go.Scatterpolar(
            name = model_name,
            r = score_list,
            theta = cat_list,
            legendgroup=f'{i}',
            marker=dict(color=colors[i]),
            hovertemplate="""Score: %{r:.2f}""",
            )
        fig.add_trace(polar, 1, 1)

    # cat langs
    for i, (model, model_name) in enumerate(model_names):
        cat_list = lang_df[lang_df['model'] == model_name]['lang'].tolist()
        score_list = lang_df[lang_df['model'] == model_name]['score'].tolist()
        cat_list += [cat_list[0]]
        score_list += [score_list[0]]
        cat_list = [LANG_NAMES[x] for x in cat_list]
        polar = go.Scatterpolar(
            name = model_name,
            r = score_list,
            theta = cat_list,
            legendgroup=f'{i}',
            marker=dict(color=colors[i]),
            hovertemplate="""Score: %{r:.2f}""",
            showlegend=False,
            )
        fig.add_trace(polar, 1, 2)


    polar_config = dict(
        angularaxis = dict(
        rotation=90, # start position of angular axis
        ),
        radialaxis = dict(
            range=[0, 10],
        ),
    )

    fig.update_layout(
        polar = polar_config,
        polar2 = polar_config,
        title='Sea-Bench (rated by GPT-4)',
    )
    return fig


def plot_by_lang_fn():
    df = get_report_df_from_jsonl()
    model_names = list(rename_map.items())

    fig = make_subplots(
        rows=3, cols=3, 
        specs=[[{'type': 'polar'}]*3] * 3,
        # subplot_titles=("By Category", "By Language"),
        subplot_titles=[LANG_NAMES[x] for x in LANGS],
        # vertical_spacing=1
    )
    # print(fig.layout.annotations)
    for ano in fig.layout.annotations:
        ano.y = ano.y + 0.02
    has_safety = ['vi', 'id', 'th']

    for lang_id, lang in enumerate(LANGS):
        cat_names = CATEGORIES if lang in has_safety else [x for x in CATEGORIES if x != 'safety']
        cat_lang_df = aggregate_df(df[df['lang'] == lang], rename_map, "category", cat_names, )
        row = lang_id // 3 + 1
        col = lang_id % 3 + 1
        polar_subplot(fig, cat_lang_df, model_names, 'category', CATEGORIES_NAMES, row, col, showlegend=lang_id == 0)

    polar_config = dict(
        angularaxis = dict(
            rotation=90, # start position of angular axis
        ),
        radialaxis = dict(
            range=[0, 10],
        ),
    )
    layer_kwargs = {f"polar{i}": polar_config for i in range(1, 10)}
    fig.update_layout(
        title='Sea-Bench - By language (rated by GPT-4)',
        height=1000,
        width=1500,
        **layer_kwargs
    )
    return fig




description = """
<h1>Sea-bench benchmark leaderboard</h1>
"""


with gr.Blocks() as demo:
    with gr.Column():
        # gr.Markdown(description)
        gr_plot_agg = gr.Plot(label="Aggregated by Category and Language")
        # gr_plot_bylang = gr.Plot(label='Breakdown by language')


    demo.load(plot_agg_fn, [], gr_plot_agg)
    # demo.load(plot_by_lang_fn, [], gr_plot_bylang)


demo.launch()