chrisjay commited on
Commit
e9f37ce
β€’
1 Parent(s): 6867483

updating only unloaded data on block reload

Browse files
Files changed (1) hide show
  1. app.py +71 -11
app.py CHANGED
@@ -7,6 +7,14 @@ from huggingface_hub import HfApi, hf_hub_download
7
  from huggingface_hub.repocard import metadata_load
8
 
9
 
 
 
 
 
 
 
 
 
10
  # Based on Omar Sanseviero work
11
  # Make model clickable link
12
  def make_clickable_model(model_name):
@@ -52,8 +60,35 @@ def parse_rewards(accuracy):
52
  return mean_reward, std_reward
53
 
54
  def get_data(rl_env):
 
55
  data = []
56
  model_ids = get_model_ids(rl_env)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  for model_id in tqdm(model_ids):
58
  meta = get_metadata(model_id)
59
  if meta is None:
@@ -71,6 +106,33 @@ def get_data(rl_env):
71
  return pd.DataFrame.from_records(data)
72
 
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  def get_data_per_env(rl_env):
75
  dataframe = get_data(rl_env)
76
  dataframe = dataframe.fillna("")
@@ -91,13 +153,6 @@ def get_data_per_env(rl_env):
91
 
92
 
93
 
94
- RL_ENVS = ['LunarLander-v2','CarRacing-v0','MountainCar-v0']
95
-
96
- RL_DETAILS ={'CarRacing-v0':{'title':" The Car Racing 🏎️ Leaderboard πŸš€",'data':get_data_per_env('CarRacing-v0')},
97
- 'MountainCar-v0':{'title':"The Mountain Car ⛰️ πŸš— Leaderboard πŸš€",'data':get_data_per_env('MountainCar-v0')},
98
- 'LunarLander-v2':{'title':" The Lunar Lander πŸŒ• Leaderboard πŸš€",'data':get_data_per_env('LunarLander-v2')}
99
- }
100
-
101
 
102
  def get_info_display(len_dataframe,env_name,name_leaderboard,is_empty):
103
  if not is_empty:
@@ -127,11 +182,10 @@ def get_info_display(len_dataframe,env_name,name_leaderboard,is_empty):
127
 
128
  def reload_all_data():
129
 
130
- print(f'Loading all data...')
131
  global RL_DETAILS,RL_ENVS
132
 
133
  for rl_env in RL_ENVS:
134
- RL_DETAILS[rl_env]['data'] = get_data_per_env(rl_env)
135
 
136
 
137
  def reload_leaderboard(rl_env):
@@ -143,8 +197,14 @@ def reload_leaderboard(rl_env):
143
 
144
  return markdown,data_html
145
 
146
- with open('app.css','r') as f:
147
- BLOCK_CSS = f.read()
 
 
 
 
 
 
148
 
149
  block = gr.Blocks(css=BLOCK_CSS)
150
  with block:
 
7
  from huggingface_hub.repocard import metadata_load
8
 
9
 
10
+ RL_ENVS = ['LunarLander-v2','CarRacing-v0','MountainCar-v0']
11
+
12
+ with open('app.css','r') as f:
13
+ BLOCK_CSS = f.read()
14
+
15
+ LOADED_MODEL_IDS = {rl_env:[] for rl_env in RL_ENVS}
16
+
17
+
18
  # Based on Omar Sanseviero work
19
  # Make model clickable link
20
  def make_clickable_model(model_name):
 
60
  return mean_reward, std_reward
61
 
62
  def get_data(rl_env):
63
+ global LOADED_MODEL_IDS
64
  data = []
65
  model_ids = get_model_ids(rl_env)
66
+ LOADED_MODEL_IDS[rl_env]+=model_ids
67
+
68
+ for model_id in tqdm(model_ids):
69
+ meta = get_metadata(model_id)
70
+ if meta is None:
71
+ continue
72
+ user_id = model_id.split('/')[0]
73
+ row = {}
74
+ row["User"] = user_id
75
+ row["Model"] = model_id
76
+ accuracy = parse_metrics_accuracy(meta)
77
+ mean_reward, std_reward = parse_rewards(accuracy)
78
+ row["Results"] = mean_reward - std_reward
79
+ row["Mean Reward"] = mean_reward
80
+ row["Std Reward"] = std_reward
81
+ data.append(row)
82
+ return pd.DataFrame.from_records(data)
83
+
84
+
85
+
86
+ def update_data(rl_env):
87
+ global LOADED_MODEL_IDS
88
+ data = []
89
+ model_ids = [x for x in get_model_ids(rl_env) if x not in LOADED_MODEL_IDS[rl_env]]
90
+ LOADED_MODEL_IDS[rl_env]+=model_ids
91
+
92
  for model_id in tqdm(model_ids):
93
  meta = get_metadata(model_id)
94
  if meta is None:
 
106
  return pd.DataFrame.from_records(data)
107
 
108
 
109
+
110
+ def update_data_per_env(rl_env):
111
+ global RL_DETAILS
112
+
113
+ _,old_dataframe,_ = RL_DETAILS[rl_env]['data']
114
+ new_dataframe = update_data(rl_env)
115
+
116
+ new_dataframe = new_dataframe.fillna("")
117
+ if not new_dataframe.empty:
118
+ new_dataframe["User"] = new_dataframe["User"].apply(make_clickable_user)
119
+ new_dataframe["Model"] = new_dataframe["Model"].apply(make_clickable_model)
120
+
121
+ dataframe = pd.concat([old_dataframe,new_dataframe])
122
+
123
+ if not dataframe.empty:
124
+
125
+ dataframe = dataframe.sort_values(by=['Results'], ascending=False)
126
+ table_html = dataframe.to_html(escape=False, index=False,justify = 'left')
127
+ return table_html,dataframe,dataframe.empty
128
+ else:
129
+ html = """<div style="color: green">
130
+ <p> βŒ› Please wait. Results will be out soon... </p>
131
+ </div>
132
+ """
133
+ return html,dataframe,dataframe.empty
134
+
135
+
136
  def get_data_per_env(rl_env):
137
  dataframe = get_data(rl_env)
138
  dataframe = dataframe.fillna("")
 
153
 
154
 
155
 
 
 
 
 
 
 
 
156
 
157
  def get_info_display(len_dataframe,env_name,name_leaderboard,is_empty):
158
  if not is_empty:
 
182
 
183
  def reload_all_data():
184
 
 
185
  global RL_DETAILS,RL_ENVS
186
 
187
  for rl_env in RL_ENVS:
188
+ RL_DETAILS[rl_env]['data'] = update_data_per_env(rl_env)
189
 
190
 
191
  def reload_leaderboard(rl_env):
 
197
 
198
  return markdown,data_html
199
 
200
+
201
+
202
+ RL_DETAILS ={'CarRacing-v0':{'title':" The Car Racing 🏎️ Leaderboard πŸš€",'data':get_data_per_env('CarRacing-v0')},
203
+ 'MountainCar-v0':{'title':"The Mountain Car ⛰️ πŸš— Leaderboard πŸš€",'data':get_data_per_env('MountainCar-v0')},
204
+ 'LunarLander-v2':{'title':" The Lunar Lander πŸŒ• Leaderboard πŸš€",'data':get_data_per_env('LunarLander-v2')}
205
+ }
206
+
207
+
208
 
209
  block = gr.Blocks(css=BLOCK_CSS)
210
  with block: