Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
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 tasks (CSV filenames) | |
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' | |
] | |
color_map = {"1": "red", "2": "orange", "3": "yellow", "4": "lightgreen", "5": "green"} # Keep color map | |
def format_stars(score): | |
try: | |
score_int = int(score) | |
except Exception: | |
score_int = 0 | |
# Render stars in black with a slightly larger font | |
return f'<span style="color: black !important; font-size:1.5em !important;">{"★" * 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})' | |
# --- Leaderboard Table Functions (Modified to dynamically calculate max energy) --- | |
def create_minimal_bar_html(energy_value_wh, energy_score, max_energy_value): | |
"""Generates HTML for the minimal bar chart with dynamic max energy.""" | |
if max_energy_value <= 0: # Avoid division by zero if max energy is 0 or negative | |
bar_percentage = 0 | |
else: | |
bar_percentage = min(100, (energy_value_wh / max_energy_value) * 100) # Cap at 100% | |
bar_color = color_map.get(str(energy_score), "gray") # Default color if score is unexpected | |
html = f""" | |
<div style="display: flex; align-items: center; gap: 5px;"> | |
<div style="width: {bar_percentage}%; height: 10px; background-color: {bar_color}; border-radius: 2px;"></div> | |
<span>{energy_value_wh:.4f} Wh</span> | |
</div> | |
""" | |
return html | |
def get_model_names(task): | |
df = pd.read_csv('data/energy/' + task) | |
if df.columns[0].startswith("Unnamed:"): | |
df = df.iloc[:, 1:] | |
df['total_gpu_energy'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000 # kWh to Wh conversion | |
df['energy_score'] = df['energy_score'].astype(int) | |
max_energy_for_task = df['total_gpu_energy'].max() # Calculate max energy for this task | |
# Create HTML bar chart for GPU Energy column, passing dynamic max_energy_for_task | |
df['GPU Energy (Wh)'] = df.apply(lambda row: create_minimal_bar_html(row['total_gpu_energy'], row['energy_score'], max_energy_for_task), axis=1) | |
df['Model'] = df['model'].apply(make_link) | |
df['Score'] = df['energy_score'].apply(format_stars) | |
df = df[['Model', 'GPU Energy (Wh)', 'Score']] # Keep only these columns | |
df = df.sort_values(by='total_gpu_energy') # Sort by underlying energy value for table order | |
df = df.drop('total_gpu_energy', axis=1) # remove the original energy column that was used for sorting | |
return df | |
def get_all_model_names(): | |
all_df = pd.DataFrame() | |
max_energy_overall = 0 # Initialize overall max energy | |
for task in tasks: | |
df = pd.read_csv('data/energy/' + task) | |
df['total_gpu_energy'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000 # kWh to Wh conversion | |
df['energy_score'] = df['energy_score'].astype(int) | |
max_energy_overall = max(max_energy_overall, df['total_gpu_energy'].max()) # Update overall max | |
all_df = pd.concat([all_df, df], ignore_index=True) | |
all_df = all_df.drop_duplicates(subset=['model']) | |
# Create HTML bar chart for GPU Energy column, passing dynamic max_energy_overall | |
all_df['GPU Energy (Wh)'] = all_df.apply(lambda row: create_minimal_bar_html(row['total_gpu_energy'], row['energy_score'], max_energy_overall), axis=1) | |
all_df['Model'] = all_df['model'].apply(make_link) | |
all_df['Score'] = all_df['energy_score'].apply(format_stars) | |
all_df = all_df.sort_values(by='total_gpu_energy') # Sort by underlying energy value for table order | |
all_df = all_df.drop('total_gpu_energy', axis=1) # remove the original energy column that was used for sorting | |
return all_df[['Model', 'GPU Energy (Wh)', 'Score']] | |
def get_text_generation_model_names(model_class): | |
df = pd.read_csv('data/energy/text_generation.csv') | |
if df.columns[0].startswith("Unnamed:"): | |
df = df.iloc[:, 1:] | |
if 'class' in df.columns: | |
df = df[df['class'] == model_class] | |
df['total_gpu_energy'] = pd.to_numeric(df['total_gpu_energy'], errors='raise') * 1000 # kWh to Wh conversion | |
df['energy_score'] = df['energy_score'].astype(int) | |
max_energy_for_class = df['total_gpu_energy'].max() # Calculate max energy for this class | |
# Create HTML bar chart for GPU Energy column, passing dynamic max_energy_for_class | |
df['GPU Energy (Wh)'] = df.apply(lambda row: create_minimal_bar_html(row['total_gpu_energy'], row['energy_score'], max_energy_for_class), axis=1) | |
df['Model'] = df['model'].apply(make_link) | |
df['Score'] = df['energy_score'].apply(format_stars) | |
df = df[['Model', 'GPU Energy (Wh)', 'Score']] # Keep only these columns | |
df = df.sort_values(by='total_gpu_energy') # Sort by underlying energy value for table order | |
df = df.drop('total_gpu_energy', axis=1) # remove the original energy column that was used for sorting | |
return df | |
def update_text_generation(model_class): | |
table = get_text_generation_model_names(model_class) | |
return table | |
# --- Build the Gradio Interface (Plots Removed, Tables with Dynamic Bars) --- | |
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; | |
} | |
/* CSS for minimal bar chart inside table cell */ | |
.minimal-bar-container { | |
display: flex; | |
align-items: center; | |
gap: 5px; /* space between bar and text */ | |
} | |
.minimal-bar { | |
height: 10px; | |
background-color: blue; /* default, will be overridden by dynamic color */ | |
border-radius: 2px; | |
} | |
""") | |
with demo: | |
gr.Markdown( | |
"""# AI Energy Score Leaderboard | |
### Welcome to the leaderboard for the [AI Energy Score Project!](https://huggingface.co/AIEnergyScore) | |
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)""" | |
) | |
with gr.Tabs(): | |
# --- Text Generation Tab with Dropdown for Model Class --- | |
with gr.TabItem("Text Generation 💬"): | |
# Dropdown moved above the leaderboard | |
model_class_dropdown = gr.Dropdown(choices=["A", "B", "C"], | |
label="Select Model Class", | |
value="A") | |
tg_table = gr.Dataframe(get_text_generation_model_names("A"), datatype="markdown") # No plot anymore | |
# Update table when the dropdown value changes | |
model_class_dropdown.change(fn=update_text_generation, | |
inputs=model_class_dropdown, | |
outputs=[tg_table]) | |
with gr.TabItem("Image Generation 📷"): | |
table = gr.Dataframe(get_model_names('image_generation.csv'), datatype="markdown") | |
with gr.TabItem("Text Classification 🎭"): | |
table = gr.Dataframe(get_model_names('text_classification.csv'), datatype="markdown") | |
with gr.TabItem("Image Classification 🖼️"): | |
table = gr.Dataframe(get_model_names('image_classification.csv'), datatype="markdown") | |
with gr.TabItem("Image Captioning 📝"): | |
table = gr.Dataframe(get_model_names('image_captioning.csv'), datatype="markdown") | |
with gr.TabItem("Summarization 📃"): | |
table = gr.Dataframe(get_model_names('summarization.csv'), datatype="markdown") | |
with gr.TabItem("Automatic Speech Recognition 💬"): | |
table = gr.Dataframe(get_model_names('asr.csv'), datatype="markdown") | |
with gr.TabItem("Object Detection 🚘"): | |
table = gr.Dataframe(get_model_names('object_detection.csv'), datatype="markdown") | |
with gr.TabItem("Sentence Similarity 📚"): | |
table = gr.Dataframe(get_model_names('sentence_similarity.csv'), datatype="markdown") | |
with gr.TabItem("Extractive QA ❔"): | |
table = gr.Dataframe(get_model_names('question_answering.csv'), datatype="markdown") | |
with gr.TabItem("All Tasks 💡"): | |
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() |