Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
import plotly.express as px | |
CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results" | |
CITATION_BUTTON_TEXT = r"""@misc{aienergyscore-leaderboard, | |
author = {Sasha Luccioni and Boris Gamazaychikov and Emma Strubell and Sara Hooker and Yacine Jernite and Carole-Jean Wu and Margaret Mitchell}, | |
title = {AI Energy Score Leaderboard - February 2025}, | |
year = {2025}, | |
publisher = {Hugging Face}, | |
howpublished = "\url{https://huggingface.co/spaces/AIEnergyScore/Leaderboard}", | |
}""" | |
# List of CSV filenames (one per task) | |
tasks = [ | |
'asr.csv', | |
'object_detection.csv', | |
'text_classification.csv', | |
'image_captioning.csv', | |
'question_answering.csv', | |
'text_generation.csv', | |
'image_classification.csv', | |
'sentence_similarity.csv', | |
'image_generation.csv', | |
'summarization.csv' | |
] | |
def format_stars(score): | |
try: | |
score_int = int(score) | |
except Exception: | |
score_int = 0 | |
return f'<span style="color: #3fa45bff; font-size:2em;">{"★" * score_int}</span>' | |
def make_link(mname): | |
parts = str(mname).split('/') | |
display_name = parts[1] if len(parts) > 1 else mname | |
return f'[{display_name}](https://huggingface.co/{mname})' | |
def read_csv_file(task): | |
""" | |
Reads a CSV from the data/energy folder using the first column as the index | |
and strips any extraneous whitespace from the column names. | |
""" | |
df = pd.read_csv('data/energy/' + task, index_col=0) | |
df.columns = df.columns.str.strip() # remove any extra whitespace | |
return df | |
def get_plots(task): | |
df = read_csv_file(task) | |
# Convert the numeric columns | |
df['total_gpu_energy'] = pd.to_numeric(df['total_gpu_energy'], errors='coerce') | |
df['energy_score'] = pd.to_numeric(df['energy_score'], errors='coerce').astype(int) | |
# Create a short model name for display on the y-axis. | |
df['Display Model'] = df['model'].apply(lambda m: m.split('/')[-1]) | |
# Define a discrete color mapping for energy scores. | |
color_map = {1: "red", 2: "orange", 3: "yellow", 4: "lightgreen", 5: "green"} | |
# Build the scatter plot. | |
fig = px.scatter( | |
df, | |
x="total_gpu_energy", | |
y="Display Model", | |
color="energy_score", | |
custom_data=['model', 'total_gpu_energy', 'energy_score'], | |
height=500, | |
width=800, | |
color_discrete_map=color_map, | |
) | |
fig.update_traces( | |
hovertemplate=( | |
"Model: %{customdata[0]}<br>" + | |
"Total GPU Energy (Wh): %{customdata[1]:.4f}<br>" + | |
"Energy Score: %{customdata[2]}" | |
) | |
) | |
fig.update_layout( | |
xaxis_title="Total GPU Energy (Wh)", | |
yaxis_title="Model", | |
margin=dict(l=40, r=40, t=40, b=40) | |
) | |
return fig | |
def get_all_plots(): | |
all_df = pd.DataFrame() | |
for task in tasks: | |
df = read_csv_file(task) | |
df['total_gpu_energy'] = pd.to_numeric(df['total_gpu_energy'], errors='coerce') | |
df['energy_score'] = pd.to_numeric(df['energy_score'], errors='coerce').astype(int) | |
df['Display Model'] = df['model'].apply(lambda m: m.split('/')[-1]) | |
all_df = pd.concat([all_df, df], ignore_index=True) | |
all_df = all_df.drop_duplicates(subset=['model']) | |
color_map = {1: "red", 2: "orange", 3: "yellow", 4: "lightgreen", 5: "green"} | |
fig = px.scatter( | |
all_df, | |
x="total_gpu_energy", | |
y="Display Model", | |
color="energy_score", | |
custom_data=['model', 'total_gpu_energy', 'energy_score'], | |
height=500, | |
width=800, | |
color_discrete_map=color_map, | |
) | |
fig.update_traces( | |
hovertemplate=( | |
"Model: %{customdata[0]}<br>" + | |
"Total GPU Energy (Wh): %{customdata[1]:.4f}<br>" + | |
"Energy Score: %{customdata[2]}" | |
) | |
) | |
fig.update_layout( | |
xaxis_title="Total GPU Energy (Wh)", | |
yaxis_title="Model", | |
margin=dict(l=40, r=40, t=40, b=40) | |
) | |
return fig | |
def get_model_names(task): | |
""" | |
For a given task, load the energy CSV and return a DataFrame with the following columns: | |
- Model (a markdown link) | |
- GPU Energy (Wh) formatted to 4 decimal places | |
- Score (a star rating based on energy_score) | |
For text_generation.csv only, also include the "Class" column if present. | |
""" | |
df = read_csv_file(task) | |
df['total_gpu_energy'] = pd.to_numeric(df['total_gpu_energy'], errors='coerce') | |
df['energy_score'] = pd.to_numeric(df['energy_score'], errors='coerce').astype(int) | |
df['GPU Energy (Wh)'] = df['total_gpu_energy'].apply(lambda x: f"{x:.4f}") | |
df['Model'] = df['model'].apply(make_link) | |
df['Score'] = df['energy_score'].apply(format_stars) | |
if 'class' in df.columns: | |
df['Class'] = df['class'] | |
df = df[['Model', 'GPU Energy (Wh)', 'Score', 'Class']] | |
else: | |
df = df[['Model', 'GPU Energy (Wh)', 'Score']] | |
# Sort by the numeric GPU energy value. | |
df = df.sort_values(by='total_gpu_energy') | |
return df | |
def get_all_model_names(): | |
""" | |
Combine data from all tasks and return a leaderboard DataFrame with: | |
- Model, GPU Energy (Wh), Score. | |
Duplicate models are dropped. | |
""" | |
all_df = pd.DataFrame() | |
for task in tasks: | |
df = read_csv_file(task) | |
df['total_gpu_energy'] = pd.to_numeric(df['total_gpu_energy'], errors='coerce') | |
df['energy_score'] = pd.to_numeric(df['energy_score'], errors='coerce').astype(int) | |
df['GPU Energy (Wh)'] = df['total_gpu_energy'].apply(lambda x: f"{x:.4f}") | |
df['Model'] = df['model'].apply(make_link) | |
df['Score'] = df['energy_score'].apply(format_stars) | |
all_df = pd.concat([all_df, df], ignore_index=True) | |
all_df = all_df.drop_duplicates(subset=['model']) | |
all_df = all_df.sort_values(by='total_gpu_energy') | |
return all_df[['Model', 'GPU Energy (Wh)', 'Score']] | |
# Build the Gradio interface. | |
demo = gr.Blocks(css=""" | |
.gr-dataframe table { | |
table-layout: fixed; | |
width: 100%; | |
} | |
.gr-dataframe th, .gr-dataframe td { | |
max-width: 150px; | |
white-space: nowrap; | |
overflow: hidden; | |
text-overflow: ellipsis; | |
} | |
""") | |
with demo: | |
gr.Markdown( | |
"""# AI Energy Score Leaderboard | |
### Welcome to the leaderboard for the [AI Energy Score Project!](https://huggingface.co/AIEnergyScore) | |
Click through the tasks below to see how different models measure up in terms of energy efficiency.""" | |
) | |
gr.Markdown( | |
"""Test your own models via the [submission portal](https://huggingface.co/spaces/AIEnergyScore/submission_portal)""" | |
) | |
with gr.Tabs(): | |
with gr.TabItem("Text Generation 💬"): | |
with gr.Row(): | |
with gr.Column(scale=2): | |
plot = gr.Plot(get_plots('text_generation.csv')) | |
with gr.Column(scale=1): | |
table = gr.Dataframe(get_model_names('text_generation.csv'), datatype="markdown") | |
with gr.TabItem("Image Generation 📷"): | |
with gr.Row(): | |
with gr.Column(): | |
plot = gr.Plot(get_plots('image_generation.csv')) | |
with gr.Column(): | |
table = gr.Dataframe(get_model_names('image_generation.csv'), datatype="markdown") | |
with gr.TabItem("Text Classification 🎭"): | |
with gr.Row(): | |
with gr.Column(): | |
plot = gr.Plot(get_plots('text_classification.csv')) | |
with gr.Column(): | |
table = gr.Dataframe(get_model_names('text_classification.csv'), datatype="markdown") | |
with gr.TabItem("Image Classification 🖼️"): | |
with gr.Row(): | |
with gr.Column(): | |
plot = gr.Plot(get_plots('image_classification.csv')) | |
with gr.Column(): | |
table = gr.Dataframe(get_model_names('image_classification.csv'), datatype="markdown") | |
with gr.TabItem("Image Captioning 📝"): | |
with gr.Row(): | |
with gr.Column(): | |
plot = gr.Plot(get_plots('image_captioning.csv')) | |
with gr.Column(): | |
table = gr.Dataframe(get_model_names('image_captioning.csv'), datatype="markdown") | |
with gr.TabItem("Summarization 📃"): | |
with gr.Row(): | |
with gr.Column(): | |
plot = gr.Plot(get_plots('summarization.csv')) | |
with gr.Column(): | |
table = gr.Dataframe(get_model_names('summarization.csv'), datatype="markdown") | |
with gr.TabItem("Automatic Speech Recognition 💬"): | |
with gr.Row(): | |
with gr.Column(): | |
plot = gr.Plot(get_plots('asr.csv')) | |
with gr.Column(): | |
table = gr.Dataframe(get_model_names('asr.csv'), datatype="markdown") | |
with gr.TabItem("Object Detection 🚘"): | |
with gr.Row(): | |
with gr.Column(): | |
plot = gr.Plot(get_plots('object_detection.csv')) | |
with gr.Column(): | |
table = gr.Dataframe(get_model_names('object_detection.csv'), datatype="markdown") | |
with gr.TabItem("Sentence Similarity 📚"): | |
with gr.Row(): | |
with gr.Column(): | |
plot = gr.Plot(get_plots('sentence_similarity.csv')) | |
with gr.Column(): | |
table = gr.Dataframe(get_model_names('sentence_similarity.csv'), datatype="markdown") | |
with gr.TabItem("Extractive QA ❔"): | |
with gr.Row(): | |
with gr.Column(): | |
plot = gr.Plot(get_plots('question_answering.csv')) | |
with gr.Column(): | |
table = gr.Dataframe(get_model_names('question_answering.csv'), datatype="markdown") | |
with gr.TabItem("All Tasks 💡"): | |
with gr.Row(): | |
with gr.Column(): | |
plot = gr.Plot(get_all_plots) | |
with gr.Column(): | |
table = gr.Dataframe(get_all_model_names, datatype="markdown") | |
with gr.Accordion("📙 Citation", open=False): | |
citation_button = gr.Textbox( | |
value=CITATION_BUTTON_TEXT, | |
label=CITATION_BUTTON_LABEL, | |
elem_id="citation-button", | |
lines=10, | |
show_copy_button=True, | |
) | |
gr.Markdown("Last updated: February 2025") | |
demo.launch() | |