bgamazay commited on
Commit
b3f5a49
·
verified ·
1 Parent(s): 88fbc65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +171 -64
app.py CHANGED
@@ -29,7 +29,7 @@ def format_stars(score):
29
  score_int = int(score)
30
  except Exception:
31
  score_int = 0
32
- # Render stars in black with a slightly larger font
33
  return f'<span style="color: black; font-size:1.5em;">{"★" * score_int}</span>'
34
 
35
  def make_link(mname):
@@ -39,9 +39,12 @@ def make_link(mname):
39
 
40
  def generate_html_table_from_df(df):
41
  """
42
- Generate an HTML table from the given DataFrame.
43
- Each GPU Energy cell contains both the numeric energy (Wh) and a horizontal bar
44
- whose width is computed relative to the maximum energy in the table.
 
 
 
45
  """
46
  max_energy = df['gpu_energy_numeric'].max() if not df.empty else 1
47
  color_map = {"1": "red", "2": "orange", "3": "yellow", "4": "lightgreen", "5": "green"}
@@ -55,7 +58,7 @@ def generate_html_table_from_df(df):
55
  for _, row in df.iterrows():
56
  energy_numeric = row['gpu_energy_numeric']
57
  energy_str = f"{energy_numeric:.4f}"
58
- # Calculate the relative width as a percentage
59
  bar_width = (energy_numeric / max_energy) * 100
60
  score_val = row['energy_score']
61
  bar_color = color_map.get(str(score_val), "gray")
@@ -70,20 +73,21 @@ def generate_html_table_from_df(df):
70
  html += '</tbody></table>'
71
  return html
72
 
73
- def get_model_names_html(task):
 
74
  df = pd.read_csv('data/energy/' + task)
75
  if df.columns[0].startswith("Unnamed:"):
76
  df = df.iloc[:, 1:]
77
- # Convert energy_score to integer and total_gpu_energy from kWh to Wh
78
  df['energy_score'] = df['energy_score'].astype(int)
 
79
  df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
80
  df['Model'] = df['model'].apply(make_link)
81
  df['Score'] = df['energy_score'].apply(format_stars)
82
- # Sort descending (high to low)
83
- df = df.sort_values(by='gpu_energy_numeric', ascending=False)
84
  return generate_html_table_from_df(df)
85
 
86
- def get_all_model_names_html():
87
  all_df = pd.DataFrame()
88
  for task in tasks:
89
  df = pd.read_csv('data/energy/' + task)
@@ -95,35 +99,66 @@ def get_all_model_names_html():
95
  df['Score'] = df['energy_score'].apply(format_stars)
96
  all_df = pd.concat([all_df, df], ignore_index=True)
97
  all_df = all_df.drop_duplicates(subset=['model'])
98
- # Sort descending
99
- all_df = all_df.sort_values(by='gpu_energy_numeric', ascending=False)
100
  return generate_html_table_from_df(all_df)
101
 
102
- def get_text_generation_model_names_html(model_class):
103
  df = pd.read_csv('data/energy/text_generation.csv')
104
  if df.columns[0].startswith("Unnamed:"):
105
  df = df.iloc[:, 1:]
106
- # Filter by model class if the "class" column exists
107
  if 'class' in df.columns:
108
  df = df[df['class'] == model_class]
109
  df['energy_score'] = df['energy_score'].astype(int)
110
  df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
111
  df['Model'] = df['model'].apply(make_link)
112
  df['Score'] = df['energy_score'].apply(format_stars)
113
- # Sort descending
114
- df = df.sort_values(by='gpu_energy_numeric', ascending=False)
115
  return generate_html_table_from_df(df)
116
 
117
- def update_text_generation(selected_display):
118
- # Mapping from display text to the internal value
 
 
119
  mapping = {
120
  "A (Single Consumer GPU) <20B parameters": "A",
121
  "B (Single Cloud GPU) 20-66B parameters": "B",
122
  "C (Multiple Cloud GPUs) >66B parameters": "C"
123
  }
124
  model_class = mapping.get(selected_display, "A")
125
- table_html = get_text_generation_model_names_html(model_class)
126
- return table_html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
  # --- Build the Gradio Interface ---
129
 
@@ -142,12 +177,11 @@ demo = gr.Blocks(css="""
142
 
143
  with demo:
144
  gr.Markdown(
145
- """# AI Energy Score Leaderboard
146
- ### Welcome to the leaderboard for the [AI Energy Score Project!](https://huggingface.co/AIEnergyScore)
147
- Select different tasks to see scored models. Submit open models for testing and learn about testing proprietary models via the [submission portal](https://huggingface.co/spaces/AIEnergyScore/submission_portal)"""
148
  )
149
 
150
- # Visually appealing header links
151
  gr.HTML('''
152
  <div style="text-align: center; margin-bottom: 20px;">
153
  <a href="https://huggingface.co/spaces/AIEnergyScore/submission_portal" style="margin: 0 15px; text-decoration: none; font-weight: bold; font-size: 1.1em;">Submission Portal</a>
@@ -158,56 +192,129 @@ Select different tasks to see scored models. Submit open models for testing and
158
  ''')
159
 
160
  with gr.Tabs():
161
- # --- Text Generation Tab with Dropdown for Model Class ---
162
  with gr.TabItem("Text Generation 💬"):
163
- # Define the dropdown with descriptive text options.
164
- model_class_options = [
165
- "A (Single Consumer GPU) <20B parameters",
166
- "B (Single Cloud GPU) 20-66B parameters",
167
- "C (Multiple Cloud GPUs) >66B parameters"
168
- ]
169
- model_class_dropdown = gr.Dropdown(
170
- choices=model_class_options,
171
- label="Select Model Class",
172
- value=model_class_options[0]
173
- )
174
- tg_table = gr.HTML(get_text_generation_model_names_html("A"))
175
- model_class_dropdown.change(
176
- fn=update_text_generation,
177
- inputs=model_class_dropdown,
178
- outputs=tg_table
179
- )
180
-
 
 
 
 
181
  with gr.TabItem("Image Generation 📷"):
182
- gr.HTML(get_model_names_html('image_generation.csv'))
183
-
 
 
 
 
 
 
 
184
  with gr.TabItem("Text Classification 🎭"):
185
- gr.HTML(get_model_names_html('text_classification.csv'))
186
-
 
 
 
 
 
 
 
187
  with gr.TabItem("Image Classification 🖼️"):
188
- gr.HTML(get_model_names_html('image_classification.csv'))
189
-
 
 
 
 
 
 
 
190
  with gr.TabItem("Image Captioning 📝"):
191
- gr.HTML(get_model_names_html('image_captioning.csv'))
192
-
 
 
 
 
 
 
 
193
  with gr.TabItem("Summarization 📃"):
194
- gr.HTML(get_model_names_html('summarization.csv'))
195
-
 
 
 
 
 
 
 
196
  with gr.TabItem("Automatic Speech Recognition 💬"):
197
- gr.HTML(get_model_names_html('asr.csv'))
198
-
 
 
 
 
 
 
 
199
  with gr.TabItem("Object Detection 🚘"):
200
- gr.HTML(get_model_names_html('object_detection.csv'))
201
-
 
 
 
 
 
 
 
202
  with gr.TabItem("Sentence Similarity 📚"):
203
- gr.HTML(get_model_names_html('sentence_similarity.csv'))
204
-
 
 
 
 
 
 
 
205
  with gr.TabItem("Extractive QA ❔"):
206
- gr.HTML(get_model_names_html('question_answering.csv'))
207
-
 
 
 
 
 
 
 
208
  with gr.TabItem("All Tasks 💡"):
209
- gr.HTML(get_all_model_names_html())
210
-
 
 
 
 
 
 
211
  with gr.Accordion("📙 Citation", open=False):
212
  citation_button = gr.Textbox(
213
  value=CITATION_BUTTON_TEXT,
@@ -218,4 +325,4 @@ Select different tasks to see scored models. Submit open models for testing and
218
  )
219
  gr.Markdown("""Last updated: February 2025""")
220
 
221
- demo.launch()
 
29
  score_int = int(score)
30
  except Exception:
31
  score_int = 0
32
+ # Render stars in black with a slightly larger font.
33
  return f'<span style="color: black; font-size:1.5em;">{"★" * score_int}</span>'
34
 
35
  def make_link(mname):
 
39
 
40
  def generate_html_table_from_df(df):
41
  """
42
+ Given a dataframe with a numeric energy column (gpu_energy_numeric),
43
+ generate an HTML table with three columns:
44
+ - Model (the link)
45
+ - GPU Energy (Wh) plus a horizontal bar whose width is proportional
46
+ to the energy value relative to the maximum in the table.
47
+ - Score (displayed as stars)
48
  """
49
  max_energy = df['gpu_energy_numeric'].max() if not df.empty else 1
50
  color_map = {"1": "red", "2": "orange", "3": "yellow", "4": "lightgreen", "5": "green"}
 
58
  for _, row in df.iterrows():
59
  energy_numeric = row['gpu_energy_numeric']
60
  energy_str = f"{energy_numeric:.4f}"
61
+ # Compute the relative width (as a percentage)
62
  bar_width = (energy_numeric / max_energy) * 100
63
  score_val = row['energy_score']
64
  bar_color = color_map.get(str(score_val), "gray")
 
73
  html += '</tbody></table>'
74
  return html
75
 
76
+ # --- Modified functions to include a sort_order parameter ---
77
+ def get_model_names_html(task, sort_order="High to Low"):
78
  df = pd.read_csv('data/energy/' + task)
79
  if df.columns[0].startswith("Unnamed:"):
80
  df = df.iloc[:, 1:]
 
81
  df['energy_score'] = df['energy_score'].astype(int)
82
+ # Convert kWh to Wh:
83
  df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
84
  df['Model'] = df['model'].apply(make_link)
85
  df['Score'] = df['energy_score'].apply(format_stars)
86
+ ascending = True if sort_order == "Low to High" else False
87
+ df = df.sort_values(by='gpu_energy_numeric', ascending=ascending)
88
  return generate_html_table_from_df(df)
89
 
90
+ def get_all_model_names_html(sort_order="High to Low"):
91
  all_df = pd.DataFrame()
92
  for task in tasks:
93
  df = pd.read_csv('data/energy/' + task)
 
99
  df['Score'] = df['energy_score'].apply(format_stars)
100
  all_df = pd.concat([all_df, df], ignore_index=True)
101
  all_df = all_df.drop_duplicates(subset=['model'])
102
+ ascending = True if sort_order == "Low to High" else False
103
+ all_df = all_df.sort_values(by='gpu_energy_numeric', ascending=ascending)
104
  return generate_html_table_from_df(all_df)
105
 
106
+ def get_text_generation_model_names_html(model_class, sort_order="High to Low"):
107
  df = pd.read_csv('data/energy/text_generation.csv')
108
  if df.columns[0].startswith("Unnamed:"):
109
  df = df.iloc[:, 1:]
 
110
  if 'class' in df.columns:
111
  df = df[df['class'] == model_class]
112
  df['energy_score'] = df['energy_score'].astype(int)
113
  df['gpu_energy_numeric'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000
114
  df['Model'] = df['model'].apply(make_link)
115
  df['Score'] = df['energy_score'].apply(format_stars)
116
+ ascending = True if sort_order == "Low to High" else False
117
+ df = df.sort_values(by='gpu_energy_numeric', ascending=ascending)
118
  return generate_html_table_from_df(df)
119
 
120
+ # --- Update functions for dropdown changes ---
121
+
122
+ # For Text Generation, two dropdowns: model class and sort order.
123
+ def update_text_generation(selected_display, sort_order):
124
  mapping = {
125
  "A (Single Consumer GPU) <20B parameters": "A",
126
  "B (Single Cloud GPU) 20-66B parameters": "B",
127
  "C (Multiple Cloud GPUs) >66B parameters": "C"
128
  }
129
  model_class = mapping.get(selected_display, "A")
130
+ return get_text_generation_model_names_html(model_class, sort_order)
131
+
132
+ # For the other tabs, each update function simply takes the sort_order.
133
+ def update_image_generation(sort_order):
134
+ return get_model_names_html('image_generation.csv', sort_order)
135
+
136
+ def update_text_classification(sort_order):
137
+ return get_model_names_html('text_classification.csv', sort_order)
138
+
139
+ def update_image_classification(sort_order):
140
+ return get_model_names_html('image_classification.csv', sort_order)
141
+
142
+ def update_image_captioning(sort_order):
143
+ return get_model_names_html('image_captioning.csv', sort_order)
144
+
145
+ def update_summarization(sort_order):
146
+ return get_model_names_html('summarization.csv', sort_order)
147
+
148
+ def update_asr(sort_order):
149
+ return get_model_names_html('asr.csv', sort_order)
150
+
151
+ def update_object_detection(sort_order):
152
+ return get_model_names_html('object_detection.csv', sort_order)
153
+
154
+ def update_sentence_similarity(sort_order):
155
+ return get_model_names_html('sentence_similarity.csv', sort_order)
156
+
157
+ def update_extractive_qa(sort_order):
158
+ return get_model_names_html('question_answering.csv', sort_order)
159
+
160
+ def update_all_tasks(sort_order):
161
+ return get_all_model_names_html(sort_order)
162
 
163
  # --- Build the Gradio Interface ---
164
 
 
177
 
178
  with demo:
179
  gr.Markdown(
180
+ """# AI Energy Score Leaderboard
181
+ ### Welcome to the leaderboard for the [AI Energy Score Project!](https://huggingface.co/AIEnergyScore) — Select different tasks to see scored models."""
 
182
  )
183
 
184
+ # Header links:
185
  gr.HTML('''
186
  <div style="text-align: center; margin-bottom: 20px;">
187
  <a href="https://huggingface.co/spaces/AIEnergyScore/submission_portal" style="margin: 0 15px; text-decoration: none; font-weight: bold; font-size: 1.1em;">Submission Portal</a>
 
192
  ''')
193
 
194
  with gr.Tabs():
195
+ # --- Text Generation Tab ---
196
  with gr.TabItem("Text Generation 💬"):
197
+ with gr.Row():
198
+ model_class_options = [
199
+ "A (Single Consumer GPU) <20B parameters",
200
+ "B (Single Cloud GPU) 20-66B parameters",
201
+ "C (Multiple Cloud GPUs) >66B parameters"
202
+ ]
203
+ model_class_dropdown = gr.Dropdown(
204
+ choices=model_class_options,
205
+ label="Select Model Class",
206
+ value=model_class_options[0]
207
+ )
208
+ sort_dropdown_tg = gr.Dropdown(
209
+ choices=["Low to High", "High to Low"],
210
+ label="Sort",
211
+ value="High to Low"
212
+ )
213
+ tg_table = gr.HTML(get_text_generation_model_names_html("A", "High to Low"))
214
+ # When either dropdown changes, update the table.
215
+ model_class_dropdown.change(fn=update_text_generation, inputs=[model_class_dropdown, sort_dropdown_tg], outputs=tg_table)
216
+ sort_dropdown_tg.change(fn=update_text_generation, inputs=[model_class_dropdown, sort_dropdown_tg], outputs=tg_table)
217
+
218
+ # --- Image Generation Tab ---
219
  with gr.TabItem("Image Generation 📷"):
220
+ sort_dropdown_img = gr.Dropdown(
221
+ choices=["Low to High", "High to Low"],
222
+ label="Sort",
223
+ value="High to Low"
224
+ )
225
+ img_table = gr.HTML(get_model_names_html('image_generation.csv', "High to Low"))
226
+ sort_dropdown_img.change(fn=update_image_generation, inputs=sort_dropdown_img, outputs=img_table)
227
+
228
+ # --- Text Classification Tab ---
229
  with gr.TabItem("Text Classification 🎭"):
230
+ sort_dropdown_tc = gr.Dropdown(
231
+ choices=["Low to High", "High to Low"],
232
+ label="Sort",
233
+ value="High to Low"
234
+ )
235
+ tc_table = gr.HTML(get_model_names_html('text_classification.csv', "High to Low"))
236
+ sort_dropdown_tc.change(fn=update_text_classification, inputs=sort_dropdown_tc, outputs=tc_table)
237
+
238
+ # --- Image Classification Tab ---
239
  with gr.TabItem("Image Classification 🖼️"):
240
+ sort_dropdown_ic = gr.Dropdown(
241
+ choices=["Low to High", "High to Low"],
242
+ label="Sort",
243
+ value="High to Low"
244
+ )
245
+ ic_table = gr.HTML(get_model_names_html('image_classification.csv', "High to Low"))
246
+ sort_dropdown_ic.change(fn=update_image_classification, inputs=sort_dropdown_ic, outputs=ic_table)
247
+
248
+ # --- Image Captioning Tab ---
249
  with gr.TabItem("Image Captioning 📝"):
250
+ sort_dropdown_icap = gr.Dropdown(
251
+ choices=["Low to High", "High to Low"],
252
+ label="Sort",
253
+ value="High to Low"
254
+ )
255
+ icap_table = gr.HTML(get_model_names_html('image_captioning.csv', "High to Low"))
256
+ sort_dropdown_icap.change(fn=update_image_captioning, inputs=sort_dropdown_icap, outputs=icap_table)
257
+
258
+ # --- Summarization Tab ---
259
  with gr.TabItem("Summarization 📃"):
260
+ sort_dropdown_sum = gr.Dropdown(
261
+ choices=["Low to High", "High to Low"],
262
+ label="Sort",
263
+ value="High to Low"
264
+ )
265
+ sum_table = gr.HTML(get_model_names_html('summarization.csv', "High to Low"))
266
+ sort_dropdown_sum.change(fn=update_summarization, inputs=sort_dropdown_sum, outputs=sum_table)
267
+
268
+ # --- Automatic Speech Recognition Tab ---
269
  with gr.TabItem("Automatic Speech Recognition 💬"):
270
+ sort_dropdown_asr = gr.Dropdown(
271
+ choices=["Low to High", "High to Low"],
272
+ label="Sort",
273
+ value="High to Low"
274
+ )
275
+ asr_table = gr.HTML(get_model_names_html('asr.csv', "High to Low"))
276
+ sort_dropdown_asr.change(fn=update_asr, inputs=sort_dropdown_asr, outputs=asr_table)
277
+
278
+ # --- Object Detection Tab ---
279
  with gr.TabItem("Object Detection 🚘"):
280
+ sort_dropdown_od = gr.Dropdown(
281
+ choices=["Low to High", "High to Low"],
282
+ label="Sort",
283
+ value="High to Low"
284
+ )
285
+ od_table = gr.HTML(get_model_names_html('object_detection.csv', "High to Low"))
286
+ sort_dropdown_od.change(fn=update_object_detection, inputs=sort_dropdown_od, outputs=od_table)
287
+
288
+ # --- Sentence Similarity Tab ---
289
  with gr.TabItem("Sentence Similarity 📚"):
290
+ sort_dropdown_ss = gr.Dropdown(
291
+ choices=["Low to High", "High to Low"],
292
+ label="Sort",
293
+ value="High to Low"
294
+ )
295
+ ss_table = gr.HTML(get_model_names_html('sentence_similarity.csv', "High to Low"))
296
+ sort_dropdown_ss.change(fn=update_sentence_similarity, inputs=sort_dropdown_ss, outputs=ss_table)
297
+
298
+ # --- Extractive QA Tab ---
299
  with gr.TabItem("Extractive QA ❔"):
300
+ sort_dropdown_qa = gr.Dropdown(
301
+ choices=["Low to High", "High to Low"],
302
+ label="Sort",
303
+ value="High to Low"
304
+ )
305
+ qa_table = gr.HTML(get_model_names_html('question_answering.csv', "High to Low"))
306
+ sort_dropdown_qa.change(fn=update_extractive_qa, inputs=sort_dropdown_qa, outputs=qa_table)
307
+
308
+ # --- All Tasks Tab ---
309
  with gr.TabItem("All Tasks 💡"):
310
+ sort_dropdown_all = gr.Dropdown(
311
+ choices=["Low to High", "High to Low"],
312
+ label="Sort",
313
+ value="High to Low"
314
+ )
315
+ all_table = gr.HTML(get_all_model_names_html("High to Low"))
316
+ sort_dropdown_all.change(fn=update_all_tasks, inputs=sort_dropdown_all, outputs=all_table)
317
+
318
  with gr.Accordion("📙 Citation", open=False):
319
  citation_button = gr.Textbox(
320
  value=CITATION_BUTTON_TEXT,
 
325
  )
326
  gr.Markdown("""Last updated: February 2025""")
327
 
328
+ demo.launch()