Koshti10 commited on
Commit
bfcf62d
·
verified ·
1 Parent(s): 09f587b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +266 -114
app.py CHANGED
@@ -1,105 +1,167 @@
1
  import gradio as gr
2
- import os
3
- from apscheduler.schedulers.background import BackgroundScheduler
4
- from huggingface_hub import HfApi
5
- from datetime import datetime, timedelta
6
 
7
- from src.assets.text_content import TITLE, INTRODUCTION_TEXT, CLEMSCORE_TEXT
8
- from src.leaderboard_utils import filter_search, get_github_data
9
- from src.plot_utils import split_models, compare_plots
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- # For restarting
12
- TIME = 200 # Seconds to restart the application on Gradio # Will not work locally
13
- # For Leaderboards
14
- dataframe_height = 800 # Height of the table in pixels
15
- # Get CSV data
16
- global primary_leaderboard_df, version_dfs, version_names
17
- primary_leaderboard_df, version_dfs, version_names, date = get_github_data()
18
-
19
- global prev_df
20
- prev_df = version_dfs[0]
21
- def select_prev_df(name):
22
- ind = version_names.index(name)
23
- prev_df = version_dfs[ind]
24
- return prev_df
25
-
26
- # API for auto restart
27
- HF_TOKEN = os.environ.get("H4_TOKEN", None)
28
- api = HfApi()
29
-
30
-
31
- def restart_space():
32
- api.restart_space(repo_id="Koshti10/leaderboard", token=HF_TOKEN)
33
-
34
-
35
-
36
- # For Plots
37
- global plot_df, OPEN_MODELS, CLOSED_MODELS
38
- plot_df = primary_leaderboard_df[0]
39
- MODELS = list(plot_df[list(plot_df.columns)[0]].unique())
40
- OPEN_MODELS, CLOSED_MODELS = split_models(MODELS)
41
-
42
-
43
- # MAIN APPLICATION
44
- main_app = gr.Blocks()
45
- with main_app:
46
  gr.HTML(TITLE)
47
  gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
48
 
49
  with gr.Tabs(elem_classes="tab-buttons") as tabs:
50
- with gr.TabItem("🥇 CLEM Leaderboard", elem_id="llm-benchmark-tab-table", id=0):
 
 
 
51
  with gr.Row():
52
  search_bar = gr.Textbox(
53
  placeholder=" 🔍 Search for models - separate multiple queries with `;` and press ENTER...",
54
  show_label=False,
55
  elem_id="search-bar",
56
  )
57
-
58
  leaderboard_table = gr.Dataframe(
59
- value=primary_leaderboard_df[0],
60
- elem_id="leaderboard-table",
61
  interactive=False,
62
  visible=True,
63
  height=dataframe_height
64
  )
65
 
 
66
  gr.HTML(CLEMSCORE_TEXT)
67
- gr.HTML(f"Last updated - {date}")
68
 
69
- # Add a dummy leaderboard to handle search queries from the primary_leaderboard_df and not update primary_leaderboard_df
 
70
  dummy_leaderboard_table = gr.Dataframe(
71
- value=primary_leaderboard_df[0],
72
- elem_id="leaderboard-table",
73
  interactive=False,
74
  visible=False
75
  )
76
-
 
77
  search_bar.submit(
78
- filter_search,
79
  [dummy_leaderboard_table, search_bar],
80
  leaderboard_table,
81
  queue=True
82
  )
83
 
84
- with gr.TabItem("📈 Plot", id=3):
 
 
 
85
  with gr.Row():
86
- open_models_selection = gr.CheckboxGroup(
87
- OPEN_MODELS,
88
- label="Open-weight Models 🌐",
89
- value=[],
90
- elem_id="value-select",
91
- interactive=True,
92
  )
93
 
94
- with gr.Row():
95
- closed_models_selection = gr.CheckboxGroup(
96
- CLOSED_MODELS,
97
- label="Closed-weight Models 💼",
98
- value=[],
99
- elem_id="value-select-2",
100
- interactive=True,
101
- )
102
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  with gr.Row():
104
  with gr.Column():
105
  show_all = gr.CheckboxGroup(
@@ -109,36 +171,41 @@ with main_app:
109
  elem_id="value-select-3",
110
  interactive=True,
111
  )
112
-
113
  with gr.Column():
114
  show_names = gr.CheckboxGroup(
115
  ["Show Names"],
116
- label ="Show names of models on the plot 🏷️",
117
  value=[],
118
  elem_id="value-select-4",
119
  interactive=True,
120
- )
121
 
122
  with gr.Column():
123
  show_legend = gr.CheckboxGroup(
124
  ["Show Legend"],
125
- label ="Show legend on the plot 💡",
126
  value=[],
127
  elem_id="value-select-5",
128
  interactive=True,
129
- )
130
  with gr.Column():
131
  mobile_view = gr.CheckboxGroup(
132
  ["Mobile View"],
133
- label ="View plot on smaller screens 📱",
134
  value=[],
135
  elem_id="value-select-6",
136
  interactive=True,
137
- )
138
 
 
 
 
 
 
139
  with gr.Row():
140
  dummy_plot_df = gr.DataFrame(
141
- value=plot_df,
142
  visible=False
143
  )
144
 
@@ -147,99 +214,184 @@ with main_app:
147
  # Output block for the plot
148
  plot_output = gr.Plot()
149
 
 
 
 
 
150
  open_models_selection.change(
151
- compare_plots,
152
- [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, mobile_view],
153
- plot_output,
 
154
  queue=True
155
  )
156
 
157
  closed_models_selection.change(
158
- compare_plots,
159
- [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, mobile_view],
160
- plot_output,
 
161
  queue=True
162
  )
163
-
164
  show_all.change(
165
- compare_plots,
166
- [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, mobile_view],
167
- plot_output,
 
168
  queue=True
169
  )
170
 
171
  show_names.change(
172
- compare_plots,
173
- [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, mobile_view],
174
- plot_output,
 
175
  queue=True
176
  )
177
 
178
  show_legend.change(
179
- compare_plots,
180
- [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, mobile_view],
181
- plot_output,
 
182
  queue=True
183
  )
184
 
185
  mobile_view.change(
186
- compare_plots,
187
- [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend, mobile_view],
188
- plot_output,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  queue=True
190
  )
191
 
192
- with gr.TabItem("🔄 Versions and Details", elem_id="details", id=2):
 
 
 
 
 
 
 
 
 
193
  with gr.Row():
194
  version_select = gr.Dropdown(
195
- version_names, label="Select Version 🕹️", value=version_names[0]
196
  )
197
  with gr.Row():
198
  search_bar_prev = gr.Textbox(
199
  placeholder=" 🔍 Search for models - separate multiple queries with `;` and press ENTER...",
200
  show_label=False,
201
- elem_id="search-bar-2",
202
  )
203
 
204
  prev_table = gr.Dataframe(
205
- value=prev_df,
206
- elem_id="leaderboard-table",
207
  interactive=False,
208
  visible=True,
209
  height=dataframe_height
210
  )
211
 
212
  dummy_prev_table = gr.Dataframe(
213
- value=prev_df,
214
- elem_id="leaderboard-table",
215
  interactive=False,
216
  visible=False
217
  )
218
 
 
 
 
219
  search_bar_prev.submit(
220
- filter_search,
221
  [dummy_prev_table, search_bar_prev],
222
  prev_table,
223
  queue=True
224
  )
225
 
226
  version_select.change(
227
- select_prev_df,
228
  [version_select],
229
  prev_table,
230
  queue=True
231
  )
232
- main_app.load()
233
 
234
- main_app.queue()
 
 
 
 
 
 
235
 
236
- # Add scheduler to auto-restart the HF space at every TIME interval and update every component each time
237
- scheduler = BackgroundScheduler()
238
- scheduler.add_job(restart_space, 'interval', seconds=TIME)
239
- scheduler.start()
240
 
241
- # Log current start time and scheduled restart time
242
- print(datetime.now())
243
- print(f"Scheduled restart at {datetime.now() + timedelta(seconds=TIME)}")
 
 
 
 
 
244
 
245
- main_app.launch()
 
1
  import gradio as gr
 
 
 
 
2
 
3
+ from src.assets.text_content import TITLE, INTRODUCTION_TEXT, CLEMSCORE_TEXT, MULTIMODAL_NAME, TEXT_NAME
4
+ from src.leaderboard_utils import query_search, get_github_data
5
+ from src.plot_utils import split_models, plotly_plot, get_plot_df, update_open_models, update_closed_models
6
+ from src.plot_utils import reset_show_all, reset_show_names, reset_show_legend, reset_mobile_view
7
+ from src.version_utils import get_versions_data
8
+
9
+ """
10
+ CONSTANTS
11
+ """
12
+ # For restarting the gradio application
13
+ TIME = 200 # in seconds # Reload will not work locally - requires HFToken # The app launches locally as expected - only without the reload utility
14
+ # For Leaderboard table
15
+ dataframe_height = 800 # Height of the table in pixels # Set on average considering all possible devices
16
+
17
+
18
+ """
19
+ GITHUB UTILS
20
+ """
21
+ github_data = get_github_data()
22
+ text_leaderboard = github_data["text"][0] # Get the text-only leaderboard for its available latest version
23
+ multimodal_leaderboard = github_data["multimodal"][0] # Get multimodal leaderboard for its available latest version.
24
+
25
+ # Show only First 4 columns for the leaderboards
26
+ text_leaderboard = text_leaderboard.iloc[:, :4]
27
+ print(f"Showing the following columns for the latest leaderboard: {text_leaderboard.columns}")
28
+ multimodal_leaderboard = multimodal_leaderboard.iloc[:, :4]
29
+ print(f"Showing the following columns for the multimodal leaderboard: {multimodal_leaderboard.columns}")
30
+
31
+
32
+ """
33
+ VERSIONS UTILS
34
+ """
35
+ versions_data = get_versions_data()
36
+ latest_version = versions_data['latest'] # Always show latest version in text-only benchmark
37
+ last_updated_date = versions_data['date']
38
+ version_names = list(versions_data.keys())
39
+ version_names = [v for v in version_names if v.startswith("v")] # Remove "latest" and "date" keys
40
+
41
+ global version_df
42
+ version_df = versions_data[latest_version]
43
+ def select_version_df(name):
44
+ return versions_data[name]
45
+
46
+ """
47
+ MAIN APPLICATION
48
+ """
49
+ hf_app = gr.Blocks()
50
+ with hf_app:
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  gr.HTML(TITLE)
53
  gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
54
 
55
  with gr.Tabs(elem_classes="tab-buttons") as tabs:
56
+ """
57
+ ####################### FIRST TAB - TEXT-LEADERBOARD #######################
58
+ """
59
+ with gr.TabItem(TEXT_NAME, elem_id="llm-benchmark-tab-table", id=0):
60
  with gr.Row():
61
  search_bar = gr.Textbox(
62
  placeholder=" 🔍 Search for models - separate multiple queries with `;` and press ENTER...",
63
  show_label=False,
64
  elem_id="search-bar",
65
  )
66
+
67
  leaderboard_table = gr.Dataframe(
68
+ value=text_leaderboard,
69
+ elem_id="text-leaderboard-table",
70
  interactive=False,
71
  visible=True,
72
  height=dataframe_height
73
  )
74
 
75
+ # Show information about the clemscore and last updated date below the table
76
  gr.HTML(CLEMSCORE_TEXT)
77
+ gr.HTML(f"Last updated - {github_data['date']}")
78
 
79
+ # Add a dummy leaderboard to handle search queries in leaderboard_table
80
+ # This will show a temporary leaderboard based on the searched value
81
  dummy_leaderboard_table = gr.Dataframe(
82
+ value=text_leaderboard,
83
+ elem_id="text-leaderboard-table-dummy",
84
  interactive=False,
85
  visible=False
86
  )
87
+
88
+ # Action after submitting a query to the search bar
89
  search_bar.submit(
90
+ query_search,
91
  [dummy_leaderboard_table, search_bar],
92
  leaderboard_table,
93
  queue=True
94
  )
95
 
96
+ """
97
+ ####################### SECOND TAB - MULTIMODAL LEADERBOARD #######################
98
+ """
99
+ with gr.TabItem(MULTIMODAL_NAME, elem_id="mm-llm-benchmark-tab-table", id=1):
100
  with gr.Row():
101
+ mm_search_bar = gr.Textbox(
102
+ placeholder=" 🔍 Search for models - separate multiple queries with `;` and press ENTER...",
103
+ show_label=False,
104
+ elem_id="search-bar",
 
 
105
  )
106
 
107
+ mm_leaderboard_table = gr.Dataframe(
108
+ value=multimodal_leaderboard,
109
+ elem_id="mm-leaderboard-table",
110
+ interactive=False,
111
+ visible=True,
112
+ height=dataframe_height
113
+ )
114
+
115
+ # Show information about the clemscore and last updated date below the table
116
+ gr.HTML(CLEMSCORE_TEXT)
117
+ gr.HTML(f"Last updated - {github_data['date']}")
118
+
119
+ # Add a dummy leaderboard to handle search queries in leaderboard_table
120
+ # This will show a temporary leaderboard based on the searched value
121
+ mm_dummy_leaderboard_table = gr.Dataframe(
122
+ value=multimodal_leaderboard,
123
+ elem_id="mm-leaderboard-table-dummy",
124
+ interactive=False,
125
+ visible=False
126
+ )
127
+
128
+ # Action after submitting a query to the search bar
129
+ mm_search_bar.submit(
130
+ query_search,
131
+ [mm_dummy_leaderboard_table, mm_search_bar],
132
+ mm_leaderboard_table,
133
+ queue=True
134
+ )
135
+
136
+ """
137
+ ####################### THIRD TAB - PLOTS - %PLAYED V/S QUALITY SCORE #######################
138
+ """
139
+ with gr.TabItem("📈 Plots", elem_id="plots", id=2):
140
+ """
141
+ DropDown Select for Text/Multimodal Leaderboard
142
+ """
143
+ leaderboard_selection = gr.Dropdown(
144
+ choices=[TEXT_NAME, MULTIMODAL_NAME],
145
+ value=TEXT_NAME,
146
+ label="Select Leaderboard 🎖️🔽",
147
+ elem_id="value-select-0",
148
+ interactive=True
149
+ )
150
+
151
+ """
152
+ Accordion Groups to select individual models - Hidden by default
153
+ """
154
+ with gr.Accordion("Select Open-weight Models 🌐", open=False):
155
+ open_models_selection = update_open_models()
156
+ clear_button_1 = gr.ClearButton(open_models_selection)
157
+
158
+ with gr.Accordion("Select Closed-weight Models 💼", open=False):
159
+ closed_models_selection = update_closed_models()
160
+ clear_button_2 = gr.ClearButton(closed_models_selection)
161
+
162
+ """
163
+ Checkbox group to control the layout of the plot
164
+ """
165
  with gr.Row():
166
  with gr.Column():
167
  show_all = gr.CheckboxGroup(
 
171
  elem_id="value-select-3",
172
  interactive=True,
173
  )
174
+
175
  with gr.Column():
176
  show_names = gr.CheckboxGroup(
177
  ["Show Names"],
178
+ label="Show names of models on the plot 🏷️",
179
  value=[],
180
  elem_id="value-select-4",
181
  interactive=True,
182
+ )
183
 
184
  with gr.Column():
185
  show_legend = gr.CheckboxGroup(
186
  ["Show Legend"],
187
+ label="Show legend on the plot 💡",
188
  value=[],
189
  elem_id="value-select-5",
190
  interactive=True,
191
+ )
192
  with gr.Column():
193
  mobile_view = gr.CheckboxGroup(
194
  ["Mobile View"],
195
+ label="View plot on smaller screens 📱",
196
  value=[],
197
  elem_id="value-select-6",
198
  interactive=True,
199
+ )
200
 
201
+ """
202
+ PLOT BLOCK
203
+ """
204
+ # Create a dummy DataFrame as an input to the plotly_plot function.
205
+ # Uses this data to plot the %played v/s quality score
206
  with gr.Row():
207
  dummy_plot_df = gr.DataFrame(
208
+ value=get_plot_df(),
209
  visible=False
210
  )
211
 
 
214
  # Output block for the plot
215
  plot_output = gr.Plot()
216
 
217
+ """
218
+ PLOT CHANGE ACTIONS
219
+ Toggle 'Select All Models' based on the values in Accordion checkbox groups
220
+ """
221
  open_models_selection.change(
222
+ plotly_plot,
223
+ [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend,
224
+ mobile_view],
225
+ [plot_output],
226
  queue=True
227
  )
228
 
229
  closed_models_selection.change(
230
+ plotly_plot,
231
+ [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend,
232
+ mobile_view],
233
+ [plot_output],
234
  queue=True
235
  )
236
+
237
  show_all.change(
238
+ plotly_plot,
239
+ [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend,
240
+ mobile_view],
241
+ [plot_output],
242
  queue=True
243
  )
244
 
245
  show_names.change(
246
+ plotly_plot,
247
+ [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend,
248
+ mobile_view],
249
+ [plot_output],
250
  queue=True
251
  )
252
 
253
  show_legend.change(
254
+ plotly_plot,
255
+ [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend,
256
+ mobile_view],
257
+ [plot_output],
258
  queue=True
259
  )
260
 
261
  mobile_view.change(
262
+ plotly_plot,
263
+ [dummy_plot_df, open_models_selection, closed_models_selection, show_all, show_names, show_legend,
264
+ mobile_view],
265
+ [plot_output],
266
+ queue=True
267
+ )
268
+ """
269
+ LEADERBOARD SELECT CHANGE ACTIONS
270
+ Update Checkbox Groups and Dummy DF based on the leaderboard selected
271
+ """
272
+ leaderboard_selection.change(
273
+ update_open_models,
274
+ [leaderboard_selection],
275
+ [open_models_selection],
276
+ queue=True
277
+ )
278
+
279
+ leaderboard_selection.change(
280
+ update_closed_models,
281
+ [leaderboard_selection],
282
+ [closed_models_selection],
283
+ queue=True
284
+ )
285
+
286
+ leaderboard_selection.change(
287
+ get_plot_df,
288
+ [leaderboard_selection],
289
+ [dummy_plot_df],
290
+ queue=True
291
+ )
292
+
293
+ ## Implement Feature - Reset Plot when Leaderboard selection changes
294
+ leaderboard_selection.change(
295
+ reset_show_all,
296
+ outputs=[show_all],
297
+ queue=True
298
+ )
299
+
300
+ open_models_selection.change(
301
+ reset_show_all,
302
+ outputs=[show_all],
303
+ queue=True
304
+ )
305
+
306
+ closed_models_selection.change(
307
+ reset_show_all,
308
+ outputs=[show_all],
309
+ queue=True
310
+ )
311
+
312
+ leaderboard_selection.change(
313
+ reset_show_names,
314
+ outputs=[show_names],
315
+ queue=True
316
+ )
317
+
318
+ leaderboard_selection.change(
319
+ reset_show_legend,
320
+ outputs=[show_legend],
321
  queue=True
322
  )
323
 
324
+ leaderboard_selection.change(
325
+ reset_mobile_view,
326
+ outputs=[mobile_view],
327
+ queue=True
328
+ )
329
+
330
+ """
331
+ ####################### FOURTH TAB - VERSIONS AND DETAILS #######################
332
+ """
333
+ with gr.TabItem("🔄 Versions and Details", elem_id="versions-details-tab", id=3):
334
  with gr.Row():
335
  version_select = gr.Dropdown(
336
+ version_names, label="Select Version 🕹️", value=latest_version
337
  )
338
  with gr.Row():
339
  search_bar_prev = gr.Textbox(
340
  placeholder=" 🔍 Search for models - separate multiple queries with `;` and press ENTER...",
341
  show_label=False,
342
+ elem_id="search-bar-3",
343
  )
344
 
345
  prev_table = gr.Dataframe(
346
+ value=version_df,
347
+ elem_id="version-leaderboard-table",
348
  interactive=False,
349
  visible=True,
350
  height=dataframe_height
351
  )
352
 
353
  dummy_prev_table = gr.Dataframe(
354
+ value=version_df,
355
+ elem_id="version-dummy-leaderboard-table",
356
  interactive=False,
357
  visible=False
358
  )
359
 
360
+ gr.HTML(CLEMSCORE_TEXT)
361
+ gr.HTML(f"Last updated - {last_updated_date}")
362
+
363
  search_bar_prev.submit(
364
+ query_search,
365
  [dummy_prev_table, search_bar_prev],
366
  prev_table,
367
  queue=True
368
  )
369
 
370
  version_select.change(
371
+ select_version_df,
372
  [version_select],
373
  prev_table,
374
  queue=True
375
  )
 
376
 
377
+ # Update Dummy Leaderboard, when changing versions
378
+ version_select.change(
379
+ select_version_df,
380
+ [version_select],
381
+ dummy_prev_table,
382
+ queue=True
383
+ )
384
 
385
+ hf_app.load()
386
+ hf_app.queue()
 
 
387
 
388
+ # # Add scheduler to auto-restart the HF space at every TIME interval and update every component each time
389
+ # scheduler = BackgroundScheduler()
390
+ # scheduler.add_job(restart_space, 'interval', seconds=TIME)
391
+ # scheduler.start()
392
+ #
393
+ # # Log current start time and scheduled restart time
394
+ # print(datetime.now())
395
+ # print(f"Scheduled restart at {datetime.now() + timedelta(seconds=TIME)}")
396
 
397
+ hf_app.launch()