import gradio as gr import requests import time 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: return float(requests.get("https://api.mexc.com/api/v3/avgPrice?symbol=COMAIUSDT").json()["price"]) except Exception as e: print(e) if i == retries - 1: raise time.sleep(retries) raise RuntimeError() def get_leaderboard_data(): com_price = get_com_price() blocks_in_day = 10_800 client = CommuneClient(node_url) emission = client.query("Emission", params=[netuid]) 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": rank, "uid": uid, "score": score}) return leaderboard_data with gr.Blocks() as demo: gr.Markdown("# Commune Leaderboard") leaderboard_table = gr.components.DataFrame( headers=["rank", "uid", "score"], datatype=["number", "number", "number"], interactive=False, visible=True ) refresh_button = gr.Button("Refresh Leaderboard") def refresh_leaderboard(): leaderboard_data = get_leaderboard_data() return gr.components.DataFrame.update(value=leaderboard_data) refresh_button.click(fn=refresh_leaderboard, outputs=leaderboard_table) # Initial load of leaderboard data leaderboard_data = get_leaderboard_data() leaderboard_table.value = leaderboard_data if __name__ == "__main__": demo.launch()