bgamazay commited on
Commit
2ec9b03
·
verified ·
1 Parent(s): 4f8bac4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -42
app.py CHANGED
@@ -3,12 +3,12 @@ import pandas as pd
3
  import plotly.express as px
4
 
5
  CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results"
6
- CITATION_BUTTON_TEXT = r"""@misc{energystarai-leaderboard,
7
  author = {Sasha Luccioni and Boris Gamazaychikov and Emma Strubell and Sara Hooker and Yacine Jernite and Carole-Jean Wu and Margaret Mitchell},
8
- title = {AI Energy Score Leaderboard v.0},
9
- year = {2024},
10
  publisher = {Hugging Face},
11
- howpublished = "\url{https://huggingface.co/spaces/EnergyStarAI/2024_Leaderboard}",
12
  }"""
13
 
14
  # List of tasks (CSV filenames)
@@ -28,17 +28,18 @@ tasks = [
28
  def format_stars(score):
29
  """
30
  Convert the energy_score (assumed to be an integer from 1 to 5)
31
- into that many star characters wrapped in a span with the given color.
 
32
  """
33
  try:
34
  score_int = int(score)
35
  except Exception:
36
  score_int = 0
37
- return f'<span style="color: #3fa45bff; font-size:1.2em;">{"★" * score_int}</span>'
38
 
39
  def make_link(mname):
40
  """
41
- Create a markdown link from the model identifier.
42
  For example, if mname is "org/model", display "model" and link to its HF page.
43
  """
44
  parts = str(mname).split('/')
@@ -48,13 +49,13 @@ def make_link(mname):
48
  def get_plots(task):
49
  """
50
  Read the energy CSV for a given task and return a Plotly scatter plot.
51
- The y-axis shows the total GPU energy (Wh) and the color is determined by energy_score.
 
52
  """
53
  df = pd.read_csv('data/energy/' + task)
54
- # Ensure energy_score is an integer (for discrete color mapping)
55
  df['energy_score'] = df['energy_score'].astype(int)
56
- # Convert kWh to Wh and round to 4 decimal places.
57
- df['Total GPU Energy (Wh)'] = (df['total_gpu_energy'] * 1000).round(4)
58
 
59
  # Define a 5-level color mapping: 1 = red, 5 = green.
60
  color_map = {
@@ -65,10 +66,11 @@ def get_plots(task):
65
  5: "green"
66
  }
67
 
 
68
  fig = px.scatter(
69
  df,
70
- x="model",
71
- y="Total GPU Energy (Wh)",
72
  custom_data=['energy_score'],
73
  height=500,
74
  width=800,
@@ -77,24 +79,24 @@ def get_plots(task):
77
  )
78
  fig.update_traces(
79
  hovertemplate="<br>".join([
80
- "Model: %{x}",
81
- "Total Energy (Wh): %{y}",
82
  "Energy Score: %{customdata[0]}"
83
  ])
84
  )
85
- fig.update_layout(xaxis_title="Model", yaxis_title="Total GPU Energy (Wh)")
86
  return fig
87
 
88
  def get_all_plots():
89
  """
90
  Combine data from all tasks and return a scatter plot.
91
- Duplicate models (if any) are dropped.
92
  """
93
  all_df = pd.DataFrame()
94
  for task in tasks:
95
  df = pd.read_csv('data/energy/' + task)
96
  df['energy_score'] = df['energy_score'].astype(int)
97
- df['Total GPU Energy (Wh)'] = (df['total_gpu_energy'] * 1000).round(4)
98
  all_df = pd.concat([all_df, df], ignore_index=True)
99
  all_df = all_df.drop_duplicates(subset=['model'])
100
 
@@ -107,8 +109,8 @@ def get_all_plots():
107
  }
108
  fig = px.scatter(
109
  all_df,
110
- x="model",
111
- y="Total GPU Energy (Wh)",
112
  custom_data=['energy_score'],
113
  height=500,
114
  width=800,
@@ -117,59 +119,68 @@ def get_all_plots():
117
  )
118
  fig.update_traces(
119
  hovertemplate="<br>".join([
120
- "Model: %{x}",
121
- "Total Energy (Wh): %{y}",
122
  "Energy Score: %{customdata[0]}"
123
  ])
124
  )
125
- fig.update_layout(xaxis_title="Model", yaxis_title="Total GPU Energy (Wh)")
126
  return fig
127
 
128
  def get_model_names(task):
129
  """
130
- For a given task, load the energy CSV and return a dataframe with three columns:
131
- - Model (a markdown link),
132
- - Rating (the star rating based on energy_score),
133
- - Total GPU Energy (Wh)
 
 
134
  """
135
  df = pd.read_csv('data/energy/' + task)
136
  df['energy_score'] = df['energy_score'].astype(int)
137
- df['Total GPU Energy (Wh)'] = (df['total_gpu_energy'] * 1000).round(4)
138
  df['Model'] = df['model'].apply(make_link)
139
- df['Rating'] = df['energy_score'].apply(format_stars)
140
- df = df.sort_values(by='Total GPU Energy (Wh)')
141
- model_names = df[['Model', 'Rating', 'Total GPU Energy (Wh)']]
142
- return model_names
 
 
 
 
 
 
 
143
 
144
  def get_all_model_names():
145
  """
146
- Combine data from all tasks and return a table of models.
 
147
  Duplicate models are dropped.
148
  """
149
  all_df = pd.DataFrame()
150
  for task in tasks:
151
  df = pd.read_csv('data/energy/' + task)
152
  df['energy_score'] = df['energy_score'].astype(int)
153
- df['Total GPU Energy (Wh)'] = (df['total_gpu_energy'] * 1000).round(4)
154
  df['Model'] = df['model'].apply(make_link)
155
- df['Rating'] = df['energy_score'].apply(format_stars)
156
  all_df = pd.concat([all_df, df], ignore_index=True)
157
  all_df = all_df.drop_duplicates(subset=['model'])
158
- all_df = all_df.sort_values(by='Total GPU Energy (Wh)')
159
- model_names = all_df[['Model', 'Rating', 'Total GPU Energy (Wh)']]
160
- return model_names
161
 
162
  # Build the Gradio interface.
163
  demo = gr.Blocks()
164
 
165
  with demo:
166
  gr.Markdown(
167
- """# AI Energy Score Leaderboard - v.0 (2024) 🌎 💻 🌟
168
- ### Welcome to the leaderboard for the [AI Energy Score Project!](https://huggingface.co/EnergyStarAI)
169
  Click through the tasks below to see how different models measure up in terms of energy efficiency."""
170
  )
171
  gr.Markdown(
172
- """Test your own models via the [submission portal](https://huggingface.co/spaces/AIEnergyScore/submission_portal)!"""
173
  )
174
 
175
  with gr.Tabs():
@@ -178,6 +189,7 @@ Click through the tasks below to see how different models measure up in terms of
178
  with gr.Column(scale=1.3):
179
  plot = gr.Plot(get_plots('text_generation.csv'))
180
  with gr.Column(scale=1):
 
181
  table = gr.Dataframe(get_model_names('text_generation.csv'), datatype="markdown")
182
 
183
  with gr.TabItem("Image Generation 📷"):
@@ -262,4 +274,4 @@ Click through the tasks below to see how different models measure up in terms of
262
  """Last updated: February 2025"""
263
  )
264
 
265
- demo.launch()
 
3
  import plotly.express as px
4
 
5
  CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results"
6
+ CITATION_BUTTON_TEXT = r"""@misc{aienergyscore-leaderboard,
7
  author = {Sasha Luccioni and Boris Gamazaychikov and Emma Strubell and Sara Hooker and Yacine Jernite and Carole-Jean Wu and Margaret Mitchell},
8
+ title = {AI Energy Score Leaderboard - February 2025},
9
+ year = {2025},
10
  publisher = {Hugging Face},
11
+ howpublished = "\url{https://huggingface.co/spaces/AIEnergyScore/Leaderboard}",
12
  }"""
13
 
14
  # List of tasks (CSV filenames)
 
28
  def format_stars(score):
29
  """
30
  Convert the energy_score (assumed to be an integer from 1 to 5)
31
+ into that many star characters wrapped in a span styled with color #3fa45bff
32
+ and with a font size increased to 2em.
33
  """
34
  try:
35
  score_int = int(score)
36
  except Exception:
37
  score_int = 0
38
+ return f'<span style="color: #3fa45bff; font-size:2em;">{"★" * score_int}</span>'
39
 
40
  def make_link(mname):
41
  """
42
+ Create a markdown link for the model.
43
  For example, if mname is "org/model", display "model" and link to its HF page.
44
  """
45
  parts = str(mname).split('/')
 
49
  def get_plots(task):
50
  """
51
  Read the energy CSV for a given task and return a Plotly scatter plot.
52
+ Now the x-axis is the numeric energy (GPU Energy (Wh)) and
53
+ the y-axis displays the model name.
54
  """
55
  df = pd.read_csv('data/energy/' + task)
 
56
  df['energy_score'] = df['energy_score'].astype(int)
57
+ # Do not multiply by 1000; simply round to 4 decimals
58
+ df['GPU Energy (Wh)'] = df['total_gpu_energy'].round(4)
59
 
60
  # Define a 5-level color mapping: 1 = red, 5 = green.
61
  color_map = {
 
66
  5: "green"
67
  }
68
 
69
+ # Create a horizontal scatter plot: x is the energy, y is the model.
70
  fig = px.scatter(
71
  df,
72
+ x="GPU Energy (Wh)",
73
+ y="model",
74
  custom_data=['energy_score'],
75
  height=500,
76
  width=800,
 
79
  )
80
  fig.update_traces(
81
  hovertemplate="<br>".join([
82
+ "Model: %{y}",
83
+ "GPU Energy (Wh): %{x}",
84
  "Energy Score: %{customdata[0]}"
85
  ])
86
  )
87
+ fig.update_layout(xaxis_title="GPU Energy (Wh)", yaxis_title="Model")
88
  return fig
89
 
90
  def get_all_plots():
91
  """
92
  Combine data from all tasks and return a scatter plot.
93
+ Duplicate models are dropped.
94
  """
95
  all_df = pd.DataFrame()
96
  for task in tasks:
97
  df = pd.read_csv('data/energy/' + task)
98
  df['energy_score'] = df['energy_score'].astype(int)
99
+ df['GPU Energy (Wh)'] = df['total_gpu_energy'].round(4)
100
  all_df = pd.concat([all_df, df], ignore_index=True)
101
  all_df = all_df.drop_duplicates(subset=['model'])
102
 
 
109
  }
110
  fig = px.scatter(
111
  all_df,
112
+ x="GPU Energy (Wh)",
113
+ y="model",
114
  custom_data=['energy_score'],
115
  height=500,
116
  width=800,
 
119
  )
120
  fig.update_traces(
121
  hovertemplate="<br>".join([
122
+ "Model: %{y}",
123
+ "GPU Energy (Wh): %{x}",
124
  "Energy Score: %{customdata[0]}"
125
  ])
126
  )
127
+ fig.update_layout(xaxis_title="GPU Energy (Wh)", yaxis_title="Model")
128
  return fig
129
 
130
  def get_model_names(task):
131
  """
132
+ For a given task, load the energy CSV and return a dataframe with the following columns:
133
+ - Model (a markdown link)
134
+ - GPU Energy (Wh)
135
+ - Score (a star rating based on energy_score)
136
+ For text_generation.csv only, also add the "Class" column from the CSV.
137
+ The final order is: Model, GPU Energy (Wh), Score, [Class].
138
  """
139
  df = pd.read_csv('data/energy/' + task)
140
  df['energy_score'] = df['energy_score'].astype(int)
141
+ df['GPU Energy (Wh)'] = df['total_gpu_energy'].round(4)
142
  df['Model'] = df['model'].apply(make_link)
143
+ df['Score'] = df['energy_score'].apply(format_stars)
144
+
145
+ # If this CSV contains a "class" column (e.g., for Text Generation), add it.
146
+ if 'class' in df.columns:
147
+ df['Class'] = df['class']
148
+ df = df[['Model', 'GPU Energy (Wh)', 'Score', 'Class']]
149
+ else:
150
+ df = df[['Model', 'GPU Energy (Wh)', 'Score']]
151
+
152
+ df = df.sort_values(by='GPU Energy (Wh)')
153
+ return df
154
 
155
  def get_all_model_names():
156
  """
157
+ Combine data from all tasks and return a leaderboard table with:
158
+ - Model, GPU Energy (Wh), Score
159
  Duplicate models are dropped.
160
  """
161
  all_df = pd.DataFrame()
162
  for task in tasks:
163
  df = pd.read_csv('data/energy/' + task)
164
  df['energy_score'] = df['energy_score'].astype(int)
165
+ df['GPU Energy (Wh)'] = df['total_gpu_energy'].round(4)
166
  df['Model'] = df['model'].apply(make_link)
167
+ df['Score'] = df['energy_score'].apply(format_stars)
168
  all_df = pd.concat([all_df, df], ignore_index=True)
169
  all_df = all_df.drop_duplicates(subset=['model'])
170
+ all_df = all_df.sort_values(by='GPU Energy (Wh)')
171
+ return all_df[['Model', 'GPU Energy (Wh)', 'Score']]
 
172
 
173
  # Build the Gradio interface.
174
  demo = gr.Blocks()
175
 
176
  with demo:
177
  gr.Markdown(
178
+ """# AI Energy Score Leaderboard
179
+ ### Welcome to the leaderboard for the [AI Energy Score Project!](https://huggingface.co/AIEnergyScore)
180
  Click through the tasks below to see how different models measure up in terms of energy efficiency."""
181
  )
182
  gr.Markdown(
183
+ """Test your own models via the [submission portal](https://huggingface.co/spaces/AIEnergyScore/submission_portal)"""
184
  )
185
 
186
  with gr.Tabs():
 
189
  with gr.Column(scale=1.3):
190
  plot = gr.Plot(get_plots('text_generation.csv'))
191
  with gr.Column(scale=1):
192
+ # For text generation, the CSV is assumed to have a "class" column.
193
  table = gr.Dataframe(get_model_names('text_generation.csv'), datatype="markdown")
194
 
195
  with gr.TabItem("Image Generation 📷"):
 
274
  """Last updated: February 2025"""
275
  )
276
 
277
+ demo.launch()