Spaces:
Runtime error
Runtime error
ashikshaffi08
commited on
used aiohttp for getting the http requests
Browse files1. Replaced synchronous requests.get with asynchronous aiohttp.ClientSession.
2. made all function async and optimized data processing
app.py
CHANGED
@@ -1,51 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import asyncio
|
4 |
-
from typing import Any
|
5 |
from gradio.themes.base import Base
|
6 |
from gradio.themes.utils import colors, fonts, sizes
|
7 |
from gradio.themes.utils.colors import Color
|
8 |
-
from typing import Iterable
|
9 |
from communex.client import CommuneClient
|
|
|
10 |
|
11 |
FONT = """<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap" rel="stylesheet">"""
|
12 |
-
|
13 |
IMAGE = """<p align="center" style="font-size: 72px;">๐๐๐๐๐
๐๐</p>"""
|
14 |
-
|
15 |
HEADER = """
|
16 |
<h2 align="center" class="typewriter">Welcome to the Synthia Commune Leaderboard!</h2>
|
17 |
<p align="center">This leaderboard showcases the top-performing miners in the Synthia Commune Subnet. The models are ranked based on their daily rewards.</p>
|
18 |
<p align="center">The Synthia subnet leverages Commune's incentives to create a permissionless mining market around distilling knowledge out of SOTA closed-source model APIs into a public dataset to accelerate the OpenSource AI space. Targeted models and strategy will adapt based on the current SOTA.</p>
|
19 |
"""
|
20 |
-
|
21 |
EVALUATION_HEADER = """<h3 align="center">Evaluation Details</h3>"""
|
22 |
-
|
23 |
EVALUATION_DETAILS = """<p align="center"><b>Name</b> represents the model name. <b>Rewards / Day</b> indicates the expected daily rewards for each model in <b>$COMAI</b>. <b>UID</b> is the unique identifier of the miner. <b>$USD Value</b> is the estimated dollar value of the daily rewards.</p>"""
|
24 |
netuid = 3
|
25 |
node_url = "wss://commune-api-node-2.communeai.net"
|
26 |
|
27 |
-
|
28 |
-
def get_com_price() -> Any:
|
29 |
retries = 5
|
30 |
-
for i in range(
|
31 |
try:
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
print(f"Fetched COM price: {price}")
|
38 |
-
return price
|
39 |
except Exception as e:
|
40 |
print(f"Error fetching COM price: {e}")
|
41 |
-
|
42 |
-
raise
|
43 |
-
_ = asyncio.sleep(retries)
|
44 |
raise RuntimeError("Failed to fetch COM price")
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
request_dict: dict[Any, Any] = {
|
49 |
"SubspaceModule": [
|
50 |
("Name", [netuid]),
|
51 |
("Emission", []),
|
@@ -53,8 +43,8 @@ def make_query(client: CommuneClient) -> tuple[dict[int, int], dict[int, str]]:
|
|
53 |
("Dividends", []),
|
54 |
],
|
55 |
}
|
56 |
-
emission_dict
|
57 |
-
name_dict
|
58 |
result = client.query_batch_map(request_dict)
|
59 |
|
60 |
emission = result["Emission"]
|
@@ -67,63 +57,47 @@ def make_query(client: CommuneClient) -> tuple[dict[int, int], dict[int, str]]:
|
|
67 |
names = result["Name"]
|
68 |
highest_uid = max(names.keys())
|
69 |
for uid in range(highest_uid + 1):
|
70 |
-
|
71 |
emission = netuid_emission[uid]
|
72 |
-
|
73 |
if emission != 0:
|
74 |
incentive = netuid_incentive[uid]
|
75 |
dividends = netuid_dividends[uid]
|
76 |
-
|
77 |
if incentive > 0:
|
78 |
emission_dict[uid] = netuid_emission[uid]
|
79 |
name_dict[uid] = names[uid]
|
80 |
-
|
81 |
return emission_dict, name_dict
|
82 |
|
83 |
-
|
84 |
async def get_leaderboard_data():
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
except Exception as e:
|
107 |
-
print(f"Error fetching leaderboard data: {e}")
|
108 |
-
return []
|
109 |
-
|
110 |
|
111 |
async def update_leaderboard_table():
|
112 |
leaderboard_data = await get_leaderboard_data()
|
113 |
-
# Convert tuples to lists
|
114 |
leaderboard_data = [list(row) for row in leaderboard_data]
|
115 |
-
# Add emojis to the leaderboard data
|
116 |
for row in leaderboard_data:
|
117 |
row[0] = f"{row[0]} ๐"
|
118 |
-
|
119 |
-
# Calculate the total USD value
|
120 |
total_usd_value = sum(float(row[4][1:]) for row in leaderboard_data)
|
121 |
-
|
122 |
-
# Calculate rewards per week and per month
|
123 |
rewards_per_week = total_usd_value * 7
|
124 |
rewards_per_month = total_usd_value * 30
|
125 |
-
|
126 |
-
return leaderboard_data, f'''
|
127 |
<div style="display: flex; justify-content: space-between; align-items: center; font-size: 16px; font-weight: bold;">
|
128 |
<div>๐๐ค๐ฉ๐๐ก $ ๐๐๐ฃ๐๐ฃ๐ ๐๐๐ฌ๐๐ง๐๐จ ๐๐๐ง ๐๐๐๐ : ${rewards_per_week:,.0f}</div>
|
129 |
<div>๐๐ค๐ฉ๐๐ก $ ๐๐๐ฃ๐๐ฃ๐ ๐๐๐ฌ๐๐ง๐๐จ ๐๐๐ง ๐ฟ๐๐ฎ: ${total_usd_value:,.0f}</div>
|
@@ -131,7 +105,6 @@ async def update_leaderboard_table():
|
|
131 |
</div>
|
132 |
'''
|
133 |
|
134 |
-
|
135 |
stone_gray = Color(
|
136 |
name="stone_gray",
|
137 |
c50="#f5f5f5",
|
@@ -147,11 +120,9 @@ stone_gray = Color(
|
|
147 |
c950="#1a1a1a",
|
148 |
)
|
149 |
|
150 |
-
|
151 |
class Seafoam(Base):
|
152 |
def __init__(
|
153 |
self,
|
154 |
-
*,
|
155 |
primary_hue: colors.Color | str = stone_gray,
|
156 |
secondary_hue: colors.Color | str = stone_gray,
|
157 |
neutral_hue: colors.Color | str = stone_gray,
|
@@ -186,12 +157,10 @@ class Seafoam(Base):
|
|
186 |
button_shadow="*shadow_drop_lg",
|
187 |
button_large_padding="32px",
|
188 |
button_primary_background_fill_hover="*button_primary_background_fill",
|
189 |
-
#
|
190 |
-
|
191 |
-
button_primary_text_color="#ffffff", # White text color
|
192 |
)
|
193 |
|
194 |
-
|
195 |
seafoam = Seafoam()
|
196 |
|
197 |
with gr.Blocks(theme=seafoam, analytics_enabled=True) as demo:
|
@@ -222,9 +191,8 @@ with gr.Blocks(theme=seafoam, analytics_enabled=True) as demo:
|
|
222 |
refresh_button.click(fn=update_leaderboard_table, outputs=[
|
223 |
leaderboard_table, total_usd_value_html])
|
224 |
|
225 |
-
# Initial load of leaderboard data
|
226 |
demo.load(update_leaderboard_table, inputs=None, outputs=[
|
227 |
leaderboard_table, total_usd_value_html])
|
228 |
|
229 |
if __name__ == "__main__":
|
230 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import asyncio
|
4 |
+
from typing import Any, Iterable
|
5 |
from gradio.themes.base import Base
|
6 |
from gradio.themes.utils import colors, fonts, sizes
|
7 |
from gradio.themes.utils.colors import Color
|
|
|
8 |
from communex.client import CommuneClient
|
9 |
+
import aiohttp
|
10 |
|
11 |
FONT = """<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap" rel="stylesheet">"""
|
|
|
12 |
IMAGE = """<p align="center" style="font-size: 72px;">๐๐๐๐๐
๐๐</p>"""
|
|
|
13 |
HEADER = """
|
14 |
<h2 align="center" class="typewriter">Welcome to the Synthia Commune Leaderboard!</h2>
|
15 |
<p align="center">This leaderboard showcases the top-performing miners in the Synthia Commune Subnet. The models are ranked based on their daily rewards.</p>
|
16 |
<p align="center">The Synthia subnet leverages Commune's incentives to create a permissionless mining market around distilling knowledge out of SOTA closed-source model APIs into a public dataset to accelerate the OpenSource AI space. Targeted models and strategy will adapt based on the current SOTA.</p>
|
17 |
"""
|
|
|
18 |
EVALUATION_HEADER = """<h3 align="center">Evaluation Details</h3>"""
|
|
|
19 |
EVALUATION_DETAILS = """<p align="center"><b>Name</b> represents the model name. <b>Rewards / Day</b> indicates the expected daily rewards for each model in <b>$COMAI</b>. <b>UID</b> is the unique identifier of the miner. <b>$USD Value</b> is the estimated dollar value of the daily rewards.</p>"""
|
20 |
netuid = 3
|
21 |
node_url = "wss://commune-api-node-2.communeai.net"
|
22 |
|
23 |
+
async def get_com_price(session: aiohttp.ClientSession) -> Any:
|
|
|
24 |
retries = 5
|
25 |
+
for i in range(retries):
|
26 |
try:
|
27 |
+
async with session.get("https://api.mexc.com/api/v3/avgPrice?symbol=COMAIUSDT") as response:
|
28 |
+
response.raise_for_status()
|
29 |
+
price = float((await response.json())["price"])
|
30 |
+
print(f"Fetched COM price: {price}")
|
31 |
+
return price
|
|
|
|
|
32 |
except Exception as e:
|
33 |
print(f"Error fetching COM price: {e}")
|
34 |
+
await asyncio.sleep(retries)
|
|
|
|
|
35 |
raise RuntimeError("Failed to fetch COM price")
|
36 |
|
37 |
+
async def make_query(client: CommuneClient) -> tuple[dict[int, int], dict[int, str]]:
|
38 |
+
request_dict = {
|
|
|
39 |
"SubspaceModule": [
|
40 |
("Name", [netuid]),
|
41 |
("Emission", []),
|
|
|
43 |
("Dividends", []),
|
44 |
],
|
45 |
}
|
46 |
+
emission_dict = {}
|
47 |
+
name_dict = {}
|
48 |
result = client.query_batch_map(request_dict)
|
49 |
|
50 |
emission = result["Emission"]
|
|
|
57 |
names = result["Name"]
|
58 |
highest_uid = max(names.keys())
|
59 |
for uid in range(highest_uid + 1):
|
|
|
60 |
emission = netuid_emission[uid]
|
|
|
61 |
if emission != 0:
|
62 |
incentive = netuid_incentive[uid]
|
63 |
dividends = netuid_dividends[uid]
|
|
|
64 |
if incentive > 0:
|
65 |
emission_dict[uid] = netuid_emission[uid]
|
66 |
name_dict[uid] = names[uid]
|
|
|
67 |
return emission_dict, name_dict
|
68 |
|
|
|
69 |
async def get_leaderboard_data():
|
70 |
+
async with aiohttp.ClientSession() as session:
|
71 |
+
try:
|
72 |
+
com_price = await get_com_price(session)
|
73 |
+
blocks_in_day = 10_800
|
74 |
+
client = CommuneClient(node_url)
|
75 |
+
emission_dict, name_dict = await make_query(client)
|
76 |
+
print("got the emission")
|
77 |
+
scores = {}
|
78 |
+
for uid, emi in emission_dict.items():
|
79 |
+
scores[uid] = (emi / 10**11) * blocks_in_day
|
80 |
+
sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)
|
81 |
+
leaderboard_data = []
|
82 |
+
for rank, (uid, score) in enumerate(sorted_scores, start=1):
|
83 |
+
name = name_dict[uid]
|
84 |
+
units = score
|
85 |
+
usd_value = score * com_price
|
86 |
+
leaderboard_data.append((rank, uid, name, units, f"${usd_value:.2f}"))
|
87 |
+
return leaderboard_data
|
88 |
+
except Exception as e:
|
89 |
+
print(f"Error fetching leaderboard data: {e}")
|
90 |
+
return []
|
|
|
|
|
|
|
|
|
91 |
|
92 |
async def update_leaderboard_table():
|
93 |
leaderboard_data = await get_leaderboard_data()
|
|
|
94 |
leaderboard_data = [list(row) for row in leaderboard_data]
|
|
|
95 |
for row in leaderboard_data:
|
96 |
row[0] = f"{row[0]} ๐"
|
|
|
|
|
97 |
total_usd_value = sum(float(row[4][1:]) for row in leaderboard_data)
|
|
|
|
|
98 |
rewards_per_week = total_usd_value * 7
|
99 |
rewards_per_month = total_usd_value * 30
|
100 |
+
return leaderboard_data, f'''
|
|
|
101 |
<div style="display: flex; justify-content: space-between; align-items: center; font-size: 16px; font-weight: bold;">
|
102 |
<div>๐๐ค๐ฉ๐๐ก $ ๐๐๐ฃ๐๐ฃ๐ ๐๐๐ฌ๐๐ง๐๐จ ๐๐๐ง ๐๐๐๐ : ${rewards_per_week:,.0f}</div>
|
103 |
<div>๐๐ค๐ฉ๐๐ก $ ๐๐๐ฃ๐๐ฃ๐ ๐๐๐ฌ๐๐ง๐๐จ ๐๐๐ง ๐ฟ๐๐ฎ: ${total_usd_value:,.0f}</div>
|
|
|
105 |
</div>
|
106 |
'''
|
107 |
|
|
|
108 |
stone_gray = Color(
|
109 |
name="stone_gray",
|
110 |
c50="#f5f5f5",
|
|
|
120 |
c950="#1a1a1a",
|
121 |
)
|
122 |
|
|
|
123 |
class Seafoam(Base):
|
124 |
def __init__(
|
125 |
self,
|
|
|
126 |
primary_hue: colors.Color | str = stone_gray,
|
127 |
secondary_hue: colors.Color | str = stone_gray,
|
128 |
neutral_hue: colors.Color | str = stone_gray,
|
|
|
157 |
button_shadow="*shadow_drop_lg",
|
158 |
button_large_padding="32px",
|
159 |
button_primary_background_fill_hover="*button_primary_background_fill",
|
160 |
+
button_primary_background_fill="#333333",
|
161 |
+
button_primary_text_color="#ffffff",
|
|
|
162 |
)
|
163 |
|
|
|
164 |
seafoam = Seafoam()
|
165 |
|
166 |
with gr.Blocks(theme=seafoam, analytics_enabled=True) as demo:
|
|
|
191 |
refresh_button.click(fn=update_leaderboard_table, outputs=[
|
192 |
leaderboard_table, total_usd_value_html])
|
193 |
|
|
|
194 |
demo.load(update_leaderboard_table, inputs=None, outputs=[
|
195 |
leaderboard_table, total_usd_value_html])
|
196 |
|
197 |
if __name__ == "__main__":
|
198 |
+
demo.launch()
|