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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -21
app.py CHANGED
@@ -69,7 +69,8 @@ def generate_html_table_from_df(df):
69
  html += '<tbody>'
70
  for _, row in df.iterrows():
71
  energy_numeric = row['gpu_energy_numeric']
72
- energy_str = f"{energy_numeric:.2f}"
 
73
  bar_width = (energy_numeric / max_energy) * 100
74
  score_val = row['energy_score']
75
  bar_color = color_map.get(str(score_val), "gray")
@@ -108,6 +109,20 @@ def compute_efficiency_ratio(df):
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:
@@ -117,9 +132,10 @@ def get_global_callout():
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,7 +159,7 @@ def get_zip_data_link():
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 = {
@@ -158,75 +174,74 @@ def update_text_generation(selected_display, sort_order):
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)
@@ -242,7 +257,7 @@ def update_all_tasks(sort_order):
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
 
@@ -267,7 +282,7 @@ demo = gr.Blocks(css="""
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>
@@ -288,11 +303,10 @@ with demo:
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():
@@ -316,7 +330,6 @@ with demo:
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
 
69
  html += '<tbody>'
70
  for _, row in df.iterrows():
71
  energy_numeric = row['gpu_energy_numeric']
72
+ # Format energy with commas and 2 decimal places.
73
+ energy_str = f"{energy_numeric:,.2f}"
74
  bar_width = (energy_numeric / max_energy) * 100
75
  score_val = row['energy_score']
76
  bar_color = color_map.get(str(score_val), "gray")
 
109
  ratio = max_val / min_val if min_val > 0 else 1
110
  return ratio
111
 
112
+ def generate_callout(ratio, scope_text):
113
+ """
114
+ Returns a right-aligned callout where the inner box is shrink-wrapped to its text.
115
+ The ratio is formatted with a comma for thousands.
116
+ """
117
+ return (
118
+ f'<div style="text-align: right;">'
119
+ f' <div style="display: inline-block; background-color: #f2f2f2; padding: 10px; '
120
+ f' border-radius: 5px; margin-bottom:10px;">'
121
+ f' Energy difference of <strong>{ratio:,.1f}x</strong> for {scope_text}.'
122
+ f' </div>'
123
+ f'</div>'
124
+ )
125
+
126
  def get_global_callout():
127
  all_df = pd.DataFrame()
128
  for task in tasks:
 
132
  df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
133
  all_df = pd.concat([all_df, df], ignore_index=True)
134
  ratio = compute_efficiency_ratio(all_df)
135
+ return generate_callout(ratio, "all models in leaderboard")
136
+
137
+ ### ZIP DOWNLOAD FUNCTIONS ###
138
 
 
139
  def zip_csv_files():
140
  data_dir = "data/energy"
141
  zip_filename = "data.zip"
 
159
  )
160
  return href
161
 
162
+ ### UPDATE FUNCTIONS (RETURNING CALLOUT AND TABLE HTML) ###
163
 
164
  def update_text_generation(selected_display, sort_order):
165
  mapping = {
 
174
  return df
175
  df = process_df('text_generation.csv', sort_order, filter_fn)
176
  ratio = compute_efficiency_ratio(df)
177
+ callout = generate_callout(ratio, "all models in task")
178
  table_html = generate_html_table_from_df(df)
179
  return callout, table_html
180
 
181
  def update_image_generation(sort_order):
182
  df = process_df('image_generation.csv', sort_order)
183
  ratio = compute_efficiency_ratio(df)
184
+ callout = generate_callout(ratio, "all models in task")
185
  table_html = generate_html_table_from_df(df)
186
  return callout, table_html
187
 
188
  def update_text_classification(sort_order):
189
  df = process_df('text_classification.csv', sort_order)
190
  ratio = compute_efficiency_ratio(df)
191
+ callout = generate_callout(ratio, "all models in task")
192
  table_html = generate_html_table_from_df(df)
193
  return callout, table_html
194
 
195
  def update_image_classification(sort_order):
196
  df = process_df('image_classification.csv', sort_order)
197
  ratio = compute_efficiency_ratio(df)
198
+ callout = generate_callout(ratio, "all models in task")
199
  table_html = generate_html_table_from_df(df)
200
  return callout, table_html
201
 
202
  def update_image_captioning(sort_order):
203
  df = process_df('image_captioning.csv', sort_order)
204
  ratio = compute_efficiency_ratio(df)
205
+ callout = generate_callout(ratio, "all models in task")
206
  table_html = generate_html_table_from_df(df)
207
  return callout, table_html
208
 
209
  def update_summarization(sort_order):
210
  df = process_df('summarization.csv', sort_order)
211
  ratio = compute_efficiency_ratio(df)
212
+ callout = generate_callout(ratio, "all models in task")
213
  table_html = generate_html_table_from_df(df)
214
  return callout, table_html
215
 
216
  def update_asr(sort_order):
217
  df = process_df('asr.csv', sort_order)
218
  ratio = compute_efficiency_ratio(df)
219
+ callout = generate_callout(ratio, "all models in task")
220
  table_html = generate_html_table_from_df(df)
221
  return callout, table_html
222
 
223
  def update_object_detection(sort_order):
224
  df = process_df('object_detection.csv', sort_order)
225
  ratio = compute_efficiency_ratio(df)
226
+ callout = generate_callout(ratio, "all models in task")
227
  table_html = generate_html_table_from_df(df)
228
  return callout, table_html
229
 
230
  def update_sentence_similarity(sort_order):
231
  df = process_df('sentence_similarity.csv', sort_order)
232
  ratio = compute_efficiency_ratio(df)
233
+ callout = generate_callout(ratio, "all models in task")
234
  table_html = generate_html_table_from_df(df)
235
  return callout, table_html
236
 
237
  def update_extractive_qa(sort_order):
238
  df = process_df('question_answering.csv', sort_order)
239
  ratio = compute_efficiency_ratio(df)
240
+ callout = generate_callout(ratio, "all models in task")
241
  table_html = generate_html_table_from_df(df)
242
  return callout, table_html
243
 
244
  def update_all_tasks(sort_order):
 
245
  all_df = pd.DataFrame()
246
  for task in tasks:
247
  df = pd.read_csv('data/energy/' + task)
 
257
  ascending = True if sort_order == "Low to High" else False
258
  all_df = all_df.sort_values(by='gpu_energy_numeric', ascending=ascending)
259
  ratio = compute_efficiency_ratio(all_df)
260
+ callout = generate_callout(ratio, "all models in leaderboard")
261
  table_html = generate_html_table_from_df(all_df)
262
  return callout, table_html
263
 
 
282
  """)
283
 
284
  with demo:
285
+ # --- Header Links ---
286
  gr.HTML(f'''
287
  <div style="display: flex; justify-content: space-evenly; align-items: center; margin-bottom: 20px;">
288
  <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>
 
303
  </div>
304
  ''')
305
 
306
+ # --- Global Callout ---
307
  global_callout = gr.HTML(get_global_callout())
308
 
309
+ gr.Markdown('<div style="text-align: center; margin-top: 10px;">Select different tasks to see scored models.</div>')
 
310
 
311
  # --- Tabs for the different tasks ---
312
  with gr.Tabs():
 
330
  )
331
  tg_callout = gr.HTML()
332
  tg_table = gr.HTML()
 
333
  init_callout, init_table = update_text_generation(model_class_options[0], "Low to High")
334
  tg_callout.value = init_callout
335
  tg_table.value = init_table