Spaces:
Running
on
Zero
Running
on
Zero
File size: 4,042 Bytes
d40e945 1593f23 d40e945 1593f23 d40e945 1593f23 d40e945 1593f23 d40e945 1593f23 d40e945 c4f7417 7128513 1593f23 7128513 d40e945 1593f23 d40e945 1593f23 |
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 |
from .config import *
from .db import *
from .models import *
import pandas as pd
def get_leaderboard(reveal_prelim = False):
conn = get_db()
cursor = conn.cursor()
sql = 'SELECT name, upvote, downvote, name AS orig_name FROM model'
# if not reveal_prelim: sql += ' WHERE EXISTS (SELECT 1 FROM model WHERE (upvote + downvote) > 750)'
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
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'])
## 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
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 = df[['order', 'name', 'score', 'votes']]
return df
def make_link_to_space(model_name, for_leaderboard=False):
# create a anchor link if a HF space
style = 'text-decoration: underline;text-decoration-style: dotted;'
title = ''
if model_name in AVAILABLE_MODELS:
style += 'color: var(--link-text-color);'
title = model_name
else:
style += 'font-style: italic;'
title = 'Disabled for Arena (See AVAILABLE_MODELS within code for why)'
model_basename = model_name
if model_name in HF_SPACES:
model_basename = HF_SPACES[model_name]['name']
try:
if(
for_leaderboard
and HF_SPACES[model_name]['is_proprietary']
):
model_basename += ' π'
title += '; π = online only or proprietary'
except:
pass
if '/' in model_name:
return 'π€ <a target="_blank" style="'+ style +'" title="'+ title +'" href="'+ 'https://huggingface.co/spaces/'+ model_name +'">'+ model_basename +'</a>'
# otherwise just return the model name
return '<span style="'+ style +'" title="'+ title +'" href="'+ 'https://huggingface.co/spaces/'+ model_name +'">'+ model_name +'</span>'
def markdown_link_to_space(model_name):
# create a anchor link if a HF space using markdown syntax
if '/' in model_name:
return 'π€ [' + model_name + '](https://huggingface.co/spaces/' + model_name + ')'
# otherwise just return the model name
return model_name
|