Update app.py
Browse files
app.py
CHANGED
|
@@ -2,124 +2,123 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
-
# ---
|
| 6 |
REPO_ID = "DontPlanToEnd/UGI-Leaderboard"
|
| 7 |
FILENAME = "ugi-leaderboard-data.csv"
|
| 8 |
|
| 9 |
-
# --- Constants for UI ---
|
| 10 |
-
TITLE = "π UGI Index Leaderboard"
|
| 11 |
-
DESCRIPTION = """
|
| 12 |
-
This is a modified version of the [UGI Leaderboard](https://huggingface.co/spaces/DontPlanToEnd/UGI-Leaderboard) that introduces the **UGI Index**.
|
| 13 |
-
|
| 14 |
-
### π The UGI Index
|
| 15 |
-
The **UGI Index** is a custom score calculated to balance intelligence, uncensored knowledge, and willingness to answer.
|
| 16 |
-
**Formula:** `(UGI Score + NatInt Score) * (W/10 Score)`
|
| 17 |
-
|
| 18 |
-
---
|
| 19 |
-
**Original Metrics:**
|
| 20 |
-
* **UGI (Uncensored General Intelligence):** Measures knowledge of sensitive topics.
|
| 21 |
-
* **NatInt (Natural Intelligence):** Measures general reasoning and knowledge.
|
| 22 |
-
* **W/10 (Willingness):** Measures how far a model can be pushed before refusing.
|
| 23 |
-
"""
|
| 24 |
-
|
| 25 |
def make_clickable_model(model_name, link):
|
| 26 |
-
"""
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
return model_name
|
| 29 |
-
return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline;text-decoration-style: dotted;">{model_name}</a>'
|
| 30 |
|
| 31 |
def get_leaderboard_data():
|
| 32 |
"""
|
| 33 |
-
Fetches
|
| 34 |
"""
|
| 35 |
try:
|
| 36 |
-
# 1. Download CSV
|
| 37 |
file_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, repo_type="space")
|
| 38 |
df = pd.read_csv(file_path)
|
| 39 |
|
| 40 |
-
# 2. Clean
|
| 41 |
-
# The original CSV has trailing spaces (e.g., "UGI ", "W/10 ")
|
| 42 |
-
# We strip them for easier processing but keep a map if needed
|
| 43 |
df.columns = df.columns.str.strip()
|
| 44 |
|
| 45 |
-
# 3.
|
| 46 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
ugi_col = next((c for c in df.columns if "UGI" in c and "Index" not in c), "UGI")
|
| 48 |
natint_col = next((c for c in df.columns if "NatInt" in c), "NatInt")
|
| 49 |
w10_col = next((c for c in df.columns if "W/10" in c), "W/10")
|
| 50 |
-
|
| 51 |
-
# 4. Calculate UGI Index
|
| 52 |
-
# Formula: (UGI + NatInt) * (W/10)
|
| 53 |
-
# Convert to numeric, coercing errors to 0 or NaN to prevent crashes
|
| 54 |
-
c_ugi = pd.to_numeric(df[ugi_col], errors='coerce').fillna(0)
|
| 55 |
-
c_natint = pd.to_numeric(df[natint_col], errors='coerce').fillna(0)
|
| 56 |
-
c_w10 = pd.to_numeric(df[w10_col], errors='coerce').fillna(0)
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
df['UGI Index'] = df['UGI Index'].round(2)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
df = df.drop(columns=['Link'])
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
# Sort by UGI Index descending
|
| 70 |
df = df.sort_values(by='UGI Index', ascending=False)
|
| 71 |
|
| 72 |
-
#
|
| 73 |
df.insert(0, 'Rank', range(1, len(df) + 1))
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
#
|
| 77 |
priority_cols = ['Rank', 'Model', 'UGI Index', ugi_col, natint_col, w10_col]
|
| 78 |
-
|
| 79 |
-
# Get all other columns that exist in the dataframe
|
| 80 |
other_cols = [c for c in df.columns if c not in priority_cols]
|
| 81 |
|
| 82 |
-
|
| 83 |
-
final_cols = priority_cols + other_cols
|
| 84 |
-
df = df[final_cols]
|
| 85 |
|
| 86 |
-
return
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
-
return pd.DataFrame({"Error": [f"
|
| 90 |
|
| 91 |
-
def
|
|
|
|
|
|
|
|
|
|
| 92 |
df = get_leaderboard_data()
|
| 93 |
if query:
|
| 94 |
-
#
|
| 95 |
df = df[df['Model'].astype(str).str.contains(query, case=False, na=False)]
|
| 96 |
return df
|
| 97 |
|
| 98 |
-
# ---
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
with gr.Row():
|
| 104 |
-
|
| 105 |
-
label="Search
|
| 106 |
-
placeholder="
|
| 107 |
-
|
| 108 |
)
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
| 112 |
-
# datatype="markdown" is crucial for rendering the HTML links
|
| 113 |
-
leaderboard_table = gr.Dataframe(
|
| 114 |
value=get_leaderboard_data,
|
| 115 |
-
datatype="markdown",
|
| 116 |
interactive=False,
|
| 117 |
wrap=True
|
| 118 |
)
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
|
| 122 |
-
|
| 123 |
|
| 124 |
-
|
| 125 |
-
demo.launch()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
+
# --- Constants ---
|
| 6 |
REPO_ID = "DontPlanToEnd/UGI-Leaderboard"
|
| 7 |
FILENAME = "ugi-leaderboard-data.csv"
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def make_clickable_model(model_name, link):
|
| 10 |
+
"""
|
| 11 |
+
Wraps the model name in an HTML anchor tag if a link exists.
|
| 12 |
+
"""
|
| 13 |
+
if pd.isna(link) or link == "" or str(link).lower() == "nan":
|
| 14 |
return model_name
|
| 15 |
+
return f'<a target="_blank" href="{link}" style="color: var(--link-text-color); text-decoration: underline; text-decoration-style: dotted;">{model_name}</a>'
|
| 16 |
|
| 17 |
def get_leaderboard_data():
|
| 18 |
"""
|
| 19 |
+
Fetches the CSV, standardizes column names, calculates the UGI Index, and formats the table.
|
| 20 |
"""
|
| 21 |
try:
|
| 22 |
+
# 1. Download the CSV
|
| 23 |
file_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, repo_type="space")
|
| 24 |
df = pd.read_csv(file_path)
|
| 25 |
|
| 26 |
+
# 2. Clean up column names (remove whitespace)
|
|
|
|
|
|
|
| 27 |
df.columns = df.columns.str.strip()
|
| 28 |
|
| 29 |
+
# 3. Standardize the 'Model' column name
|
| 30 |
+
# Find whatever column looks like "Model" and rename it to "Model" to prevent KeyErrors
|
| 31 |
+
model_col_candidate = next((c for c in df.columns if "Model" in c), None)
|
| 32 |
+
if model_col_candidate:
|
| 33 |
+
df.rename(columns={model_col_candidate: "Model"}, inplace=True)
|
| 34 |
+
else:
|
| 35 |
+
# Fallback if no model column is found
|
| 36 |
+
df['Model'] = "Unknown"
|
| 37 |
+
|
| 38 |
+
# 4. Identify other specific columns dynamically
|
| 39 |
ugi_col = next((c for c in df.columns if "UGI" in c and "Index" not in c), "UGI")
|
| 40 |
natint_col = next((c for c in df.columns if "NatInt" in c), "NatInt")
|
| 41 |
w10_col = next((c for c in df.columns if "W/10" in c), "W/10")
|
| 42 |
+
link_col = next((c for c in df.columns if "Link" in c), None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# 5. Ensure numeric data for calculations
|
| 45 |
+
for col in [ugi_col, natint_col, w10_col]:
|
| 46 |
+
# Force convert to numeric, turning errors (like strings) into NaN, then fill with 0
|
| 47 |
+
df[col] = pd.to_numeric(df[col], errors='coerce').fillna(0)
|
| 48 |
+
|
| 49 |
+
# 6. Calculate the UGI Index
|
| 50 |
+
# Formula: (UGI + NatInt) * (W/10)
|
| 51 |
+
df['UGI Index'] = (df[ugi_col] + df[natint_col]) * df[w10_col]
|
| 52 |
df['UGI Index'] = df['UGI Index'].round(2)
|
| 53 |
|
| 54 |
+
# 7. Make Model Names Clickable
|
| 55 |
+
if link_col and link_col in df.columns:
|
| 56 |
+
df['Model'] = df.apply(lambda x: make_clickable_model(x['Model'], x[link_col]), axis=1)
|
| 57 |
+
# Remove the Link column so it doesn't show up twice
|
| 58 |
+
df = df.drop(columns=[link_col])
|
|
|
|
| 59 |
|
| 60 |
+
# 8. Sort by UGI Index (Descending)
|
|
|
|
| 61 |
df = df.sort_values(by='UGI Index', ascending=False)
|
| 62 |
|
| 63 |
+
# 9. Add Ranking
|
| 64 |
df.insert(0, 'Rank', range(1, len(df) + 1))
|
| 65 |
|
| 66 |
+
# 10. Reorder Columns
|
| 67 |
+
# Ensure these main columns come first
|
| 68 |
priority_cols = ['Rank', 'Model', 'UGI Index', ugi_col, natint_col, w10_col]
|
| 69 |
+
# Add whatever other columns exist in the CSV
|
|
|
|
| 70 |
other_cols = [c for c in df.columns if c not in priority_cols]
|
| 71 |
|
| 72 |
+
final_df = df[priority_cols + other_cols]
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
return final_df
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
+
return pd.DataFrame({"Error": [f"An error occurred: {str(e)}"]})
|
| 78 |
|
| 79 |
+
def search_models(query):
|
| 80 |
+
"""
|
| 81 |
+
Filters the dataframe based on the search query.
|
| 82 |
+
"""
|
| 83 |
df = get_leaderboard_data()
|
| 84 |
if query:
|
| 85 |
+
# We can now safely access 'Model' because we renamed it in the data loading step
|
| 86 |
df = df[df['Model'].astype(str).str.contains(query, case=False, na=False)]
|
| 87 |
return df
|
| 88 |
|
| 89 |
+
# --- Gradio Interface ---
|
| 90 |
+
custom_css = """
|
| 91 |
+
footer {visibility: hidden}
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
with gr.Blocks(css=custom_css, title="UGI Index Leaderboard") as demo:
|
| 95 |
+
gr.Markdown("# π UGI Index Leaderboard")
|
| 96 |
+
gr.Markdown("""
|
| 97 |
+
**A reordered view of the [UGI Leaderboard](https://huggingface.co/spaces/DontPlanToEnd/UGI-Leaderboard) based on the UGI Index.**
|
| 98 |
|
| 99 |
+
The **UGI Index** combines uncensored knowledge, general intelligence, and willingness to answer.
|
| 100 |
+
|
| 101 |
+
$$ \\text{UGI Index} = (\\text{UGI Score} + \\text{NatInt Score}) \\times \\text{W/10 Score} $$
|
| 102 |
+
""")
|
| 103 |
+
|
| 104 |
with gr.Row():
|
| 105 |
+
search_bar = gr.Textbox(
|
| 106 |
+
label="Search",
|
| 107 |
+
placeholder="Search for a model name...",
|
| 108 |
+
show_label=False
|
| 109 |
)
|
| 110 |
+
refresh_btn = gr.Button("Refresh Leaderboard")
|
| 111 |
|
| 112 |
+
leaderboard = gr.Dataframe(
|
|
|
|
|
|
|
| 113 |
value=get_leaderboard_data,
|
| 114 |
+
datatype="markdown", # Essential for rendering HTML links
|
| 115 |
interactive=False,
|
| 116 |
wrap=True
|
| 117 |
)
|
| 118 |
|
| 119 |
+
# Event Listeners
|
| 120 |
+
search_bar.change(fn=search_models, inputs=search_bar, outputs=leaderboard)
|
| 121 |
+
refresh_btn.click(fn=get_leaderboard_data, outputs=leaderboard)
|
| 122 |
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
demo.launch()
|