Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,123 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import json
|
3 |
-
import os
|
4 |
-
import pandas as pd
|
5 |
-
import time
|
6 |
-
|
7 |
-
# Configuration
|
8 |
-
FOLDER_TO_WATCH = "model_results" # Folder where JSON files are stored
|
9 |
-
REFRESH_INTERVAL = 5 # Seconds between automatic refreshes
|
10 |
-
COLUMNS_TO_DISPLAY = ["model_name", "accuracy", "precision", "recall", "f1_score", "timestamp"]
|
11 |
-
|
12 |
-
# Create the folder if it doesn't exist
|
13 |
-
os.makedirs(FOLDER_TO_WATCH, exist_ok=True)
|
14 |
-
|
15 |
-
def load_data():
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import pandas as pd
|
5 |
+
import time
|
6 |
+
|
7 |
+
# Configuration
|
8 |
+
FOLDER_TO_WATCH = "model_results" # Folder where JSON files are stored
|
9 |
+
REFRESH_INTERVAL = 5 # Seconds between automatic refreshes
|
10 |
+
COLUMNS_TO_DISPLAY = ["model_name", "accuracy", "precision", "recall", "f1_score", "timestamp"]
|
11 |
+
|
12 |
+
# Create the folder if it doesn't exist
|
13 |
+
os.makedirs(FOLDER_TO_WATCH, exist_ok=True)
|
14 |
+
|
15 |
+
def load_data():
|
16 |
+
"""Load all JSON files from the folder and return as DataFrame"""
|
17 |
+
data = []
|
18 |
+
for filename in os.listdir(FOLDER_TO_WATCH):
|
19 |
+
if filename.endswith('.json'):
|
20 |
+
try:
|
21 |
+
with open(os.path.join(FOLDER_TO_WATCH, filename), 'r') as f:
|
22 |
+
model_data = json.load(f)
|
23 |
+
data.append(model_data)
|
24 |
+
except Exception as e:
|
25 |
+
print(f"Error loading {filename}: {e}")
|
26 |
+
|
27 |
+
if data:
|
28 |
+
return pd.DataFrame(data)
|
29 |
+
return pd.DataFrame(columns=COLUMNS_TO_DISPLAY)
|
30 |
+
|
31 |
+
def update_leaderboard(sort_by="accuracy", sort_order="descending"):
|
32 |
+
"""Update the leaderboard with current data and sorting"""
|
33 |
+
current_data = load_data()
|
34 |
+
|
35 |
+
# Get available numeric columns for sorting
|
36 |
+
numeric_cols = [col for col in current_data.columns if pd.api.types.is_numeric_dtype(current_data[col])]
|
37 |
+
|
38 |
+
# Default to accuracy if selected column isn't available
|
39 |
+
sort_by = sort_by if sort_by in numeric_cols else "accuracy"
|
40 |
+
|
41 |
+
# Apply sorting
|
42 |
+
if not current_data.empty and sort_by in current_data.columns:
|
43 |
+
ascending = sort_order == "ascending"
|
44 |
+
current_data = current_data.sort_values(by=sort_by, ascending=ascending)
|
45 |
+
|
46 |
+
# Select only the columns we want to display
|
47 |
+
display_cols = [col for col in COLUMNS_TO_DISPLAY if col in current_data.columns]
|
48 |
+
return current_data[display_cols], time.strftime("%Y-%m-%d %H:%M:%S"), gr.Dropdown(choices=numeric_cols)
|
49 |
+
|
50 |
+
with gr.Blocks(title="Model Leaderboard") as demo:
|
51 |
+
gr.Markdown("# π Model Performance Leaderboard")
|
52 |
+
gr.Markdown(f"Automatically updates from JSON files in `{FOLDER_TO_WATCH}` folder")
|
53 |
+
|
54 |
+
# Get initial data to populate sort options
|
55 |
+
initial_data = load_data()
|
56 |
+
numeric_cols = [col for col in initial_data.columns if pd.api.types.is_numeric_dtype(initial_data[col])]
|
57 |
+
|
58 |
+
with gr.Row():
|
59 |
+
with gr.Column(scale=1):
|
60 |
+
sort_by = gr.Dropdown(
|
61 |
+
label="Sort by metric",
|
62 |
+
choices=numeric_cols,
|
63 |
+
value="accuracy"
|
64 |
+
)
|
65 |
+
sort_order = gr.Radio(
|
66 |
+
label="Sort order",
|
67 |
+
choices=["descending", "ascending"],
|
68 |
+
value="descending"
|
69 |
+
)
|
70 |
+
refresh_btn = gr.Button("π Refresh", variant="secondary")
|
71 |
+
with gr.Column(scale=2):
|
72 |
+
last_update = gr.Textbox(label="Last Updated", interactive=False)
|
73 |
+
|
74 |
+
leaderboard = gr.Dataframe(
|
75 |
+
headers=COLUMNS_TO_DISPLAY,
|
76 |
+
datatype=["str"] + ["number"]*(len(COLUMNS_TO_DISPLAY)-1),
|
77 |
+
interactive=False,
|
78 |
+
wrap=True
|
79 |
+
)
|
80 |
+
|
81 |
+
# Input components for interactive controls
|
82 |
+
inputs = [sort_by, sort_order]
|
83 |
+
|
84 |
+
# Manual refresh
|
85 |
+
refresh_btn.click(
|
86 |
+
fn=update_leaderboard,
|
87 |
+
inputs=inputs,
|
88 |
+
outputs=[leaderboard, last_update, sort_by]
|
89 |
+
)
|
90 |
+
|
91 |
+
# Auto-refresh when sorting changes
|
92 |
+
sort_by.change(
|
93 |
+
fn=update_leaderboard,
|
94 |
+
inputs=inputs,
|
95 |
+
outputs=[leaderboard, last_update, sort_by]
|
96 |
+
)
|
97 |
+
sort_order.change(
|
98 |
+
fn=update_leaderboard,
|
99 |
+
inputs=inputs,
|
100 |
+
outputs=[leaderboard, last_update, sort_by]
|
101 |
+
)
|
102 |
+
|
103 |
+
# Initial load and periodic refresh
|
104 |
+
def auto_refresh(sort_by, sort_order):
|
105 |
+
while True:
|
106 |
+
time.sleep(REFRESH_INTERVAL)
|
107 |
+
df, timestamp, _ = update_leaderboard(sort_by, sort_order)
|
108 |
+
yield df, timestamp, gr.Dropdown(choices=numeric_cols)
|
109 |
+
|
110 |
+
demo.queue()
|
111 |
+
demo.load(
|
112 |
+
fn=lambda: update_leaderboard("accuracy", "descending"),
|
113 |
+
outputs=[leaderboard, last_update, sort_by]
|
114 |
+
)
|
115 |
+
leaderboard.change(
|
116 |
+
fn=auto_refresh,
|
117 |
+
inputs=inputs,
|
118 |
+
outputs=[leaderboard, last_update, sort_by],
|
119 |
+
show_progress=False
|
120 |
+
)
|
121 |
+
|
122 |
+
if __name__ == "__main__":
|
123 |
+
demo.launch()
|