File size: 12,950 Bytes
3c75092
a6e43e6
3c75092
0bb476f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6e43e6
3c75092
 
0bb476f
 
 
 
6734e22
0bb476f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c75092
 
0bb476f
 
 
80c5be6
 
0bb476f
80c5be6
0bb476f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c75092
0bb476f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80c5be6
0bb476f
 
80c5be6
0bb476f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6e43e6
3c75092
a6e43e6
0bb476f
3c75092
0bb476f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ffee3
a6e43e6
0bb476f
a6e43e6
 
0bb476f
a6e43e6
0bb476f
 
 
3c75092
 
0bb476f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c75092
 
a6e43e6
3c75092
a6e43e6
3c75092
f1fda01
6734e22
f1fda01
3c75092
0bb476f
3c75092
0bb476f
 
 
 
80c5be6
0bb476f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import abc

import gradio as gr
from loguru import logger
import pandas as pd
from collections import defaultdict

from judgerbench.preprocess.gen_table import (
    format_timestamp,
    generate_table, 
    build_l1_df,
    # build_l2_df,
)
from judgerbench.meta_data import (
    LEADERBORAD_INTRODUCTION,
    LEADERBOARD_MD,
    LEADERBOARD_FILE_MAPPING,
    MAIN_FIELDS,
    DEFAULT_BENCH,
    STYLE_CLASS_MAPPING,
    CITATION_BUTTON_TEXT,
    CITATION_BUTTON_LABEL,
)


def refresh_dataframe(required_fields):
    df = generate_table(MAIN_FIELDS)

    comp = gr.DataFrame(
        value=df,
        type='pandas',
        interactive=False,
        visible=True
    )

    return comp


with gr.Blocks() as demo:
    # struct = load_results()
    # timestamp = struct['time']

    # EVAL_TIME = format_timestamp(timestamp)
    EVAL_TIME = '20241022'

    # results = struct['results']
    # N_MODEL = len(results)
    # N_DATA = len(results['LLaVA-v1.5-7B']) - 1

    N_MODEL = 10
    N_DATA = 100

    # DATASETS = list(results['LLaVA-v1.5-7B'])
    # DATASETS.remove('META')
    # print(DATASETS)

    gr.Markdown(LEADERBORAD_INTRODUCTION.format(
        # N_MODEL, 
        # N_DATA, 
        EVAL_TIME
    ))
    # structs = [abc.abstractproperty() for _ in range(N_DATA)]

    with gr.Tabs(elem_classes='tab-buttons') as tabs:
        for cur_id, (filename, filepath) in enumerate(LEADERBOARD_FILE_MAPPING.items()):

            tab_name = filename
            # if filename == "overall":
            #     tab_name = '๐Ÿ… JudgerBench Main Leaderboard'

            with gr.Tab(tab_name.upper(), elem_id=f'tab_{cur_id}', id=cur_id):

                # gr.Markdown(LEADERBOARD_MD['MAIN'])
                # _, check_box = build_l1_df(MAIN_FIELDS)
                table = generate_table(filename=filename)

                # type_map = check_box['type_map']
                type_map = defaultdict(lambda: 'number')
                type_map['Model'] = 'str'
                type_map['Class'] = 'str'
                type_map['Rank'] = 'number'

                # required_fields = gr.State(
                #     check_box['essential'] 
                #     # + ["Average"]
                # )

                # checkbox_group = gr.CheckboxGroup(
                #     choices=[item for item in check_box['all'] if item not in required_fields.value],
                #     value=[item for item in check_box['default'] if item not in required_fields.value],
                #     label='Evaluation Metrics',
                #     interactive=True,
                # )

                # headers = (
                #     ['Rank'] +
                #     required_fields.value +
                #     [item for item in check_box['all'] if item not in required_fields.value]
                #     # checkbox_group.value
                # )

                table['Rank'] = list(range(1, len(table) + 1))

                # Rearrange columns
                if "Class" in table.columns:
                    starting_columns = ["Rank", "Models", "Class"]
                else:
                    starting_columns = ["Rank", "Models"]

                table = table[starting_columns + [ col for col in table.columns if col not in starting_columns ]]

                headers = (
                    # ['Rank'] +
                    list(table.columns)
                )

                if "Class" in table.columns:
                    def cell_styler(v):
                        df = v.copy()

                        class_var = df[['Class']].copy()

                        df.loc[:, :] = ''
                        df[['Class']] = class_var.map(lambda x: f"background-color: {STYLE_CLASS_MAPPING[x]}")
                        logger.info(df['Class'])

                        return df

                    table_styler = (
                        table.style.apply(cell_styler, axis=None)
                        .format(precision=1)
                    )
                else:
                    table_styler = table.style.format(precision=1)

                # with gr.Row():
                #     model_size = gr.CheckboxGroup(
                #         choices=MODEL_SIZE,
                #         value=MODEL_SIZE,
                #         label='Model Size',
                #         interactive=True
                #     )
                #     model_type = gr.CheckboxGroup(
                #         choices=MODEL_TYPE,
                #         value=MODEL_TYPE,
                #         label='Model Type',
                #         interactive=True
                #     )
                data_component = gr.DataFrame(
                    value=table_styler,
                    type='pandas',
                    datatype=[type_map[x] for x in headers],
                    interactive=False,
                    visible=True
                )

                def filter_df(
                        required_fields,
                        fields,
                        # model_size, 
                        # model_type
                    ):
                    # filter_list = ['Avg Score', 'Avg Rank', 'OpenSource', 'Verified']
                    headers = ['Rank'] + required_fields + fields

                    # new_fields = [field for field in fields if field not in filter_list]
                    df = generate_table(fields)
                    logger.info(f"{df.columns=}")

                    # df['flag'] = [model_size_flag(x, model_size) for x in df['Param (B)']]
                    # df = df[df['flag']]
                    # df.pop('flag')

                    # if len(df):
                    #     df['flag'] = [model_type_flag(df.iloc[i], model_type) for i in range(len(df))]
                    #     df = df[df['flag']]
                    #     df.pop('flag')

                    df['Rank'] = list(range(1, len(df) + 1))

                    comp = gr.DataFrame(
                        value=df[headers],
                        type='pandas',
                        datatype=[type_map[x] for x in headers],
                        interactive=False,
                        visible=True
                    )
                    
                    return comp

                # for cbox in [
                #         # checkbox_group, 
                #         # model_size, 
                #         # model_type
                #     ]:
                #     cbox.change(
                #         fn=refresh_dataframe,
                #         inputs=[required_fields],
                #         outputs=data_component
                #     ).then(
                #         fn=filter_df,
                #         inputs=[
                #             required_fields,
                #             checkbox_group, 
                #             # model_size, 
                #             # model_type
                #         ], 
                #         outputs=data_component
                #     )

            # with gr.Tab('๐Ÿ” About', elem_id='about', id=1):
            #     gr.Markdown(urlopen(VLMEVALKIT_README).read().decode())

            # for i, dataset in enumerate(DATASETS):
            #     with gr.Tab(f'๐Ÿ“Š {dataset} Leaderboard', elem_id=dataset, id=i + 2):
            #         if dataset in LEADERBOARD_MD:
            #             gr.Markdown(LEADERBOARD_MD[dataset])

            #         s = structs[i]
            #         s.table, s.check_box = build_l2_df(results, dataset)
            #         s.type_map = s.check_box['type_map']
            #         s.type_map['Rank'] = 'number'

            #         s.checkbox_group = gr.CheckboxGroup(
            #             choices=s.check_box['all'],
            #             value=s.check_box['required'],
            #             label=f'{dataset} CheckBoxes',
            #             interactive=True,
            #         )
            #         s.headers = ['Rank'] + s.check_box['essential'] + s.checkbox_group.value
            #         s.table['Rank'] = list(range(1, len(s.table) + 1))

            #         with gr.Row():
            #             s.model_size = gr.CheckboxGroup(
            #                 choices=MODEL_SIZE,
            #                 value=MODEL_SIZE,
            #                 label='Model Size',
            #                 interactive=True
            #             )
            #             s.model_type = gr.CheckboxGroup(
            #                 choices=MODEL_TYPE,
            #                 value=MODEL_TYPE,
            #                 label='Model Type',
            #                 interactive=True
            #             )
            #         s.data_component = gr.components.DataFrame(
            #             value=s.table[s.headers],
            #             type='pandas',
            #             datatype=[s.type_map[x] for x in s.headers],
            #             interactive=False,
            #             visible=True)
            #         s.dataset = gr.Textbox(value=dataset, label=dataset, visible=False)

            #         def filter_df_l2(dataset_name, fields, model_size, model_type):
            #             s = structs[DATASETS.index(dataset_name)]
            #             headers = ['Rank'] + s.check_box['essential'] + fields
            #             df = cp.deepcopy(s.table)
            #             df['flag'] = [model_size_flag(x, model_size) for x in df['Param (B)']]
            #             df = df[df['flag']]
            #             df.pop('flag')
            #             if len(df):
            #                 df['flag'] = [model_type_flag(df.iloc[i], model_type) for i in range(len(df))]
            #                 df = df[df['flag']]
            #                 df.pop('flag')
            #             df['Rank'] = list(range(1, len(df) + 1))

            #             comp = gr.components.DataFrame(
            #                 value=df[headers],
            #                 type='pandas',
            #                 datatype=[s.type_map[x] for x in headers],
            #                 interactive=False,
            #                 visible=True)
            #             return comp

            #         for cbox in [s.checkbox_group, s.model_size, s.model_type]:
            #             cbox.change(
            #                 fn=filter_df_l2,
            #                 inputs=[s.dataset, s.checkbox_group, s.model_size, s.model_type],
            #                 outputs=s.data_component)

    with gr.Row():
        with gr.Accordion('Citation', open=False):
            citation_button = gr.Textbox(
                value=CITATION_BUTTON_TEXT,
                label=CITATION_BUTTON_LABEL,
                elem_id='citation-button',
                lines=7,
            )


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--host", type=str, default="0.0.0.0")
    parser.add_argument("--port", type=int, default="7860")
    parser.add_argument(
        "--share",
        action="store_true",
        help="Whether to generate a public, shareable link",
    )
    parser.add_argument(
        "--concurrency-count",
        type=int,
        default=10,
        help="The concurrency count of the gradio queue",
    )
    parser.add_argument(
        "--max-threads",
        type=int,
        default=200,
        help="The maximum number of threads available to process non-async functions.",
    )
    # parser.add_argument(
    #     "--gradio-auth-path",
    #     type=str,
    #     help='Set the gradio authentication file path. The file should contain one or more user:password pairs in this format: "u1:p1,u2:p2,u3:p3"',
    #     default=None,
    # )
    parser.add_argument(
        "--gradio-root-path",
        type=str,
        help="Sets the gradio root path, eg /abc/def. Useful when running behind a reverse-proxy or at a custom URL path prefix",
    )
    parser.add_argument(
        "--ga-id",
        type=str,
        help="the Google Analytics ID",
        default=None,
    )
    parser.add_argument(
        "--use-remote-storage",
        action="store_true",
        default=False,
        help="Uploads image files to google cloud storage if set to true",
    )
    args = parser.parse_args()
    logger.info(f"args: {args}")

    # Set authorization credentials
    # auth = None
    # if args.gradio_auth_path is not None:
    #     auth = parse_gradio_auth_creds(args.gradio_auth_path)

    demo.queue(
        default_concurrency_limit=args.concurrency_count,
        status_update_rate=10,
        api_open=False,
    ).launch(
        server_name=args.host,
        server_port=args.port,
        share=args.share,
        max_threads=args.max_threads,
        # auth=auth,
        root_path=args.gradio_root_path,
        # debug=True,
        show_error=True,
        allowed_paths=["../.."]
    )