ThomasSimonini HF staff commited on
Commit
88d91f4
β€’
1 Parent(s): 9681540

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -41
app.py CHANGED
@@ -1,21 +1,21 @@
 
1
  import json
2
-
3
  import requests
4
 
5
- from datasets import load_dataset
6
-
7
  import gradio as gr
8
- from apscheduler.schedulers.background import BackgroundScheduler
9
-
10
-
11
- from huggingface_hub import HfApi, hf_hub_download
12
- from huggingface_hub.repocard import metadata_load
13
  import pandas as pd
 
 
 
14
 
15
  from utils import *
16
 
 
 
 
17
 
18
  block = gr.Blocks()
 
19
 
20
  # Containing the data
21
  rl_envs = [
@@ -135,8 +135,6 @@ rl_envs = [
135
  }
136
  ]
137
 
138
-
139
-
140
  def get_metadata(model_id):
141
  try:
142
  readme_path = hf_hub_download(model_id, filename="README.md")
@@ -181,13 +179,11 @@ def get_model_ids(rl_env):
181
  api = HfApi()
182
  models = api.list_models(filter=rl_env)
183
  model_ids = [x.modelId for x in models]
184
- #print(model_ids)
185
  return model_ids
186
 
187
- def get_model_dataframe(rl_env):
188
  # Get model ids associated with rl_env
189
  model_ids = get_model_ids(rl_env)
190
- #print(model_ids)
191
  data = []
192
  for model_id in model_ids:
193
  """
@@ -200,8 +196,8 @@ def get_model_dataframe(rl_env):
200
  continue
201
  user_id = model_id.split('/')[0]
202
  row = {}
203
- row["User"] = make_clickable_user(user_id)
204
- row["Model"] = make_clickable_model(model_id)
205
  accuracy = parse_metrics_accuracy(meta)
206
  mean_reward, std_reward = parse_rewards(accuracy)
207
  mean_reward = mean_reward if not pd.isna(mean_reward) else 0
@@ -210,14 +206,53 @@ def get_model_dataframe(rl_env):
210
  row["Mean Reward"] = mean_reward
211
  row["Std Reward"] = std_reward
212
  data.append(row)
213
- print("DATA", data)
214
  ranked_dataframe = rank_dataframe(pd.DataFrame.from_records(data))
215
- print("RANKED", ranked_dataframe)
 
 
 
 
 
 
 
 
 
 
 
216
  return ranked_dataframe
217
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
  def rank_dataframe(dataframe):
220
- #print("DATAFRAME", dataframe)
221
  dataframe = dataframe.sort_values(by=['Results'], ascending=False)
222
  if not 'Ranking' in dataframe.columns:
223
  dataframe.insert(0, 'Ranking', [i for i in range(1,len(dataframe)+1)])
@@ -226,33 +261,56 @@ def rank_dataframe(dataframe):
226
  return dataframe
227
 
228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  with block:
230
  gr.Markdown(f"""
231
  # πŸ† The Deep Reinforcement Learning Course Leaderboard πŸ†
232
 
233
- This is the leaderboard of trained agents during the Deep Reinforcement Learning Course. A free course from beginner to expert.
234
 
235
- Just choose which environment you trained your agent on and with Ctrl+F find your rank πŸ†
236
-
237
- **The leaderboard is updated every hour. If you don't find your model, go to the bottom of the page and click on the refresh button**
238
 
 
239
  We use **lower bound result to sort the models: mean_reward - std_reward.**
240
 
241
- You **can click on the model's name** to be redirected to its model card which includes documentation.
 
242
 
 
243
  πŸ€– You want to try to train your agents? <a href="https://huggingface.co/deep-rl-course/unit0/introduction?fw=pt" target="_blank"> Check the Hugging Face free Deep Reinforcement Learning Course πŸ€— </a>.
244
-
245
- You want to compare two agents? <a href="https://huggingface.co/spaces/ThomasSimonini/Compare-Reinforcement-Learning-Agents" target="_blank">It's possible using this Spaces demo πŸ‘€ </a>.
246
-
247
  πŸ”§ There is an **environment missing?** Please open an issue.
248
-
249
- For the RL course progress check out <a href="https://huggingface.co/spaces/ThomasSimonini/Check-my-progress-Deep-RL-Course" target="_blank"> User Progress App </a>
250
  """)
251
-
252
- #for rl_env in RL_ENVS:
253
  for i in range(0, len(rl_envs)):
254
  rl_env = rl_envs[i]
255
-
256
  with gr.TabItem(rl_env["rl_env_beautiful"]) as rl_tab:
257
  with gr.Row():
258
  markdown = """
@@ -260,14 +318,25 @@ with block:
260
 
261
  """.format(name_leaderboard = rl_env["rl_env_beautiful"], video_link = rl_env["video_link"])
262
  gr.Markdown(markdown)
 
 
263
  with gr.Row():
264
- rl_env["global"] = gr.components.Dataframe(value= get_model_dataframe(rl_env["rl_env"]), headers=["Ranking πŸ†", "User πŸ€—", "Model id πŸ€–", "Results", "Mean Reward", "Std Reward"], datatype=["number", "markdown", "markdown", "number", "number", "number"])
 
 
 
 
265
  with gr.Row():
266
- data_run = gr.Button("Refresh")
267
- #print("rl_env", rl_env["rl_env"])
268
- val = gr.Variable(value=[rl_env["rl_env"]])
269
- data_run.click(get_model_dataframe, inputs=[val], outputs =rl_env["global"])
270
-
 
 
 
 
 
271
 
272
  block.launch()
273
 
@@ -281,9 +350,10 @@ def refresh_leaderboard():
281
  temp = get_model_dataframe(rl_env)
282
  rl_env["global"] = temp
283
  print("The leaderboard has been updated")
 
 
284
 
285
  scheduler = BackgroundScheduler()
286
  # Refresh every hour
287
- scheduler.add_job(func=refresh_leaderboard, trigger="interval", seconds=3600)
288
- scheduler.start()
289
-
 
1
+ import os
2
  import json
 
3
  import requests
4
 
 
 
5
  import gradio as gr
 
 
 
 
 
6
  import pandas as pd
7
+ from huggingface_hub import HfApi, hf_hub_download, snapshot_download
8
+ from huggingface_hub.repocard import metadata_load
9
+ from apscheduler.schedulers.background import BackgroundScheduler
10
 
11
  from utils import *
12
 
13
+ DATASET_REPO_URL = "https://huggingface.co/datasets/huggingface-projects/drlc-leaderboard-data"
14
+ DATASET_REPO_ID = "huggingface-projects/drlc-leaderboard-data"
15
+ HF_TOKEN = os.environ.get("HF_TOKEN")
16
 
17
  block = gr.Blocks()
18
+ api = HfApi(token=HF_TOKEN)
19
 
20
  # Containing the data
21
  rl_envs = [
 
135
  }
136
  ]
137
 
 
 
138
  def get_metadata(model_id):
139
  try:
140
  readme_path = hf_hub_download(model_id, filename="README.md")
 
179
  api = HfApi()
180
  models = api.list_models(filter=rl_env)
181
  model_ids = [x.modelId for x in models]
 
182
  return model_ids
183
 
184
+ def update_leaderboard_dataset(rl_env):
185
  # Get model ids associated with rl_env
186
  model_ids = get_model_ids(rl_env)
 
187
  data = []
188
  for model_id in model_ids:
189
  """
 
196
  continue
197
  user_id = model_id.split('/')[0]
198
  row = {}
199
+ row["User"] = user_id
200
+ row["Model"] = model_id
201
  accuracy = parse_metrics_accuracy(meta)
202
  mean_reward, std_reward = parse_rewards(accuracy)
203
  mean_reward = mean_reward if not pd.isna(mean_reward) else 0
 
206
  row["Mean Reward"] = mean_reward
207
  row["Std Reward"] = std_reward
208
  data.append(row)
209
+
210
  ranked_dataframe = rank_dataframe(pd.DataFrame.from_records(data))
211
+ new_history = ranked_dataframe
212
+ filename = rl_env + ".csv"
213
+ new_history.to_csv(filename, index=False)
214
+ csv_path = os.path.abspath(filename)
215
+
216
+ api.upload_file(
217
+ path_or_fileobj= csv_path,
218
+ path_in_repo= filename,
219
+ repo_id="huggingface-projects/drlc-leaderboard-data",
220
+ repo_type="dataset",
221
+ commit_message="Update dataset")
222
+
223
  return ranked_dataframe
224
+
225
+ def download_leaderboard_dataset():
226
+ path = snapshot_download(repo_id=DATASET_REPO_ID, repo_type="dataset")
227
+ return path
228
+
229
+ def get_data(rl_env, path) -> pd.DataFrame:
230
+ """
231
+ Get data from rl_env
232
+ :return: data as a pandas DataFrame
233
+ """
234
+ csv_path = path + "/" + rl_env + ".csv"
235
+ data = pd.read_csv(csv_path)
236
+
237
+ for index, row in data.iterrows():
238
+ user_id = row["User"]
239
+ data.loc[index, "User"] = make_clickable_user(user_id)
240
+ model_id = row["Model"]
241
+ data.loc[index, "Model"] = make_clickable_model(model_id)
242
+
243
+ return data
244
+
245
+ def get_data_no_html(rl_env, path) -> pd.DataFrame:
246
+ """
247
+ Get data from rl_env
248
+ :return: data as a pandas DataFrame
249
+ """
250
+ csv_path = path + "/" + rl_env + ".csv"
251
+ data = pd.read_csv(csv_path)
252
+
253
+ return data
254
 
255
  def rank_dataframe(dataframe):
 
256
  dataframe = dataframe.sort_values(by=['Results'], ascending=False)
257
  if not 'Ranking' in dataframe.columns:
258
  dataframe.insert(0, 'Ranking', [i for i in range(1,len(dataframe)+1)])
 
261
  return dataframe
262
 
263
 
264
+ def run_update_dataset():
265
+ for i in range(0, len(rl_envs)):
266
+ rl_env = rl_envs[i]
267
+ update_leaderboard_dataset(rl_env["rl_env"])
268
+
269
+
270
+ def filter_data(rl_env, path, user_id):
271
+ print("RL ENV", rl_env)
272
+ print("PATH", path)
273
+ print("USER ID", user_id)
274
+ data_df = get_data_no_html(rl_env, path)
275
+ print(data_df)
276
+ models = []
277
+ models = data_df[data_df["User"] == user_id]
278
+
279
+ for index, row in models.iterrows():
280
+ user_id = row["User"]
281
+ models.loc[index, "User"] = make_clickable_user(user_id)
282
+ model_id = row["Model"]
283
+ models.loc[index, "Model"] = make_clickable_model(model_id)
284
+
285
+
286
+ print(models)
287
+ return models
288
+
289
  with block:
290
  gr.Markdown(f"""
291
  # πŸ† The Deep Reinforcement Learning Course Leaderboard πŸ†
292
 
293
+ This is the leaderboard of trained agents during the <a href="https://huggingface.co/learn/deep-rl-course/unit0/introduction?fw=pt">Deep Reinforcement Learning Course</a>. A free course from beginner to expert.
294
 
295
+ ### We only display the best 100 models
296
+ If you want to **find yours, type your user id and click on Search my models.**
297
+ You **can click on the model's name** to be redirected to its model card, including documentation.
298
 
299
+ ### How are the results calculated?
300
  We use **lower bound result to sort the models: mean_reward - std_reward.**
301
 
302
+ ### I can't find my model 😭
303
+ The leaderboard is **updated every hour** if you can't find your models, just wait for the next update.
304
 
305
+ ### The Deep RL Course
306
  πŸ€– You want to try to train your agents? <a href="https://huggingface.co/deep-rl-course/unit0/introduction?fw=pt" target="_blank"> Check the Hugging Face free Deep Reinforcement Learning Course πŸ€— </a>.
307
+
 
 
308
  πŸ”§ There is an **environment missing?** Please open an issue.
 
 
309
  """)
310
+ path_ = download_leaderboard_dataset()
311
+
312
  for i in range(0, len(rl_envs)):
313
  rl_env = rl_envs[i]
 
314
  with gr.TabItem(rl_env["rl_env_beautiful"]) as rl_tab:
315
  with gr.Row():
316
  markdown = """
 
318
 
319
  """.format(name_leaderboard = rl_env["rl_env_beautiful"], video_link = rl_env["video_link"])
320
  gr.Markdown(markdown)
321
+ #gr.Textbox(value=filter_data(rl_env["rl_env"], path_))
322
+
323
  with gr.Row():
324
+ gr.Markdown("""
325
+ ## Search your models
326
+ Simply type your user id to find your models
327
+ """)
328
+
329
  with gr.Row():
330
+ user_id = gr.Textbox(label= "Your user id")
331
+ search_btn = gr.Button("Search my models πŸ”Ž")
332
+ env = gr.Variable(rl_env["rl_env"])
333
+ grpath = gr.Variable(path_)
334
+ with gr.Row():
335
+ gr_dataframe = gr.components.Dataframe(value=get_data(rl_env["rl_env"], path_), headers=["Ranking πŸ†", "User πŸ€—", "Model id πŸ€–", "Results", "Mean Reward", "Std Reward"], datatype=["number", "markdown", "markdown", "number", "number", "number"], row_count=(100, 'fixed'))
336
+
337
+ with gr.Row():
338
+ #gr_search_dataframe = gr.components.Dataframe(headers=["Ranking πŸ†", "User πŸ€—", "Model id πŸ€–", "Results", "Mean Reward", "Std Reward"], datatype=["number", "markdown", "markdown", "number", "number", "number"], visible=False)
339
+ search_btn.click(fn=filter_data, inputs=[env, grpath, user_id], outputs=gr_dataframe, api_name="filter_data")
340
 
341
  block.launch()
342
 
 
350
  temp = get_model_dataframe(rl_env)
351
  rl_env["global"] = temp
352
  print("The leaderboard has been updated")
353
+
354
+ block.launch()
355
 
356
  scheduler = BackgroundScheduler()
357
  # Refresh every hour
358
+ scheduler.add_job(func=run_update_dataset, trigger="interval", seconds=3600)
359
+ scheduler.start()