pkalkman commited on
Commit
e63522b
·
1 Parent(s): 2d941f4

read the last_update file with the date and time the update process was last run

Browse files
Files changed (3) hide show
  1. .DS_Store +0 -0
  2. README.md +0 -1
  3. app.py +17 -167
.DS_Store ADDED
Binary file (6.15 kB). View file
 
README.md CHANGED
@@ -7,7 +7,6 @@ sdk: gradio
7
  sdk_version: 3.4
8
  app_file: app.py
9
  pinned: false
10
- startup_duration_timeout: 2h
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
 
7
  sdk_version: 3.4
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
app.py CHANGED
@@ -1,16 +1,10 @@
1
  import os
2
  import json
3
- import requests
4
 
5
  import datetime
6
  import gradio as gr
7
  import pandas as pd
8
- from huggingface_hub import HfApi, hf_hub_download, snapshot_download
9
- from huggingface_hub.repocard import metadata_load
10
- from apscheduler.schedulers.background import BackgroundScheduler
11
-
12
-
13
- from tqdm.contrib.concurrent import thread_map
14
 
15
  from utils import make_clickable_model
16
  from utils import make_clickable_user
@@ -26,153 +20,6 @@ api = HfApi(token=HF_TOKEN)
26
  with open('envs.json', 'r') as f:
27
  rl_envs = json.load(f)
28
 
29
- def get_metadata(model_id):
30
- try:
31
- readme_path = hf_hub_download(model_id, filename="README.md", etag_timeout=180)
32
- return metadata_load(readme_path)
33
- except requests.exceptions.HTTPError:
34
- # 404 README.md not found
35
- return None
36
-
37
- def parse_metrics_accuracy(meta):
38
- if "model-index" not in meta:
39
- return None
40
- result = meta["model-index"][0]["results"]
41
- metrics = result[0]["metrics"]
42
- accuracy = metrics[0]["value"]
43
- return accuracy
44
-
45
- # We keep the worst case episode
46
- def parse_rewards(accuracy):
47
- default_std = -1000
48
- default_reward=-1000
49
- if accuracy != None:
50
- accuracy = str(accuracy)
51
- parsed = accuracy.split('+/-')
52
- if len(parsed)>1:
53
- mean_reward = float(parsed[0].strip())
54
- std_reward = float(parsed[1].strip())
55
- elif len(parsed)==1: #only mean reward
56
- mean_reward = float(parsed[0].strip())
57
- std_reward = float(0)
58
- else:
59
- mean_reward = float(default_std)
60
- std_reward = float(default_reward)
61
-
62
- else:
63
- mean_reward = float(default_std)
64
- std_reward = float(default_reward)
65
- return mean_reward, std_reward
66
-
67
-
68
- def get_model_ids(rl_env):
69
- api = HfApi()
70
- models = api.list_models(filter=rl_env)
71
- model_ids = [x.modelId for x in models]
72
- return model_ids
73
-
74
- # Parralelized version
75
- def update_leaderboard_dataset_parallel(rl_env, path):
76
- # Get model ids associated with rl_env
77
- model_ids = get_model_ids(rl_env)
78
-
79
- def process_model(model_id):
80
- meta = get_metadata(model_id)
81
- #LOADED_MODEL_METADATA[model_id] = meta if meta is not None else ''
82
- if meta is None:
83
- return None
84
- user_id = model_id.split('/')[0]
85
- row = {}
86
- row["User"] = user_id
87
- row["Model"] = model_id
88
- accuracy = parse_metrics_accuracy(meta)
89
- mean_reward, std_reward = parse_rewards(accuracy)
90
- mean_reward = mean_reward if not pd.isna(mean_reward) else 0
91
- std_reward = std_reward if not pd.isna(std_reward) else 0
92
- row["Results"] = mean_reward - std_reward
93
- row["Mean Reward"] = mean_reward
94
- row["Std Reward"] = std_reward
95
- return row
96
-
97
- data = list(thread_map(process_model, model_ids, desc="Processing models"))
98
-
99
- # Filter out None results (models with no metadata)
100
- data = [row for row in data if row is not None]
101
-
102
- ranked_dataframe = rank_dataframe(pd.DataFrame.from_records(data))
103
- new_history = ranked_dataframe
104
- file_path = path + "/" + rl_env + ".csv"
105
- new_history.to_csv(file_path, index=False)
106
-
107
- return ranked_dataframe
108
-
109
-
110
- def update_leaderboard_dataset(rl_env, path):
111
- # Get model ids associated with rl_env
112
- model_ids = get_model_ids(rl_env)
113
- data = []
114
- for model_id in model_ids:
115
- """
116
- readme_path = hf_hub_download(model_id, filename="README.md")
117
- meta = metadata_load(readme_path)
118
- """
119
- meta = get_metadata(model_id)
120
- #LOADED_MODEL_METADATA[model_id] = meta if meta is not None else ''
121
- if meta is None:
122
- continue
123
- user_id = model_id.split('/')[0]
124
- row = {}
125
- row["User"] = user_id
126
- row["Model"] = model_id
127
- accuracy = parse_metrics_accuracy(meta)
128
- mean_reward, std_reward = parse_rewards(accuracy)
129
- mean_reward = mean_reward if not pd.isna(mean_reward) else 0
130
- std_reward = std_reward if not pd.isna(std_reward) else 0
131
- row["Results"] = mean_reward - std_reward
132
- row["Mean Reward"] = mean_reward
133
- row["Std Reward"] = std_reward
134
- data.append(row)
135
-
136
- ranked_dataframe = rank_dataframe(pd.DataFrame.from_records(data))
137
- new_history = ranked_dataframe
138
- file_path = path + "/" + rl_env + ".csv"
139
- new_history.to_csv(file_path, index=False)
140
-
141
- return ranked_dataframe
142
-
143
-
144
- def get_data_no_html(rl_env, path) -> pd.DataFrame:
145
- """
146
- Get data from rl_env
147
- :return: data as a pandas DataFrame
148
- """
149
- csv_path = path + "/" + rl_env + ".csv"
150
- data = pd.read_csv(csv_path)
151
-
152
- return data
153
-
154
-
155
- def rank_dataframe(dataframe):
156
- dataframe = dataframe.sort_values(by=['Results', 'User', 'Model'], ascending=False)
157
- if not 'Ranking' in dataframe.columns:
158
- dataframe.insert(0, 'Ranking', [i for i in range(1,len(dataframe)+1)])
159
- else:
160
- dataframe['Ranking'] = [i for i in range(1,len(dataframe)+1)]
161
- return dataframe
162
-
163
-
164
- def run_update_dataset():
165
- path_ = download_leaderboard_dataset()
166
- for i in range(0, len(rl_envs)):
167
- rl_env = rl_envs[i]
168
- update_leaderboard_dataset_parallel(rl_env["rl_env"], path_)
169
-
170
- api.upload_folder(
171
- folder_path=path_,
172
- repo_id="pkalkman/drlc-leaderboard-data",
173
- repo_type="dataset",
174
- commit_message="Update dataset")
175
-
176
 
177
  def download_leaderboard_dataset():
178
  # Download the dataset from the Hugging Face Hub
@@ -197,16 +44,20 @@ def get_data(rl_env, path) -> pd.DataFrame:
197
 
198
  def get_last_refresh_time(path) -> str:
199
  """
200
- Get the latest modification time of any CSV file in the dataset path
201
  """
202
- # Get list of all CSV files in the dataset path
203
- csv_files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.csv')]
204
 
205
- # Get the latest modification time
206
- latest_time = max([os.path.getmtime(f) for f in csv_files])
207
-
208
- # Convert to human-readable format
209
- return datetime.datetime.fromtimestamp(latest_time).strftime('%Y-%m-%d %H:%M:%S')
 
 
 
 
210
 
211
 
212
  with block:
@@ -215,19 +66,18 @@ with block:
215
  last_refresh_time = get_last_refresh_time(path_)
216
 
217
  gr.Markdown(f"""
218
- # 🏆 Deep Reinforcement Learning Course Leaderboard 🏆
219
-
220
  Presenting the latest leaderboard from the Hugging Face Deep RL Course - refresh ({last_refresh_time}).
221
  """)
222
 
223
-
224
  for i in range(0, len(rl_envs)):
225
  rl_env = rl_envs[i]
226
  with gr.TabItem(rl_env["rl_env_beautiful"]):
227
  with gr.Row():
228
  markdown = f"""
229
  # {rl_env['rl_env_beautiful']}
230
-
231
  ### Leaderboard for {rl_env['rl_env_beautiful']}
232
  """
233
  gr.Markdown(markdown)
@@ -242,4 +92,4 @@ with block:
242
  row_count=(100, 'fixed')
243
  )
244
 
245
- block.launch()
 
1
  import os
2
  import json
 
3
 
4
  import datetime
5
  import gradio as gr
6
  import pandas as pd
7
+ from huggingface_hub import HfApi, snapshot_download
 
 
 
 
 
8
 
9
  from utils import make_clickable_model
10
  from utils import make_clickable_user
 
20
  with open('envs.json', 'r') as f:
21
  rl_envs = json.load(f)
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  def download_leaderboard_dataset():
25
  # Download the dataset from the Hugging Face Hub
 
44
 
45
  def get_last_refresh_time(path) -> str:
46
  """
47
+ Get the last update time from the last_update.txt file in the dataset path.
48
  """
49
+ # Path to the last_update.txt file
50
+ update_file_path = os.path.join(path, 'last_update.txt')
51
 
52
+ # Check if the file exists
53
+ if os.path.exists(update_file_path):
54
+ # Read the content of the file (the timestamp)
55
+ with open(update_file_path, 'r') as f:
56
+ last_refresh_time = f.read().strip()
57
+ return last_refresh_time
58
+ else:
59
+ # Fallback: If the file is missing, return a default message
60
+ return "Last update time not available"
61
 
62
 
63
  with block:
 
66
  last_refresh_time = get_last_refresh_time(path_)
67
 
68
  gr.Markdown(f"""
69
+ # 🏆 Deep Reinforcement Learning Course Leaderboard (Mirror)🏆
70
+
71
  Presenting the latest leaderboard from the Hugging Face Deep RL Course - refresh ({last_refresh_time}).
72
  """)
73
 
 
74
  for i in range(0, len(rl_envs)):
75
  rl_env = rl_envs[i]
76
  with gr.TabItem(rl_env["rl_env_beautiful"]):
77
  with gr.Row():
78
  markdown = f"""
79
  # {rl_env['rl_env_beautiful']}
80
+
81
  ### Leaderboard for {rl_env['rl_env_beautiful']}
82
  """
83
  gr.Markdown(markdown)
 
92
  row_count=(100, 'fixed')
93
  )
94
 
95
+ block.launch()