k-mktr commited on
Commit
5ac9254
1 Parent(s): 5282662

Create internal_stats.py

Browse files
Files changed (1) hide show
  1. internal_stats.py +193 -0
internal_stats.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from datetime import datetime, timezone
3
+ from typing import Dict, Any
4
+ from nc_py_api import Nextcloud
5
+ import arena_config
6
+ from leaderboard import load_leaderboard, get_human_readable_name, get_model_size
7
+
8
+ def get_internal_stats() -> Dict[str, Any]:
9
+ leaderboard = load_leaderboard()
10
+
11
+ total_battles = sum(
12
+ model_data['wins'] + model_data['losses']
13
+ for model_data in leaderboard.values()
14
+ )
15
+
16
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
17
+
18
+ active_models = len(leaderboard)
19
+
20
+ most_battles = max(
21
+ (model_data['wins'] + model_data['losses'], model)
22
+ for model, model_data in leaderboard.items()
23
+ )
24
+
25
+ highest_win_rate = max(
26
+ (model_data['wins'] / (model_data['wins'] + model_data['losses']) if (model_data['wins'] + model_data['losses']) > 0 else 0, model)
27
+ for model, model_data in leaderboard.items()
28
+ )
29
+
30
+ most_diverse_opponent = max(
31
+ (len(model_data['opponents']), model)
32
+ for model, model_data in leaderboard.items()
33
+ )
34
+
35
+ stats = {
36
+ "timestamp": timestamp,
37
+ "total_battles": total_battles,
38
+ "active_models": active_models,
39
+ "most_battles": {
40
+ "model": get_human_readable_name(most_battles[1]),
41
+ "battles": most_battles[0]
42
+ },
43
+ "highest_win_rate": {
44
+ "model": get_human_readable_name(highest_win_rate[1]),
45
+ "win_rate": f"{highest_win_rate[0]:.2%}"
46
+ },
47
+ "most_diverse_opponent": {
48
+ "model": get_human_readable_name(most_diverse_opponent[1]),
49
+ "unique_opponents": most_diverse_opponent[0]
50
+ }
51
+ }
52
+
53
+ return stats
54
+
55
+ def save_internal_stats(stats: Dict[str, Any]) -> bool:
56
+ nc = Nextcloud(
57
+ nextcloud_url=arena_config.NEXTCLOUD_URL,
58
+ nc_auth_user=arena_config.NEXTCLOUD_USERNAME,
59
+ nc_auth_pass=arena_config.NEXTCLOUD_PASSWORD
60
+ )
61
+
62
+ try:
63
+ json_data = json.dumps(stats, indent=2)
64
+ nc.files.upload(arena_config.NEXTCLOUD_INTERNAL_STATS_PATH, json_data.encode('utf-8'))
65
+ return True
66
+ except Exception as e:
67
+ print(f"Error saving internal stats to Nextcloud: {str(e)}")
68
+ return False
69
+
70
+ def save_local_stats(stats: Dict[str, Any], filename: str = "internal_stats.json") -> bool:
71
+ try:
72
+ with open(filename, 'w') as f:
73
+ json.dump(stats, f, indent=2)
74
+ return True
75
+ except Exception as e:
76
+ print(f"Error saving internal stats to local file: {str(e)}")
77
+ return False
78
+
79
+ def get_fun_stats() -> Dict[str, Any]:
80
+ leaderboard = load_leaderboard()
81
+
82
+ total_battles = sum(
83
+ model_data['wins'] + model_data['losses']
84
+ for model_data in leaderboard.values()
85
+ )
86
+
87
+ timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
88
+
89
+ active_models = len(leaderboard)
90
+
91
+ most_battles = max(
92
+ (model_data['wins'] + model_data['losses'], model)
93
+ for model, model_data in leaderboard.items()
94
+ )
95
+
96
+ highest_win_rate = max(
97
+ (model_data['wins'] / (model_data['wins'] + model_data['losses']) if (model_data['wins'] + model_data['losses']) > 0 else 0, model)
98
+ for model, model_data in leaderboard.items()
99
+ )
100
+
101
+ most_diverse_opponent = max(
102
+ (len(model_data['opponents']), model)
103
+ for model, model_data in leaderboard.items()
104
+ )
105
+
106
+ # Existing fun stats
107
+ underdog_champion = min(
108
+ ((get_model_size(model), model_data['wins'] / (model_data['wins'] + model_data['losses'])) if (model_data['wins'] + model_data['losses']) > 0 else (get_model_size(model), 0), model)
109
+ for model, model_data in leaderboard.items()
110
+ )
111
+
112
+ most_consistent = min(
113
+ (abs(model_data['wins'] - model_data['losses']), model)
114
+ for model, model_data in leaderboard.items()
115
+ if (model_data['wins'] + model_data['losses']) > 10 # Minimum battles threshold
116
+ )
117
+
118
+ biggest_rivalry = max(
119
+ (results['wins'] + results['losses'], (model, opponent))
120
+ for model, data in leaderboard.items()
121
+ for opponent, results in data['opponents'].items()
122
+ )
123
+
124
+ # New fun stats
125
+ david_vs_goliath = max(
126
+ ((get_model_size(opponent) - get_model_size(model), model_data['opponents'][opponent]['wins']), (model, opponent))
127
+ for model, model_data in leaderboard.items()
128
+ for opponent in model_data['opponents']
129
+ if get_model_size(opponent) > get_model_size(model) and model_data['opponents'][opponent]['wins'] > 0
130
+ )
131
+
132
+ comeback_king = max(
133
+ (model_data['wins'] - model_data['losses'], model)
134
+ for model, model_data in leaderboard.items()
135
+ if model_data['losses'] > model_data['wins']
136
+ )
137
+
138
+ pyrrhic_victor = min(
139
+ (model_data['wins'] / (model_data['wins'] + model_data['losses']) if (model_data['wins'] + model_data['losses']) > 0 else float('inf'), model)
140
+ for model, model_data in leaderboard.items()
141
+ if model_data['wins'] > model_data['losses'] and (model_data['wins'] + model_data['losses']) > 10
142
+ )
143
+
144
+ stats = {
145
+ "timestamp": timestamp,
146
+ "total_battles": total_battles,
147
+ "active_models": active_models,
148
+ "most_battles": {
149
+ "model": get_human_readable_name(most_battles[1]),
150
+ "battles": most_battles[0]
151
+ },
152
+ "highest_win_rate": {
153
+ "model": get_human_readable_name(highest_win_rate[1]),
154
+ "win_rate": f"{highest_win_rate[0]:.2%}"
155
+ },
156
+ "most_diverse_opponent": {
157
+ "model": get_human_readable_name(most_diverse_opponent[1]),
158
+ "unique_opponents": most_diverse_opponent[0]
159
+ },
160
+ "underdog_champion": {
161
+ "model": get_human_readable_name(underdog_champion[1]),
162
+ "size": f"{underdog_champion[0][0]}B",
163
+ "win_rate": f"{underdog_champion[0][1]:.2%}"
164
+ },
165
+ "most_consistent": {
166
+ "model": get_human_readable_name(most_consistent[1]),
167
+ "win_loss_difference": most_consistent[0]
168
+ },
169
+ "biggest_rivalry": {
170
+ "model1": get_human_readable_name(biggest_rivalry[1][0]),
171
+ "model2": get_human_readable_name(biggest_rivalry[1][1]),
172
+ "total_battles": biggest_rivalry[0]
173
+ },
174
+ "david_vs_goliath": {
175
+ "david": get_human_readable_name(david_vs_goliath[1][0]),
176
+ "goliath": get_human_readable_name(david_vs_goliath[1][1]),
177
+ "size_difference": f"{david_vs_goliath[0][0]:.1f}B",
178
+ "wins": david_vs_goliath[0][1]
179
+ },
180
+ "comeback_king": {
181
+ "model": get_human_readable_name(comeback_king[1]),
182
+ "comeback_margin": comeback_king[0]
183
+ },
184
+ "pyrrhic_victor": {
185
+ "model": get_human_readable_name(pyrrhic_victor[1]),
186
+ "win_rate": f"{pyrrhic_victor[0]:.2%}"
187
+ }
188
+ }
189
+
190
+ return stats
191
+
192
+ if __name__ == "__main__":
193
+ stats = get_internal_stats()