Quentin Gallouédec commited on
Commit
7b86855
1 Parent(s): 69cf5b3

backend dedicated file

Browse files
Files changed (2) hide show
  1. app.py +2 -73
  2. src/backend.py +82 -0
app.py CHANGED
@@ -1,17 +1,15 @@
1
  import json
2
  import os
3
- import pprint
4
  import re
5
- import tempfile
6
 
7
  import gradio as gr
8
  import numpy as np
9
  import pandas as pd
10
  from apscheduler.schedulers.background import BackgroundScheduler
11
- from huggingface_hub import CommitOperationAdd, HfApi, hf_hub_download
12
 
 
13
  from src.css_html_js import dark_mode_gradio_js
14
- from src.evaluation import evaluate
15
  from src.logging import configure_root_logger, setup_logger
16
 
17
  configure_root_logger()
@@ -20,7 +18,6 @@ logger = setup_logger(__name__)
20
  API = HfApi(token=os.environ.get("TOKEN"))
21
  RESULTS_REPO = f"open-rl-leaderboard/results"
22
 
23
- pp = pprint.PrettyPrinter(width=80)
24
 
25
  ALL_ENV_IDS = {
26
  "Atari": [
@@ -42,74 +39,6 @@ ALL_ENV_IDS = {
42
  }
43
 
44
 
45
- def _backend_routine():
46
- # List only the text classification models
47
- rl_models = list(API.list_models(filter="reinforcement-learning"))
48
- logger.info(f"Found {len(rl_models)} RL models")
49
- compatible_models = []
50
- for model in rl_models:
51
- filenames = [sib.rfilename for sib in model.siblings]
52
- if "agent.pt" in filenames:
53
- compatible_models.append((model.modelId, model.sha))
54
-
55
- logger.info(f"Found {len(compatible_models)} compatible models")
56
-
57
- # Get the results
58
- pattern = re.compile(r"^[^/]*/[^/]*/[^/]*results_[a-f0-9]+\.json$")
59
- filenames = API.list_repo_files(RESULTS_REPO, repo_type="dataset")
60
- filenames = [filename for filename in filenames if pattern.match(filename)]
61
-
62
- evaluated_models = set()
63
- for filename in filenames:
64
- path = hf_hub_download(repo_id=RESULTS_REPO, filename=filename, repo_type="dataset")
65
- with open(path) as fp:
66
- report = json.load(fp)
67
- evaluated_models.add((report["config"]["model_id"], report["config"]["model_sha"]))
68
-
69
- # Find the models that are not associated with any results
70
- pending_models = set(compatible_models) - evaluated_models
71
- logger.info(f"Found {len(pending_models)} pending models")
72
-
73
- # Run an evaluation on the models
74
- with tempfile.TemporaryDirectory() as tmp_dir:
75
- commits = []
76
- for model_id, sha in pending_models:
77
- logger.info(f"Running evaluation on {model_id}")
78
- report = {"config": {"model_id": model_id, "model_sha": sha}}
79
- try:
80
- evaluations = evaluate(model_id, revision=sha)
81
- except Exception as e:
82
- logger.error(f"Error evaluating {model_id}: {e}")
83
- evaluations = None
84
-
85
- if evaluations is not None:
86
- report["results"] = evaluations
87
- report["status"] = "DONE"
88
- else:
89
- report["status"] = "FAILED"
90
-
91
- # Update the results
92
- dumped = json.dumps(report, indent=2)
93
- path_in_repo = f"{model_id}/results_{sha}.json"
94
- local_path = os.path.join(tmp_dir, path_in_repo)
95
- os.makedirs(os.path.dirname(local_path), exist_ok=True)
96
- with open(local_path, "w") as f:
97
- f.write(dumped)
98
-
99
- commits.append(CommitOperationAdd(path_in_repo=path_in_repo, path_or_fileobj=local_path))
100
-
101
- API.create_commit(
102
- repo_id=RESULTS_REPO, commit_message="Add evaluation results", operations=commits, repo_type="dataset"
103
- )
104
-
105
-
106
- def backend_routine():
107
- try:
108
- _backend_routine()
109
- except Exception as e:
110
- logger.error(f"{e.__class__.__name__}: {str(e)}")
111
-
112
-
113
  def get_leaderboard_df():
114
  # List all results files in results repo
115
  pattern = re.compile(r"^[^/]*/[^/]*/[^/]*results_[a-f0-9]+\.json$")
 
1
  import json
2
  import os
 
3
  import re
 
4
 
5
  import gradio as gr
6
  import numpy as np
7
  import pandas as pd
8
  from apscheduler.schedulers.background import BackgroundScheduler
9
+ from huggingface_hub import HfApi, hf_hub_download
10
 
11
+ from src.backend import backend_routine
12
  from src.css_html_js import dark_mode_gradio_js
 
13
  from src.logging import configure_root_logger, setup_logger
14
 
15
  configure_root_logger()
 
18
  API = HfApi(token=os.environ.get("TOKEN"))
19
  RESULTS_REPO = f"open-rl-leaderboard/results"
20
 
 
21
 
22
  ALL_ENV_IDS = {
23
  "Atari": [
 
39
  }
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def get_leaderboard_df():
43
  # List all results files in results repo
44
  pattern = re.compile(r"^[^/]*/[^/]*/[^/]*results_[a-f0-9]+\.json$")
src/backend.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import re
4
+ import tempfile
5
+
6
+ from huggingface_hub import CommitOperationAdd, HfApi, hf_hub_download
7
+
8
+ from src.evaluation import evaluate
9
+ from src.logging import setup_logger
10
+
11
+ logger = setup_logger(__name__)
12
+
13
+ API = HfApi(token=os.environ.get("TOKEN"))
14
+ RESULTS_REPO = "open-rl-leaderboard/results"
15
+
16
+
17
+ def _backend_routine():
18
+ # List only the text classification models
19
+ rl_models = list(API.list_models(filter="reinforcement-learning"))
20
+ logger.info(f"Found {len(rl_models)} RL models")
21
+ compatible_models = []
22
+ for model in rl_models:
23
+ filenames = [sib.rfilename for sib in model.siblings]
24
+ if "agent.pt" in filenames:
25
+ compatible_models.append((model.modelId, model.sha))
26
+
27
+ logger.info(f"Found {len(compatible_models)} compatible models")
28
+
29
+ # Get the results
30
+ pattern = re.compile(r"^[^/]*/[^/]*/[^/]*results_[a-f0-9]+\.json$")
31
+ filenames = API.list_repo_files(RESULTS_REPO, repo_type="dataset")
32
+ filenames = [filename for filename in filenames if pattern.match(filename)]
33
+
34
+ evaluated_models = set()
35
+ for filename in filenames:
36
+ path = hf_hub_download(repo_id=RESULTS_REPO, filename=filename, repo_type="dataset")
37
+ with open(path) as fp:
38
+ report = json.load(fp)
39
+ evaluated_models.add((report["config"]["model_id"], report["config"]["model_sha"]))
40
+
41
+ # Find the models that are not associated with any results
42
+ pending_models = set(compatible_models) - evaluated_models
43
+ logger.info(f"Found {len(pending_models)} pending models")
44
+
45
+ # Run an evaluation on the models
46
+ with tempfile.TemporaryDirectory() as tmp_dir:
47
+ commits = []
48
+ for model_id, sha in pending_models:
49
+ logger.info(f"Running evaluation on {model_id}")
50
+ report = {"config": {"model_id": model_id, "model_sha": sha}}
51
+ try:
52
+ evaluations = evaluate(model_id, revision=sha)
53
+ except Exception as e:
54
+ logger.error(f"Error evaluating {model_id}: {e}")
55
+ evaluations = None
56
+
57
+ if evaluations is not None:
58
+ report["results"] = evaluations
59
+ report["status"] = "DONE"
60
+ else:
61
+ report["status"] = "FAILED"
62
+
63
+ # Update the results
64
+ dumped = json.dumps(report, indent=2)
65
+ path_in_repo = f"{model_id}/results_{sha}.json"
66
+ local_path = os.path.join(tmp_dir, path_in_repo)
67
+ os.makedirs(os.path.dirname(local_path), exist_ok=True)
68
+ with open(local_path, "w") as f:
69
+ f.write(dumped)
70
+
71
+ commits.append(CommitOperationAdd(path_in_repo=path_in_repo, path_or_fileobj=local_path))
72
+
73
+ API.create_commit(
74
+ repo_id=RESULTS_REPO, commit_message="Add evaluation results", operations=commits, repo_type="dataset"
75
+ )
76
+
77
+
78
+ def backend_routine():
79
+ try:
80
+ _backend_routine()
81
+ except Exception as e:
82
+ logger.error(f"{e.__class__.__name__}: {str(e)}")