Spaces:
Sleeping
Sleeping
| 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 | |