File size: 7,756 Bytes
1ddbee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fb3ba6
 
 
 
1ddbee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import gradio as gr
import pandas as pd
import numpy as np

from collections import defaultdict


LENGTHS = ["dataset_total_score", "4k", "8k", "16k", "32k", "64k", "128k"]
datasets_params = json.load(open("datasets_config.json", "r"))
TASKS = datasets_params.keys()


def make_default_md():
    leaderboard_md = f"""

    πŸ… LIBRA LeaderBoard

    | [GitHub](https://github.com/ai-forever/LIBRA) | [Datasets](https://huggingface.co/datasets/ai-forever/LIBRA) |

    """
    return leaderboard_md


def make_model_desc_md():
    with open("docs/description.md", "r") as f:
        description = f.read()
    return description


def make_overall_table_by_tasks(files):
    results = defaultdict(list)

    result_dct = {}
    for file in files:
        if not file.endswith("json"): continue
        path = "results/" + file
        data = json.load(open(path))
        model_name = file.split('/')[-1].split(".json")[0]
        result_dct[model_name] = {}
        for dataset in data.keys():
            if dataset == "total_score":
                result_dct[model_name][dataset] = round(data[dataset] * 100, 1)
                continue
            result_dct[model_name][dataset] = round(data[dataset]["dataset_total_score"] * 100, 1)

    for file in files:
        if not file.endswith("json"): continue
        model_name = file.split('/')[-1].split(".json")[0]
        results['Model'].append(model_name)
        for key in result_dct[model_name].keys():
            if key == "total_score":
                results["Total Score"].append(result_dct[model_name][key])
            else:
                results[datasets_params[key]["name"]].append(result_dct[model_name][key])

    table = pd.DataFrame(results).sort_values(['Total Score'], ascending=False)
    cols = table.columns.tolist()
    cols = [cols[0]] + [cols[22]] + cols[1:22]
    return table[cols]


def make_overall_table_by_lengths(files):
    results = defaultdict(list)

    result_dct = {}
    for file in files:
        if not file.endswith("json"): continue
        path = "results/" + file
        data = json.load(open(path))
        model_name = file.split('/')[-1].split(".json")[0]
        result_dct[model_name] = {}
        for dataset in data.keys():
            if dataset == "total_score":
                result_dct[model_name][dataset] = data[dataset]
                continue
            for length in data[dataset].keys():
                if length == "dataset_total_score": continue
                if length not in result_dct[model_name]:
                    result_dct[model_name][length] = []
                result_dct[model_name][length].append(data[dataset][length])

    for model_name in result_dct.keys():
        for length in result_dct[model_name].keys():
            result_dct[model_name][length] = round(np.mean(result_dct[model_name][length]) * 100, 1)

    for file in files:
        if not file.endswith("json"): continue
        model_name = file.split('/')[-1].split(".json")[0]
        results['Model'].append(model_name)
        for key in result_dct[model_name].keys():
            if key == "total_score":
                results["Total Score"].append(result_dct[model_name][key])
            else:
                results[key].append(result_dct[model_name][key])

    table = pd.DataFrame(results).sort_values(['Total Score'], ascending=False)
    cols = table.columns.tolist()
    cols = [cols[0]] + [cols[7]] + cols[1:7]
    return table[cols]


def load_model(files, tab_name):
    results = defaultdict(list)

    for file in files:
        if not file.endswith("json"): continue
        model_name = file.split('/')[-1].split(".json")[0]
        results['Model'].append(model_name)
        result = json.load(open("results/" + file, "r"))
        for length in LENGTHS:
            if length in result[tab_name].keys():
                if length == "dataset_total_score":
                    results["Dataset Total Score"].append(round(result[tab_name][length] * 100, 1))
                    continue
                results[length].append(round(result[tab_name][length] * 100, 1))
            else:
                results[length].append("-")

    return pd.DataFrame(results).sort_values(['Dataset Total Score'], ascending=False)


def build_leaderboard_tab(files):
    default_md = make_default_md()
    md_1 = gr.Markdown(default_md, elem_id="leaderboard_markdown")

    with gr.Tabs() as tabs:

        with gr.Tab("Results by Lengths", id=0):
            df = make_overall_table_by_lengths(files)
            gr.Dataframe(
                headers=[
                            "Model",
                        ] + LENGTHS,
                datatype=[
                    "markdown",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                ],
                value=df,
                elem_id="arena_leaderboard_dataframe",
                height=700,
                wrap=True,
            )

        with gr.Tab("Results by Tasks", id=1):
            df = make_overall_table_by_tasks(files)
            gr.Dataframe(
                headers=[
                            "Model",
                        ] + LENGTHS,
                datatype=[
                    "markdown",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str",
                    "str"
                ],
                value=df,
                elem_id="arena_leaderboard_dataframe",
                height=700,
                wrap=False,
            )

        for tab_id, tab_name in enumerate(TASKS):
                df = load_model(files, tab_name)
                with gr.Tab(datasets_params[tab_name]["name"], id=tab_id+2):
                    gr.Dataframe(
                        headers=[
                            "Model",
                        ] + LENGTHS,
                        datatype=[
                            "markdown",
                            "str",
                            "str",
                            "str",
                            "str",
                            "str",
                            "str",
                            "str",
                        ],
                        value=df,
                        elem_id="arena_leaderboard_dataframe",
                        height=700,
                        wrap=True,
                    )

        with gr.Tab("Description", id=tab_id + 3):
            desc_md = make_model_desc_md()
            gr.Markdown(desc_md, elem_id="leaderboard_markdown")

    return [md_1]


def build_demo(files):
    text_size = gr.themes.sizes.text_lg

    with gr.Blocks(title="LIBRA leaderboard",
                   theme=gr.themes.Base(text_size=text_size)) as demo:
        build_leaderboard_tab(files)
    return demo


if __name__ == "__main__":
    files = os.listdir("results")
    demo = build_demo(files)
    demo.launch(share=False)