bgamazay commited on
Commit
2caedfe
Β·
verified Β·
1 Parent(s): c6df42e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -208
app.py CHANGED
@@ -27,12 +27,13 @@ tasks = [
27
  'summarization.csv'
28
  ]
29
 
 
 
30
  def format_stars(score):
31
  try:
32
  score_int = int(score)
33
  except Exception:
34
  score_int = 0
35
- # Render stars in green with a slightly larger font.
36
  return f'<span style="color: #3fa45bff; font-size:1.5em;">{"β˜…" * score_int}</span>'
37
 
38
  def make_link(mname):
@@ -41,7 +42,6 @@ def make_link(mname):
41
  return f'<a href="https://huggingface.co/{mname}" target="_blank">{display_name}</a>'
42
 
43
  def extract_link_text(html_link):
44
- """Extracts the inner text from an HTML link."""
45
  start = html_link.find('>') + 1
46
  end = html_link.rfind('</a>')
47
  if start > 0 and end > start:
@@ -50,13 +50,7 @@ def extract_link_text(html_link):
50
  return html_link
51
 
52
  def generate_html_table_from_df(df):
53
- """
54
- Generates an HTML table with four columns:
55
- - Model (with link)
56
- - Provider (extracted from the model field)
57
- - GPU Energy (Wh) plus a horizontal bar
58
- - Score (as stars)
59
- """
60
  if not df.empty:
61
  max_length = max(len(extract_link_text(link)) for link in df['Model'])
62
  else:
@@ -70,7 +64,7 @@ def generate_html_table_from_df(df):
70
  html += '<th style="text-align: left; padding: 8px;" title="Model name with link to Hugging Face">Model</th>'
71
  html += '<th style="text-align: left; padding: 8px;" title="AI Provider extracted from the model name">Provider</th>'
72
  html += '<th style="text-align: left; padding: 8px;" title="GPU energy consumed in Watt-hours for 1,000 queries">GPU Energy (Wh)</th>'
73
- html += '<th style="text-align: left; padding: 8px;" title="Energy efficiency score">Score</th>'
74
  html += '</tr></thead>'
75
  html += '<tbody>'
76
  for _, row in df.iterrows():
@@ -91,9 +85,30 @@ def generate_html_table_from_df(df):
91
  html += '</tbody></table>'
92
  return f'<div class="table-container">{html}</div>'
93
 
94
- # --- Functions for creating the efficiency difference callout cards ---
95
- def get_efficiency_diff_for_all():
96
- """Calculates the efficiency difference across all models."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  all_df = pd.DataFrame()
98
  for task in tasks:
99
  df = pd.read_csv('data/energy/' + task)
@@ -101,41 +116,10 @@ def get_efficiency_diff_for_all():
101
  df = df.iloc[:, 1:]
102
  df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
103
  all_df = pd.concat([all_df, df], ignore_index=True)
104
- if all_df.empty:
105
- return "<div>No data available</div>"
106
- min_val = all_df['gpu_energy_numeric'].min()
107
- max_val = all_df['gpu_energy_numeric'].max()
108
- diff = max_val - min_val
109
- # A colorful gradient card for global stats.
110
- return (
111
- f"<div style='background: linear-gradient(135deg, #f6d365, #fda085); padding: 15px; "
112
- f"border-radius: 8px; margin: 10px; color: #333;'>"
113
- f"<strong>All Models:</strong> Efficiency difference is <strong>{diff:.2f} Wh</strong> "
114
- f"(min: {min_val:.2f} Wh, max: {max_val:.2f} Wh)"
115
- f"</div>"
116
- )
117
-
118
- def get_efficiency_diff_for_task(task_filename):
119
- """Calculates the efficiency difference for models in a given task."""
120
- df = pd.read_csv('data/energy/' + task_filename)
121
- if df.columns[0].startswith("Unnamed:"):
122
- df = df.iloc[:, 1:]
123
- df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
124
- if df.empty:
125
- return "<div>No data available</div>"
126
- min_val = df['gpu_energy_numeric'].min()
127
- max_val = df['gpu_energy_numeric'].max()
128
- diff = max_val - min_val
129
- # A different gradient for the selected task
130
- return (
131
- f"<div style='background: linear-gradient(135deg, #a8e063, #56ab2f); padding: 15px; "
132
- f"border-radius: 8px; margin: 10px; color: #333;'>"
133
- f"<strong>Selected Task:</strong> Efficiency difference is <strong>{diff:.2f} Wh</strong> "
134
- f"(min: {min_val:.2f} Wh, max: {max_val:.2f} Wh)"
135
- f"</div>"
136
- )
137
 
138
- # --- Function to zip all CSV files (unchanged) ---
139
  def zip_csv_files():
140
  data_dir = "data/energy"
141
  zip_filename = "data.zip"
@@ -159,54 +143,8 @@ def get_zip_data_link():
159
  )
160
  return href
161
 
162
- # --- Modified functions to include a sort_order parameter ---
163
- def get_model_names_html(task, sort_order="Low to High"):
164
- df = pd.read_csv('data/energy/' + task)
165
- if df.columns[0].startswith("Unnamed:"):
166
- df = df.iloc[:, 1:]
167
- df['energy_score'] = df['energy_score'].astype(int)
168
- df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
169
- # Add Provider column (text before the slash in the model field)
170
- df['Provider'] = df['model'].apply(lambda x: str(x).split('/')[0])
171
- df['Model'] = df['model'].apply(make_link)
172
- df['Score'] = df['energy_score'].apply(format_stars)
173
- ascending = (sort_order == "Low to High")
174
- df = df.sort_values(by='gpu_energy_numeric', ascending=ascending)
175
- return generate_html_table_from_df(df)
176
-
177
- def get_all_model_names_html(sort_order="Low to High"):
178
- all_df = pd.DataFrame()
179
- for task in tasks:
180
- df = pd.read_csv('data/energy/' + task)
181
- if df.columns[0].startswith("Unnamed:"):
182
- df = df.iloc[:, 1:]
183
- df['energy_score'] = df['energy_score'].astype(int)
184
- df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
185
- df['Provider'] = df['model'].apply(lambda x: str(x).split('/')[0])
186
- df['Model'] = df['model'].apply(make_link)
187
- df['Score'] = df['energy_score'].apply(format_stars)
188
- all_df = pd.concat([all_df, df], ignore_index=True)
189
- all_df = all_df.drop_duplicates(subset=['model'])
190
- ascending = (sort_order == "Low to High")
191
- all_df = all_df.sort_values(by='gpu_energy_numeric', ascending=ascending)
192
- return generate_html_table_from_df(all_df)
193
-
194
- def get_text_generation_model_names_html(model_class, sort_order="Low to High"):
195
- df = pd.read_csv('data/energy/text_generation.csv')
196
- if df.columns[0].startswith("Unnamed:"):
197
- df = df.iloc[:, 1:]
198
- if 'class' in df.columns:
199
- df = df[df['class'] == model_class]
200
- df['energy_score'] = df['energy_score'].astype(int)
201
- df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
202
- df['Provider'] = df['model'].apply(lambda x: str(x).split('/')[0])
203
- df['Model'] = df['model'].apply(make_link)
204
- df['Score'] = df['energy_score'].apply(format_stars)
205
- ascending = (sort_order == "Low to High")
206
- df = df.sort_values(by='gpu_energy_numeric', ascending=ascending)
207
- return generate_html_table_from_df(df)
208
 
209
- # --- Update functions for dropdown changes ---
210
  def update_text_generation(selected_display, sort_order):
211
  mapping = {
212
  "A (Single Consumer GPU) <20B parameters": "A",
@@ -214,60 +152,102 @@ def update_text_generation(selected_display, sort_order):
214
  "C (Multiple Cloud GPUs) >66B parameters": "C"
215
  }
216
  model_class = mapping.get(selected_display, "A")
217
- table_html = get_text_generation_model_names_html(model_class, sort_order)
218
- # Update the task-specific callout for text generation
219
- task_diff_html = get_efficiency_diff_for_task('text_generation.csv')
220
- return table_html, task_diff_html
 
 
 
 
 
221
 
222
  def update_image_generation(sort_order):
223
- table_html = get_model_names_html('image_generation.csv', sort_order)
224
- task_diff_html = get_efficiency_diff_for_task('image_generation.csv')
225
- return table_html, task_diff_html
 
 
226
 
227
  def update_text_classification(sort_order):
228
- table_html = get_model_names_html('text_classification.csv', sort_order)
229
- task_diff_html = get_efficiency_diff_for_task('text_classification.csv')
230
- return table_html, task_diff_html
 
 
231
 
232
  def update_image_classification(sort_order):
233
- table_html = get_model_names_html('image_classification.csv', sort_order)
234
- task_diff_html = get_efficiency_diff_for_task('image_classification.csv')
235
- return table_html, task_diff_html
 
 
236
 
237
  def update_image_captioning(sort_order):
238
- table_html = get_model_names_html('image_captioning.csv', sort_order)
239
- task_diff_html = get_efficiency_diff_for_task('image_captioning.csv')
240
- return table_html, task_diff_html
 
 
241
 
242
  def update_summarization(sort_order):
243
- table_html = get_model_names_html('summarization.csv', sort_order)
244
- task_diff_html = get_efficiency_diff_for_task('summarization.csv')
245
- return table_html, task_diff_html
 
 
246
 
247
  def update_asr(sort_order):
248
- table_html = get_model_names_html('asr.csv', sort_order)
249
- task_diff_html = get_efficiency_diff_for_task('asr.csv')
250
- return table_html, task_diff_html
 
 
251
 
252
  def update_object_detection(sort_order):
253
- table_html = get_model_names_html('object_detection.csv', sort_order)
254
- task_diff_html = get_efficiency_diff_for_task('object_detection.csv')
255
- return table_html, task_diff_html
 
 
256
 
257
  def update_sentence_similarity(sort_order):
258
- table_html = get_model_names_html('sentence_similarity.csv', sort_order)
259
- task_diff_html = get_efficiency_diff_for_task('sentence_similarity.csv')
260
- return table_html, task_diff_html
 
 
261
 
262
  def update_extractive_qa(sort_order):
263
- table_html = get_model_names_html('question_answering.csv', sort_order)
264
- task_diff_html = get_efficiency_diff_for_task('question_answering.csv')
265
- return table_html, task_diff_html
 
 
266
 
267
  def update_all_tasks(sort_order):
268
- return get_all_model_names_html(sort_order)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
 
270
- # --- Build the Gradio Interface ---
271
  demo = gr.Blocks(css="""
272
  .gr-dataframe table {
273
  table-layout: fixed;
@@ -287,7 +267,7 @@ demo = gr.Blocks(css="""
287
  """)
288
 
289
  with demo:
290
- # --- Header Links ---
291
  gr.HTML(f'''
292
  <div style="display: flex; justify-content: space-evenly; align-items: center; margin-bottom: 20px;">
293
  <a href="https://huggingface.co/spaces/AIEnergyScore/submission_portal" style="text-decoration: none; font-weight: bold; font-size: 1.1em; color: black; font-family: 'Inter', sans-serif;">Submission Portal</a>
@@ -299,23 +279,22 @@ with demo:
299
  </div>
300
  ''')
301
 
302
- # --- Logo and Subtitle ---
303
  gr.HTML('''
304
- <div style="margin-top: 0px; text-align: center;">
305
  <img src="https://huggingface.co/spaces/AIEnergyScore/Leaderboard/resolve/main/logo.png"
306
  alt="Logo"
307
- style="max-width: 300px; height: auto; margin-bottom: 10px;">
308
  </div>
309
  ''')
310
- gr.Markdown('<div style="text-align: center; font-size: 1.2em;">Welcome to the AI Energy Score leaderboard. Select different tasks to see scored models.</div>')
311
-
312
- # --- Callout Cards (Row at the Top) ---
313
- with gr.Row():
314
- all_models_card = gr.HTML(get_efficiency_diff_for_all())
315
- # Initially, we show the stats for text_generation as default for the selected task.
316
- selected_task_card = gr.HTML(get_efficiency_diff_for_task('text_generation.csv'))
317
 
318
- # --- Tabs for the Different Tasks ---
 
 
 
 
 
 
319
  with gr.Tabs():
320
  # --- Text Generation Tab ---
321
  with gr.TabItem("Text Generation πŸ’¬"):
@@ -335,18 +314,14 @@ with demo:
335
  label="Sort",
336
  value="Low to High"
337
  )
338
- # Two outputs: the table and the task callout card.
339
- tg_table = gr.HTML(get_text_generation_model_names_html("A", "Low to High"))
340
- model_class_dropdown.change(
341
- fn=update_text_generation,
342
- inputs=[model_class_dropdown, sort_dropdown_tg],
343
- outputs=[tg_table, selected_task_card]
344
- )
345
- sort_dropdown_tg.change(
346
- fn=update_text_generation,
347
- inputs=[model_class_dropdown, sort_dropdown_tg],
348
- outputs=[tg_table, selected_task_card]
349
- )
350
 
351
  # --- Image Generation Tab ---
352
  with gr.TabItem("Image Generation πŸ“·"):
@@ -355,12 +330,12 @@ with demo:
355
  label="Sort",
356
  value="Low to High"
357
  )
358
- img_table = gr.HTML(get_model_names_html('image_generation.csv', "Low to High"))
359
- sort_dropdown_img.change(
360
- fn=update_image_generation,
361
- inputs=sort_dropdown_img,
362
- outputs=[img_table, selected_task_card]
363
- )
364
 
365
  # --- Text Classification Tab ---
366
  with gr.TabItem("Text Classification 🎭"):
@@ -369,12 +344,12 @@ with demo:
369
  label="Sort",
370
  value="Low to High"
371
  )
372
- tc_table = gr.HTML(get_model_names_html('text_classification.csv', "Low to High"))
373
- sort_dropdown_tc.change(
374
- fn=update_text_classification,
375
- inputs=sort_dropdown_tc,
376
- outputs=[tc_table, selected_task_card]
377
- )
378
 
379
  # --- Image Classification Tab ---
380
  with gr.TabItem("Image Classification πŸ–ΌοΈ"):
@@ -383,12 +358,12 @@ with demo:
383
  label="Sort",
384
  value="Low to High"
385
  )
386
- ic_table = gr.HTML(get_model_names_html('image_classification.csv', "Low to High"))
387
- sort_dropdown_ic.change(
388
- fn=update_image_classification,
389
- inputs=sort_dropdown_ic,
390
- outputs=[ic_table, selected_task_card]
391
- )
392
 
393
  # --- Image Captioning Tab ---
394
  with gr.TabItem("Image Captioning πŸ“"):
@@ -397,12 +372,12 @@ with demo:
397
  label="Sort",
398
  value="Low to High"
399
  )
400
- icap_table = gr.HTML(get_model_names_html('image_captioning.csv', "Low to High"))
401
- sort_dropdown_icap.change(
402
- fn=update_image_captioning,
403
- inputs=sort_dropdown_icap,
404
- outputs=[icap_table, selected_task_card]
405
- )
406
 
407
  # --- Summarization Tab ---
408
  with gr.TabItem("Summarization πŸ“ƒ"):
@@ -411,12 +386,12 @@ with demo:
411
  label="Sort",
412
  value="Low to High"
413
  )
414
- sum_table = gr.HTML(get_model_names_html('summarization.csv', "Low to High"))
415
- sort_dropdown_sum.change(
416
- fn=update_summarization,
417
- inputs=sort_dropdown_sum,
418
- outputs=[sum_table, selected_task_card]
419
- )
420
 
421
  # --- Automatic Speech Recognition Tab ---
422
  with gr.TabItem("Automatic Speech Recognition πŸ’¬"):
@@ -425,12 +400,12 @@ with demo:
425
  label="Sort",
426
  value="Low to High"
427
  )
428
- asr_table = gr.HTML(get_model_names_html('asr.csv', "Low to High"))
429
- sort_dropdown_asr.change(
430
- fn=update_asr,
431
- inputs=sort_dropdown_asr,
432
- outputs=[asr_table, selected_task_card]
433
- )
434
 
435
  # --- Object Detection Tab ---
436
  with gr.TabItem("Object Detection 🚘"):
@@ -439,12 +414,12 @@ with demo:
439
  label="Sort",
440
  value="Low to High"
441
  )
442
- od_table = gr.HTML(get_model_names_html('object_detection.csv', "Low to High"))
443
- sort_dropdown_od.change(
444
- fn=update_object_detection,
445
- inputs=sort_dropdown_od,
446
- outputs=[od_table, selected_task_card]
447
- )
448
 
449
  # --- Sentence Similarity Tab ---
450
  with gr.TabItem("Sentence Similarity πŸ“š"):
@@ -453,12 +428,12 @@ with demo:
453
  label="Sort",
454
  value="Low to High"
455
  )
456
- ss_table = gr.HTML(get_model_names_html('sentence_similarity.csv', "Low to High"))
457
- sort_dropdown_ss.change(
458
- fn=update_sentence_similarity,
459
- inputs=sort_dropdown_ss,
460
- outputs=[ss_table, selected_task_card]
461
- )
462
 
463
  # --- Extractive QA Tab ---
464
  with gr.TabItem("Extractive QA ❔"):
@@ -467,22 +442,26 @@ with demo:
467
  label="Sort",
468
  value="Low to High"
469
  )
470
- qa_table = gr.HTML(get_model_names_html('question_answering.csv', "Low to High"))
471
- sort_dropdown_qa.change(
472
- fn=update_extractive_qa,
473
- inputs=sort_dropdown_qa,
474
- outputs=[qa_table, selected_task_card]
475
- )
476
 
477
- # --- All Tasks Tab (only table update) ---
478
  with gr.TabItem("All Tasks πŸ’‘"):
479
  sort_dropdown_all = gr.Dropdown(
480
  choices=["Low to High", "High to Low"],
481
  label="Sort",
482
  value="Low to High"
483
  )
484
- all_table = gr.HTML(get_all_model_names_html("Low to High"))
485
- sort_dropdown_all.change(fn=update_all_tasks, inputs=sort_dropdown_all, outputs=all_table)
 
 
 
 
486
 
487
  with gr.Accordion("πŸ“™ Citation", open=False):
488
  citation_button = gr.Textbox(
 
27
  'summarization.csv'
28
  ]
29
 
30
+ ### HELPER FUNCTIONS ###
31
+
32
  def format_stars(score):
33
  try:
34
  score_int = int(score)
35
  except Exception:
36
  score_int = 0
 
37
  return f'<span style="color: #3fa45bff; font-size:1.5em;">{"β˜…" * score_int}</span>'
38
 
39
  def make_link(mname):
 
42
  return f'<a href="https://huggingface.co/{mname}" target="_blank">{display_name}</a>'
43
 
44
  def extract_link_text(html_link):
 
45
  start = html_link.find('>') + 1
46
  end = html_link.rfind('</a>')
47
  if start > 0 and end > start:
 
50
  return html_link
51
 
52
  def generate_html_table_from_df(df):
53
+ # Compute a static width for the Model column based on the longest model name.
 
 
 
 
 
 
54
  if not df.empty:
55
  max_length = max(len(extract_link_text(link)) for link in df['Model'])
56
  else:
 
64
  html += '<th style="text-align: left; padding: 8px;" title="Model name with link to Hugging Face">Model</th>'
65
  html += '<th style="text-align: left; padding: 8px;" title="AI Provider extracted from the model name">Provider</th>'
66
  html += '<th style="text-align: left; padding: 8px;" title="GPU energy consumed in Watt-hours for 1,000 queries">GPU Energy (Wh)</th>'
67
+ html += '<th style="text-align: left; padding: 8px;" title="Energy efficiency score (stars)">Score</th>'
68
  html += '</tr></thead>'
69
  html += '<tbody>'
70
  for _, row in df.iterrows():
 
85
  html += '</tbody></table>'
86
  return f'<div class="table-container">{html}</div>'
87
 
88
+ def process_df(task, sort_order="Low to High", filter_fn=None):
89
+ df = pd.read_csv('data/energy/' + task)
90
+ if df.columns[0].startswith("Unnamed:"):
91
+ df = df.iloc[:, 1:]
92
+ df['energy_score'] = df['energy_score'].astype(int)
93
+ df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
94
+ if filter_fn is not None:
95
+ df = filter_fn(df)
96
+ df['Provider'] = df['model'].apply(lambda x: str(x).split('/')[0])
97
+ df['Model'] = df['model'].apply(make_link)
98
+ df['Score'] = df['energy_score'].apply(format_stars)
99
+ ascending = True if sort_order == "Low to High" else False
100
+ df = df.sort_values(by='gpu_energy_numeric', ascending=ascending)
101
+ return df
102
+
103
+ def compute_efficiency_ratio(df):
104
+ if df.empty:
105
+ return 1
106
+ min_val = df['gpu_energy_numeric'].min()
107
+ max_val = df['gpu_energy_numeric'].max()
108
+ ratio = max_val / min_val if min_val > 0 else 1
109
+ return ratio
110
+
111
+ def get_global_callout():
112
  all_df = pd.DataFrame()
113
  for task in tasks:
114
  df = pd.read_csv('data/energy/' + task)
 
116
  df = df.iloc[:, 1:]
117
  df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
118
  all_df = pd.concat([all_df, df], ignore_index=True)
119
+ ratio = compute_efficiency_ratio(all_df)
120
+ return f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in leaderboard.</div>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
+ ### ZIP DOWNLOAD (unchanged) ###
123
  def zip_csv_files():
124
  data_dir = "data/energy"
125
  zip_filename = "data.zip"
 
143
  )
144
  return href
145
 
146
+ ### UPDATE FUNCTIONS FOR TASKS (returning both callout and table) ###
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
 
148
  def update_text_generation(selected_display, sort_order):
149
  mapping = {
150
  "A (Single Consumer GPU) <20B parameters": "A",
 
152
  "C (Multiple Cloud GPUs) >66B parameters": "C"
153
  }
154
  model_class = mapping.get(selected_display, "A")
155
+ def filter_fn(df):
156
+ if 'class' in df.columns:
157
+ return df[df['class'] == model_class]
158
+ return df
159
+ df = process_df('text_generation.csv', sort_order, filter_fn)
160
+ ratio = compute_efficiency_ratio(df)
161
+ callout = f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom:10px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in task.</div>'
162
+ table_html = generate_html_table_from_df(df)
163
+ return callout, table_html
164
 
165
  def update_image_generation(sort_order):
166
+ df = process_df('image_generation.csv', sort_order)
167
+ ratio = compute_efficiency_ratio(df)
168
+ callout = f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom:10px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in task.</div>'
169
+ table_html = generate_html_table_from_df(df)
170
+ return callout, table_html
171
 
172
  def update_text_classification(sort_order):
173
+ df = process_df('text_classification.csv', sort_order)
174
+ ratio = compute_efficiency_ratio(df)
175
+ callout = f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom:10px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in task.</div>'
176
+ table_html = generate_html_table_from_df(df)
177
+ return callout, table_html
178
 
179
  def update_image_classification(sort_order):
180
+ df = process_df('image_classification.csv', sort_order)
181
+ ratio = compute_efficiency_ratio(df)
182
+ callout = f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom:10px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in task.</div>'
183
+ table_html = generate_html_table_from_df(df)
184
+ return callout, table_html
185
 
186
  def update_image_captioning(sort_order):
187
+ df = process_df('image_captioning.csv', sort_order)
188
+ ratio = compute_efficiency_ratio(df)
189
+ callout = f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom:10px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in task.</div>'
190
+ table_html = generate_html_table_from_df(df)
191
+ return callout, table_html
192
 
193
  def update_summarization(sort_order):
194
+ df = process_df('summarization.csv', sort_order)
195
+ ratio = compute_efficiency_ratio(df)
196
+ callout = f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom:10px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in task.</div>'
197
+ table_html = generate_html_table_from_df(df)
198
+ return callout, table_html
199
 
200
  def update_asr(sort_order):
201
+ df = process_df('asr.csv', sort_order)
202
+ ratio = compute_efficiency_ratio(df)
203
+ callout = f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom:10px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in task.</div>'
204
+ table_html = generate_html_table_from_df(df)
205
+ return callout, table_html
206
 
207
  def update_object_detection(sort_order):
208
+ df = process_df('object_detection.csv', sort_order)
209
+ ratio = compute_efficiency_ratio(df)
210
+ callout = f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom:10px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in task.</div>'
211
+ table_html = generate_html_table_from_df(df)
212
+ return callout, table_html
213
 
214
  def update_sentence_similarity(sort_order):
215
+ df = process_df('sentence_similarity.csv', sort_order)
216
+ ratio = compute_efficiency_ratio(df)
217
+ callout = f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom:10px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in task.</div>'
218
+ table_html = generate_html_table_from_df(df)
219
+ return callout, table_html
220
 
221
  def update_extractive_qa(sort_order):
222
+ df = process_df('question_answering.csv', sort_order)
223
+ ratio = compute_efficiency_ratio(df)
224
+ callout = f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom:10px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in task.</div>'
225
+ table_html = generate_html_table_from_df(df)
226
+ return callout, table_html
227
 
228
  def update_all_tasks(sort_order):
229
+ # Process all CSV files together
230
+ all_df = pd.DataFrame()
231
+ for task in tasks:
232
+ df = pd.read_csv('data/energy/' + task)
233
+ if df.columns[0].startswith("Unnamed:"):
234
+ df = df.iloc[:, 1:]
235
+ df['energy_score'] = df['energy_score'].astype(int)
236
+ df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
237
+ df['Provider'] = df['model'].apply(lambda x: str(x).split('/')[0])
238
+ df['Model'] = df['model'].apply(make_link)
239
+ df['Score'] = df['energy_score'].apply(format_stars)
240
+ all_df = pd.concat([all_df, df], ignore_index=True)
241
+ all_df = all_df.drop_duplicates(subset=['model'])
242
+ ascending = True if sort_order == "Low to High" else False
243
+ all_df = all_df.sort_values(by='gpu_energy_numeric', ascending=ascending)
244
+ ratio = compute_efficiency_ratio(all_df)
245
+ callout = f'<div style="background-color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom:10px;">Energy efficiency difference of <strong>{round(ratio, 1)}x</strong> for all models in leaderboard.</div>'
246
+ table_html = generate_html_table_from_df(all_df)
247
+ return callout, table_html
248
+
249
+ ### BUILD THE GRADIO INTERFACE ###
250
 
 
251
  demo = gr.Blocks(css="""
252
  .gr-dataframe table {
253
  table-layout: fixed;
 
267
  """)
268
 
269
  with demo:
270
+ # --- Header Links (evenly spaced) ---
271
  gr.HTML(f'''
272
  <div style="display: flex; justify-content: space-evenly; align-items: center; margin-bottom: 20px;">
273
  <a href="https://huggingface.co/spaces/AIEnergyScore/submission_portal" style="text-decoration: none; font-weight: bold; font-size: 1.1em; color: black; font-family: 'Inter', sans-serif;">Submission Portal</a>
 
279
  </div>
280
  ''')
281
 
282
+ # --- Centered Logo ---
283
  gr.HTML('''
284
+ <div style="text-align: center; margin-top: 0px;">
285
  <img src="https://huggingface.co/spaces/AIEnergyScore/Leaderboard/resolve/main/logo.png"
286
  alt="Logo"
287
+ style="display: inline-block; max-width: 300px; height: auto;">
288
  </div>
289
  ''')
 
 
 
 
 
 
 
290
 
291
+ # --- Global Callout (for all models in leaderboard) ---
292
+ global_callout = gr.HTML(get_global_callout())
293
+
294
+ # --- Welcome Text (moved below the callouts) ---
295
+ gr.Markdown('<div style="text-align: center; margin-top: 10px;">Welcome to the AI Energy Score leaderboard. Select different tasks to see scored models.</div>')
296
+
297
+ # --- Tabs for the different tasks ---
298
  with gr.Tabs():
299
  # --- Text Generation Tab ---
300
  with gr.TabItem("Text Generation πŸ’¬"):
 
314
  label="Sort",
315
  value="Low to High"
316
  )
317
+ tg_callout = gr.HTML()
318
+ tg_table = gr.HTML()
319
+ # Set initial values
320
+ init_callout, init_table = update_text_generation(model_class_options[0], "Low to High")
321
+ tg_callout.value = init_callout
322
+ tg_table.value = init_table
323
+ model_class_dropdown.change(fn=update_text_generation, inputs=[model_class_dropdown, sort_dropdown_tg], outputs=[tg_callout, tg_table])
324
+ sort_dropdown_tg.change(fn=update_text_generation, inputs=[model_class_dropdown, sort_dropdown_tg], outputs=[tg_callout, tg_table])
 
 
 
 
325
 
326
  # --- Image Generation Tab ---
327
  with gr.TabItem("Image Generation πŸ“·"):
 
330
  label="Sort",
331
  value="Low to High"
332
  )
333
+ img_callout = gr.HTML()
334
+ img_table = gr.HTML()
335
+ init_callout, init_table = update_image_generation("Low to High")
336
+ img_callout.value = init_callout
337
+ img_table.value = init_table
338
+ sort_dropdown_img.change(fn=update_image_generation, inputs=sort_dropdown_img, outputs=[img_callout, img_table])
339
 
340
  # --- Text Classification Tab ---
341
  with gr.TabItem("Text Classification 🎭"):
 
344
  label="Sort",
345
  value="Low to High"
346
  )
347
+ tc_callout = gr.HTML()
348
+ tc_table = gr.HTML()
349
+ init_callout, init_table = update_text_classification("Low to High")
350
+ tc_callout.value = init_callout
351
+ tc_table.value = init_table
352
+ sort_dropdown_tc.change(fn=update_text_classification, inputs=sort_dropdown_tc, outputs=[tc_callout, tc_table])
353
 
354
  # --- Image Classification Tab ---
355
  with gr.TabItem("Image Classification πŸ–ΌοΈ"):
 
358
  label="Sort",
359
  value="Low to High"
360
  )
361
+ ic_callout = gr.HTML()
362
+ ic_table = gr.HTML()
363
+ init_callout, init_table = update_image_classification("Low to High")
364
+ ic_callout.value = init_callout
365
+ ic_table.value = init_table
366
+ sort_dropdown_ic.change(fn=update_image_classification, inputs=sort_dropdown_ic, outputs=[ic_callout, ic_table])
367
 
368
  # --- Image Captioning Tab ---
369
  with gr.TabItem("Image Captioning πŸ“"):
 
372
  label="Sort",
373
  value="Low to High"
374
  )
375
+ icap_callout = gr.HTML()
376
+ icap_table = gr.HTML()
377
+ init_callout, init_table = update_image_captioning("Low to High")
378
+ icap_callout.value = init_callout
379
+ icap_table.value = init_table
380
+ sort_dropdown_icap.change(fn=update_image_captioning, inputs=sort_dropdown_icap, outputs=[icap_callout, icap_table])
381
 
382
  # --- Summarization Tab ---
383
  with gr.TabItem("Summarization πŸ“ƒ"):
 
386
  label="Sort",
387
  value="Low to High"
388
  )
389
+ sum_callout = gr.HTML()
390
+ sum_table = gr.HTML()
391
+ init_callout, init_table = update_summarization("Low to High")
392
+ sum_callout.value = init_callout
393
+ sum_table.value = init_table
394
+ sort_dropdown_sum.change(fn=update_summarization, inputs=sort_dropdown_sum, outputs=[sum_callout, sum_table])
395
 
396
  # --- Automatic Speech Recognition Tab ---
397
  with gr.TabItem("Automatic Speech Recognition πŸ’¬"):
 
400
  label="Sort",
401
  value="Low to High"
402
  )
403
+ asr_callout = gr.HTML()
404
+ asr_table = gr.HTML()
405
+ init_callout, init_table = update_asr("Low to High")
406
+ asr_callout.value = init_callout
407
+ asr_table.value = init_table
408
+ sort_dropdown_asr.change(fn=update_asr, inputs=sort_dropdown_asr, outputs=[asr_callout, asr_table])
409
 
410
  # --- Object Detection Tab ---
411
  with gr.TabItem("Object Detection 🚘"):
 
414
  label="Sort",
415
  value="Low to High"
416
  )
417
+ od_callout = gr.HTML()
418
+ od_table = gr.HTML()
419
+ init_callout, init_table = update_object_detection("Low to High")
420
+ od_callout.value = init_callout
421
+ od_table.value = init_table
422
+ sort_dropdown_od.change(fn=update_object_detection, inputs=sort_dropdown_od, outputs=[od_callout, od_table])
423
 
424
  # --- Sentence Similarity Tab ---
425
  with gr.TabItem("Sentence Similarity πŸ“š"):
 
428
  label="Sort",
429
  value="Low to High"
430
  )
431
+ ss_callout = gr.HTML()
432
+ ss_table = gr.HTML()
433
+ init_callout, init_table = update_sentence_similarity("Low to High")
434
+ ss_callout.value = init_callout
435
+ ss_table.value = init_table
436
+ sort_dropdown_ss.change(fn=update_sentence_similarity, inputs=sort_dropdown_ss, outputs=[ss_callout, ss_table])
437
 
438
  # --- Extractive QA Tab ---
439
  with gr.TabItem("Extractive QA ❔"):
 
442
  label="Sort",
443
  value="Low to High"
444
  )
445
+ qa_callout = gr.HTML()
446
+ qa_table = gr.HTML()
447
+ init_callout, init_table = update_extractive_qa("Low to High")
448
+ qa_callout.value = init_callout
449
+ qa_table.value = init_table
450
+ sort_dropdown_qa.change(fn=update_extractive_qa, inputs=sort_dropdown_qa, outputs=[qa_callout, qa_table])
451
 
452
+ # --- All Tasks Tab ---
453
  with gr.TabItem("All Tasks πŸ’‘"):
454
  sort_dropdown_all = gr.Dropdown(
455
  choices=["Low to High", "High to Low"],
456
  label="Sort",
457
  value="Low to High"
458
  )
459
+ all_callout = gr.HTML()
460
+ all_table = gr.HTML()
461
+ init_callout, init_table = update_all_tasks("Low to High")
462
+ all_callout.value = init_callout
463
+ all_table.value = init_table
464
+ sort_dropdown_all.change(fn=update_all_tasks, inputs=sort_dropdown_all, outputs=[all_callout, all_table])
465
 
466
  with gr.Accordion("πŸ“™ Citation", open=False):
467
  citation_button = gr.Textbox(