afght12's picture
Update app.py
6f5ee6f verified
raw
history blame
2.97 kB
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()