File size: 9,604 Bytes
aff7ba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
646d9e2
aff7ba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a749388
 
aff7ba4
 
 
 
 
 
 
 
 
 
a749388
aff7ba4
a749388
 
aff7ba4
a749388
aff7ba4
a749388
 
aff7ba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a749388
aff7ba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0cb5d5
 
 
 
 
 
 
 
 
 
aff7ba4
e0cb5d5
aff7ba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0c4f8a
aff7ba4
f0c4f8a
aff7ba4
 
 
 
 
a749388
aff7ba4
 
a749388
aff7ba4
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
import aiohttp

FONT = """<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap" rel="stylesheet">"""
HEADER = """
<h2 align="center" class="typewriter">Welcome to the Zangief - CommuneAI Translation Subnet Leaderboard!</h2>
<p align="center">This leaderboard showcases the top-performing miners in the Zangief - CommuneAI Translation Subnet. The models are ranked based on their daily rewards.</p>
<p align="center">Zangief is a subnet dedicated to language translation. The goal of the subnet is to collectively bootstrap a language translation application that supports dozens of different languages, communication styles, and specific areas of expertise.</p>
<p align="center">The actors that power the subnet are the miners and validators. The validators generate source material to be translated and pass the source material to the miners. The miners run web services that respond to the given source input with high quality translation. The miners also respond to queries that are served from an end-user application. Over time, the validators will also curate high quality translations to the source material which itself will be cleaned and compiled into a dataset. The dataset that is produced from the mining and validating activity on the subnet will be open source. This dataset can be used to train models or provide useful translations for subtitles or other online media.</p>
"""
EVALUATION_HEADER = """<h3 align="center">Evaluation Details</h3>"""
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>"""
LANGUAGES_SUPPORTED = """
<h3 align="center">Languages Supported</h3>
<p align="center">Arabic, Chinese, English, French, German, Hebrew, Hindi, Portuguese, Russian, Spanish, Urdu, Vietnamese, and more to come!</p>
"""
netuid = 1
node_url = "wss://commune-api-node-2.communeai.net"

async def get_com_price(session: aiohttp.ClientSession) -> Any:
    retries = 5
    for i in range(retries):
        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}")
            await asyncio.sleep(retries)
    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())
    for uid in range(highest_uid + 1):
        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:
        try:
            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
        except Exception as e:
            print(f"Error fetching leaderboard data: {e}")
            return []

async def update_leaderboard_table():
    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
    return leaderboard_data, f'''
    <div style="display: flex; justify-content: space-between; align-items: center; font-size: 16px; font-weight: bold;">
        <div>๐™๐™ค๐™ฉ๐™–๐™ก $ ๐™ˆ๐™ž๐™ฃ๐™ž๐™ฃ๐™œ ๐™๐™š๐™ฌ๐™–๐™ง๐™™๐™จ ๐™‹๐™š๐™ง ๐™’๐™š๐™š๐™ : ${rewards_per_week:,.0f}</div>
        <div>๐™๐™ค๐™ฉ๐™–๐™ก $ ๐™ˆ๐™ž๐™ฃ๐™ž๐™ฃ๐™œ ๐™๐™š๐™ฌ๐™–๐™ง๐™™๐™จ ๐™‹๐™š๐™ง ๐˜ฟ๐™–๐™ฎ: ${total_usd_value:,.0f}</div>
        <div>๐™๐™ค๐™ฉ๐™–๐™ก $ ๐™ˆ๐™ž๐™ฃ๐™ž๐™ฃ๐™œ ๐™๐™š๐™ฌ๐™–๐™ง๐™™๐™จ ๐™‹๐™š๐™ง ๐™ˆ๐™ค๐™ฃ๐™ฉ๐™: ${rewards_per_month:,.0f}</div>
    </div>
    '''

stone_gray = Color(
    name="stone_gray",
    c50="#f5f5f5",
    c100="#e9e9e9",
    c200="#d9d9d9",
    c300="#c4c4c4",
    c400="#a6a6a6",
    c500="#8f8f8f",
    c600="#737373",
    c700="#595959",
    c800="#464646",
    c900="#262626",
    c950="#1a1a1a",
)

class Seafoam(Base):
    def __init__(
        self,
        primary_hue: colors.Color | str = stone_gray,
        secondary_hue: colors.Color | str = stone_gray,
        neutral_hue: colors.Color | str = stone_gray,
        spacing_size: sizes.Size | str = sizes.spacing_md,
        radius_size: sizes.Size | str = sizes.radius_md,
        text_size: sizes.Size | str = sizes.text_lg,
        font: fonts.Font | str | Iterable[fonts.Font | str] = (
            fonts.GoogleFont("Quicksand"),
            "ui-sans-serif",
            "sans-serif",
        ),
        font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
            fonts.GoogleFont("IBM Plex Mono"),
            "ui-monospace",
            "monospace",
        ),
    ):
        super().__init__(
            primary_hue=primary_hue,
            secondary_hue=secondary_hue,
            neutral_hue=neutral_hue,
            spacing_size=spacing_size,
            radius_size=radius_size,
            text_size=text_size,
            font=font,
            font_mono=font_mono,
        )
        super().set(
            block_title_text_weight="600",
            block_border_width="3px",
            block_shadow="*shadow_drop_lg",
            button_shadow="*shadow_drop_lg",
            button_large_padding="32px",
            button_primary_background_fill_hover="*button_primary_background_fill",
            button_primary_background_fill="#333333",
            button_primary_text_color="#ffffff",
        )

seafoam = Seafoam()

custom_css = """
#custom-image {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
}
"""

with gr.Blocks(theme=seafoam, analytics_enabled=True, css=custom_css) as demo:
    gr.HTML(FONT)
    gr.Image("zang_pi.png", elem_id="custom-image")
    gr.HTML(HEADER)
    gr.HTML(EVALUATION_HEADER)
    gr.HTML(EVALUATION_DETAILS)
    gr.HTML(LANGUAGES_SUPPORTED)

    total_usd_value_html = gr.HTML(
        elem_id="total-usd-value",
        value='''
            <div style="display: flex; justify-content: space-between; align-items: center; font-size: 16px; font-weight: bold;">
                <div>๐™๐™ค๐™ฉ๐™–๐™ก $ ๐™ˆ๐™ž๐™ฃ๐™ž๐™ฃ๐™œ ๐™๐™š๐™ฌ๐™–๐™ง๐™™๐™จ ๐™‹๐™š๐™ง ๐™’๐™š๐™š๐™ : $0.00</div>
                <div>๐™๐™ค๐™ฉ๐™–๐™ก $ ๐™ˆ๐™ž๐™ฃ๐™ž๐™ฃ๐™œ ๐™๐™š๐™ฌ๐™–๐™ง๐™™๐™จ ๐™‹๐™š๐™ง ๐˜ฟ๐™–๐™ฎ: $0.00</div>
                <div>๐™๐™ค๐™ฉ๐™–๐™ก $ ๐™ˆ๐™ž๐™ฃ๐™ž๐™ฃ๐™œ ๐™๐™š๐™ฌ๐™–๐™ง๐™™๐™จ ๐™‹๐™š๐™ง ๐™ˆ๐™ค๐™ฃ๐™ฉ๐™: $0.00</div>
            </div>
        '''
    )
    leaderboard_table = gr.Dataframe(
        headers=["Rank ๐Ÿ†", "UID", "Name", "Rewards / Day", "$USD Value"],
        datatype=["str", "str", "str", "str", "str"],
        interactive=False,
        visible=True,
        elem_id="leaderboard-table",
    )
    refresh_button = gr.Button("Refresh Leaderboard")
    refresh_button.click(fn=update_leaderboard_table, outputs=[
                         leaderboard_table, total_usd_value_html])

    demo.load(update_leaderboard_table, inputs=None, outputs=[
              leaderboard_table, total_usd_value_html])

if __name__ == "__main__":
    demo.launch()