Leaderboard / app.py
bgamazay's picture
Update app.py
d3ee32e verified
raw
history blame
5.2 kB
import os, glob
import pandas as pd
import gradio as gr
from datasets import load_dataset
from huggingface_hub import HfApi
OWNER = "AIEnergyScore"
TOKEN = os.environ.get("DEBUG")
API = HfApi(token=TOKEN)
def get_leaderboard_models():
"""
Reads CSV files from the leaderboard directory and returns a DataFrame
containing the 'model' and 'task' columns.
If no CSV files are found, returns an empty DataFrame with those columns.
"""
path = r'leaderboard_v0_data/energy'
filenames = glob.glob(os.path.join(path, "*.csv"))
data = []
for filename in filenames:
data.append(pd.read_csv(filename))
if not data:
return pd.DataFrame(columns=['model', 'task'])
leaderboard_data = pd.concat(data, ignore_index=True)
return leaderboard_data[['model', 'task']]
def print_existing_models():
"""
Loads a dataset of requests and returns the models that have been benchmarked.
"""
requests = load_dataset("AIEnergyScore/requests_debug", split="test", token=TOKEN)
requests_dset = requests.to_pandas()
model_df = requests_dset[['model', 'status']]
model_df = model_df[model_df['status'] == 'COMPLETED']
return model_df
def highlight_cols(x):
df = x.copy()
df[df['status'] == 'COMPLETED'] = 'color: green'
df[df['status'] == 'PENDING'] = 'color: orange'
df[df['status'] == 'FAILED'] = 'color: red'
return df
# Apply styling to the recently benchmarked models table.
existing_models = print_existing_models()
formatted_df = existing_models.style.apply(highlight_cols, axis=None)
def get_zip_data_link():
"""
Returns an HTML link for downloading logs.
"""
return (
'<a href="https://example.com/download.zip" '
'style="text-decoration: none; font-weight: bold; font-size: 1.1em; '
'color: black; font-family: \'Inter\', sans-serif;">Download Logs</a>'
)
with gr.Blocks() as demo:
# --- Custom CSS for layout and styling ---
gr.HTML('''
<style>
/* Evenly space the header links */
.header-links {
display: flex;
justify-content: space-evenly;
align-items: center;
margin: 10px 0;
}
/* Center the subtitle text */
.centered-subtitle {
text-align: center;
font-size: 1.4em;
margin-bottom: 20px;
}
/* Full width container for matching widget edges */
.full-width {
width: 100% !important;
}
</style>
''')
# --- Header Links ---
with gr.Row(elem_classes="header-links"):
leaderboard_link = gr.HTML(
'<a href="https://huggingface.co/spaces/AIEnergyScore/Leaderboard" '
'style="text-decoration: none; font-weight: bold; font-size: 1.1em; '
'color: black; font-family: \'Inter\', sans-serif;">Leaderboard</a>'
)
submission_link = gr.HTML(
'<a href="https://huggingface.co/spaces/AIEnergyScore/submission_portal" '
'style="text-decoration: none; font-weight: bold; font-size: 1.1em; '
'color: black; font-family: \'Inter\', sans-serif;">Submission Portal</a>'
)
label_link = gr.HTML(
'<a href="https://huggingface.co/spaces/AIEnergyScore/Label" '
'style="text-decoration: none; font-weight: bold; font-size: 1.1em; '
'color: black; font-family: \'Inter\', sans-serif;">Label Generator</a>'
)
faq_link = gr.HTML(
'<a href="https://huggingface.github.io/AIEnergyScore/#faq" '
'style="text-decoration: none; font-weight: bold; font-size: 1.1em; '
'color: black; font-family: \'Inter\', sans-serif;">FAQ</a>'
)
documentation_link = gr.HTML(
'<a href="https://huggingface.github.io/AIEnergyScore/#documentation" '
'style="text-decoration: none; font-weight: bold; font-size: 1.1em; '
'color: black; font-family: \'Inter\', sans-serif;">Documentation</a>'
)
download_link = gr.HTML(get_zip_data_link())
community_link = gr.HTML(
'<a href="https://huggingface.co/spaces/AIEnergyScore/README/discussions" '
'style="text-decoration: none; font-weight: bold; font-size: 1.1em; '
'color: black; font-family: \'Inter\', sans-serif;">Community</a>'
)
# --- Logo (Centered) ---
gr.HTML('''
<div style="text-align: center; margin-top: 20px;">
<img src="https://huggingface.co/spaces/AIEnergyScore/Leaderboard/resolve/main/logo.png"
alt="Logo"
style="max-width: 500px; height: auto;">
</div>
''')
# --- Centered Subtitle ---
gr.Markdown('<p class="centered-subtitle">Welcome to the AI Energy Score Leaderboard. Explore the top-performing models below.</p>')
# --- Leaderboard Tables ---
with gr.Column(elem_classes="full-width"):
with gr.Accordion("Latest Leaderboard", open=True):
gr.Dataframe(get_leaderboard_models(), elem_classes="full-width")
with gr.Accordion("Recently Benchmarked Models", open=False):
gr.Dataframe(formatted_df, elem_classes="full-width")
demo.launch()