# Code adapted from: https://huggingface.co/spaces/RaoFoundation/pretraining-leaderboard/blob/main/app.py
import os
import datetime
from typing import Dict
import gradio as gr
from dotenv import load_dotenv
from huggingface_hub import HfApi
from apscheduler.schedulers.background import BackgroundScheduler
import competitions
import utils
FONT = (
""""""
)
TITLE = """
Finetuning Subnet Leaderboard
"""
HEADER = """
Finetuning is a Bittensor subnet that rewards miners for producing finetuned models in defined competitions. The model with the best head-to-head score in each competition receive a steady emission of TAO."""
EVALUATION_DETAILS = """
Name: the 🤗 Hugging Face model name (click to go to the model card)
Rewards / Day: the expected rewards per day based on current ranking.
Last Average Loss: the last loss value on the evaluation data for the model as calculated by a validator (lower is better)
UID: the Bittensor UID of the miner
Block: the Bittensor block that the model was submitted in
More stats on taostats."""
EVALUATION_HEADER = """
Shows the latest internal evaluation statistics as calculated by the Opentensor validator
Last Updated: {datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")} (UTC)
"""
def restart_space():
API.restart_space(repo_id=HF_REPO_ID, token=HF_TOKEN)
def main():
# To avoid leaderboard failures, infinitely try until we get all data
# needed to populate the dashboard
state_vars = utils.load_state_vars()
model_data = state_vars["model_data"]
vali_runs = state_vars["vali_runs"]
scores = state_vars["scores"]
validator_df = state_vars["validator_df"]
benchmarks = state_vars.get("benchmarks", None)
benchmark_timestamp = state_vars.get("benchmark_timestamp", None)
demo = gr.Blocks(css=".typewriter {font-family: 'JMH Typewriter', sans-serif;}")
with demo:
gr.HTML(FONT)
gr.HTML(TITLE)
gr.HTML(HEADER)
# TODO: Re-enable once ""SubtensorModule.BlocksSinceEpoch" not found" issue is resolved.
# gr.HTML(value=get_next_update_div(current_block, next_epoch_block))
# TODO: Figure out the best approach to showing the per competition rewards.
gr.Label(
value={
f"{c.namespace}/{c.name} ({c.commit[0:8]}) · (τ{round(c.emission, 2):,})": c.incentive
for c in model_data
if c.incentive
},
num_top_classes=10,
)
if benchmarks is not None:
with gr.Accordion("Top Model Benchmarks"):
gr.components.Dataframe(benchmarks)
gr.HTML("""
PPL computed using a stride of 512. See here for the full code.
""")
gr.HTML(f"""
Last Updated: {benchmark_timestamp.strftime("%Y-%m-%d %H:%M:%S")} (UTC)
""")
with gr.Accordion("Evaluation Stats"):
gr.HTML(EVALUATION_HEADER)
show_stale = gr.Checkbox(label="Show Stale", interactive=True)
competition_leaderboards = []
# TODO: Dynamically generate per-competition leaderboards based on model_data.
competition_details = competitions.COMPETITION_DETAILS[1]
with gr.Accordion(f"{competition_details.name} competition"):
gr.HTML(competition_details.html_description)
competition_leaderboards.append(gr.components.Dataframe(
value=utils.leaderboard_data(model_data, scores, show_stale.value),
headers=["Name", "Win Rate", "Average Loss", "Weight", "UID", "Block"],
datatype=["markdown", "number", "number", "number", "number", "number"],
elem_id="leaderboard-table",
interactive=False,
visible=True,
))
gr.HTML(EVALUATION_DETAILS)
show_stale.change(
lambda stale: utils.leaderboard_data(model_data, scores, stale),
inputs=[show_stale],
outputs=competition_leaderboards,
)
# TODO: Make this a multi-competition line plot
gr.LinePlot(
utils.get_losses_over_time(vali_runs),
x="timestamp",
x_title="Date",
y="SN9_MODEL",
y_title="Average Loss",
tooltip="SN9_MODEL",
interactive=True,
visible=True,
width=1024,
title="Best Average Loss Over Time",
)
with gr.Accordion("Validator Stats"):
gr.components.Dataframe(
utils.make_validator_dataframe(validator_df, model_data),
interactive=False,
visible=True,
)
gr.HTML(value=get_last_updated_div())
scheduler = BackgroundScheduler()
scheduler.add_job(
restart_space, "interval", seconds=60 * 30
) # restart every 15 minutes
scheduler.start()
demo.launch()
main()