TTS-Spaces-Arena / app /leaderboard.py
Pendrokar's picture
make_link for prev_model
1973202
raw
history blame
3.26 kB
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)
df['votes'] = df['upvote'] + df['downvote']
# df['score'] = round((df['upvote'] / df['votes']) * 100, 2) # Percentage score
## ELO SCORE
df['score'] = 1200
df['score_diff'] = ""
for i in range(len(df)):
for j in range(len(df)):
if i != j:
try:
expected_a = 1 / (1 + 10 ** ((df['score'].iloc[j] - df['score'].iloc[i]) / 400))
expected_b = 1 / (1 + 10 ** ((df['score'].iloc[i] - df['score'].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, 'score'] += round(32 * (actual_a - expected_a))
df.at[j, 'score'] += 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['score'] = round(df['score'])
df['score_diff'] = df['score']
if (
reveal_prelim == False
and len(leaderboard_df) == 0
):
leaderboard_df = df
if (reveal_prelim == False):
for i in range(len(df)):
score_diff = (df['score'].iloc[i] - leaderboard_df['score'].iloc[i])
if (score_diff == 0):
continue
if (score_diff > 0):
plus = '<em style="color: green; font-family: monospace">+'
else:
plus = '<em style="color: red; font-family: monospace">'
df.at[i, 'score_diff'] = str(df['score'].iloc[i]) + plus + str(score_diff) +'</em>'
## ELO SCORE
df = df.sort_values(by='score', 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['score'] = df['score_diff']
df = df[['order', 'name', 'score', 'votes']]
return df