Spaces:
Runtime error
Runtime error
Added data loading and reloading utility
Browse files* Add basic run instruction for api
* Add reload scheduler
* remove comment
* Disable test mode by default and add explicit host
---------
Co-authored-by: schampoux <sergio@macrocosmos.ai>
README.md
CHANGED
@@ -11,3 +11,6 @@ license: apache-2.0
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
+
|
15 |
+
|
16 |
+
To run the API `python api.py`
|
api.py
CHANGED
@@ -1,46 +1,52 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
import datetime
|
4 |
|
5 |
-
import pandas as pd
|
6 |
-
import bittensor as bt
|
7 |
-
from typing import Dict, List, Any, Optional, Tuple
|
8 |
from flask import Flask, request, jsonify
|
|
|
9 |
|
|
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
|
13 |
# Global variables (saves time on loading data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
state_vars = utils.test_load_state_vars()
|
16 |
-
metagraph = state_vars["metagraph"]
|
17 |
-
model_data = state_vars["model_data"]
|
18 |
-
vali_runs = state_vars["vali_runs"]
|
19 |
-
scores = state_vars["scores"]
|
20 |
-
validator_df = state_vars["validator_df"]
|
21 |
-
benchmarks = state_vars.get("benchmarks", None)
|
22 |
-
benchmark_timestamp = state_vars.get("benchmark_timestamp", None)
|
23 |
|
24 |
@app.route('/', methods=['GET'])
|
25 |
def home():
|
26 |
return "Welcome to the Bittensor Pretraining Leaderboard API!"
|
27 |
|
28 |
-
@app.route('/reload', methods=['GET'])
|
29 |
-
def reload():
|
30 |
-
"""
|
31 |
-
Reload the state variables
|
32 |
-
"""
|
33 |
-
global metagraph, model_data, vali_runs, scores, validator_df, benchmarks, benchmark_timestamp
|
34 |
-
state_vars = utils.load_state_vars()
|
35 |
-
metagraph = state_vars["metagraph"]
|
36 |
-
model_data = state_vars["model_data"]
|
37 |
-
vali_runs = state_vars["vali_runs"]
|
38 |
-
scores = state_vars["scores"]
|
39 |
-
validator_df = state_vars["validator_df"]
|
40 |
-
benchmarks = state_vars.get("benchmarks", None)
|
41 |
-
benchmark_timestamp = state_vars.get("benchmark_timestamp", None)
|
42 |
|
43 |
-
|
|
|
|
|
|
|
44 |
|
45 |
@app.route('/benchmark', methods=['GET'])
|
46 |
def benchmark():
|
@@ -52,6 +58,9 @@ def benchmark():
|
|
52 |
- benchmark_timestamp: String
|
53 |
"""
|
54 |
|
|
|
|
|
|
|
55 |
return jsonify(
|
56 |
{
|
57 |
"benchmarks": benchmarks.to_dict(orient='records'),
|
@@ -66,6 +75,9 @@ def metagraph():
|
|
66 |
Returns:
|
67 |
- metagraph_data: List of dicts (from pandas DataFrame)
|
68 |
"""
|
|
|
|
|
|
|
69 |
return jsonify(
|
70 |
utils.make_metagraph_dataframe(metagraph).to_dict(orient='records')
|
71 |
)
|
@@ -77,6 +89,9 @@ def leaderboard():
|
|
77 |
Returns:
|
78 |
- leaderboard_data: List of dicts (from pandas DataFrame)
|
79 |
"""
|
|
|
|
|
|
|
80 |
show_stale = request.args.get('show_stale')
|
81 |
return jsonify(
|
82 |
utils.leaderboard_data(model_data, scores, show_stale=show_stale)
|
@@ -90,6 +105,8 @@ def loss():
|
|
90 |
Returns:
|
91 |
- losses_over_time: List of dicts (from pandas DataFrame)
|
92 |
"""
|
|
|
|
|
93 |
return jsonify(
|
94 |
utils.get_losses_over_time(vali_runs).to_dict(orient='records')
|
95 |
)
|
@@ -102,11 +119,17 @@ def validator():
|
|
102 |
Returns:
|
103 |
- validator_data: List of dicts (from pandas DataFrame)
|
104 |
"""
|
|
|
|
|
|
|
105 |
return jsonify(
|
106 |
utils.make_validator_dataframe(validator_df, model_data).to_dict(orient='records')
|
107 |
)
|
108 |
|
109 |
|
110 |
if __name__ == '__main__':
|
|
|
|
|
|
|
111 |
|
112 |
-
app.run(port=5000, debug=True)
|
|
|
1 |
+
|
2 |
+
import atexit
|
3 |
import datetime
|
4 |
|
|
|
|
|
|
|
5 |
from flask import Flask, request, jsonify
|
6 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
7 |
|
8 |
+
import utils
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
12 |
# Global variables (saves time on loading data)
|
13 |
+
state_vars = None
|
14 |
+
reload_timestamp = datetime.datetime.now().strftime('%D %T')
|
15 |
+
|
16 |
+
|
17 |
+
def load_data(test=False):
|
18 |
+
"""
|
19 |
+
Reload the state variables
|
20 |
+
"""
|
21 |
+
global state_vars, reload_timestamp
|
22 |
+
if test:
|
23 |
+
state_vars = utils.test_load_state_vars()
|
24 |
+
else:
|
25 |
+
state_vars = utils.load_state_vars()
|
26 |
+
|
27 |
+
reload_timestamp = datetime.datetime.now().strftime('%D %T')
|
28 |
+
|
29 |
+
print(f'Reloaded data at {reload_timestamp}')
|
30 |
+
|
31 |
+
|
32 |
+
def start_scheduler():
|
33 |
+
scheduler = BackgroundScheduler()
|
34 |
+
scheduler.add_job(func=load_data, trigger="interval", seconds=60*30)
|
35 |
+
scheduler.start()
|
36 |
+
|
37 |
+
# Shut down the scheduler when exiting the app
|
38 |
+
atexit.register(lambda: scheduler.shutdown())
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
@app.route('/', methods=['GET'])
|
42 |
def home():
|
43 |
return "Welcome to the Bittensor Pretraining Leaderboard API!"
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
@app.route('/updated', methods=['GET'])
|
47 |
+
def updated():
|
48 |
+
return reload_timestamp
|
49 |
+
|
50 |
|
51 |
@app.route('/benchmark', methods=['GET'])
|
52 |
def benchmark():
|
|
|
58 |
- benchmark_timestamp: String
|
59 |
"""
|
60 |
|
61 |
+
benchmarks = state_vars.get("benchmarks", None)
|
62 |
+
benchmark_timestamp = state_vars.get("benchmark_timestamp", None)
|
63 |
+
|
64 |
return jsonify(
|
65 |
{
|
66 |
"benchmarks": benchmarks.to_dict(orient='records'),
|
|
|
75 |
Returns:
|
76 |
- metagraph_data: List of dicts (from pandas DataFrame)
|
77 |
"""
|
78 |
+
|
79 |
+
metagraph = state_vars["metagraph"]
|
80 |
+
|
81 |
return jsonify(
|
82 |
utils.make_metagraph_dataframe(metagraph).to_dict(orient='records')
|
83 |
)
|
|
|
89 |
Returns:
|
90 |
- leaderboard_data: List of dicts (from pandas DataFrame)
|
91 |
"""
|
92 |
+
|
93 |
+
model_data = state_vars["model_data"]
|
94 |
+
scores = state_vars["scores"]
|
95 |
show_stale = request.args.get('show_stale')
|
96 |
return jsonify(
|
97 |
utils.leaderboard_data(model_data, scores, show_stale=show_stale)
|
|
|
105 |
Returns:
|
106 |
- losses_over_time: List of dicts (from pandas DataFrame)
|
107 |
"""
|
108 |
+
vali_runs = state_vars["vali_runs"]
|
109 |
+
|
110 |
return jsonify(
|
111 |
utils.get_losses_over_time(vali_runs).to_dict(orient='records')
|
112 |
)
|
|
|
119 |
Returns:
|
120 |
- validator_data: List of dicts (from pandas DataFrame)
|
121 |
"""
|
122 |
+
model_data = state_vars["model_data"]
|
123 |
+
validator_df = state_vars["validator_df"]
|
124 |
+
|
125 |
return jsonify(
|
126 |
utils.make_validator_dataframe(validator_df, model_data).to_dict(orient='records')
|
127 |
)
|
128 |
|
129 |
|
130 |
if __name__ == '__main__':
|
131 |
+
|
132 |
+
load_data()
|
133 |
+
start_scheduler()
|
134 |
|
135 |
+
app.run(host='0.0.0.0', port=5000, debug=True)
|
utils.py
CHANGED
@@ -367,7 +367,6 @@ def load_state_vars() -> dict[Any]:
|
|
367 |
for uid, stats in validator_df.items():
|
368 |
weight_keys.update(stats[-1].keys())
|
369 |
|
370 |
-
# TODO: re-enable benchmarks
|
371 |
# Enable benchmark if the flag is set
|
372 |
if BENCHMARK_FLAG:
|
373 |
benchmarks, benchmark_timestamp = get_benchmarks()
|
|
|
367 |
for uid, stats in validator_df.items():
|
368 |
weight_keys.update(stats[-1].keys())
|
369 |
|
|
|
370 |
# Enable benchmark if the flag is set
|
371 |
if BENCHMARK_FLAG:
|
372 |
benchmarks, benchmark_timestamp = get_benchmarks()
|