Lakoc commited on
Commit
2c5fd3f
β€’
1 Parent(s): 1fa987c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +202 -203
app.py CHANGED
@@ -84,209 +84,208 @@ MAX_SUBMISSIONS_PER_24H = 2
84
  # CHALLENGE_NAME = 'NOTSOFAR1'
85
 
86
 
87
- if __name__ == '__main__':
88
- with (gr.Blocks(theme=gr.themes.Soft(text_size=text_md), css="footer {visibility: hidden}") as main):
89
- app_state = gr.State({})
90
- # with gr.Row():
91
- # greet_name = gr.Textbox(label="Name")
92
- # greet_output = gr.Textbox(label="Greetings")
93
- # greet_btn = gr.Button("Greet")
94
- # greet_btn.click(fn=greet, inputs=greet_name, outputs=greet_output).success(
95
- # fn=save_json,
96
- # inputs=[greet_name, greet_output],
97
- # outputs=None,
98
- # )
99
-
100
  with gr.Row():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  with gr.Row():
102
- gr.Markdown(HEADER_MARKDOWN)
103
-
104
- with gr.Row():
105
-
106
- # Leaderboards Tab #
107
- ####################
108
- def populate_leaderboard(leaderboard_type, dataset_version):
109
- gr.Info('Loading leaderboard...')
110
- time.sleep(1)
111
- leaderboard_df = leaderboard_server.get_leaderboard()
112
- # leaderboard_df = lb_server.get_leaderboard(
113
- # submission_type=leaderboard_type, dataset_version=dataset_version)
114
- # if leaderboard_df.empty:
115
- return leaderboard_df
116
- # return leaderboard_df
117
-
118
-
119
- def create_leaderboard_tab(tab_name: str, idx: int, dataset_version_dropdown: gr.Dropdown):
120
- # dataset_version = dataset_version_dropdown.value
121
- print(f'Creating tab for {tab_name}, idx={idx}, dataset_version={dataset_version_dropdown}')
122
- with gr.Tab(id=tab_name, label=tab_name) as leaderboard_tab:
123
- leaderboard_table = gr.DataFrame(populate_leaderboard(tab_name, None)) if idx == 0 \
124
- else gr.DataFrame(pd.DataFrame(columns=['No submissions yet']))
125
- leaderboard_tab.select(fn=populate_leaderboard,
126
- inputs=[gr.Text(tab_name, visible=False)],
127
- outputs=[leaderboard_table])
128
- return leaderboard_table
129
-
130
- def on_dropdown_change():
131
- first_tab_name = LEADERBOARD_TYPES[0]
132
- leaderboard_server.on_submit()
133
-
134
- return gr.Tabs(selected=first_tab_name), populate_leaderboard(first_tab_name, None)
135
-
136
-
137
- with gr.Tab('Leaderboards') as leaderboards_tab:
138
- with gr.Row():
139
- gr.Markdown(LEADERBOARD_TAB_TITLE_MARKDOWN)
140
- # with gr.Row():
141
- # with gr.Column():
142
- # dataset_version_drop = gr.Dropdown(choices=DATASET_VERSIONS, multiselect=False,
143
- # value=DATASET_VERSIONS[-1], label="Dataset",
144
- # interactive=True)
145
- # with gr.Column():
146
- # gr.Markdown('') # Empty column for spacing
147
- # with gr.Column():
148
- # gr.Markdown('') # Empty column for spacing
149
- # with gr.Column():
150
- # gr.Markdown('') # Empty column for spacing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  with gr.Row():
152
- with gr.Tabs() as leaderboards_tabs:
153
- leaderboard_tables_list = []
154
- for leaderboard_idx, leaderboard_type in enumerate(LEADERBOARD_TYPES):
155
- l_tab = create_leaderboard_tab(leaderboard_type, leaderboard_idx, None)
156
- leaderboard_tables_list.append(l_tab)
157
-
158
- # dataset_version_drop.select(fn=on_dropdown_change, inputs=[dataset_version_drop],
159
- # outputs=[leaderboards_tabs, leaderboard_tables_list[0]])
160
-
161
-
162
- # Submission Tab #
163
- ##################
164
- with gr.Tab('Submission'):
165
- with gr.Column():
166
- def on_submit_pressed():
167
- return gr.update(value='Processing submission...', interactive=False)
168
-
169
- def validate_submission_inputs(team_name, submission_zip, submission_type, token):
170
- if not team_name or not submission_zip or not submission_type:
171
- raise ValueError('Please fill in all fields')
172
- if not os.path.exists(submission_zip):
173
- raise ValueError('File does not exist')
174
- # if not submission_zip.endswith('.zip'):
175
- # raise ValueError('File must be a zip')
176
- # if not token:
177
- # raise ValueError('Please insert a valid Hugging Face token')
178
-
179
- def process_submission(team_name, submission, submission_type, description,
180
- app_state, request: gr.Request):
181
- logging.info(f'{team_name}: new submission for track: {submission_type}')
182
- try:
183
- token = app_state.get('hf_token')
184
- validate_submission_inputs(team_name, submission, submission_type, token)
185
- except ValueError as err:
186
- gr.Warning(str(err))
187
- return
188
-
189
-
190
- # metadata = {'challenge_name': CHALLENGE_NAME,
191
- # "dataset_version": DATASET_VERSIONS[-1],
192
- # 'team_name': team_name,
193
- # 'submission_type': submission_type,
194
- # 'description': description,
195
- # 'token': token,
196
- # 'file_name': os.path.basename(submission_zip),
197
- # 'file_size_mb': os.path.getsize(submission_zip) / 1024 / 1024,
198
- # 'ip': request.client.host}
199
- leaderboard_server.save_json(submission)
200
-
201
- try:
202
- gr.Info('Processing submission...')
203
- # response = lb_server.add_submission(token=token, file_path=submission_zip, metadata=metadata)
204
- # if 'error' in response:
205
- # gr.Warning(f'Failed to process submission - {response["error"]}')
206
- # else:
207
- gr.Info('Done processing submission')
208
- except Exception as e:
209
- gr.Warning(f'Submission failed to upload - {e}')
210
-
211
- def on_submit_done():
212
- on_dropdown_change()
213
- leaderboard_server.on_submit()
214
- # leaderboard_tab.children[0] = gr.DataFrame(populate_leaderboard(None, None))
215
- # leaderboard_tab.render()
216
- return gr.update(value='Submit', interactive=True)
217
-
218
- gr.Markdown(SUBMISSION_TAB_TITLE_MARKDOWN)
219
- submission_team_name_tb = gr.Textbox(label='Team Name')
220
- submission_file_path = gr.File(label='Upload your results', type='filepath')
221
- submission_type_radio = gr.Radio(label='Submission Track', choices=LEADERBOARD_TYPES)
222
- with gr.Row():
223
- hf_token_tb = gr.Textbox(label='Token', type='password')
224
- submissions_24h_txt = gr.Textbox(label='Submissions 24h', value='')
225
- description_tb = gr.Textbox(label='Description', type='text')
226
- submission_btn = gr.Button(value='Submit', interactive=True)
227
- gr.Markdown('### * Please make sure you are using NOTSOFAR dev-set-2 for your submissions')
228
-
229
- submission_btn.click(
230
- fn=on_submit_pressed,
231
- outputs=[submission_btn]
232
- ).then(
233
- fn=process_submission,
234
- inputs=[submission_team_name_tb, submission_file_path,
235
- submission_type_radio, description_tb, app_state]
236
- ).then(
237
- fn=on_submit_done,
238
- outputs=[submission_btn]
239
- ).then(
240
- fn=on_dropdown_change,
241
- outputs=[leaderboards_tabs, leaderboard_tables_list[0]]
242
- )
243
-
244
- # # My Submissions Tab #
245
- # ######################
246
- # with gr.Tab('My Submissions') as my_submissions_tab:
247
- # def on_my_submissions_tab_select(app_state):
248
- # hf_token = app_state.get('hf_token')
249
- # if not hf_token:
250
- # return pd.DataFrame(columns=['Please insert your Hugging Face token'])
251
- # # submissions = lb_server.get_submissions_by_hf_token(hf_token=hf_token)
252
- # # if submissions.empty:
253
- # # submissions = pd.DataFrame(columns=['No submissions yet'])
254
- # # return submissions
255
- #
256
- # gr.Markdown(MY_SUBMISSIONS_TAB_TITLE_MARKDOWN)
257
- # my_submissions_table = gr.DataFrame()
258
- #
259
- # my_submissions_tab.select(fn=on_my_submissions_tab_select, inputs=[app_state],
260
- # outputs=[my_submissions_table])
261
- # my_submissions_token_tb = gr.Textbox(label='Token', type='password')
262
-
263
- def on_token_insert(hf_token, app_state):
264
- gr.Info(f'Verifying token...')
265
-
266
- submission_count = None
267
- # if hf_token:
268
- # submission_count = lb_server.get_submission_count_last_24_hours(hf_token=hf_token)
269
-
270
- if submission_count is None:
271
- # Invalid token
272
- app_state['hf_token'] = None
273
- submissions_24h_str = ''
274
- team_submissions_df = pd.DataFrame(columns=['Invalid Token'])
275
- gr.Warning('Invalid token')
276
-
277
- # else:
278
- # app_state['hf_token'] = hf_token
279
- # submissions_24h_str = f'{submission_count}/{MAX_SUBMISSIONS_PER_24H}'
280
- # team_submissions_df = lb_server.get_submissions_by_hf_token(hf_token=hf_token)
281
- # if team_submissions_df.empty:
282
- # team_submissions_df = pd.DataFrame(columns=['No submissions yet'])
283
- # gr.Info('Token verified!')
284
-
285
- return app_state, team_submissions_df, submissions_24h_str
286
-
287
- hf_token_tb.change(fn=on_token_insert, inputs=[hf_token_tb, app_state],
288
- outputs=[app_state, submissions_24h_txt])
289
- # my_submissions_token_tb.change(fn=on_token_insert, inputs=[my_submissions_token_tb, app_state],
290
- # outputs=[app_state, my_submissions_table, submissions_24h_txt])
291
-
292
- main.launch()
 
84
  # CHALLENGE_NAME = 'NOTSOFAR1'
85
 
86
 
87
+ with (gr.Blocks(theme=gr.themes.Soft(text_size=text_md), css="footer {visibility: hidden}") as main):
88
+ app_state = gr.State({})
89
+ # with gr.Row():
90
+ # greet_name = gr.Textbox(label="Name")
91
+ # greet_output = gr.Textbox(label="Greetings")
92
+ # greet_btn = gr.Button("Greet")
93
+ # greet_btn.click(fn=greet, inputs=greet_name, outputs=greet_output).success(
94
+ # fn=save_json,
95
+ # inputs=[greet_name, greet_output],
96
+ # outputs=None,
97
+ # )
98
+
99
+ with gr.Row():
100
  with gr.Row():
101
+ gr.Markdown(HEADER_MARKDOWN)
102
+
103
+ with gr.Row():
104
+
105
+ # Leaderboards Tab #
106
+ ####################
107
+ def populate_leaderboard(leaderboard_type, dataset_version):
108
+ gr.Info('Loading leaderboard...')
109
+ time.sleep(1)
110
+ leaderboard_df = leaderboard_server.get_leaderboard()
111
+ # leaderboard_df = lb_server.get_leaderboard(
112
+ # submission_type=leaderboard_type, dataset_version=dataset_version)
113
+ # if leaderboard_df.empty:
114
+ return leaderboard_df
115
+ # return leaderboard_df
116
+
117
+
118
+ def create_leaderboard_tab(tab_name: str, idx: int, dataset_version_dropdown: gr.Dropdown):
119
+ # dataset_version = dataset_version_dropdown.value
120
+ print(f'Creating tab for {tab_name}, idx={idx}, dataset_version={dataset_version_dropdown}')
121
+ with gr.Tab(id=tab_name, label=tab_name) as leaderboard_tab:
122
+ leaderboard_table = gr.DataFrame(populate_leaderboard(tab_name, None)) if idx == 0 \
123
+ else gr.DataFrame(pd.DataFrame(columns=['No submissions yet']))
124
+ leaderboard_tab.select(fn=populate_leaderboard,
125
+ inputs=[gr.Text(tab_name, visible=False)],
126
+ outputs=[leaderboard_table])
127
+ return leaderboard_table
128
+
129
+ def on_dropdown_change():
130
+ first_tab_name = LEADERBOARD_TYPES[0]
131
+ leaderboard_server.on_submit()
132
+
133
+ return gr.Tabs(selected=first_tab_name), populate_leaderboard(first_tab_name, None)
134
+
135
+
136
+ with gr.Tab('Leaderboards') as leaderboards_tab:
137
  with gr.Row():
138
+ gr.Markdown(LEADERBOARD_TAB_TITLE_MARKDOWN)
139
+ # with gr.Row():
140
+ # with gr.Column():
141
+ # dataset_version_drop = gr.Dropdown(choices=DATASET_VERSIONS, multiselect=False,
142
+ # value=DATASET_VERSIONS[-1], label="Dataset",
143
+ # interactive=True)
144
+ # with gr.Column():
145
+ # gr.Markdown('') # Empty column for spacing
146
+ # with gr.Column():
147
+ # gr.Markdown('') # Empty column for spacing
148
+ # with gr.Column():
149
+ # gr.Markdown('') # Empty column for spacing
150
+ with gr.Row():
151
+ with gr.Tabs() as leaderboards_tabs:
152
+ leaderboard_tables_list = []
153
+ for leaderboard_idx, leaderboard_type in enumerate(LEADERBOARD_TYPES):
154
+ l_tab = create_leaderboard_tab(leaderboard_type, leaderboard_idx, None)
155
+ leaderboard_tables_list.append(l_tab)
156
+
157
+ # dataset_version_drop.select(fn=on_dropdown_change, inputs=[dataset_version_drop],
158
+ # outputs=[leaderboards_tabs, leaderboard_tables_list[0]])
159
+
160
+
161
+ # Submission Tab #
162
+ ##################
163
+ with gr.Tab('Submission'):
164
+ with gr.Column():
165
+ def on_submit_pressed():
166
+ return gr.update(value='Processing submission...', interactive=False)
167
+
168
+ def validate_submission_inputs(team_name, submission_zip, submission_type, token):
169
+ if not team_name or not submission_zip or not submission_type:
170
+ raise ValueError('Please fill in all fields')
171
+ if not os.path.exists(submission_zip):
172
+ raise ValueError('File does not exist')
173
+ # if not submission_zip.endswith('.zip'):
174
+ # raise ValueError('File must be a zip')
175
+ # if not token:
176
+ # raise ValueError('Please insert a valid Hugging Face token')
177
+
178
+ def process_submission(team_name, submission, submission_type, description,
179
+ app_state, request: gr.Request):
180
+ logging.info(f'{team_name}: new submission for track: {submission_type}')
181
+ try:
182
+ token = app_state.get('hf_token')
183
+ validate_submission_inputs(team_name, submission, submission_type, token)
184
+ except ValueError as err:
185
+ gr.Warning(str(err))
186
+ return
187
+
188
+
189
+ # metadata = {'challenge_name': CHALLENGE_NAME,
190
+ # "dataset_version": DATASET_VERSIONS[-1],
191
+ # 'team_name': team_name,
192
+ # 'submission_type': submission_type,
193
+ # 'description': description,
194
+ # 'token': token,
195
+ # 'file_name': os.path.basename(submission_zip),
196
+ # 'file_size_mb': os.path.getsize(submission_zip) / 1024 / 1024,
197
+ # 'ip': request.client.host}
198
+ leaderboard_server.save_json(submission)
199
+
200
+ try:
201
+ gr.Info('Processing submission...')
202
+ # response = lb_server.add_submission(token=token, file_path=submission_zip, metadata=metadata)
203
+ # if 'error' in response:
204
+ # gr.Warning(f'Failed to process submission - {response["error"]}')
205
+ # else:
206
+ gr.Info('Done processing submission')
207
+ except Exception as e:
208
+ gr.Warning(f'Submission failed to upload - {e}')
209
+
210
+ def on_submit_done():
211
+ on_dropdown_change()
212
+ leaderboard_server.on_submit()
213
+ # leaderboard_tab.children[0] = gr.DataFrame(populate_leaderboard(None, None))
214
+ # leaderboard_tab.render()
215
+ return gr.update(value='Submit', interactive=True)
216
+
217
+ gr.Markdown(SUBMISSION_TAB_TITLE_MARKDOWN)
218
+ submission_team_name_tb = gr.Textbox(label='Team Name')
219
+ submission_file_path = gr.File(label='Upload your results', type='filepath')
220
+ submission_type_radio = gr.Radio(label='Submission Track', choices=LEADERBOARD_TYPES)
221
  with gr.Row():
222
+ hf_token_tb = gr.Textbox(label='Token', type='password')
223
+ submissions_24h_txt = gr.Textbox(label='Submissions 24h', value='')
224
+ description_tb = gr.Textbox(label='Description', type='text')
225
+ submission_btn = gr.Button(value='Submit', interactive=True)
226
+ gr.Markdown('### * Please make sure you are using NOTSOFAR dev-set-2 for your submissions')
227
+
228
+ submission_btn.click(
229
+ fn=on_submit_pressed,
230
+ outputs=[submission_btn]
231
+ ).then(
232
+ fn=process_submission,
233
+ inputs=[submission_team_name_tb, submission_file_path,
234
+ submission_type_radio, description_tb, app_state]
235
+ ).then(
236
+ fn=on_submit_done,
237
+ outputs=[submission_btn]
238
+ ).then(
239
+ fn=on_dropdown_change,
240
+ outputs=[leaderboards_tabs, leaderboard_tables_list[0]]
241
+ )
242
+
243
+ # # My Submissions Tab #
244
+ # ######################
245
+ # with gr.Tab('My Submissions') as my_submissions_tab:
246
+ # def on_my_submissions_tab_select(app_state):
247
+ # hf_token = app_state.get('hf_token')
248
+ # if not hf_token:
249
+ # return pd.DataFrame(columns=['Please insert your Hugging Face token'])
250
+ # # submissions = lb_server.get_submissions_by_hf_token(hf_token=hf_token)
251
+ # # if submissions.empty:
252
+ # # submissions = pd.DataFrame(columns=['No submissions yet'])
253
+ # # return submissions
254
+ #
255
+ # gr.Markdown(MY_SUBMISSIONS_TAB_TITLE_MARKDOWN)
256
+ # my_submissions_table = gr.DataFrame()
257
+ #
258
+ # my_submissions_tab.select(fn=on_my_submissions_tab_select, inputs=[app_state],
259
+ # outputs=[my_submissions_table])
260
+ # my_submissions_token_tb = gr.Textbox(label='Token', type='password')
261
+
262
+ def on_token_insert(hf_token, app_state):
263
+ gr.Info(f'Verifying token...')
264
+
265
+ submission_count = None
266
+ # if hf_token:
267
+ # submission_count = lb_server.get_submission_count_last_24_hours(hf_token=hf_token)
268
+
269
+ if submission_count is None:
270
+ # Invalid token
271
+ app_state['hf_token'] = None
272
+ submissions_24h_str = ''
273
+ team_submissions_df = pd.DataFrame(columns=['Invalid Token'])
274
+ gr.Warning('Invalid token')
275
+
276
+ # else:
277
+ # app_state['hf_token'] = hf_token
278
+ # submissions_24h_str = f'{submission_count}/{MAX_SUBMISSIONS_PER_24H}'
279
+ # team_submissions_df = lb_server.get_submissions_by_hf_token(hf_token=hf_token)
280
+ # if team_submissions_df.empty:
281
+ # team_submissions_df = pd.DataFrame(columns=['No submissions yet'])
282
+ # gr.Info('Token verified!')
283
+
284
+ return app_state, team_submissions_df, submissions_24h_str
285
+
286
+ hf_token_tb.change(fn=on_token_insert, inputs=[hf_token_tb, app_state],
287
+ outputs=[app_state, submissions_24h_txt])
288
+ # my_submissions_token_tb.change(fn=on_token_insert, inputs=[my_submissions_token_tb, app_state],
289
+ # outputs=[app_state, my_submissions_table, submissions_24h_txt])
290
+
291
+ main.launch()