Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 8,083 Bytes
d40e945 5d5cc81 d40e945 9ed4a11 5d5cc81 d40e945 5d5cc81 d40e945 5d5cc81 d40e945 c4f7417 5d5cc81 7128513 5d5cc81 7128513 5d5cc81 d40e945 5d5cc81 6356013 5d5cc81 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
from .config import *
from .db import *
from .models import *
import pandas as pd
def get_leaderboard(reveal_prelim = False, hide_battle_votes = False, sort_by_elo = False, hide_proprietary = False):
conn = get_db()
cursor = conn.cursor()
if hide_battle_votes:
sql = '''
SELECT m.name,
SUM(CASE WHEN v.username NOT LIKE '%_battle' AND v.vote = 1 THEN 1 ELSE 0 END) as upvote,
SUM(CASE WHEN v.username NOT LIKE '%_battle' AND v.vote = -1 THEN 1 ELSE 0 END) as downvote
FROM model m
LEFT JOIN vote v ON m.name = v.model
GROUP BY m.name
'''
else:
sql = '''
SELECT name,
SUM(CASE WHEN vote = 1 THEN 1 ELSE 0 END) as upvote,
SUM(CASE WHEN vote = -1 THEN 1 ELSE 0 END) as downvote
FROM model
LEFT JOIN vote ON model.name = vote.model
GROUP BY name
'''
cursor.execute(sql)
data = cursor.fetchall()
df = pd.DataFrame(data, columns=['name', 'upvote', 'downvote'])
df['name'] = df['name'].replace(model_names).replace('Anonymous Sparkle', 'Fish Speech v1.5')
# Calculate total votes and win rate
df['votes'] = df['upvote'] + df['downvote']
df['win_rate'] = (df['upvote'] / df['votes'] * 100).round(1)
# Remove models with no votes
df = df[df['votes'] > 0]
# Filter out rows with insufficient votes if not revealing preliminary results
if not reveal_prelim:
df = df[df['votes'] > 500]
## Calculate ELO SCORE (kept as secondary metric)
df['elo'] = 1200
for i in range(len(df)):
for j in range(len(df)):
if i != j:
try:
expected_a = 1 / (1 + 10 ** ((df['elo'].iloc[j] - df['elo'].iloc[i]) / 400))
expected_b = 1 / (1 + 10 ** ((df['elo'].iloc[i] - df['elo'].iloc[j]) / 400))
actual_a = df['upvote'].iloc[i] / df['votes'].iloc[i] if df['votes'].iloc[i] > 0 else 0.5
actual_b = df['upvote'].iloc[j] / df['votes'].iloc[j] if df['votes'].iloc[j] > 0 else 0.5
df.iloc[i, df.columns.get_loc('elo')] += 32 * (actual_a - expected_a)
df.iloc[j, df.columns.get_loc('elo')] += 32 * (actual_b - expected_b)
except Exception as e:
print(f"Error in ELO calculation for rows {i} and {j}: {str(e)}")
continue
df['elo'] = round(df['elo'])
# Sort based on user preference
sort_column = 'elo' if sort_by_elo else 'win_rate'
df = df.sort_values(by=sort_column, ascending=False)
df['order'] = ['#' + str(i + 1) for i in range(len(df))]
# Select and order columns for display
df = df[['order', 'name', 'win_rate', 'votes', 'elo']]
# Remove proprietary models if filter is enabled
if hide_proprietary:
df = df[~df['name'].isin(closed_source)]
# Convert DataFrame to markdown table with CSS styling
markdown_table = """
<style>
/* Reset any Gradio table styles */
.leaderboard-table,
.leaderboard-table th,
.leaderboard-table td {
border: none !important;
border-collapse: separate !important;
border-spacing: 0 !important;
}
.leaderboard-container {
background: var(--background-fill-primary);
border: 1px solid var(--border-color-primary);
border-radius: 12px;
padding: 4px;
margin: 10px 0;
width: 100%;
overflow-x: auto; /* Enable horizontal scroll */
}
.leaderboard-scroll {
max-height: 600px;
overflow-y: auto;
border-radius: 8px;
}
.leaderboard-table {
width: 100%;
border-spacing: 0;
border-collapse: separate;
font-size: 15px;
line-height: 1.5;
table-layout: auto; /* Allow flexible column widths */
}
.leaderboard-table th {
background: var(--background-fill-secondary);
color: var(--body-text-color);
font-weight: 600;
text-align: left;
padding: 12px 16px;
position: sticky;
top: 0;
z-index: 1;
}
.leaderboard-table th:after {
content: '';
position: absolute;
left: 0;
bottom: 0;
width: 100%;
border-bottom: 1px solid var(--border-color-primary);
}
.leaderboard-table td {
padding: 12px 16px;
color: var(--body-text-color);
}
.leaderboard-table tr td {
border-bottom: 1px solid var(--border-color-primary);
}
.leaderboard-table tr:last-child td {
border-bottom: none;
}
.leaderboard-table tr:hover td {
background: var(--background-fill-secondary);
}
/* Column-specific styles */
.leaderboard-table .col-rank {
width: 70px;
min-width: 70px; /* Prevent rank from shrinking */
}
.leaderboard-table .col-model {
min-width: 200px; /* Minimum width before scrolling */
}
.leaderboard-table .col-winrate {
width: 100px;
min-width: 100px; /* Prevent win rate from shrinking */
}
.leaderboard-table .col-votes {
width: 100px;
min-width: 100px; /* Prevent votes from shrinking */
}
.leaderboard-table .col-arena {
width: 100px;
min-width: 100px; /* Prevent arena score from shrinking */
}
.win-rate {
display: inline-block;
font-weight: 600;
padding: 4px 8px;
border-radius: 6px;
min-width: 65px;
text-align: center;
}
.win-rate-excellent {
background-color: var(--color-accent);
color: var(--color-accent-foreground);
}
.win-rate-good {
background-color: var(--color-accent-soft);
color: var(--body-text-color);
}
.win-rate-average {
background-color: var(--background-fill-secondary);
color: var(--body-text-color);
border: 1px solid var(--border-color-primary);
}
.win-rate-below {
background-color: var(--error-background-fill);
color: var(--body-text-color);
}
.model-link {
color: var(--body-text-color) !important;
text-decoration: none !important;
border-bottom: 2px dashed rgba(128, 128, 128, 0.3);
}
.model-link:hover {
color: var(--color-accent) !important;
border-bottom-color: var(--color-accent) !important;
}
.proprietary-badge {
display: inline-block;
font-size: 12px;
padding: 2px 6px;
border-radius: 4px;
background-color: var(--background-fill-secondary);
color: var(--body-text-color);
margin-left: 6px;
border: 1px solid var(--border-color-primary);
}
</style>
<div class="leaderboard-container">
<div class="leaderboard-scroll">
<table class="leaderboard-table">
<thead>
<tr>
<th class="col-rank">Rank</th>
<th class="col-model">Model</th>
<th class="col-winrate">Win Rate</th>
<th class="col-votes">Votes</th>
""" + ("""<th class="col-arena">Arena Score</th>""" if sort_by_elo else "") + """
</tr>
</thead>
<tbody>
"""
def get_win_rate_class(win_rate):
if win_rate >= 60:
return "win-rate-excellent"
elif win_rate >= 55:
return "win-rate-good"
elif win_rate >= 45:
return "win-rate-average"
else:
return "win-rate-below"
for _, row in df.iterrows():
win_rate_class = get_win_rate_class(row['win_rate'])
win_rate_html = f'<span class="win-rate {win_rate_class}">{row["win_rate"]}%</span>'
# Add link to model name if available and proprietary badge if closed source
model_name = row['name']
original_model_name = model_name
if model_name in model_links:
model_name = f'<a href="{model_links[model_name]}" target="_blank" class="model-link">{model_name}</a>'
if original_model_name in closed_source:
model_name += '<span class="proprietary-badge">Proprietary</span>'
markdown_table += f'''<tr>
<td class="col-rank">{row['order']}</td>
<td class="col-model">{model_name}</td>
<td class="col-winrate">{win_rate_html}</td>
<td class="col-votes">{row['votes']:,}</td>''' + (
f'''<td class="col-arena">{int(row['elo'])}</td>''' if sort_by_elo else ""
) + "</tr>\n"
markdown_table += "</tbody></table></div></div>"
return markdown_table
|