Spaces:
Runtime error
Runtime error
File size: 2,973 Bytes
714cdab b925697 714cdab 0587701 714cdab fd8c150 714cdab fd8c150 714cdab b925697 fd8c150 714cdab b925697 fd8c150 eed803b fd8c150 ba801bf fd8c150 f68f35f b925697 07cd623 2da42f8 e07ccbf f68f35f b925697 714cdab 2da42f8 714cdab 2da42f8 e07ccbf 0587701 ba801bf 714cdab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
import gradio as gr
import requests
import asyncio
from typing import Any
from communex.client import CommuneClient
from communex.balance import from_nano
netuid = 1
node_url = "wss://commune.api.onfinality.io/public-ws"
def get_com_price() -> Any:
retries = 5
for i in range(0, retries):
try:
price = float(requests.get("https://api.mexc.com/api/v3/avgPrice?symbol=COMAIUSDT").json()["price"])
print(f"Fetched COM price: {price}")
return price
except Exception as e:
print(f"Error fetching COM price: {e}")
if i == retries - 1:
raise
asyncio.sleep(retries)
raise RuntimeError("Failed to fetch COM price")
async def get_leaderboard_data():
try:
com_price = get_com_price()
blocks_in_day = 10_800
client = CommuneClient(node_url)
emission = client.query("Emission", params=[netuid])
print("got tha emission")
scores = {}
for uid, emi in enumerate(emission):
scores[uid] = ((emi / 10**11) * blocks_in_day) * com_price
sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)
leaderboard_data = []
for rank, (uid, score) in enumerate(sorted_scores, start=1):
leaderboard_data.append((rank, uid, score))
return leaderboard_data
except Exception as e:
print(f"Error fetching leaderboard data: {e}")
return []
async def update_leaderboard_table():
leaderboard_data = await get_leaderboard_data()
return leaderboard_data
custom_css = """
body {
background-color: white;
color: black;
font-family: Arial, sans-serif;
font-size: 16px;
}
.gradio-container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.gradio-dataframe {
width: 100%;
background-color: #f5f5f5;
border-collapse: collapse;
margin-top: 20px;
}
.gradio-dataframe th,
.gradio-dataframe td {
padding: 10px;
text-align: left;
border-bottom: 1px solid #ddd;
}
.gradio-dataframe th {
background-color: #f2f2f2;
font-weight: bold;
}
.gradio-button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
margin-top: 20px;
}
.gradio-button:hover {
background-color: #45a049;
}
"""
with gr.Blocks(css=custom_css) as demo:
gr.Markdown("# Synthia Leaderboard")
leaderboard_table = gr.components.Dataframe(
headers=["rank", "uid", "score"],
datatype=["number", "number", "number"],
interactive=False,
visible=True,
elem_id="leaderboard-table"
)
refresh_button = gr.Button("Refresh Leaderboard")
refresh_button.click(fn=update_leaderboard_table, outputs=leaderboard_table)
# Initial load of leaderboard data
demo.load(update_leaderboard_table, inputs=None, outputs=leaderboard_table)
if __name__ == "__main__":
demo.launch() |