qgallouedec HF staff commited on
Commit
c53f091
1 Parent(s): 0f73e9a

update backend

Browse files
Files changed (2) hide show
  1. .gitignore +2 -1
  2. src/backend.py +13 -13
.gitignore CHANGED
@@ -1,2 +1,3 @@
1
  __pycache__/
2
- .DS_Store
 
 
1
  __pycache__/
2
+ .DS_Store
3
+ rl-trained_agents/
src/backend.py CHANGED
@@ -5,6 +5,7 @@ import os
5
  import random
6
  import shutil
7
  import sys
 
8
  import zipfile
9
  from pathlib import Path
10
  from typing import Optional
@@ -217,8 +218,7 @@ def download_from_hub(
217
  shutil.rmtree(log_path)
218
  else:
219
  raise ValueError(
220
- f"The folder {log_path} already exists, use --force to overwrite it, "
221
- "or choose '--exp-id 0' to create a new folder"
222
  )
223
 
224
  logger.info(f"Saving to {log_path}")
@@ -518,14 +518,9 @@ RESULTS_REPO = "open-rl-leaderboard/results_v2"
518
 
519
  def _backend_routine():
520
  # List only the text classification models
521
- sb3_models = [
522
- (model.modelId, model.sha) for model in API.list_models(filter=["reinforcement-learning", "stable-baselines3"])
523
- ]
524
  logger.info(f"Found {len(sb3_models)} SB3 models")
525
- # Get the results
526
- dataset = load_dataset(
527
- RESULTS_REPO, split="train", download_mode="force_redownload", verification_mode="no_checks"
528
- )
529
  evaluated_models = [("/".join([x["user_id"], x["model_id"]]), x["sha"]) for x in dataset]
530
  pending_models = list(set(sb3_models) - set(evaluated_models))
531
  logger.info(f"Found {len(pending_models)} pending models")
@@ -533,8 +528,11 @@ def _backend_routine():
533
  if len(pending_models) == 0:
534
  return None
535
 
 
 
 
536
  # Select a random model
537
- repo_id, sha = random.choice(pending_models)
538
  user_id, model_id = repo_id.split("/")
539
  row = {"model_id": model_id, "user_id": user_id, "sha": sha}
540
 
@@ -555,17 +553,18 @@ def _backend_routine():
555
  row["episodic_returns"] = episodic_returns
556
  except Exception as e:
557
  logger.error(f"Error evaluating {model_id}: {e}")
 
558
  row["status"] = "FAILED"
559
 
560
  else:
561
  logger.error(f"No environment found for {model_id}")
562
  row["status"] = "FAILED"
563
 
564
- dataset = load_dataset(
565
- RESULTS_REPO, split="train", download_mode="force_redownload", verification_mode="no_checks"
566
- ) # Reload the dataset, in case it was updated
567
  dataset = dataset.add_item(row)
568
  dataset.push_to_hub(RESULTS_REPO, split="train")
 
569
 
570
 
571
  def backend_routine():
@@ -573,6 +572,7 @@ def backend_routine():
573
  _backend_routine()
574
  except Exception as e:
575
  logger.error(f"{e.__class__.__name__}: {str(e)}")
 
576
 
577
 
578
  if __name__ == "__main__":
 
5
  import random
6
  import shutil
7
  import sys
8
+ import time
9
  import zipfile
10
  from pathlib import Path
11
  from typing import Optional
 
218
  shutil.rmtree(log_path)
219
  else:
220
  raise ValueError(
221
+ f"The folder {log_path} already exists, use --force to overwrite it, " "or choose '--exp-id 0' to create a new folder"
 
222
  )
223
 
224
  logger.info(f"Saving to {log_path}")
 
518
 
519
  def _backend_routine():
520
  # List only the text classification models
521
+ sb3_models = [(model.modelId, model.sha) for model in API.list_models(filter=["reinforcement-learning", "stable-baselines3"])]
 
 
522
  logger.info(f"Found {len(sb3_models)} SB3 models")
523
+ dataset = load_dataset(RESULTS_REPO, split="train", download_mode="force_redownload", verification_mode="no_checks")
 
 
 
524
  evaluated_models = [("/".join([x["user_id"], x["model_id"]]), x["sha"]) for x in dataset]
525
  pending_models = list(set(sb3_models) - set(evaluated_models))
526
  logger.info(f"Found {len(pending_models)} pending models")
 
528
  if len(pending_models) == 0:
529
  return None
530
 
531
+ # Shuffle the dataset
532
+ random.shuffle(pending_models)
533
+
534
  # Select a random model
535
+ repo_id, sha = pending_models.pop()
536
  user_id, model_id = repo_id.split("/")
537
  row = {"model_id": model_id, "user_id": user_id, "sha": sha}
538
 
 
553
  row["episodic_returns"] = episodic_returns
554
  except Exception as e:
555
  logger.error(f"Error evaluating {model_id}: {e}")
556
+ logger.exception(e)
557
  row["status"] = "FAILED"
558
 
559
  else:
560
  logger.error(f"No environment found for {model_id}")
561
  row["status"] = "FAILED"
562
 
563
+ # load the last version of the dataset
564
+ dataset = load_dataset(RESULTS_REPO, split="train", download_mode="force_redownload", verification_mode="no_checks")
 
565
  dataset = dataset.add_item(row)
566
  dataset.push_to_hub(RESULTS_REPO, split="train")
567
+ time.sleep(60) # Sleep for 1 minute to avoid rate limiting
568
 
569
 
570
  def backend_routine():
 
572
  _backend_routine()
573
  except Exception as e:
574
  logger.error(f"{e.__class__.__name__}: {str(e)}")
575
+ logger.exception(e)
576
 
577
 
578
  if __name__ == "__main__":