bulubula's picture
update leader info fix
fe0c719
raw
history blame
7.15 kB
import streamlit as st
import wandb
import pandas as pd
import os
import time
import datetime
from utils import fetch_competition_summary, fetch_models_evaluation, get_all_competition_summary, highlight_score_column
# from dotenv import load_dotenv
from cfg import *
# load_dotenv()
st.set_page_config(layout="wide")
table_font_size = 16
st.markdown(TABLE_STYLE, unsafe_allow_html=True)
### WANDB
# Access the API key from the environment variable
wandb_api_key = os.getenv('WANDB_API_KEY')
# Log in to wandb using the API key
if wandb_api_key:
wandb.login(key=wandb_api_key)
# wandb.login()
else:
st.error("WANDB_API_KEY not found in environment variables.")
wandb_api = wandb.Api()
@st.cache_data
def update_leader_info(leader_info, competition, best_model):
if leader_info.get(competition) is None:
leader_info[competition] = {
"Username": best_model["Username"],
"Hotkey": best_model["Hotkey"],
"Date": time.strftime("%Y-%m-%d"),
"Days on Top": 1
}
else:
if leader_info[competition]["Hotkey"] == best_model["Hotkey"]:
# count the number of days on top
start_date = datetime.datetime.strptime(leader_info[competition]["Date"], "%Y-%m-%d")
current_date = datetime.datetime.now()
days_on_top = (current_date - start_date).days
leader_info[competition]["Days on Top"] = days_on_top + 1
else:
leader_info[competition]["Username"] = best_model["Username"]
leader_info[competition]["Hotkey"] = best_model["Hotkey"]
leader_info[competition]["Date"] = time.strftime("%Y-%m-%d")
leader_info[competition]["Days on Top"] = 1
return leader_info[competition]
@st.cache_data()
def load_competition_data(last_update_time=None):
competition_summaries = {}
model_evaluations = {}
for competition in COMPETITIONS:
competition_summaries[competition] = fetch_competition_summary(wandb_api, COMPETITIONS, competition)
model_evaluations[competition] = fetch_models_evaluation(wandb_api, COMPETITIONS, competition)
last_update_time = time.time()
return competition_summaries, model_evaluations, last_update_time
# Streamlit app main function
def main():
st.markdown(HEADER, unsafe_allow_html=True)
if 'last_update_time' not in st.session_state:
st.session_state.last_update_time = None
if "leader_info" not in st.session_state:
st.session_state.leader_info = {}
if 'selected_competition' not in st.session_state:
st.session_state.selected_competition = None
if st.session_state.last_update_time is None or (time.time() - st.session_state.last_update_time > UPDATE_INTERVAL):
competition_summaries, model_evaluations, st.session_state.last_update_time = load_competition_data(st.session_state.last_update_time)
for competition in COMPETITIONS:
if not competition_summaries[competition].empty:
# get all winning hotkeys and number of wins
winning_hotkeys = competition_summaries[competition]["Winning Hotkey"].value_counts()
# if not empty, get the best hotkey
if not winning_hotkeys.empty:
best_hotkey = winning_hotkeys.idxmax()
# get the best model with the winning hotkey
best_model = model_evaluations[competition][model_evaluations[competition]["Hotkey"] == best_hotkey].iloc[0]
st.session_state.leader_info[competition] = update_leader_info(st.session_state.leader_info, competition, best_model)
else:
st.session_state.leader_info[competition] = {
"Username": "N/A",
"Hotkey": "N/A",
"Date": "N/A",
"UID": "N/A",
"Days on Top": "N/A"
}
else:
competition_summaries, model_evaluations, _ = load_competition_data(st.session_state.last_update_time)
number_of_competitions, number_of_runs = get_all_competition_summary(wandb_api, COMPETITIONS)
last_updated = datetime.datetime.fromtimestamp(st.session_state.last_update_time).strftime('%Y-%m-%d %H:%M:%S')
# Set the desired font size here (in pixels)
summary_font_size = 14 # You can adjust this value as needed
st.subheader("###")
st.markdown("<h2 style='text-align: center; font-size: 28px;'>Competitions</h2>", unsafe_allow_html=True)
st.write("#### Select a competition to view more details and rankings.")
# Create a header for the table
cols = st.columns([1, 3, 2, 2, 2, 1, 2])
headers = ["Index", "Competition Name", "Leader", "Date", "Hotkey", "Days on Top"]
for col, header in zip(cols, headers):
col.write(header)
for index, (competition, details) in enumerate(COMPETITIONS.items(), start=1):
leader_info = st.session_state.get("leader_info", {}).get(competition, {})
cols = st.columns([1, 3, 2, 2, 2, 1, 2])
cols[0].write(index)
if cols[1].button(competition):
st.session_state.selected_competition = competition
cols[2].write(leader_info.get("Username", "N/A"))
cols[3].write(leader_info.get("Date", "N/A"))
cols[4].write(leader_info.get("Hotkey", "N/A"))
cols[5].write(leader_info.get("Days on Top", "N/A"))
if st.session_state.selected_competition:
competition_name = st.session_state.selected_competition
competition_details = COMPETITIONS.get(competition_name, {})
description = competition_details.get("description", "No description available.")
st.subheader(f"Competition: {competition_name}")
st.write(description)
st.subheader("Competition Summary")
competition_summary_df = competition_summaries.get(competition_name, pd.DataFrame())
if not competition_summary_df.empty:
st.dataframe(competition_summary_df, height=500)
else:
st.warning("No competition summary data available.")
st.subheader("Models Evaluation")
models_evaluation_df = model_evaluations.get(competition_name, pd.DataFrame())
if not models_evaluation_df.empty:
st.dataframe(models_evaluation_df.style.apply(highlight_score_column, axis=0))
else:
st.warning("No models evaluation data available.")
else:
st.write("Please select a competition to view details.")
st.subheader("Summary information")
st.markdown(f"""
<div class="table-container">
<table class="summary-table">
<tr>
<th>Number of competitions</th>
<th>Number of models run</th>
<th>Last updated</th>
</tr>
<tr>
<td>{number_of_competitions}</td>
<td>{number_of_runs}</td>
<td>{last_updated}</td>
</tr>
</table>
</div>
""", unsafe_allow_html=True)
# Run the app
if __name__ == "__main__":
main()