Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,14 +4,12 @@ import typing
|
|
4 |
from bittensor.extrinsics.serving import get_metadata
|
5 |
from dataclasses import dataclass
|
6 |
import requests
|
7 |
-
import wandb
|
8 |
import math
|
9 |
import os
|
10 |
import datetime
|
11 |
import time
|
12 |
from dotenv import load_dotenv
|
13 |
from huggingface_hub import HfApi
|
14 |
-
from apscheduler.schedulers.background import BackgroundScheduler
|
15 |
|
16 |
load_dotenv()
|
17 |
|
@@ -76,9 +74,9 @@ def get_tao_price() -> float:
|
|
76 |
try:
|
77 |
return float(requests.get("https://api.kucoin.com/api/v1/market/stats?symbol=TAO-USDT").json()["data"]["last"])
|
78 |
except:
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
raise RuntimeError()
|
83 |
|
84 |
def get_validator_weights(metagraph: bt.metagraph) -> typing.Dict[int, typing.Tuple[float, int, typing.Dict[int, float]]]:
|
@@ -112,7 +110,7 @@ def get_subnet_data(subtensor: bt.subtensor, metagraph: bt.metagraph) -> typing.
|
|
112 |
|
113 |
model_data = None
|
114 |
try:
|
115 |
-
model_data = ModelData.from_compressed_str(uid, hotkey, chain_str, block, incentive, emission)
|
116 |
except:
|
117 |
continue
|
118 |
|
@@ -121,7 +119,7 @@ def get_subnet_data(subtensor: bt.subtensor, metagraph: bt.metagraph) -> typing.
|
|
121 |
|
122 |
def floatable(x) -> bool:
|
123 |
return (isinstance(x, float) and not math.isnan(x) and not math.isinf(x)) or isinstance(x, int)
|
124 |
-
|
125 |
def get_float_score(key: str, history) -> typing.Tuple[typing.Optional[float], bool]:
|
126 |
if key in history:
|
127 |
data = list(history[key])
|
@@ -144,32 +142,6 @@ def get_sample(uid, history) -> typing.Optional[typing.Tuple[str, str]]:
|
|
144 |
return prompt, response
|
145 |
return None
|
146 |
|
147 |
-
def get_scores(uids: typing.List[int]) -> typing.Dict[int, typing.Dict[str, typing.Optional[float | str]]]:
|
148 |
-
api = wandb.Api()
|
149 |
-
runs = list(api.runs(VALIDATOR_WANDB_PROJECT))
|
150 |
-
|
151 |
-
result = {}
|
152 |
-
for run in runs:
|
153 |
-
history = run.history()
|
154 |
-
for uid in uids:
|
155 |
-
if uid in result.keys():
|
156 |
-
continue
|
157 |
-
perplexity, perplexity_fresh = get_float_score(f"perplexity_data.{uid}", history)
|
158 |
-
win_rate, win_rate_fresh = get_float_score(f"win_rate_data.{uid}", history)
|
159 |
-
win_total, win_total_fresh = get_float_score(f"win_total_data.{uid}", history)
|
160 |
-
weight, weight_fresh = get_float_score(f"weight_data.{uid}", history)
|
161 |
-
sample = get_sample(uid, history)
|
162 |
-
result[uid] = {
|
163 |
-
"perplexity": perplexity,
|
164 |
-
"win_rate": win_rate,
|
165 |
-
"win_total": win_total,
|
166 |
-
"weight": weight,
|
167 |
-
"sample": sample,
|
168 |
-
"fresh": perplexity_fresh and win_rate_fresh and win_total_fresh
|
169 |
-
}
|
170 |
-
if len(result.keys()) == len(uids):
|
171 |
-
break
|
172 |
-
return result
|
173 |
|
174 |
def format_score(uid, scores, key) -> typing.Optional[float]:
|
175 |
if uid in scores:
|
@@ -192,8 +164,6 @@ tao_price = get_tao_price()
|
|
192 |
leaderboard_df = get_subnet_data(subtensor, metagraph)
|
193 |
leaderboard_df.sort(key=lambda x: x.incentive, reverse=True)
|
194 |
|
195 |
-
scores = get_scores([x.uid for x in leaderboard_df])
|
196 |
-
|
197 |
current_block = metagraph.block.item()
|
198 |
next_update = next_tempo(
|
199 |
SUBNET_START_BLOCK,
|
@@ -214,19 +184,6 @@ def get_next_update():
|
|
214 |
delta = next_update_time - now
|
215 |
return f"""<div align="center" style="font-size: larger;">Next reward update: <b>{blocks_to_go}</b> blocks (~{int(delta.total_seconds() // 60)} minutes)</div>"""
|
216 |
|
217 |
-
def leaderboard_data(show_stale: bool):
|
218 |
-
value = [
|
219 |
-
[
|
220 |
-
f'[{c.namespace}/{c.name} ({c.commit[0:8]})](https://huggingface.co/{c.namespace}/{c.name}/commit/{c.commit})',
|
221 |
-
format_score(c.uid, scores, "win_rate"),
|
222 |
-
format_score(c.uid, scores, "perplexity"),
|
223 |
-
format_score(c.uid, scores, "weight"),
|
224 |
-
c.uid,
|
225 |
-
c.block
|
226 |
-
] for c in leaderboard_df if scores[c.uid]["fresh"] or show_stale
|
227 |
-
]
|
228 |
-
return value
|
229 |
-
|
230 |
demo = gr.Blocks(css=".typewriter {font-family: 'JMH Typewriter', sans-serif;}")
|
231 |
with demo:
|
232 |
gr.HTML(FONT)
|
@@ -240,7 +197,7 @@ with demo:
|
|
240 |
value={ f"{c.namespace}/{c.name} ({c.commit[0:8]}) · ${round(c.emission * tao_price, 2):,} (τ{round(c.emission, 2):,})": c.incentive for c in leaderboard_df if c.incentive},
|
241 |
num_top_classes=10,
|
242 |
)
|
243 |
-
|
244 |
with gr.Accordion("Evaluation Stats"):
|
245 |
gr.HTML(EVALUATION_HEADER)
|
246 |
|
@@ -251,27 +208,16 @@ with demo:
|
|
251 |
with gr.Tab(f"{entry.namespace}/{entry.name} ({entry.commit[0:8]})"):
|
252 |
gr.Chatbot([sample])
|
253 |
|
254 |
-
show_stale = gr.Checkbox(label="Show Stale", interactive=True)
|
255 |
-
leaderboard_table = gr.components.Dataframe(
|
256 |
-
value=leaderboard_data(show_stale.value),
|
257 |
-
headers=["Name", "Win Rate", "Perplexity", "Weight", "UID", "Block"],
|
258 |
-
datatype=["markdown", "number", "number", "number", "number", "number"],
|
259 |
-
elem_id="leaderboard-table",
|
260 |
-
interactive=False,
|
261 |
-
visible=True,
|
262 |
-
)
|
263 |
-
gr.HTML(EVALUATION_DETAILS)
|
264 |
-
show_stale.change(leaderboard_data, [show_stale], leaderboard_table)
|
265 |
|
266 |
with gr.Accordion("Validator Stats"):
|
267 |
validator_table = gr.components.Dataframe(
|
268 |
value=[
|
269 |
[uid, int(validator_df[uid][1]), round(validator_df[uid][0], 4)] + [validator_df[uid][-1].get(c.uid) for c in leaderboard_df if c.incentive]
|
270 |
for uid, _ in sorted(
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
],
|
276 |
headers=["UID", "Stake (τ)", "V-Trust"] + [f"{c.namespace}/{c.name} ({c.commit[0:8]})" for c in leaderboard_df if c.incentive],
|
277 |
datatype=["number", "number", "number"] + ["number" for c in leaderboard_df if c.incentive],
|
@@ -279,11 +225,4 @@ with demo:
|
|
279 |
visible=True,
|
280 |
)
|
281 |
|
282 |
-
def restart_space():
|
283 |
-
API.restart_space(repo_id=REPO_ID, token=H4_TOKEN)
|
284 |
-
|
285 |
-
scheduler = BackgroundScheduler()
|
286 |
-
scheduler.add_job(restart_space, "interval", seconds=60 * 15) # restart every 15 minutes
|
287 |
-
scheduler.start()
|
288 |
-
|
289 |
demo.launch()
|
|
|
4 |
from bittensor.extrinsics.serving import get_metadata
|
5 |
from dataclasses import dataclass
|
6 |
import requests
|
|
|
7 |
import math
|
8 |
import os
|
9 |
import datetime
|
10 |
import time
|
11 |
from dotenv import load_dotenv
|
12 |
from huggingface_hub import HfApi
|
|
|
13 |
|
14 |
load_dotenv()
|
15 |
|
|
|
74 |
try:
|
75 |
return float(requests.get("https://api.kucoin.com/api/v1/market/stats?symbol=TAO-USDT").json()["data"]["last"])
|
76 |
except:
|
77 |
+
if i == METAGRAPH_RETRIES - 1:
|
78 |
+
raise
|
79 |
+
time.sleep(METAGRAPH_DELAY_SECS)
|
80 |
raise RuntimeError()
|
81 |
|
82 |
def get_validator_weights(metagraph: bt.metagraph) -> typing.Dict[int, typing.Tuple[float, int, typing.Dict[int, float]]]:
|
|
|
110 |
|
111 |
model_data = None
|
112 |
try:
|
113 |
+
model_data = ModelData.from_compressed_str(uid, hotkey, chain_str, block, incentive, emission)
|
114 |
except:
|
115 |
continue
|
116 |
|
|
|
119 |
|
120 |
def floatable(x) -> bool:
|
121 |
return (isinstance(x, float) and not math.isnan(x) and not math.isinf(x)) or isinstance(x, int)
|
122 |
+
|
123 |
def get_float_score(key: str, history) -> typing.Tuple[typing.Optional[float], bool]:
|
124 |
if key in history:
|
125 |
data = list(history[key])
|
|
|
142 |
return prompt, response
|
143 |
return None
|
144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
def format_score(uid, scores, key) -> typing.Optional[float]:
|
147 |
if uid in scores:
|
|
|
164 |
leaderboard_df = get_subnet_data(subtensor, metagraph)
|
165 |
leaderboard_df.sort(key=lambda x: x.incentive, reverse=True)
|
166 |
|
|
|
|
|
167 |
current_block = metagraph.block.item()
|
168 |
next_update = next_tempo(
|
169 |
SUBNET_START_BLOCK,
|
|
|
184 |
delta = next_update_time - now
|
185 |
return f"""<div align="center" style="font-size: larger;">Next reward update: <b>{blocks_to_go}</b> blocks (~{int(delta.total_seconds() // 60)} minutes)</div>"""
|
186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
demo = gr.Blocks(css=".typewriter {font-family: 'JMH Typewriter', sans-serif;}")
|
188 |
with demo:
|
189 |
gr.HTML(FONT)
|
|
|
197 |
value={ f"{c.namespace}/{c.name} ({c.commit[0:8]}) · ${round(c.emission * tao_price, 2):,} (τ{round(c.emission, 2):,})": c.incentive for c in leaderboard_df if c.incentive},
|
198 |
num_top_classes=10,
|
199 |
)
|
200 |
+
|
201 |
with gr.Accordion("Evaluation Stats"):
|
202 |
gr.HTML(EVALUATION_HEADER)
|
203 |
|
|
|
208 |
with gr.Tab(f"{entry.namespace}/{entry.name} ({entry.commit[0:8]})"):
|
209 |
gr.Chatbot([sample])
|
210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
with gr.Accordion("Validator Stats"):
|
213 |
validator_table = gr.components.Dataframe(
|
214 |
value=[
|
215 |
[uid, int(validator_df[uid][1]), round(validator_df[uid][0], 4)] + [validator_df[uid][-1].get(c.uid) for c in leaderboard_df if c.incentive]
|
216 |
for uid, _ in sorted(
|
217 |
+
zip(validator_df.keys(), [validator_df[x][1] for x in validator_df.keys()]),
|
218 |
+
key=lambda x: x[1],
|
219 |
+
reverse=True
|
220 |
+
)
|
221 |
],
|
222 |
headers=["UID", "Stake (τ)", "V-Trust"] + [f"{c.namespace}/{c.name} ({c.commit[0:8]})" for c in leaderboard_df if c.incentive],
|
223 |
datatype=["number", "number", "number"] + ["number" for c in leaderboard_df if c.incentive],
|
|
|
225 |
visible=True,
|
226 |
)
|
227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
demo.launch()
|