Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
| 3 |
|
| 4 |
CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results"
|
| 5 |
CITATION_BUTTON_TEXT = r"""@misc{aienergyscore-leaderboard,
|
|
@@ -37,20 +39,38 @@ def make_link(mname):
|
|
| 37 |
display_name = parts[1] if len(parts) > 1 else mname
|
| 38 |
return f'<a href="https://huggingface.co/{mname}" target="_blank">{display_name}</a>'
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def generate_html_table_from_df(df):
|
| 41 |
"""
|
| 42 |
Given a dataframe with a numeric energy column (gpu_energy_numeric),
|
| 43 |
generate an HTML table with three columns:
|
| 44 |
-
- Model (the link)
|
| 45 |
- GPU Energy (Wh) plus a horizontal bar whose width is proportional
|
| 46 |
to the energy value relative to the maximum in the table.
|
| 47 |
- Score (displayed as stars)
|
| 48 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
max_energy = df['gpu_energy_numeric'].max() if not df.empty else 1
|
| 50 |
color_map = {"1": "red", "2": "orange", "3": "yellow", "4": "lightgreen", "5": "green"}
|
| 51 |
html = '<table style="width:100%; border-collapse: collapse; font-family: Arial, sans-serif;">'
|
| 52 |
-
|
| 53 |
-
html += '<
|
|
|
|
| 54 |
html += '<th style="text-align: left; padding: 8px;">GPU Energy (Wh)</th>'
|
| 55 |
html += '<th style="text-align: left; padding: 8px;">Score</th>'
|
| 56 |
html += '</tr></thead>'
|
|
@@ -63,7 +83,8 @@ def generate_html_table_from_df(df):
|
|
| 63 |
score_val = row['energy_score']
|
| 64 |
bar_color = color_map.get(str(score_val), "gray")
|
| 65 |
html += '<tr>'
|
| 66 |
-
|
|
|
|
| 67 |
html += (
|
| 68 |
f'<td style="padding: 8px;">{energy_str}<br>'
|
| 69 |
f'<div style="background-color: {bar_color}; width: {bar_width:.1f}%; height: 10px;"></div></td>'
|
|
@@ -73,6 +94,17 @@ def generate_html_table_from_df(df):
|
|
| 73 |
html += '</tbody></table>'
|
| 74 |
return html
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# --- Modified functions to include a sort_order parameter ---
|
| 77 |
def get_model_names_html(task, sort_order="High to Low"):
|
| 78 |
df = pd.read_csv('data/energy/' + task)
|
|
@@ -181,17 +213,14 @@ with demo:
|
|
| 181 |
### Welcome to the leaderboard for the [AI Energy Score Project!](https://huggingface.co/AIEnergyScore) — Select different tasks to see scored models."""
|
| 182 |
)
|
| 183 |
|
| 184 |
-
# Header links
|
| 185 |
-
gr.
|
| 186 |
-
|
| 187 |
-
<a href="https://huggingface.co/spaces/AIEnergyScore/
|
| 188 |
-
|
| 189 |
-
<a href="https://huggingface.github.io/AIEnergyScore/#faq" style="margin: 0 15px; text-decoration: none; font-weight: bold; font-size: 1.1em;">FAQ</a>
|
| 190 |
-
<a href="https://huggingface.github.io/AIEnergyScore/#documentation" style="margin: 0 15px; text-decoration: none; font-weight: bold; font-size: 1.1em;">Documentation</a>
|
| 191 |
-
<a href="https://huggingface.co/spaces/AIEnergyScore/README/discussions" style="margin: 0 15px; text-decoration: none; font-weight: bold; font-size: 1.1em;">Community</a>
|
| 192 |
-
|
| 193 |
-
</div>
|
| 194 |
-
''')
|
| 195 |
|
| 196 |
with gr.Tabs():
|
| 197 |
# --- Text Generation Tab ---
|
|
@@ -327,4 +356,4 @@ with demo:
|
|
| 327 |
)
|
| 328 |
gr.Markdown("""Last updated: February 2025""")
|
| 329 |
|
| 330 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import zipfile
|
| 5 |
|
| 6 |
CITATION_BUTTON_LABEL = "Copy the following snippet to cite these results"
|
| 7 |
CITATION_BUTTON_TEXT = r"""@misc{aienergyscore-leaderboard,
|
|
|
|
| 39 |
display_name = parts[1] if len(parts) > 1 else mname
|
| 40 |
return f'<a href="https://huggingface.co/{mname}" target="_blank">{display_name}</a>'
|
| 41 |
|
| 42 |
+
def extract_link_text(html_link):
|
| 43 |
+
"""Extracts the inner text from an HTML link."""
|
| 44 |
+
start = html_link.find('>') + 1
|
| 45 |
+
end = html_link.rfind('</a>')
|
| 46 |
+
if start > 0 and end > start:
|
| 47 |
+
return html_link[start:end]
|
| 48 |
+
else:
|
| 49 |
+
return html_link
|
| 50 |
+
|
| 51 |
def generate_html_table_from_df(df):
|
| 52 |
"""
|
| 53 |
Given a dataframe with a numeric energy column (gpu_energy_numeric),
|
| 54 |
generate an HTML table with three columns:
|
| 55 |
+
- Model (the link, with a fixed width based on the longest model name)
|
| 56 |
- GPU Energy (Wh) plus a horizontal bar whose width is proportional
|
| 57 |
to the energy value relative to the maximum in the table.
|
| 58 |
- Score (displayed as stars)
|
| 59 |
"""
|
| 60 |
+
# Compute a static width (in pixels) for the Model column based on the longest model name.
|
| 61 |
+
if not df.empty:
|
| 62 |
+
max_length = max(len(extract_link_text(link)) for link in df['Model'])
|
| 63 |
+
else:
|
| 64 |
+
max_length = 10
|
| 65 |
+
# Multiply by an estimated average character width (10 pixels) and add some extra padding.
|
| 66 |
+
static_width = max_length * 10 + 16
|
| 67 |
+
|
| 68 |
max_energy = df['gpu_energy_numeric'].max() if not df.empty else 1
|
| 69 |
color_map = {"1": "red", "2": "orange", "3": "yellow", "4": "lightgreen", "5": "green"}
|
| 70 |
html = '<table style="width:100%; border-collapse: collapse; font-family: Arial, sans-serif;">'
|
| 71 |
+
# Apply the static width to the Model column header.
|
| 72 |
+
html += f'<thead><tr style="background-color: #f2f2f2;">'
|
| 73 |
+
html += f'<th style="text-align: left; padding: 8px; width: {static_width}px;">Model</th>'
|
| 74 |
html += '<th style="text-align: left; padding: 8px;">GPU Energy (Wh)</th>'
|
| 75 |
html += '<th style="text-align: left; padding: 8px;">Score</th>'
|
| 76 |
html += '</tr></thead>'
|
|
|
|
| 83 |
score_val = row['energy_score']
|
| 84 |
bar_color = color_map.get(str(score_val), "gray")
|
| 85 |
html += '<tr>'
|
| 86 |
+
# Apply the static width to the Model column cell.
|
| 87 |
+
html += f'<td style="padding: 8px; width: {static_width}px;">{row["Model"]}</td>'
|
| 88 |
html += (
|
| 89 |
f'<td style="padding: 8px;">{energy_str}<br>'
|
| 90 |
f'<div style="background-color: {bar_color}; width: {bar_width:.1f}%; height: 10px;"></div></td>'
|
|
|
|
| 94 |
html += '</tbody></table>'
|
| 95 |
return html
|
| 96 |
|
| 97 |
+
# --- Function to zip all CSV files ---
|
| 98 |
+
def zip_csv_files():
|
| 99 |
+
data_dir = "data/energy"
|
| 100 |
+
zip_filename = "data.zip"
|
| 101 |
+
with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as zipf:
|
| 102 |
+
for filename in os.listdir(data_dir):
|
| 103 |
+
if filename.endswith(".csv"):
|
| 104 |
+
filepath = os.path.join(data_dir, filename)
|
| 105 |
+
zipf.write(filepath, arcname=filename)
|
| 106 |
+
return zip_filename
|
| 107 |
+
|
| 108 |
# --- Modified functions to include a sort_order parameter ---
|
| 109 |
def get_model_names_html(task, sort_order="High to Low"):
|
| 110 |
df = pd.read_csv('data/energy/' + task)
|
|
|
|
| 213 |
### Welcome to the leaderboard for the [AI Energy Score Project!](https://huggingface.co/AIEnergyScore) — Select different tasks to see scored models."""
|
| 214 |
)
|
| 215 |
|
| 216 |
+
# Header links (now using a row of components, including a Download Data button)
|
| 217 |
+
with gr.Row():
|
| 218 |
+
submission_link = gr.HTML('<a href="https://huggingface.co/spaces/AIEnergyScore/submission_portal" style="margin: 0 15px; text-decoration: none; font-weight: bold; font-size: 1.1em;">Submission Portal</a>')
|
| 219 |
+
label_link = gr.HTML('<a href="https://huggingface.co/spaces/AIEnergyScore/Label" style="margin: 0 15px; text-decoration: none; font-weight: bold; font-size: 1.1em;">Label Generator</a>')
|
| 220 |
+
download_button = gr.DownloadButton("Download Data", fn=zip_csv_files, file_name="data.zip", variant="link", style={"margin": "0 15px", "font-weight": "bold", "font-size": "1.1em"})
|
| 221 |
+
faq_link = gr.HTML('<a href="https://huggingface.github.io/AIEnergyScore/#faq" style="margin: 0 15px; text-decoration: none; font-weight: bold; font-size: 1.1em;">FAQ</a>')
|
| 222 |
+
documentation_link = gr.HTML('<a href="https://huggingface.github.io/AIEnergyScore/#documentation" style="margin: 0 15px; text-decoration: none; font-weight: bold; font-size: 1.1em;">Documentation</a>')
|
| 223 |
+
community_link = gr.HTML('<a href="https://huggingface.co/spaces/AIEnergyScore/README/discussions" style="margin: 0 15px; text-decoration: none; font-weight: bold; font-size: 1.1em;">Community</a>')
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
with gr.Tabs():
|
| 226 |
# --- Text Generation Tab ---
|
|
|
|
| 356 |
)
|
| 357 |
gr.Markdown("""Last updated: February 2025""")
|
| 358 |
|
| 359 |
+
demo.launch()
|