1
+ #!/usr/bin/env python
2
+
3
+ import argparse
4
+ import dataclasses
5
+ from pathlib import Path
6
+ import pandas as pd
7
+ import json
8
+
9
+
10
+ @dataclasses .dataclass
11
+ class Submission :
12
+ """Represents a single submission"""
13
+ date : str
14
+ author : str
15
+ model : str
16
+ scores : dict [str , float ]
17
+
18
+ @classmethod
19
+ def from_file (cls , path : Path ):
20
+ data = json .loads (path .read_text ())
21
+ return cls (** data )
22
+
23
+ def to_single_score_dict (self , score = "default" ) -> dict [str , float ]:
24
+ data = dataclasses .asdict (self )
25
+ del data ["scores" ]
26
+ data ["score" ] = self .scores [score ]
27
+ return data
28
+
29
+
30
+ class LeaderboardAggregator :
31
+ def __init__ (self ):
32
+ self .submissions = []
33
+
34
+ def load_file (self , path : Path ):
35
+ submission = Submission .from_file (path )
36
+ self .submissions .append (submission )
37
+
38
+ def format_markdown (self , score = "default" ) -> str :
39
+ df = pd .DataFrame .from_records ([s .to_single_score_dict (score ) for s in self .submissions ])
40
+ print (df )
41
+ df = df .sort_values (by = "score" , ascending = False )
42
+ df = df .reset_index (drop = True )
43
+ return df .to_markdown (index = False )
44
+
45
+ def to_file (self , path : Path , score = "default" ):
46
+ path .write_text (self .format_markdown (score ))
47
+
48
+
49
+ def main (input : str , output : str ) -> None :
50
+ la = LeaderboardAggregator ()
51
+ for inpt in Path (input ).rglob ("score.json" ):
52
+ print ("Loading" , inpt )
53
+ la .load_file (inpt )
54
+ la .to_file (Path (output ))
55
+
56
+
57
+ def cli ():
58
+ parser = argparse .ArgumentParser (description = "Aggregate leaderboard data and format table" )
59
+ parser .add_argument ("--input" , help = "Path to leaderboard data" )
60
+ parser .add_argument ("--output" , help = "Path to save leaderboard table" )
61
+ return parser
62
+
63
+
64
+ if __name__ == "__main__" :
65
+ main (** vars (cli ().parse_args ()))
0 commit comments