File size: 13,348 Bytes
f417250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77887da
 
f417250
 
 
 
 
 
 
1fa987c
f417250
 
 
 
 
 
 
1fa987c
f417250
 
1fa987c
f417250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fa987c
 
f417250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import glob
import os
import logging

import pandas as pd
import gradio as gr
from gradio.themes.utils.sizes import text_md

from content import (HEADER_MARKDOWN, LEADERBOARD_TAB_TITLE_MARKDOWN, SUBMISSION_TAB_TITLE_MARKDOWN,
                     )

import json
from datetime import datetime
from pathlib import Path
from uuid import uuid4
import time
import gradio as gr

from huggingface_hub import HfApi, snapshot_download


JSON_DATASET_DIR = Path("../json_dataset")
JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)

JSON_DATASET_PATH = JSON_DATASET_DIR / f"train-{uuid4()}.json"


api = HfApi()

ORG= "CZLC"
REPO = f"{ORG}/LLM_benchmark_data"

def greet(name: str) -> str:
    return "Hello " + name + "!"


DATASET_VERSIONS = ['dev-set-1', 'dev-set-2']

HF_TOKEN = os.environ.get("HF_TOKEN")



class LeaderboardServer:
    def __init__(self, server_address):
        self.server_address = server_address
        self.repo_type = "dataset"
        self.local_leaderboard = snapshot_download(self.server_address,repo_type=self.repo_type, token=HF_TOKEN)

    def on_submit(self):
        self.local_leaderboard = snapshot_download(self.server_address,repo_type=self.repo_type, token=HF_TOKEN)

    def get_leaderboard(self):
        results = []
        for submission in glob.glob(os.path.join(self.local_leaderboard, "../data") + "/*.json"):
            data = json.load(open(submission))
            submission_id = data["metadata"]["model_description"]
            local_results = {group: data["results"][group]['acc'] for group in data['results']}
            local_results["submission_id"] = submission_id
            results.append(local_results)
        dataframe = pd.DataFrame.from_records(results)
        return dataframe

    def save_json(self,file) -> None:
        filename = os.path.basename(file)
        api.upload_file(
            path_or_fileobj=file,
            path_in_repo=f"data/{filename}",
            repo_id=self.server_address,
            repo_type=self.repo_type,
            token=HF_TOKEN,
        )



leaderboard_server =  LeaderboardServer(REPO)


logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')


LEADERBOARD_TYPES = ['LLM',]
MAX_SUBMISSIONS_PER_24H = 2
# DATASET_VERSIONS = ['dev-set-1', 'dev-set-2']
# CHALLENGE_NAME = 'NOTSOFAR1'


if __name__ == '__main__':
    with (gr.Blocks(theme=gr.themes.Soft(text_size=text_md), css="footer {visibility: hidden}") as main):
        app_state = gr.State({})
        # with gr.Row():
        #     greet_name = gr.Textbox(label="Name")
        #     greet_output = gr.Textbox(label="Greetings")
        # greet_btn = gr.Button("Greet")
        # greet_btn.click(fn=greet, inputs=greet_name, outputs=greet_output).success(
        #     fn=save_json,
        #     inputs=[greet_name, greet_output],
        #     outputs=None,
        # )

        with gr.Row():
            with gr.Row():
                gr.Markdown(HEADER_MARKDOWN)

        with gr.Row():

            # Leaderboards Tab #
            ####################
            def populate_leaderboard(leaderboard_type, dataset_version):
                gr.Info('Loading leaderboard...')
                time.sleep(1)
                leaderboard_df = leaderboard_server.get_leaderboard()
                # leaderboard_df = lb_server.get_leaderboard(
                #     submission_type=leaderboard_type, dataset_version=dataset_version)
                # if leaderboard_df.empty:
                return leaderboard_df
                # return leaderboard_df


            def create_leaderboard_tab(tab_name: str, idx: int, dataset_version_dropdown: gr.Dropdown):
                # dataset_version = dataset_version_dropdown.value
                print(f'Creating tab for {tab_name}, idx={idx}, dataset_version={dataset_version_dropdown}')
                with gr.Tab(id=tab_name, label=tab_name) as leaderboard_tab:
                    leaderboard_table = gr.DataFrame(populate_leaderboard(tab_name, None)) if idx == 0 \
                        else gr.DataFrame(pd.DataFrame(columns=['No submissions yet']))
                    leaderboard_tab.select(fn=populate_leaderboard,
                                           inputs=[gr.Text(tab_name, visible=False)],
                                           outputs=[leaderboard_table])
                    return leaderboard_table

            def on_dropdown_change():
                first_tab_name = LEADERBOARD_TYPES[0]
                leaderboard_server.on_submit()

                return gr.Tabs(selected=first_tab_name), populate_leaderboard(first_tab_name, None)


            with gr.Tab('Leaderboards') as leaderboards_tab:
                with gr.Row():
                    gr.Markdown(LEADERBOARD_TAB_TITLE_MARKDOWN)
                # with gr.Row():
                #     with gr.Column():
                #         dataset_version_drop = gr.Dropdown(choices=DATASET_VERSIONS, multiselect=False,
                #                                            value=DATASET_VERSIONS[-1], label="Dataset",
                #                                            interactive=True)
                #     with gr.Column():
                #         gr.Markdown('')  # Empty column for spacing
                #     with gr.Column():
                #         gr.Markdown('')  # Empty column for spacing
                #     with gr.Column():
                #         gr.Markdown('')  # Empty column for spacing
                with gr.Row():
                    with gr.Tabs() as leaderboards_tabs:
                        leaderboard_tables_list = []
                        for leaderboard_idx, leaderboard_type in enumerate(LEADERBOARD_TYPES):
                            l_tab = create_leaderboard_tab(leaderboard_type, leaderboard_idx, None)
                            leaderboard_tables_list.append(l_tab)

                # dataset_version_drop.select(fn=on_dropdown_change, inputs=[dataset_version_drop],
                #                             outputs=[leaderboards_tabs, leaderboard_tables_list[0]])


            # Submission Tab #
            ##################
            with gr.Tab('Submission'):
                with gr.Column():
                    def on_submit_pressed():
                        return gr.update(value='Processing submission...', interactive=False)

                    def validate_submission_inputs(team_name, submission_zip, submission_type, token):
                        if not team_name or not submission_zip or not submission_type:
                            raise ValueError('Please fill in all fields')
                        if not os.path.exists(submission_zip):
                            raise ValueError('File does not exist')
                        # if not submission_zip.endswith('.zip'):
                        #     raise ValueError('File must be a zip')
                        # if not token:
                        #     raise ValueError('Please insert a valid Hugging Face token')

                    def process_submission(team_name, submission, submission_type, description,
                                           app_state, request: gr.Request):
                        logging.info(f'{team_name}: new submission for track: {submission_type}')
                        try:
                            token = app_state.get('hf_token')
                            validate_submission_inputs(team_name, submission, submission_type, token)
                        except ValueError as err:
                            gr.Warning(str(err))
                            return


                        # metadata = {'challenge_name': CHALLENGE_NAME,
                        #             "dataset_version": DATASET_VERSIONS[-1],
                        #             'team_name': team_name,
                        #             'submission_type': submission_type,
                        #             'description': description,
                        #             'token': token,
                        #             'file_name': os.path.basename(submission_zip),
                        #             'file_size_mb': os.path.getsize(submission_zip) / 1024 / 1024,
                        #             'ip': request.client.host}
                        leaderboard_server.save_json(submission)

                        try:
                            gr.Info('Processing submission...')
                            # response = lb_server.add_submission(token=token, file_path=submission_zip, metadata=metadata)
                            # if 'error' in response:
                            #     gr.Warning(f'Failed to process submission - {response["error"]}')
                            # else:
                            gr.Info('Done processing submission')
                        except Exception as e:
                            gr.Warning(f'Submission failed to upload - {e}')

                    def on_submit_done():
                        on_dropdown_change()
                        leaderboard_server.on_submit()
                        # leaderboard_tab.children[0] = gr.DataFrame(populate_leaderboard(None, None))
                        # leaderboard_tab.render()
                        return gr.update(value='Submit', interactive=True)

                    gr.Markdown(SUBMISSION_TAB_TITLE_MARKDOWN)
                    submission_team_name_tb = gr.Textbox(label='Team Name')
                    submission_file_path = gr.File(label='Upload your results', type='filepath')
                    submission_type_radio = gr.Radio(label='Submission Track', choices=LEADERBOARD_TYPES)
                    with gr.Row():
                        hf_token_tb = gr.Textbox(label='Token', type='password')
                        submissions_24h_txt = gr.Textbox(label='Submissions 24h', value='')
                    description_tb = gr.Textbox(label='Description', type='text')
                    submission_btn = gr.Button(value='Submit', interactive=True)
                    gr.Markdown('### * Please make sure you are using NOTSOFAR dev-set-2 for your submissions')

                    submission_btn.click(
                        fn=on_submit_pressed,
                        outputs=[submission_btn]
                    ).then(
                        fn=process_submission,
                        inputs=[submission_team_name_tb, submission_file_path,
                                submission_type_radio, description_tb, app_state]
                    ).then(
                        fn=on_submit_done,
                        outputs=[submission_btn]
                    ).then(
                        fn=on_dropdown_change,
                                        outputs=[leaderboards_tabs, leaderboard_tables_list[0]]
                    )

            # # My Submissions Tab #
            # ######################
            # with gr.Tab('My Submissions') as my_submissions_tab:
            #     def on_my_submissions_tab_select(app_state):
            #         hf_token = app_state.get('hf_token')
            #         if not hf_token:
            #             return pd.DataFrame(columns=['Please insert your Hugging Face token'])
            #         # submissions = lb_server.get_submissions_by_hf_token(hf_token=hf_token)
            #         # if submissions.empty:
            #         #     submissions = pd.DataFrame(columns=['No submissions yet'])
            #         # return submissions
            #
            #     gr.Markdown(MY_SUBMISSIONS_TAB_TITLE_MARKDOWN)
            #     my_submissions_table = gr.DataFrame()
            #
            #     my_submissions_tab.select(fn=on_my_submissions_tab_select, inputs=[app_state],
            #                               outputs=[my_submissions_table])
            #     my_submissions_token_tb = gr.Textbox(label='Token', type='password')

        def on_token_insert(hf_token, app_state):
            gr.Info(f'Verifying token...')

            submission_count = None
            # if hf_token:
                # submission_count = lb_server.get_submission_count_last_24_hours(hf_token=hf_token)

            if submission_count is None:
                # Invalid token
                app_state['hf_token'] = None
                submissions_24h_str = ''
                team_submissions_df = pd.DataFrame(columns=['Invalid Token'])
                gr.Warning('Invalid token')

            # else:
            #     app_state['hf_token'] = hf_token
            #     submissions_24h_str = f'{submission_count}/{MAX_SUBMISSIONS_PER_24H}'
            #     team_submissions_df = lb_server.get_submissions_by_hf_token(hf_token=hf_token)
            #     if team_submissions_df.empty:
            #         team_submissions_df = pd.DataFrame(columns=['No submissions yet'])
            #     gr.Info('Token verified!')

            return app_state, team_submissions_df, submissions_24h_str

        hf_token_tb.change(fn=on_token_insert, inputs=[hf_token_tb, app_state],
                           outputs=[app_state, submissions_24h_txt])
        # my_submissions_token_tb.change(fn=on_token_insert, inputs=[my_submissions_token_tb, app_state],
        #                                outputs=[app_state, my_submissions_table, submissions_24h_txt])

        main.launch()