Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,9 +4,8 @@ import time
|
|
4 |
from typing import Any
|
5 |
from communex.client import CommuneClient
|
6 |
from communex.balance import from_nano
|
7 |
-
import json
|
8 |
|
9 |
-
netuid =
|
10 |
node_url = "wss://commune.api.onfinality.io/public-ws"
|
11 |
|
12 |
def get_com_price() -> Any:
|
@@ -21,37 +20,26 @@ def get_com_price() -> Any:
|
|
21 |
time.sleep(retries)
|
22 |
raise RuntimeError()
|
23 |
|
24 |
-
def
|
25 |
com_price = get_com_price()
|
26 |
blocks_in_day = 10_800
|
27 |
client = CommuneClient(node_url)
|
28 |
emission = client.query("Emission", params=[netuid])
|
29 |
-
|
30 |
for uid, emi in enumerate(emission):
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
def load_leaderboard():
|
35 |
-
with open("leaderboard.json", "r") as f:
|
36 |
-
leaderboard_data = json.load(f)
|
37 |
-
return leaderboard_data
|
38 |
-
|
39 |
-
def save_leaderboard():
|
40 |
-
scores = get_score()
|
41 |
sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)
|
42 |
leaderboard_data = []
|
43 |
for rank, (uid, score) in enumerate(sorted_scores, start=1):
|
44 |
leaderboard_data.append({"rank": rank, "uid": uid, "score": score})
|
45 |
-
|
46 |
-
|
47 |
|
48 |
with gr.Blocks() as demo:
|
49 |
gr.Markdown("# Commune Leaderboard")
|
50 |
|
51 |
-
leaderboard_data = load_leaderboard()
|
52 |
-
|
53 |
leaderboard_table = gr.components.DataFrame(
|
54 |
-
value=leaderboard_data,
|
55 |
headers=["rank", "uid", "score"],
|
56 |
datatype=["number", "number", "number"],
|
57 |
interactive=False,
|
@@ -61,11 +49,14 @@ with gr.Blocks() as demo:
|
|
61 |
refresh_button = gr.Button("Refresh Leaderboard")
|
62 |
|
63 |
def refresh_leaderboard():
|
64 |
-
|
65 |
-
|
66 |
-
return gr.components.DataFrame.update(value=updated_data)
|
67 |
|
68 |
refresh_button.click(fn=refresh_leaderboard, outputs=leaderboard_table)
|
|
|
|
|
|
|
|
|
69 |
|
70 |
if __name__ == "__main__":
|
71 |
demo.launch()
|
|
|
4 |
from typing import Any
|
5 |
from communex.client import CommuneClient
|
6 |
from communex.balance import from_nano
|
|
|
7 |
|
8 |
+
netuid = 1
|
9 |
node_url = "wss://commune.api.onfinality.io/public-ws"
|
10 |
|
11 |
def get_com_price() -> Any:
|
|
|
20 |
time.sleep(retries)
|
21 |
raise RuntimeError()
|
22 |
|
23 |
+
def get_leaderboard_data():
|
24 |
com_price = get_com_price()
|
25 |
blocks_in_day = 10_800
|
26 |
client = CommuneClient(node_url)
|
27 |
emission = client.query("Emission", params=[netuid])
|
28 |
+
scores = {}
|
29 |
for uid, emi in enumerate(emission):
|
30 |
+
scores[uid] = ((emi / 10**11) * blocks_in_day) * com_price
|
31 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)
|
33 |
leaderboard_data = []
|
34 |
for rank, (uid, score) in enumerate(sorted_scores, start=1):
|
35 |
leaderboard_data.append({"rank": rank, "uid": uid, "score": score})
|
36 |
+
|
37 |
+
return leaderboard_data
|
38 |
|
39 |
with gr.Blocks() as demo:
|
40 |
gr.Markdown("# Commune Leaderboard")
|
41 |
|
|
|
|
|
42 |
leaderboard_table = gr.components.DataFrame(
|
|
|
43 |
headers=["rank", "uid", "score"],
|
44 |
datatype=["number", "number", "number"],
|
45 |
interactive=False,
|
|
|
49 |
refresh_button = gr.Button("Refresh Leaderboard")
|
50 |
|
51 |
def refresh_leaderboard():
|
52 |
+
leaderboard_data = get_leaderboard_data()
|
53 |
+
return gr.components.DataFrame.update(value=leaderboard_data)
|
|
|
54 |
|
55 |
refresh_button.click(fn=refresh_leaderboard, outputs=leaderboard_table)
|
56 |
+
|
57 |
+
# Initial load of leaderboard data
|
58 |
+
leaderboard_data = get_leaderboard_data()
|
59 |
+
leaderboard_table.value = leaderboard_data
|
60 |
|
61 |
if __name__ == "__main__":
|
62 |
demo.launch()
|