Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import pandas as pd
|
|
| 3 |
import numpy as np
|
| 4 |
from functools import partial
|
| 5 |
from gradio_rangeslider import RangeSlider
|
|
|
|
| 6 |
|
| 7 |
custom_css = """
|
| 8 |
.tab-nav button {
|
|
@@ -42,12 +43,32 @@ custom_css = """
|
|
| 42 |
UGI_COLS = ['#P', 'Model', 'UGI π', 'W/10 π', 'Unruly', 'Internet', 'Stats', 'Writing', 'PolContro']
|
| 43 |
WRITING_STYLE_COLS = ['#P', 'Model', 'Reg+MyScore π', 'Reg+Int π', 'MyScore π', 'ASSSβ¬οΈ', 'SMOGβ¬οΈ', 'Yuleβ¬οΈ']
|
| 44 |
ANIME_RATING_COLS = ['#P', 'Model', 'Score π', 'Dif', 'Cor', 'Std']
|
|
|
|
| 45 |
|
| 46 |
# Load the leaderboard data from a CSV file
|
| 47 |
def load_leaderboard_data(csv_file_path):
|
| 48 |
try:
|
| 49 |
df = pd.read_csv(csv_file_path)
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
df.drop(columns=['Link'], inplace=True)
|
| 52 |
|
| 53 |
# Round numeric columns to 3 decimal places
|
|
@@ -61,30 +82,30 @@ def load_leaderboard_data(csv_file_path):
|
|
| 61 |
return df
|
| 62 |
except Exception as e:
|
| 63 |
print(f"Error loading CSV file: {e}")
|
| 64 |
-
return pd.DataFrame(columns=UGI_COLS + WRITING_STYLE_COLS + ANIME_RATING_COLS)
|
| 65 |
|
| 66 |
# Update the leaderboard table based on the search query and parameter range filters
|
| 67 |
-
def update_table(df: pd.DataFrame, query: str, param_ranges: list, columns: list, w10_range: tuple) -> pd.DataFrame:
|
| 68 |
filtered_df = df.copy()
|
| 69 |
if param_ranges:
|
| 70 |
param_mask = pd.Series(False, index=filtered_df.index)
|
| 71 |
for param_range in param_ranges:
|
| 72 |
if param_range == '~2':
|
| 73 |
-
param_mask |= (filtered_df['Params'] < 2.5)
|
| 74 |
elif param_range == '~4':
|
| 75 |
-
param_mask |= ((filtered_df['Params'] >= 2.5) & (filtered_df['Params'] < 6))
|
| 76 |
elif param_range == '~8':
|
| 77 |
-
param_mask |= ((filtered_df['Params'] >= 6) & (filtered_df['Params'] < 9.5))
|
| 78 |
elif param_range == '~13':
|
| 79 |
-
param_mask |= ((filtered_df['Params'] >= 9.5) & (filtered_df['Params'] < 16))
|
| 80 |
elif param_range == '~20':
|
| 81 |
-
param_mask |= ((filtered_df['Params'] >= 16) & (filtered_df['Params'] < 28))
|
| 82 |
elif param_range == '~34':
|
| 83 |
-
param_mask |= ((filtered_df['Params'] >= 28) & (filtered_df['Params'] < 40))
|
| 84 |
elif param_range == '~50':
|
| 85 |
-
param_mask |= ((filtered_df['Params'] >= 40) & (filtered_df['Params'] < 65))
|
| 86 |
elif param_range == '~70+':
|
| 87 |
-
param_mask |= (filtered_df['Params'] >= 65)
|
| 88 |
filtered_df = filtered_df[param_mask]
|
| 89 |
|
| 90 |
if query:
|
|
@@ -94,6 +115,17 @@ def update_table(df: pd.DataFrame, query: str, param_ranges: list, columns: list
|
|
| 94 |
if 'W/10 π' in filtered_df.columns:
|
| 95 |
filtered_df = filtered_df[(filtered_df['W/10 π'] >= w10_range[0]) & (filtered_df['W/10 π'] <= w10_range[1])]
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return filtered_df[columns]
|
| 98 |
|
| 99 |
# Define the Gradio interface
|
|
@@ -129,13 +161,21 @@ with GraInter:
|
|
| 129 |
)
|
| 130 |
with gr.Column(scale=2):
|
| 131 |
w10_range = RangeSlider(minimum=0, maximum=10, value=(0, 10), step=0.1, label="W/10 Range")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
# Load the initial leaderboard data
|
| 134 |
leaderboard_df = load_leaderboard_data("ugi-leaderboard-data.csv")
|
| 135 |
|
| 136 |
with gr.Tabs():
|
| 137 |
with gr.TabItem("UGI-Leaderboard"):
|
| 138 |
-
datatypes_ugi = ['html' if col == 'Model' else 'str' for col in UGI_COLS]
|
| 139 |
leaderboard_table_ugi = gr.Dataframe(
|
| 140 |
value=leaderboard_df[UGI_COLS],
|
| 141 |
datatype=datatypes_ugi,
|
|
@@ -170,7 +210,7 @@ with GraInter:
|
|
| 170 |
|
| 171 |
with gr.TabItem("Writing Style"):
|
| 172 |
leaderboard_df_ws = leaderboard_df.sort_values(by='Reg+MyScore π', ascending=False)
|
| 173 |
-
datatypes_ws = ['html' if col == 'Model' else 'str' for col in WRITING_STYLE_COLS]
|
| 174 |
leaderboard_table_ws = gr.Dataframe(
|
| 175 |
value=leaderboard_df_ws[WRITING_STYLE_COLS],
|
| 176 |
datatype=datatypes_ws,
|
|
@@ -210,7 +250,7 @@ with GraInter:
|
|
| 210 |
leaderboard_df_arp_na = leaderboard_df_arp[leaderboard_df_arp[['Dif', 'Cor']].isna().any(axis=1)]
|
| 211 |
leaderboard_df_arp = leaderboard_df_arp[~leaderboard_df_arp[['Dif', 'Cor']].isna().any(axis=1)]
|
| 212 |
|
| 213 |
-
datatypes_arp = ['html' if col == 'Model' else 'str' for col in ANIME_RATING_COLS]
|
| 214 |
|
| 215 |
leaderboard_table_arp = gr.Dataframe(
|
| 216 |
value=leaderboard_df_arp[ANIME_RATING_COLS],
|
|
@@ -248,36 +288,42 @@ with GraInter:
|
|
| 248 |
**NA:** When models either reply with one number for every anime, give ratings not between 1 and 10, or don't give every anime in the list a rating.
|
| 249 |
""")
|
| 250 |
|
| 251 |
-
def update_all_tables(query, param_ranges, w10_range):
|
| 252 |
-
ugi_table = update_table(leaderboard_df, query, param_ranges, UGI_COLS, w10_range)
|
| 253 |
|
| 254 |
ws_df = leaderboard_df.sort_values(by='Reg+MyScore π', ascending=False)
|
| 255 |
-
ws_table = update_table(ws_df, query, param_ranges, WRITING_STYLE_COLS, w10_range)
|
| 256 |
|
| 257 |
arp_df = leaderboard_df.sort_values(by='Score π', ascending=False)
|
| 258 |
arp_df_na = arp_df[arp_df[['Dif', 'Cor']].isna().any(axis=1)]
|
| 259 |
arp_df = arp_df[~arp_df[['Dif', 'Cor']].isna().any(axis=1)]
|
| 260 |
|
| 261 |
-
arp_table = update_table(arp_df, query, param_ranges, ANIME_RATING_COLS, w10_range)
|
| 262 |
-
arp_na_table = update_table(arp_df_na, query, param_ranges, ANIME_RATING_COLS, w10_range).fillna('NA')
|
| 263 |
|
| 264 |
return ugi_table, ws_table, arp_table, arp_na_table
|
| 265 |
|
| 266 |
search_bar.change(
|
| 267 |
fn=update_all_tables,
|
| 268 |
-
inputs=[search_bar, filter_columns_size, w10_range],
|
| 269 |
outputs=[leaderboard_table_ugi, leaderboard_table_ws, leaderboard_table_arp, leaderboard_table_arp_na]
|
| 270 |
)
|
| 271 |
|
| 272 |
filter_columns_size.change(
|
| 273 |
fn=update_all_tables,
|
| 274 |
-
inputs=[search_bar, filter_columns_size, w10_range],
|
| 275 |
outputs=[leaderboard_table_ugi, leaderboard_table_ws, leaderboard_table_arp, leaderboard_table_arp_na]
|
| 276 |
)
|
| 277 |
|
| 278 |
w10_range.change(
|
| 279 |
fn=update_all_tables,
|
| 280 |
-
inputs=[search_bar, filter_columns_size, w10_range],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
outputs=[leaderboard_table_ugi, leaderboard_table_ws, leaderboard_table_arp, leaderboard_table_arp_na]
|
| 282 |
)
|
| 283 |
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from functools import partial
|
| 5 |
from gradio_rangeslider import RangeSlider
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
|
| 8 |
custom_css = """
|
| 9 |
.tab-nav button {
|
|
|
|
| 43 |
UGI_COLS = ['#P', 'Model', 'UGI π', 'W/10 π', 'Unruly', 'Internet', 'Stats', 'Writing', 'PolContro']
|
| 44 |
WRITING_STYLE_COLS = ['#P', 'Model', 'Reg+MyScore π', 'Reg+Int π', 'MyScore π', 'ASSSβ¬οΈ', 'SMOGβ¬οΈ', 'Yuleβ¬οΈ']
|
| 45 |
ANIME_RATING_COLS = ['#P', 'Model', 'Score π', 'Dif', 'Cor', 'Std']
|
| 46 |
+
ADDITIONAL_COLS = ['Release Date', 'Date Added', 'Active Params', 'Total Params']
|
| 47 |
|
| 48 |
# Load the leaderboard data from a CSV file
|
| 49 |
def load_leaderboard_data(csv_file_path):
|
| 50 |
try:
|
| 51 |
df = pd.read_csv(csv_file_path)
|
| 52 |
+
|
| 53 |
+
# Convert date columns to datetime
|
| 54 |
+
for col in ['Release Date', 'Date Added']:
|
| 55 |
+
df[col] = pd.to_datetime(df[col], errors='coerce')
|
| 56 |
+
|
| 57 |
+
# Calculate the date two weeks ago from today
|
| 58 |
+
two_weeks_ago = datetime.now() - timedelta(days=9)
|
| 59 |
+
|
| 60 |
+
# Add π to the model name if Date Added is within the last two weeks
|
| 61 |
+
df['Model'] = df.apply(
|
| 62 |
+
lambda row: f'π {row["Model"]}' if pd.notna(row["Date Added"]) and row["Date Added"] >= two_weeks_ago else row["Model"],
|
| 63 |
+
axis=1
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Add hyperlink to the model name
|
| 67 |
+
df['Model'] = df.apply(
|
| 68 |
+
lambda row: f'<a href="{row["Link"]}" target="_blank" style="color: blue; text-decoration: none;">{row["Model"]}</a>' if pd.notna(row["Link"]) else row["Model"],
|
| 69 |
+
axis=1
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
df.drop(columns=['Link'], inplace=True)
|
| 73 |
|
| 74 |
# Round numeric columns to 3 decimal places
|
|
|
|
| 82 |
return df
|
| 83 |
except Exception as e:
|
| 84 |
print(f"Error loading CSV file: {e}")
|
| 85 |
+
return pd.DataFrame(columns=UGI_COLS + WRITING_STYLE_COLS + ANIME_RATING_COLS + ADDITIONAL_COLS)
|
| 86 |
|
| 87 |
# Update the leaderboard table based on the search query and parameter range filters
|
| 88 |
+
def update_table(df: pd.DataFrame, query: str, param_ranges: list, columns: list, w10_range: tuple, additional_cols: list) -> pd.DataFrame:
|
| 89 |
filtered_df = df.copy()
|
| 90 |
if param_ranges:
|
| 91 |
param_mask = pd.Series(False, index=filtered_df.index)
|
| 92 |
for param_range in param_ranges:
|
| 93 |
if param_range == '~2':
|
| 94 |
+
param_mask |= (filtered_df['Total Params'] < 2.5)
|
| 95 |
elif param_range == '~4':
|
| 96 |
+
param_mask |= ((filtered_df['Total Params'] >= 2.5) & (filtered_df['Total Params'] < 6))
|
| 97 |
elif param_range == '~8':
|
| 98 |
+
param_mask |= ((filtered_df['Total Params'] >= 6) & (filtered_df['Total Params'] < 9.5))
|
| 99 |
elif param_range == '~13':
|
| 100 |
+
param_mask |= ((filtered_df['Total Params'] >= 9.5) & (filtered_df['Total Params'] < 16))
|
| 101 |
elif param_range == '~20':
|
| 102 |
+
param_mask |= ((filtered_df['Total Params'] >= 16) & (filtered_df['Total Params'] < 28))
|
| 103 |
elif param_range == '~34':
|
| 104 |
+
param_mask |= ((filtered_df['Total Params'] >= 28) & (filtered_df['Total Params'] < 40))
|
| 105 |
elif param_range == '~50':
|
| 106 |
+
param_mask |= ((filtered_df['Total Params'] >= 40) & (filtered_df['Total Params'] < 65))
|
| 107 |
elif param_range == '~70+':
|
| 108 |
+
param_mask |= (filtered_df['Total Params'] >= 65)
|
| 109 |
filtered_df = filtered_df[param_mask]
|
| 110 |
|
| 111 |
if query:
|
|
|
|
| 115 |
if 'W/10 π' in filtered_df.columns:
|
| 116 |
filtered_df = filtered_df[(filtered_df['W/10 π'] >= w10_range[0]) & (filtered_df['W/10 π'] <= w10_range[1])]
|
| 117 |
|
| 118 |
+
# Add selected additional columns
|
| 119 |
+
columns = columns + [col for col in additional_cols if col in ADDITIONAL_COLS]
|
| 120 |
+
|
| 121 |
+
# Ensure date columns are sorted as dates and then formatted as strings
|
| 122 |
+
if 'Release Date' in columns:
|
| 123 |
+
filtered_df['Release Date'] = pd.to_datetime(filtered_df['Release Date'], errors='coerce')
|
| 124 |
+
filtered_df['Release Date'] = filtered_df['Release Date'].dt.strftime('%Y-%m-%d')
|
| 125 |
+
if 'Date Added' in columns:
|
| 126 |
+
filtered_df['Date Added'] = pd.to_datetime(filtered_df['Date Added'], errors='coerce')
|
| 127 |
+
filtered_df['Date Added'] = filtered_df['Date Added'].dt.strftime('%Y-%m-%d')
|
| 128 |
+
|
| 129 |
return filtered_df[columns]
|
| 130 |
|
| 131 |
# Define the Gradio interface
|
|
|
|
| 161 |
)
|
| 162 |
with gr.Column(scale=2):
|
| 163 |
w10_range = RangeSlider(minimum=0, maximum=10, value=(0, 10), step=0.1, label="W/10 Range")
|
| 164 |
+
with gr.Row():
|
| 165 |
+
additional_columns = gr.CheckboxGroup(
|
| 166 |
+
label="Additional Columns",
|
| 167 |
+
choices=ADDITIONAL_COLS,
|
| 168 |
+
value=[],
|
| 169 |
+
interactive=True,
|
| 170 |
+
elem_id="additional-columns",
|
| 171 |
+
)
|
| 172 |
|
| 173 |
# Load the initial leaderboard data
|
| 174 |
leaderboard_df = load_leaderboard_data("ugi-leaderboard-data.csv")
|
| 175 |
|
| 176 |
with gr.Tabs():
|
| 177 |
with gr.TabItem("UGI-Leaderboard"):
|
| 178 |
+
datatypes_ugi = ['html' if col == 'Model' else 'str' for col in UGI_COLS + ADDITIONAL_COLS]
|
| 179 |
leaderboard_table_ugi = gr.Dataframe(
|
| 180 |
value=leaderboard_df[UGI_COLS],
|
| 181 |
datatype=datatypes_ugi,
|
|
|
|
| 210 |
|
| 211 |
with gr.TabItem("Writing Style"):
|
| 212 |
leaderboard_df_ws = leaderboard_df.sort_values(by='Reg+MyScore π', ascending=False)
|
| 213 |
+
datatypes_ws = ['html' if col == 'Model' else 'str' for col in WRITING_STYLE_COLS + ADDITIONAL_COLS]
|
| 214 |
leaderboard_table_ws = gr.Dataframe(
|
| 215 |
value=leaderboard_df_ws[WRITING_STYLE_COLS],
|
| 216 |
datatype=datatypes_ws,
|
|
|
|
| 250 |
leaderboard_df_arp_na = leaderboard_df_arp[leaderboard_df_arp[['Dif', 'Cor']].isna().any(axis=1)]
|
| 251 |
leaderboard_df_arp = leaderboard_df_arp[~leaderboard_df_arp[['Dif', 'Cor']].isna().any(axis=1)]
|
| 252 |
|
| 253 |
+
datatypes_arp = ['html' if col == 'Model' else 'str' for col in ANIME_RATING_COLS + ADDITIONAL_COLS]
|
| 254 |
|
| 255 |
leaderboard_table_arp = gr.Dataframe(
|
| 256 |
value=leaderboard_df_arp[ANIME_RATING_COLS],
|
|
|
|
| 288 |
**NA:** When models either reply with one number for every anime, give ratings not between 1 and 10, or don't give every anime in the list a rating.
|
| 289 |
""")
|
| 290 |
|
| 291 |
+
def update_all_tables(query, param_ranges, w10_range, additional_cols):
|
| 292 |
+
ugi_table = update_table(leaderboard_df, query, param_ranges, UGI_COLS, w10_range, additional_cols)
|
| 293 |
|
| 294 |
ws_df = leaderboard_df.sort_values(by='Reg+MyScore π', ascending=False)
|
| 295 |
+
ws_table = update_table(ws_df, query, param_ranges, WRITING_STYLE_COLS, w10_range, additional_cols)
|
| 296 |
|
| 297 |
arp_df = leaderboard_df.sort_values(by='Score π', ascending=False)
|
| 298 |
arp_df_na = arp_df[arp_df[['Dif', 'Cor']].isna().any(axis=1)]
|
| 299 |
arp_df = arp_df[~arp_df[['Dif', 'Cor']].isna().any(axis=1)]
|
| 300 |
|
| 301 |
+
arp_table = update_table(arp_df, query, param_ranges, ANIME_RATING_COLS, w10_range, additional_cols)
|
| 302 |
+
arp_na_table = update_table(arp_df_na, query, param_ranges, ANIME_RATING_COLS, w10_range, additional_cols).fillna('NA')
|
| 303 |
|
| 304 |
return ugi_table, ws_table, arp_table, arp_na_table
|
| 305 |
|
| 306 |
search_bar.change(
|
| 307 |
fn=update_all_tables,
|
| 308 |
+
inputs=[search_bar, filter_columns_size, w10_range, additional_columns],
|
| 309 |
outputs=[leaderboard_table_ugi, leaderboard_table_ws, leaderboard_table_arp, leaderboard_table_arp_na]
|
| 310 |
)
|
| 311 |
|
| 312 |
filter_columns_size.change(
|
| 313 |
fn=update_all_tables,
|
| 314 |
+
inputs=[search_bar, filter_columns_size, w10_range, additional_columns],
|
| 315 |
outputs=[leaderboard_table_ugi, leaderboard_table_ws, leaderboard_table_arp, leaderboard_table_arp_na]
|
| 316 |
)
|
| 317 |
|
| 318 |
w10_range.change(
|
| 319 |
fn=update_all_tables,
|
| 320 |
+
inputs=[search_bar, filter_columns_size, w10_range, additional_columns],
|
| 321 |
+
outputs=[leaderboard_table_ugi, leaderboard_table_ws, leaderboard_table_arp, leaderboard_table_arp_na]
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
additional_columns.change(
|
| 325 |
+
fn=update_all_tables,
|
| 326 |
+
inputs=[search_bar, filter_columns_size, w10_range, additional_columns],
|
| 327 |
outputs=[leaderboard_table_ugi, leaderboard_table_ws, leaderboard_table_arp, leaderboard_table_arp_na]
|
| 328 |
)
|
| 329 |
|