Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,120 +1,177 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
from huggingface_hub import list_models
|
4 |
import plotly.express as px
|
5 |
|
6 |
CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results"
|
7 |
CITATION_BUTTON_TEXT = r"""@misc{energystarai-leaderboard,
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def get_plots(task):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
fig.update_traces(
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
34 |
)
|
|
|
35 |
return fig
|
36 |
|
37 |
def get_all_plots():
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
for task in tasks:
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
fig.update_traces(
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
55 |
)
|
|
|
56 |
return fig
|
57 |
|
58 |
-
def make_link(mname):
|
59 |
-
link = "["+ str(mname).split('/')[1] +'](https://huggingface.co/'+str(mname)+")"
|
60 |
-
return link
|
61 |
-
|
62 |
def get_model_names(task):
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
75 |
return model_names
|
76 |
|
77 |
def get_all_model_names():
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
80 |
for task in tasks:
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
all_df=all_df.drop_duplicates(subset=['model'])
|
91 |
-
all_df['Parameters'] = all_df['parameters'].apply(format_params)
|
92 |
-
all_df['Model'] = all_df['model'].apply(make_link)
|
93 |
-
all_df= all_df.sort_values('Total GPU Energy (Wh)')
|
94 |
-
model_names = all_df[['Model','Rating','Total GPU Energy (Wh)', 'Parameters']]
|
95 |
return model_names
|
96 |
|
97 |
-
|
98 |
-
def format_params(num):
|
99 |
-
if num > 1000000000:
|
100 |
-
if not num % 1000000000:
|
101 |
-
return f'{num // 1000000000}B'
|
102 |
-
return f'{round(num / 1000000000, 1)}B'
|
103 |
-
return f'{num // 1000000}M'
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
demo = gr.Blocks()
|
108 |
|
109 |
with demo:
|
110 |
gr.Markdown(
|
111 |
"""# AI Energy Score Leaderboard - v.0 (2024) 🌎 💻 🌟
|
112 |
-
|
113 |
-
|
114 |
)
|
115 |
gr.Markdown(
|
116 |
"""Test your own models via the [submission portal](https://huggingface.co/spaces/AIEnergyScore/submission_portal)!"""
|
117 |
-
|
|
|
118 |
with gr.Tabs():
|
119 |
with gr.TabItem("Text Generation 💬"):
|
120 |
with gr.Row():
|
@@ -122,91 +179,87 @@ with demo:
|
|
122 |
plot = gr.Plot(get_plots('text_generation.csv'))
|
123 |
with gr.Column(scale=1):
|
124 |
table = gr.Dataframe(get_model_names('text_generation.csv'), datatype="markdown")
|
125 |
-
|
126 |
with gr.TabItem("Image Generation 📷"):
|
127 |
with gr.Row():
|
128 |
with gr.Column():
|
129 |
plot = gr.Plot(get_plots('image_generation.csv'))
|
130 |
with gr.Column():
|
131 |
table = gr.Dataframe(get_model_names('image_generation.csv'), datatype="markdown")
|
132 |
-
|
133 |
with gr.TabItem("Text Classification 🎭"):
|
134 |
with gr.Row():
|
135 |
with gr.Column():
|
136 |
plot = gr.Plot(get_plots('text_classification.csv'))
|
137 |
with gr.Column():
|
138 |
table = gr.Dataframe(get_model_names('text_classification.csv'), datatype="markdown")
|
139 |
-
|
140 |
with gr.TabItem("Image Classification 🖼️"):
|
141 |
with gr.Row():
|
142 |
with gr.Column():
|
143 |
plot = gr.Plot(get_plots('image_classification.csv'))
|
144 |
with gr.Column():
|
145 |
table = gr.Dataframe(get_model_names('image_classification.csv'), datatype="markdown")
|
146 |
-
|
147 |
with gr.TabItem("Image Captioning 📝"):
|
148 |
with gr.Row():
|
149 |
with gr.Column():
|
150 |
plot = gr.Plot(get_plots('image_captioning.csv'))
|
151 |
with gr.Column():
|
152 |
table = gr.Dataframe(get_model_names('image_captioning.csv'), datatype="markdown")
|
|
|
153 |
with gr.TabItem("Summarization 📃"):
|
154 |
with gr.Row():
|
155 |
with gr.Column():
|
156 |
plot = gr.Plot(get_plots('summarization.csv'))
|
157 |
with gr.Column():
|
158 |
table = gr.Dataframe(get_model_names('summarization.csv'), datatype="markdown")
|
159 |
-
|
160 |
-
with gr.TabItem("Automatic Speech Recognition 💬
|
161 |
with gr.Row():
|
162 |
with gr.Column():
|
163 |
plot = gr.Plot(get_plots('asr.csv'))
|
164 |
with gr.Column():
|
165 |
table = gr.Dataframe(get_model_names('asr.csv'), datatype="markdown")
|
166 |
-
|
167 |
with gr.TabItem("Object Detection 🚘"):
|
168 |
with gr.Row():
|
169 |
with gr.Column():
|
170 |
plot = gr.Plot(get_plots('object_detection.csv'))
|
171 |
with gr.Column():
|
172 |
table = gr.Dataframe(get_model_names('object_detection.csv'), datatype="markdown")
|
173 |
-
|
174 |
with gr.TabItem("Sentence Similarity 📚"):
|
175 |
with gr.Row():
|
176 |
with gr.Column():
|
177 |
plot = gr.Plot(get_plots('sentence_similarity.csv'))
|
178 |
with gr.Column():
|
179 |
table = gr.Dataframe(get_model_names('sentence_similarity.csv'), datatype="markdown")
|
180 |
-
|
181 |
with gr.TabItem("Extractive QA ❔"):
|
182 |
with gr.Row():
|
183 |
with gr.Column():
|
184 |
plot = gr.Plot(get_plots('question_answering.csv'))
|
185 |
with gr.Column():
|
186 |
table = gr.Dataframe(get_model_names('question_answering.csv'), datatype="markdown")
|
187 |
-
|
188 |
with gr.TabItem("All Tasks 💡"):
|
189 |
with gr.Row():
|
190 |
with gr.Column():
|
191 |
plot = gr.Plot(get_all_plots)
|
192 |
with gr.Column():
|
193 |
table = gr.Dataframe(get_all_model_names, datatype="markdown")
|
194 |
-
|
195 |
-
with gr.Accordion("Methodology", open = False):
|
196 |
-
gr.Markdown(
|
197 |
-
"""For each of the ten tasks above, we created a custom dataset with 1,000 entries (see all of the datasets on our [org Hub page](https://huggingface.co/EnergyStarAI)).
|
198 |
-
We then tested each of the models from the leaderboard on the appropriate task on Nvidia H100 GPUs, measuring the energy consumed using [Code Carbon](https://mlco2.github.io/codecarbon/), an open-source Python package for tracking the environmental impacts of code.
|
199 |
-
We developed and used a [Docker container](https://github.com/huggingface/EnergyStarAI/) to maximize the reproducibility of results, and to enable members of the community to benchmark internal models.
|
200 |
-
Reach out to us if you want to collaborate!
|
201 |
-
""")
|
202 |
with gr.Accordion("📙 Citation", open=False):
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
gr.Markdown(
|
211 |
-
|
|
|
|
|
212 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
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)
|
15 |
+
tasks = [
|
16 |
+
'asr.csv',
|
17 |
+
'object_detection.csv',
|
18 |
+
'text_classification.csv',
|
19 |
+
'image_captioning.csv',
|
20 |
+
'question_answering.csv',
|
21 |
+
'text_generation.csv',
|
22 |
+
'image_classification.csv',
|
23 |
+
'sentence_similarity.csv',
|
24 |
+
'image_generation.csv',
|
25 |
+
'summarization.csv'
|
26 |
+
]
|
27 |
+
|
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('/')
|
45 |
+
display_name = parts[1] if len(parts) > 1 else mname
|
46 |
+
return f'[{display_name}](https://huggingface.co/{mname})'
|
47 |
|
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 = {
|
61 |
+
1: "red",
|
62 |
+
2: "orange",
|
63 |
+
3: "yellow",
|
64 |
+
4: "lightgreen",
|
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,
|
75 |
+
color="energy_score",
|
76 |
+
color_discrete_map=color_map
|
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 |
+
|
101 |
+
color_map = {
|
102 |
+
1: "red",
|
103 |
+
2: "orange",
|
104 |
+
3: "yellow",
|
105 |
+
4: "lightgreen",
|
106 |
+
5: "green"
|
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,
|
115 |
+
color="energy_score",
|
116 |
+
color_discrete_map=color_map
|
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():
|
176 |
with gr.TabItem("Text Generation 💬"):
|
177 |
with gr.Row():
|
|
|
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 📷"):
|
184 |
with gr.Row():
|
185 |
with gr.Column():
|
186 |
plot = gr.Plot(get_plots('image_generation.csv'))
|
187 |
with gr.Column():
|
188 |
table = gr.Dataframe(get_model_names('image_generation.csv'), datatype="markdown")
|
189 |
+
|
190 |
with gr.TabItem("Text Classification 🎭"):
|
191 |
with gr.Row():
|
192 |
with gr.Column():
|
193 |
plot = gr.Plot(get_plots('text_classification.csv'))
|
194 |
with gr.Column():
|
195 |
table = gr.Dataframe(get_model_names('text_classification.csv'), datatype="markdown")
|
196 |
+
|
197 |
with gr.TabItem("Image Classification 🖼️"):
|
198 |
with gr.Row():
|
199 |
with gr.Column():
|
200 |
plot = gr.Plot(get_plots('image_classification.csv'))
|
201 |
with gr.Column():
|
202 |
table = gr.Dataframe(get_model_names('image_classification.csv'), datatype="markdown")
|
203 |
+
|
204 |
with gr.TabItem("Image Captioning 📝"):
|
205 |
with gr.Row():
|
206 |
with gr.Column():
|
207 |
plot = gr.Plot(get_plots('image_captioning.csv'))
|
208 |
with gr.Column():
|
209 |
table = gr.Dataframe(get_model_names('image_captioning.csv'), datatype="markdown")
|
210 |
+
|
211 |
with gr.TabItem("Summarization 📃"):
|
212 |
with gr.Row():
|
213 |
with gr.Column():
|
214 |
plot = gr.Plot(get_plots('summarization.csv'))
|
215 |
with gr.Column():
|
216 |
table = gr.Dataframe(get_model_names('summarization.csv'), datatype="markdown")
|
217 |
+
|
218 |
+
with gr.TabItem("Automatic Speech Recognition 💬"):
|
219 |
with gr.Row():
|
220 |
with gr.Column():
|
221 |
plot = gr.Plot(get_plots('asr.csv'))
|
222 |
with gr.Column():
|
223 |
table = gr.Dataframe(get_model_names('asr.csv'), datatype="markdown")
|
224 |
+
|
225 |
with gr.TabItem("Object Detection 🚘"):
|
226 |
with gr.Row():
|
227 |
with gr.Column():
|
228 |
plot = gr.Plot(get_plots('object_detection.csv'))
|
229 |
with gr.Column():
|
230 |
table = gr.Dataframe(get_model_names('object_detection.csv'), datatype="markdown")
|
231 |
+
|
232 |
with gr.TabItem("Sentence Similarity 📚"):
|
233 |
with gr.Row():
|
234 |
with gr.Column():
|
235 |
plot = gr.Plot(get_plots('sentence_similarity.csv'))
|
236 |
with gr.Column():
|
237 |
table = gr.Dataframe(get_model_names('sentence_similarity.csv'), datatype="markdown")
|
238 |
+
|
239 |
with gr.TabItem("Extractive QA ❔"):
|
240 |
with gr.Row():
|
241 |
with gr.Column():
|
242 |
plot = gr.Plot(get_plots('question_answering.csv'))
|
243 |
with gr.Column():
|
244 |
table = gr.Dataframe(get_model_names('question_answering.csv'), datatype="markdown")
|
245 |
+
|
246 |
with gr.TabItem("All Tasks 💡"):
|
247 |
with gr.Row():
|
248 |
with gr.Column():
|
249 |
plot = gr.Plot(get_all_plots)
|
250 |
with gr.Column():
|
251 |
table = gr.Dataframe(get_all_model_names, datatype="markdown")
|
252 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
with gr.Accordion("📙 Citation", open=False):
|
254 |
+
citation_button = gr.Textbox(
|
255 |
+
value=CITATION_BUTTON_TEXT,
|
256 |
+
label=CITATION_BUTTON_LABEL,
|
257 |
+
elem_id="citation-button",
|
258 |
+
lines=10,
|
259 |
+
show_copy_button=True,
|
260 |
+
)
|
261 |
gr.Markdown(
|
262 |
+
"""Last updated: February 2025"""
|
263 |
+
)
|
264 |
+
|
265 |
demo.launch()
|