Spaces:
Sleeping
Sleeping
dnisąpoliczone
Browse files
app.py
CHANGED
@@ -1,172 +1,176 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import wandb
|
3 |
-
import pandas as pd
|
4 |
-
import os
|
5 |
-
import time
|
6 |
-
from utils import fetch_runs_to_df, fetch_run, fetch_models_to_df
|
7 |
-
|
8 |
-
### WANDB
|
9 |
-
|
10 |
-
# Access the API key from the environment variable
|
11 |
-
wandb_api_key = os.getenv('WANDB_API_KEY')
|
12 |
-
|
13 |
-
# Log in to wandb using the API key
|
14 |
-
if wandb_api_key:
|
15 |
-
wandb.login(key=wandb_api_key)
|
16 |
-
else:
|
17 |
-
st.error("WANDB_API_KEY not found in environment variables.")
|
18 |
-
|
19 |
-
# Initialize W&B API
|
20 |
-
api = wandb.Api()
|
21 |
-
|
22 |
-
### STREAMLIT APP
|
23 |
-
|
24 |
-
# Define available projects (bookmarks)
|
25 |
-
projects = {
|
26 |
-
"Competition 1": {"entity": "urbaniak-bruno-safescanai", "project": "pytorch-intro", "description": "This is a beginner-friendly competition using PyTorch."},
|
27 |
-
"Competition 2": {"entity": "urbaniak-bruno-safescanai", "project": "basic-intro", "description": "This competition focuses on basic introduction to AI models."},
|
28 |
-
"Competition 3 (mamymodelexd)": {"entity": "urbaniak-bruno-safescanai", "project": "simple-cnn", "description": "This is an advanced competition for CNN model training."},
|
29 |
-
"Competition 4": {"entity": "urbaniak-bruno-safescanai", "project": "model-validation", "description": "This competition covers techniques for model validation."},
|
30 |
-
"Competition 5": {"entity": "safe-scan-ai", "project": "melanoma-1", "description": "This competition focuses on melanoma detection."},
|
31 |
-
# Add more projects as needed
|
32 |
-
}
|
33 |
-
|
34 |
-
# Initialize leader info
|
35 |
-
@st.cache_data
|
36 |
-
def update_leader_info(leader_info, competition, best_model):
|
37 |
-
if leader_info.get(competition) is None:
|
38 |
-
leader_info[competition] = {
|
39 |
-
"Username": best_model["Username"],
|
40 |
-
"Model Name": best_model["Model Name"],
|
41 |
-
"Hotkey": best_model["Hotkey"],
|
42 |
-
"Date": time.strftime("%Y-%m-%d"),
|
43 |
-
"UID": "xd",
|
44 |
-
"Days on Top": 1
|
45 |
-
}
|
46 |
-
else:
|
47 |
-
if leader_info[competition]["UID"] == "xd": #best_model["ID"]:
|
48 |
-
|
49 |
-
|
50 |
-
leader_info[competition]["
|
51 |
-
|
52 |
-
leader_info[competition]["
|
53 |
-
|
54 |
-
leader_info[competition]["
|
55 |
-
leader_info[competition]["
|
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 |
-
if
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
st.write("
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
cols[3]
|
123 |
-
cols[
|
124 |
-
cols[
|
125 |
-
cols[
|
126 |
-
cols[
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
cols
|
142 |
-
cols[
|
143 |
-
cols[
|
144 |
-
|
145 |
-
cols[
|
146 |
-
cols[
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
st.
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import wandb
|
3 |
+
import pandas as pd
|
4 |
+
import os
|
5 |
+
import time
|
6 |
+
from utils import fetch_runs_to_df, fetch_run, fetch_models_to_df
|
7 |
+
|
8 |
+
### WANDB
|
9 |
+
|
10 |
+
# Access the API key from the environment variable
|
11 |
+
wandb_api_key = os.getenv('WANDB_API_KEY')
|
12 |
+
|
13 |
+
# Log in to wandb using the API key
|
14 |
+
if wandb_api_key:
|
15 |
+
wandb.login(key=wandb_api_key)
|
16 |
+
else:
|
17 |
+
st.error("WANDB_API_KEY not found in environment variables.")
|
18 |
+
|
19 |
+
# Initialize W&B API
|
20 |
+
api = wandb.Api()
|
21 |
+
|
22 |
+
### STREAMLIT APP
|
23 |
+
|
24 |
+
# Define available projects (bookmarks)
|
25 |
+
projects = {
|
26 |
+
"Competition 1": {"entity": "urbaniak-bruno-safescanai", "project": "pytorch-intro", "description": "This is a beginner-friendly competition using PyTorch."},
|
27 |
+
"Competition 2": {"entity": "urbaniak-bruno-safescanai", "project": "basic-intro", "description": "This competition focuses on basic introduction to AI models."},
|
28 |
+
"Competition 3 (mamymodelexd)": {"entity": "urbaniak-bruno-safescanai", "project": "simple-cnn", "description": "This is an advanced competition for CNN model training."},
|
29 |
+
"Competition 4": {"entity": "urbaniak-bruno-safescanai", "project": "model-validation", "description": "This competition covers techniques for model validation."},
|
30 |
+
"Competition 5": {"entity": "safe-scan-ai", "project": "melanoma-1", "description": "This competition focuses on melanoma detection."},
|
31 |
+
# Add more projects as needed
|
32 |
+
}
|
33 |
+
|
34 |
+
# Initialize leader info
|
35 |
+
@st.cache_data
|
36 |
+
def update_leader_info(leader_info, competition, best_model):
|
37 |
+
if leader_info.get(competition) is None:
|
38 |
+
leader_info[competition] = {
|
39 |
+
"Username": best_model["Username"],
|
40 |
+
"Model Name": best_model["Model Name"],
|
41 |
+
"Hotkey": best_model["Hotkey"],
|
42 |
+
"Date": time.strftime("%Y-%m-%d"),
|
43 |
+
"UID": "xd",
|
44 |
+
"Days on Top": 1
|
45 |
+
}
|
46 |
+
else:
|
47 |
+
if leader_info[competition]["UID"] == "xd": #best_model["ID"]:
|
48 |
+
# update the days on top to the difference between the current date and the date of the last update
|
49 |
+
current_date = time.strftime("%Y-%m-%d")
|
50 |
+
last_update_date = leader_info[competition]["Date"]
|
51 |
+
days_on_top = (current_date - last_update_date).days
|
52 |
+
leader_info[competition]["Days on Top"] = days_on_top
|
53 |
+
else:
|
54 |
+
leader_info[competition]["Username"] = best_model["Username"]
|
55 |
+
leader_info[competition]["Model Name"] = best_model["Model Name"]
|
56 |
+
leader_info[competition]["Hotkey"] = best_model["Hotkey"]
|
57 |
+
leader_info[competition]["Date"] = time.strftime("%Y-%m-%d")
|
58 |
+
leader_info[competition]["UID"] = best_model["ID"]
|
59 |
+
leader_info[competition]["Days on Top"] = 1
|
60 |
+
return leader_info[competition]
|
61 |
+
|
62 |
+
|
63 |
+
@st.cache_data()
|
64 |
+
def load_competition_data(last_update_time=None):
|
65 |
+
ranking_dfs = {}
|
66 |
+
for competition, details in projects.items():
|
67 |
+
df = fetch_runs_to_df(api, projects, competition)
|
68 |
+
if not df.empty:
|
69 |
+
# Convert to DataFrame
|
70 |
+
ranking_df = fetch_models_to_df(api, projects, competition, df)
|
71 |
+
|
72 |
+
# Rank the models by accuracy, then recall, then ROC AUC
|
73 |
+
rank_by = ["Recall", "Accuracy", "ROC AUC"]
|
74 |
+
ascending = [False, False, False]
|
75 |
+
ranking_df = ranking_df.sort_values(by=rank_by, ascending=ascending)
|
76 |
+
|
77 |
+
ranking_dfs[competition] = ranking_df
|
78 |
+
|
79 |
+
# Update the timestamp of the last update
|
80 |
+
last_update_time = time.time()
|
81 |
+
return ranking_dfs, last_update_time
|
82 |
+
|
83 |
+
|
84 |
+
# Streamlit app main function
|
85 |
+
def main():
|
86 |
+
# Set Streamlit page configuration to wide
|
87 |
+
st.set_page_config(layout="wide")
|
88 |
+
st.title("LaUltimate Dashboard")
|
89 |
+
st.subheader("Welcome to the Competition Dashboard!")
|
90 |
+
st.write("Explore the various AI competitions and their respective rankings. Select a competition to view more details and rankings.")
|
91 |
+
|
92 |
+
# Define the update interval (e.g., 10 minutes)
|
93 |
+
update_interval = 4 * 60 # 10 minutes in seconds
|
94 |
+
|
95 |
+
# Initialize session state to track the last update time
|
96 |
+
if 'last_update_time' not in st.session_state:
|
97 |
+
st.session_state.last_update_time = None
|
98 |
+
# Initialize or update leader_info
|
99 |
+
if "leader_info" not in st.session_state:
|
100 |
+
st.session_state.leader_info = {}
|
101 |
+
|
102 |
+
# Check if we need to refresh the data
|
103 |
+
if st.session_state.last_update_time is None or (time.time() - st.session_state.last_update_time > update_interval):
|
104 |
+
ranking_dfs, st.session_state.last_update_time = load_competition_data(st.session_state.last_update_time)
|
105 |
+
|
106 |
+
# Update leader info for each competition
|
107 |
+
for competition in ranking_dfs:
|
108 |
+
best_model = ranking_dfs[competition].iloc[0]
|
109 |
+
st.session_state.leader_info[competition] = update_leader_info(st.session_state.leader_info, competition, best_model)
|
110 |
+
|
111 |
+
else:
|
112 |
+
ranking_dfs, _ = load_competition_data(st.session_state.last_update_time)
|
113 |
+
|
114 |
+
# Display the time of the last update
|
115 |
+
st.write(f"Last updated: {time.ctime(st.session_state.last_update_time)}")
|
116 |
+
|
117 |
+
st.markdown("### Competitions")
|
118 |
+
|
119 |
+
st.write("### Select Competition")
|
120 |
+
|
121 |
+
# Create a header for the table with additional columns
|
122 |
+
cols = st.columns([1, 3, 2, 2, 2, 2, 1, 2]) # Adjust column width ratios if needed
|
123 |
+
cols[0].write("Index")
|
124 |
+
cols[1].write("Competition Name")
|
125 |
+
cols[2].write("Leader")
|
126 |
+
cols[3].write("Model")
|
127 |
+
cols[4].write("Date")
|
128 |
+
cols[5].write("UID")
|
129 |
+
cols[6].write("Hotkey")
|
130 |
+
cols[7].write("Days on Top")
|
131 |
+
|
132 |
+
|
133 |
+
# Display the project names in a table-like structure with buttons and placeholders for additional data
|
134 |
+
for index, (competition, details) in enumerate(projects.items(), start=1):
|
135 |
+
best_model = ranking_dfs[competition].iloc[0]
|
136 |
+
|
137 |
+
# Use the leader_info from session_state
|
138 |
+
leader_info = st.session_state.get("leader_info")
|
139 |
+
|
140 |
+
|
141 |
+
cols = st.columns([1, 3, 2, 2, 2, 2, 1, 2])
|
142 |
+
cols[0].write(index)
|
143 |
+
if cols[1].button(competition):
|
144 |
+
st.experimental_set_query_params(comp=competition)
|
145 |
+
cols[2].write(leader_info[competition]["Username"])
|
146 |
+
cols[3].write(leader_info[competition]["Model Name"])
|
147 |
+
cols[4].write(leader_info[competition]["Date"])
|
148 |
+
cols[5].write(leader_info[competition]["UID"])
|
149 |
+
cols[6].write(leader_info[competition]["Hotkey"])
|
150 |
+
cols[7].write(leader_info[competition]["Days on Top"])
|
151 |
+
|
152 |
+
|
153 |
+
# Get query parameters to identify which competition page to show
|
154 |
+
query_params = st.experimental_get_query_params()
|
155 |
+
|
156 |
+
if "comp" in query_params:
|
157 |
+
competition_name = query_params["comp"][0]
|
158 |
+
st.write(f"Selected competition: {competition_name}")
|
159 |
+
competition_details = projects.get(competition_name, {})
|
160 |
+
description = competition_details.get("description", "No description available.")
|
161 |
+
|
162 |
+
st.header(f"Competition: {competition_name}")
|
163 |
+
st.write(description)
|
164 |
+
|
165 |
+
df = ranking_dfs[competition_name]
|
166 |
+
if not df.empty:
|
167 |
+
st.dataframe(df)
|
168 |
+
else:
|
169 |
+
st.warning("No runs available for ranking.")
|
170 |
+
else:
|
171 |
+
st.write("Please select a competition to view details.")
|
172 |
+
|
173 |
+
|
174 |
+
# Run the app
|
175 |
+
if __name__ == "__main__":
|
176 |
+
main()
|