Spaces:
Running
on
Zero
Running
on
Zero
File size: 3,333 Bytes
d40e945 48445ab d40e945 48445ab 1593f23 48445ab 1593f23 d40e945 1593f23 d40e945 1593f23 d40e945 1593f23 54bc519 d40e945 54bc519 1593f23 d40e945 54bc519 d40e945 c4f7417 54bc519 7128513 54bc519 7128513 54bc519 48445ab 54bc519 48445ab 54bc519 48445ab 54bc519 48445ab 54bc519 1593f23 54bc519 4eba378 |
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 |
from .config import *
from .db import *
from .models import *
from .synth import top_five
import pandas as pd
# for diff
leaderboard_df = {}
def get_leaderboard(reveal_prelim = False):
global leaderboard_df
conn = get_db()
cursor = conn.cursor()
sql = 'SELECT name, upvote, downvote, name AS orig_name FROM model'
if not reveal_prelim: sql += ' WHERE (upvote + downvote) > 300'
cursor.execute(sql)
data = cursor.fetchall()
df = pd.DataFrame(data, columns=['name', 'upvote', 'downvote', 'orig_name'])
# df['license'] = df['name'].map(model_license)
df['name'] = df['name'].replace(model_names)
for i in range(len(df)):
df.loc[i, "name"] = make_link_to_space(df['name'][i], True)
# Calculate total votes and win rate
df['votes'] = df['upvote'] + df['downvote']
df['win_rate'] = (df['upvote'] / df['votes'] * 100).round(1)
# df['score'] = round((df['upvote'] / df['votes']) * 100, 2) # Percentage score
## ELO SCORE
df['elo'] = 1200
df['elo_diff'] = ""
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.at[i, 'elo'] += round(32 * (actual_a - expected_a))
df.at[j, 'elo'] += round(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'])
df['elo_diff'] = df['elo']
if (
reveal_prelim == False
and len(leaderboard_df) == 0
):
leaderboard_df = df
if (reveal_prelim == False):
for i in range(len(df)):
elo_diff = (df['elo'].iloc[i] - leaderboard_df['elo'].iloc[i])
if (elo_diff == 0):
continue
if (elo_diff > 0):
plus = '<em style="color: green; font-family: monospace">+'
else:
plus = '<em style="color: red; font-family: monospace">'
df.at[i, 'elo_diff'] = str(df['elo'].iloc[i]) + plus + str(elo_diff) +'</em>'
## ELO score
df = df.sort_values(by='elo', ascending=False)
# medals
def assign_medal(rank, assign):
rank = str(rank + 1)
if assign:
if rank == '1':
rank += '🥇'
elif rank == '2':
rank += '🥈'
elif rank == '3':
rank += '🥉'
return '#'+ rank
df['order'] = [assign_medal(i, not reveal_prelim and len(df) > 2) for i in range(len(df))]
# fetch top_five
for orig_name in df['orig_name']:
if (
reveal_prelim
and len(top_five) < 5
and orig_name in AVAILABLE_MODELS.keys()
):
top_five.append(orig_name)
df['elo'] = df['elo_diff']
df = df[['order', 'name', 'win_rate', 'elo', 'votes']]
return df
|