-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleaderboard.py
More file actions
181 lines (143 loc) · 5.77 KB
/
Copy pathleaderboard.py
File metadata and controls
181 lines (143 loc) · 5.77 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
"""
Leaderboard Management System
Manages benchmark scores and rankings for multiple LLM models.
"""
import json
from datetime import datetime, timezone
from pathlib import Path
from typing import Dict, List, Optional, Any
class Leaderboard:
"""Manages benchmark scores and rankings."""
def __init__(self, data_file: Optional[str] = None):
"""
Initialize the leaderboard.
Args:
data_file: Path to JSON file for persistence.
Defaults to leaderboard.json in project root.
"""
if data_file is None:
data_file = Path(__file__).parent / "leaderboard.json"
self.data_file = Path(data_file)
self.data = self._load()
def _load(self) -> Dict:
"""Load leaderboard data from file."""
if self.data_file.exists():
try:
with open(self.data_file, 'r', encoding='utf-8') as f:
return json.load(f)
except (json.JSONDecodeError, IOError):
return {"models": {}, "meta": {"version": "1.0"}}
return {"models": {}, "meta": {"version": "1.0"}}
def _save(self):
"""Save leaderboard data to file."""
with open(self.data_file, 'w', encoding='utf-8') as f:
json.dump(self.data, f, indent=2)
def add_result(
self,
model_name: str,
benchmark: str,
score: float,
details: Optional[Dict] = None
):
"""
Add or update a benchmark result for a model.
Supports multiple runs by appending to a list.
Args:
model_name: The model identifier (e.g., "openai/gpt-4")
benchmark: The benchmark name (e.g., "maze")
score: The numeric score
details: Optional additional details about the run
"""
if "models" not in self.data:
self.data["models"] = {}
if model_name not in self.data["models"]:
self.data["models"][model_name] = {}
# Initialize as list if not present
if benchmark not in self.data["models"][model_name]:
self.data["models"][model_name][benchmark] = []
# Migrate old dict format to list if necessary
current_data = self.data["models"][model_name][benchmark]
if isinstance(current_data, dict):
self.data["models"][model_name][benchmark] = [current_data]
# Store the result
self.data["models"][model_name][benchmark].append({
"score": score,
"timestamp": datetime.now(timezone.utc).isoformat(),
"details": details or {}
})
self._save()
# Update markdown file
from leaderboard_exports import save_to_markdown_file
save_to_markdown_file(self.data)
def get_result(self, model_name: str, benchmark: str) -> Optional[Dict]:
"""
Get the most recent result for a model and benchmark.
Args:
model_name: The model identifier
benchmark: The benchmark name
Returns:
Result dict or None if not found
"""
data = self.data.get("models", {}).get(model_name, {}).get(benchmark)
if not data:
return None
# Handle list format (return last item)
if isinstance(data, list):
return data[-1] if data else None
# Handle legacy dict format
return data
def get_all_benchmarks(self) -> List[str]:
"""Get list of all benchmarks that have results."""
benchmarks = set()
for model_benchmarks in self.data.get("models", {}).values():
benchmarks.update(model_benchmarks.keys())
return sorted(benchmarks)
def remove_model(self, model_name: str) -> bool:
"""
Remove a model from the leaderboard.
Args:
model_name: The model identifier
Returns:
True if removed, False if not found
"""
if model_name in self.data.get("models", {}):
del self.data["models"][model_name]
self._save()
# Update markdown file
from leaderboard_exports import save_to_markdown_file
save_to_markdown_file(self.data)
return True
return False
def format_cli_table(self, benchmark: Optional[str] = None) -> str:
"""
Format leaderboard for CLI display.
Args:
benchmark: Specific benchmark to display, or None for all
Returns:
Formatted string for terminal output
"""
from leaderboard_exports import format_cli_table
return format_cli_table(self.data, benchmark)
def export_markdown(self, benchmark: Optional[str] = None) -> str:
"""
Export leaderboard as markdown table.
Args:
benchmark: Specific benchmark to export, or None for all
Returns:
Markdown formatted string
"""
from leaderboard_exports import export_markdown
return export_markdown(self.data, benchmark)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Leaderboard Manager")
parser.add_argument("--update", action="store_true", help="Regenerate LEADERBOARD.md from json")
parser.add_argument("--benchmark", "-b", help="Filter by benchmark")
args = parser.parse_args()
lb = Leaderboard()
if args.update:
from leaderboard_exports import save_to_markdown_file
save_to_markdown_file(lb.data)
else:
print(lb.format_cli_table(args.benchmark))