zera09 commited on
Commit
6633a3c
Β·
verified Β·
1 Parent(s): cd40727

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -76
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
- data = []
17
- for filename in os.listdir(FOLDER_TO_WATCH):
18
- if filename.endswith('.json'):
19
- try:
20
- with open(os.path.join(FOLDER_TO_WATCH, filename), 'r') as f:
21
- model_data = json.load(f)
22
- data.append(model_data)
23
- except Exception as e:
24
- print(f"Error loading {filename}: {e}")
25
-
26
- if data:
27
- df = pd.DataFrame(data)
28
- if not df.empty and 'accuracy' in df.columns:
29
- df = df.sort_values(by='accuracy', ascending=False)
30
- return df[COLUMNS_TO_DISPLAY] if all(col in df.columns for col in COLUMNS_TO_DISPLAY) else df
31
- return pd.DataFrame(columns=COLUMNS_TO_DISPLAY)
32
-
33
- def update_leaderboard():
34
- current_data = load_data()
35
- return current_data, time.strftime("%Y-%m-%d %H:%M:%S")
36
-
37
- with gr.Blocks(title="Model Leaderboard") as demo:
38
- gr.Markdown("# Model Performance Leaderboard")
39
- gr.Markdown(f"Automatically updates from JSON files in `{FOLDER_TO_WATCH}` folder")
40
-
41
- with gr.Row():
42
- refresh_btn = gr.Button("πŸ”„ Refresh", variant="secondary")
43
- last_update = gr.Textbox(label="Last Updated", interactive=False)
44
-
45
- leaderboard = gr.Dataframe(
46
- headers=COLUMNS_TO_DISPLAY,
47
- interactive=False,
48
- wrap=True
49
- )
50
-
51
- refresh_btn.click(
52
- fn=update_leaderboard,
53
- outputs=[leaderboard, last_update]
54
- )
55
-
56
- # New way to implement auto-refresh
57
- demo.load(
58
- fn=lambda: update_leaderboard(),
59
- outputs=[leaderboard, last_update]
60
- )
61
-
62
- # Alternative auto-refresh method
63
- def auto_refresh():
64
- while True:
65
- time.sleep(REFRESH_INTERVAL)
66
- yield update_leaderboard()
67
-
68
- demo.queue()
69
- leaderboard.change(
70
- fn=auto_refresh,
71
- outputs=[leaderboard, last_update],
72
- show_progress=False
73
- )
74
-
75
- if __name__ == "__main__":
76
- demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()