import gradio as gr import requests import asyncio from typing import Any, Iterable from gradio.themes.base import Base from gradio.themes.utils import colors, fonts, sizes from gradio.themes.utils.colors import Color from communex.client import CommuneClient from communex.misc import get_map_modules from typing import Any, cast import aiohttp import time FONT = """""" HEADER = """
This leaderboard showcases the top-performing miners in the Nya - CommuneAI Compute Subnet. The models are ranked based on their daily rewards.
Nya is a subnet dedicated to computing. Nya means purpose in Swahili. Training models like ChatGPT and Llama costs approximately $300 million, requiring billion-dollar datacenters. This expense is a significant barrier for many researchers. Inspired by initiatives like Folding at Home and Learning at Home, we envision a decentralized network that democratizes AI research and development through a shared mission and purpose. During the global pandemic, Folding at Home saw such an influx of participants that its computing power exceeded the world's largest supercomputers. Recognizing this potential, we are developing technology to enable the decentralized training of large models, benefiting all participants. We believe that the network's value will grow as more participants join and as we continue to release new models and technologies.
In this subnet, miners are rewarded for completing assigned tasks, which involve machine learning computations instead of solving cryptographic puzzles like in Bitcoin mining. The results are validated to ensure correctness and maintain network integrity. Rewards are distributed based on each miner's share of the total computation performed, meaning the more computation a miner completes, the more rewards they receive.
""" EVALUATION_HEADER = """Name represents the model name. Rewards / Day indicates the expected daily rewards for each model in $COMAI. UID is the unique identifier of the miner. $USD Value is the estimated dollar value of the daily rewards.
""" netuid = 18 node_url = "wss://commune-api-node-2.communeai.net" def get_validator_uids(client, netuid): modules = cast(dict[str, Any], get_map_modules(client, netuid=netuid)) modules = [value for _, value in modules.items()] validator_uids = [] for module in modules: if not (module["incentive"] == module["dividends"] == 0 or module["incentive"] > module["dividends"]): validator_uids.append(int(module['uid'])) return validator_uids async def get_com_price(session: aiohttp.ClientSession) -> float: try: async with session.get("https://api.mexc.com/api/v3/avgPrice?symbol=COMAIUSDT") as response: response.raise_for_status() price = float((await response.json())["price"]) print(f"Fetched COM price: {price}") return price except Exception as e: print(f"Error fetching COM price: {e}") raise RuntimeError("Failed to fetch COM price") async def make_query(client: CommuneClient) -> tuple[dict[int, int], dict[int, str]]: request_dict = { "SubspaceModule": [ ("Name", [netuid]), ("Emission", []), ("Incentive", []), ("Dividends", []), ], } emission_dict = {} name_dict = {} result = client.query_batch_map(request_dict) print("Query result:", result) emission = result["Emission"] netuid_emission = emission[netuid] incentive = result["Incentive"] netuid_incentive = incentive[netuid] dividends = result["Dividends"] netuid_dividends = dividends[netuid] names = result["Name"] highest_uid = max(names.keys()) validator_uids = get_validator_uids(client, netuid) for uid in range(highest_uid + 1): if uid in validator_uids: continue emission = netuid_emission[uid] if emission != 0: incentive = netuid_incentive[uid] dividends = netuid_dividends[uid] if incentive > 0: emission_dict[uid] = netuid_emission[uid] name_dict[uid] = names[uid] print("Emission dict:", emission_dict) print("Name dict:", name_dict) return emission_dict, name_dict async def get_leaderboard_data(): async with aiohttp.ClientSession() as session: com_price = await get_com_price(session) blocks_in_day = 10_800 client = CommuneClient(node_url) emission_dict, name_dict = await make_query(client) print("Got the emission") scores = {} for uid, emi in emission_dict.items(): scores[uid] = (emi / 10**11) * blocks_in_day sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True) leaderboard_data = [] for rank, (uid, score) in enumerate(sorted_scores, start=1): name = name_dict[uid] units = score usd_value = score * com_price leaderboard_data.append((rank, uid, name, units, f"${usd_value:.2f}")) print("Leaderboard data:", leaderboard_data) return leaderboard_data async def update_leaderboard_table(): start_time = time.time() leaderboard_data = await get_leaderboard_data() leaderboard_data = [list(row) for row in leaderboard_data] for row in leaderboard_data: row[0] = f"{row[0]} 🏆" total_usd_value = sum(float(row[4][1:]) for row in leaderboard_data) rewards_per_week = total_usd_value * 7 rewards_per_month = total_usd_value * 30 print(f"Updated leaderboard in {time.time() - start_time:.2f} seconds") return leaderboard_data, f'''