File size: 2,988 Bytes
0893185
d35349d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0893185
d35349d
 
 
 
0893185
d35349d
0893185
 
 
 
 
d35349d
0893185
 
d35349d
0893185
 
 
 
 
d35349d
0893185
 
 
 
 
 
 
 
 
 
 
 
d35349d
0893185
 
 
 
 
 
 
d35349d
0893185
 
 
 
d35349d
 
 
 
 
 
 
 
 
 
 
0893185
d35349d
 
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

import json
import pandas as pd

def load_leaderboard_from_json(json_path="leaderboard_data.json"):
    """Load leaderboard data from JSON file"""
    try:
        with open(json_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
        return data['leaderboard']
    except FileNotFoundError:
        print(f"JSON file {json_path} not found")
        return []
    except json.JSONDecodeError:
        print(f"Error decoding JSON file {json_path}")
        return []

def create_leaderboard_df(json_path="leaderboard_data.json"):
    """Create a pandas DataFrame from JSON leaderboard data (dynamic columns)."""
    leaderboard_data = load_leaderboard_from_json(json_path)
    if not leaderboard_data:
        return pd.DataFrame()

    df = pd.DataFrame(leaderboard_data)

    # Sort by Overall if present
    if 'Overall' in df.columns:
        df = df.sort_values('Overall', ascending=False).reset_index(drop=True)
    else:
        df = df.reset_index(drop=True)

    meta_cols = ['model', 'link', 'hf', 'open_source', 'release_date']
    score_cols = [c for c in df.columns if c not in meta_cols]

    # Keep "Overall" first if present, then the rest in file order
    ordered_score_cols = []
    if 'Overall' in score_cols:
        ordered_score_cols.append('Overall')
    ordered_score_cols += [c for c in score_cols if c != 'Overall']

    def add_ranking_icon_and_link(idx, row):
        model_name = row.get('model', 'N/A')
        paper_link = row.get('link', '-') or '-'
        anchor = f'<a href="{paper_link}" target="_blank">{model_name}</a>' if paper_link and paper_link != '-' else model_name
        if idx == 0:
            return f'🥇 {anchor}'
        elif idx == 1:
            return f'🥈 {anchor}'
        elif idx == 2:
            return f'🥉 {anchor}'
        else:
            return anchor

    # Base display columns
    display_df = pd.DataFrame({
        'Model Name (clickable)': [add_ranking_icon_and_link(i, r) for i, r in df.iterrows()],
        'Release Date': df.get('release_date', '-'),
        'HF Model': df.get('hf', '-').apply(lambda x: f'<a href="{x}" target="_blank">🤗</a>' if isinstance(x, str) and x != "-" else "-"),
        'Open Source': df.get('open_source', False).apply(lambda x: '✓' if bool(x) else '✗'),
    })

    # Append each score column with 2-decimal formatting
    for col in ordered_score_cols:
        col_vals = pd.to_numeric(df[col], errors='coerce')
        display_df[col] = col_vals.map(lambda x: f"{x:.2f}" if pd.notnull(x) else "-")

    return display_df

def get_leaderboard_stats(json_path="leaderboard_data.json"):
    """Get statistics about the leaderboard"""
    leaderboard_data = load_leaderboard_from_json(json_path)
    if not leaderboard_data:
        return {}
    df = pd.DataFrame(leaderboard_data)
    stats = {
        'total_models': len(df),
        'open_source_models': int(df['open_source'].sum()) if 'open_source' in df.columns else 0,
    }
    return stats