File size: 12,722 Bytes
88d91f4
383512a
059d8f0
a1f3b5b
383512a
 
88d91f4
 
 
e653f9c
b774671
 
383512a
fb67b80
88d91f4
 
 
e9f37ce
383512a
88d91f4
383512a
 
a51fb44
 
 
 
 
 
 
 
383512a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da681e8
 
 
 
 
 
383512a
 
 
 
 
 
 
 
 
 
 
 
e653f9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383512a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7216d20
 
9681540
 
 
 
17ddb08
9681540
a9fec77
 
 
 
 
 
7216d20
 
 
 
 
383512a
 
99e39f3
3ed1bd5
99e39f3
 
383512a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
474e3e0
383512a
474e3e0
 
 
 
 
383512a
 
 
e9f37ce
383512a
 
 
 
e9f37ce
391b960
383512a
 
 
 
 
059d8f0
b774671
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b21f2ca
383512a
059d8f0
e9f37ce
383512a
 
 
 
 
059d8f0
383512a
059d8f0
 
 
 
88d91f4
 
059d8f0
 
b9ceb4f
 
059d8f0
 
 
 
88d91f4
383512a
88d91f4
b21f2ca
 
88d91f4
383512a
88d91f4
 
1347af3
 
88d91f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383512a
 
d625373
383512a
 
 
 
 
e9f37ce
7fa09dc
88d91f4
1347af3
88d91f4
 
b774671
88d91f4
3dcfe9e
 
 
 
 
88d91f4
 
 
 
 
 
 
 
 
 
 
 
 
 
0496749
e90393b
0496749
059d8f0
383512a
 
059d8f0
88d91f4
383512a
88d91f4
 
 
383512a
88d91f4
383512a
6867483
88d91f4
deddda8
383512a
88d91f4
659a76d
88d91f4
383512a
 
6b1339b
88d91f4
383512a
 
 
 
 
 
 
 
 
3dcfe9e
88d91f4
383512a
88d91f4
 
 
 
 
383512a
88d91f4
 
58a3f61
88d91f4
1347af3
88d91f4
99e39f3
88d91f4
 
 
1347af3
58a3f61
 
1347af3
 
99e39f3
0fd6c86
5dac6c6
 
 
6acd633
5dac6c6
 
99e39f3
f6889a3
5dac6c6
0fd6c86
 
99e39f3
6acd633
e90393b
93397bb
f6889a3
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import os
import json
import requests

import gradio as gr
import pandas as pd
from huggingface_hub import HfApi, hf_hub_download, snapshot_download
from huggingface_hub.repocard import metadata_load
from apscheduler.schedulers.background import BackgroundScheduler

from tqdm.contrib.concurrent import thread_map

from utils import *

DATASET_REPO_URL = "https://huggingface.co/datasets/huggingface-projects/drlc-leaderboard-data"
DATASET_REPO_ID = "huggingface-projects/drlc-leaderboard-data"
HF_TOKEN = os.environ.get("HF_TOKEN")

block = gr.Blocks()
api = HfApi(token=HF_TOKEN)

# Containing the data
rl_envs = [
{
"rl_env_beautiful": "LunarLander-v2 🚀",
"rl_env": "LunarLander-v2",
"video_link": "",
"global": None
},    
{
"rl_env_beautiful": "CartPole-v1",
"rl_env": "CartPole-v1",
"video_link": "https://huggingface.co/sb3/ppo-CartPole-v1/resolve/main/replay.mp4",
"global": None
},
{
"rl_env_beautiful": "FrozenLake-v1-4x4-no_slippery ❄️",
"rl_env": "FrozenLake-v1-4x4-no_slippery",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "FrozenLake-v1-8x8-no_slippery ❄️",
"rl_env": "FrozenLake-v1-8x8-no_slippery",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "FrozenLake-v1-4x4 ❄️",
"rl_env": "FrozenLake-v1-4x4",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "FrozenLake-v1-8x8 ❄️",
"rl_env": "FrozenLake-v1-8x8",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "Taxi-v3 🚖",
"rl_env": "Taxi-v3",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "CarRacing-v0 🏎️",
"rl_env": "CarRacing-v0",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "CarRacing-v2 🏎️",
"rl_env": "CarRacing-v2",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "MountainCar-v0 ⛰️",
"rl_env": "MountainCar-v0",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "SpaceInvadersNoFrameskip-v4 👾",
"rl_env": "SpaceInvadersNoFrameskip-v4",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "PongNoFrameskip-v4 🎾",
"rl_env": "PongNoFrameskip-v4",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "BreakoutNoFrameskip-v4 🧱",
"rl_env": "BreakoutNoFrameskip-v4",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "QbertNoFrameskip-v4 🐦",
"rl_env": "QbertNoFrameskip-v4",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "BipedalWalker-v3",
"rl_env": "BipedalWalker-v3",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "Walker2DBulletEnv-v0",
"rl_env": "Walker2DBulletEnv-v0",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "AntBulletEnv-v0",
"rl_env": "AntBulletEnv-v0",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "HalfCheetahBulletEnv-v0",
"rl_env": "HalfCheetahBulletEnv-v0",
"video_link": "",
"global": None
},
{
"rl_env_beautiful": "PandaReachDense-v2",
"rl_env": "PandaReachDense-v2",
"video_link": "",
"global": None
},  
{
"rl_env_beautiful": "PandaReachDense-v3",
"rl_env": "PandaReachDense-v3",
"video_link": "",
"global": None
},  
{
"rl_env_beautiful": "Pixelcopter-PLE-v0",
"rl_env": "Pixelcopter-PLE-v0",
"video_link": "",
"global": None
}
]

def restart():
    print("RESTART")
    api.restart_space(repo_id="huggingface-projects/Deep-Reinforcement-Learning-Leaderboard")

def get_metadata(model_id):
    try:
        readme_path = hf_hub_download(model_id, filename="README.md")
        return metadata_load(readme_path)
    except requests.exceptions.HTTPError:
        # 404 README.md not found
        return None
        
def parse_metrics_accuracy(meta):
    if "model-index" not in meta:
        return None
    result = meta["model-index"][0]["results"]
    metrics = result[0]["metrics"]
    accuracy = metrics[0]["value"]
    return accuracy

# We keep the worst case episode
def parse_rewards(accuracy):
    default_std = -1000
    default_reward=-1000
    if accuracy !=  None:
        accuracy = str(accuracy)
        parsed =  accuracy.split('+/-')
        if len(parsed)>1:
            mean_reward = float(parsed[0].strip())
            std_reward =  float(parsed[1].strip())
        elif len(parsed)==1: #only mean reward
            mean_reward = float(parsed[0].strip())
            std_reward =  float(0) 
        else: 
            mean_reward = float(default_std)
            std_reward = float(default_reward)

    else:
        mean_reward = float(default_std)
        std_reward = float(default_reward)
    return mean_reward, std_reward


def get_model_ids(rl_env):
    api = HfApi()
    models = api.list_models(filter=rl_env)
    model_ids = [x.modelId for x in models]
    return model_ids

# Parralelized version
def update_leaderboard_dataset_parallel(rl_env, path):
    # Get model ids associated with rl_env
    model_ids = get_model_ids(rl_env)

    def process_model(model_id):
        meta = get_metadata(model_id)
        #LOADED_MODEL_METADATA[model_id] = meta if meta is not None else ''
        if meta is None:
            return None
        user_id = model_id.split('/')[0]
        row = {}
        row["User"] = user_id
        row["Model"] = model_id
        accuracy = parse_metrics_accuracy(meta)
        mean_reward, std_reward = parse_rewards(accuracy)
        mean_reward = mean_reward if not pd.isna(mean_reward) else 0
        std_reward = std_reward if not pd.isna(std_reward) else 0
        row["Results"] = mean_reward - std_reward
        row["Mean Reward"] = mean_reward
        row["Std Reward"] = std_reward
        return row

    data = list(thread_map(process_model, model_ids, desc="Processing models"))

    # Filter out None results (models with no metadata)
    data = [row for row in data if row is not None]

    ranked_dataframe = rank_dataframe(pd.DataFrame.from_records(data))
    new_history = ranked_dataframe
    file_path = path + "/" + rl_env + ".csv"
    new_history.to_csv(file_path, index=False)

    return ranked_dataframe


def update_leaderboard_dataset(rl_env, path):
    # Get model ids associated with rl_env
    model_ids = get_model_ids(rl_env)
    data = []
    for model_id in model_ids:
        """
        readme_path = hf_hub_download(model_id, filename="README.md")
        meta = metadata_load(readme_path)
        """
        meta = get_metadata(model_id)
        #LOADED_MODEL_METADATA[model_id] = meta if meta is not None else ''
        if meta is None:
            continue
        user_id = model_id.split('/')[0]
        row = {}
        row["User"] = user_id
        row["Model"] = model_id
        accuracy = parse_metrics_accuracy(meta)
        mean_reward, std_reward = parse_rewards(accuracy)
        mean_reward = mean_reward if not pd.isna(mean_reward) else 0
        std_reward = std_reward if not pd.isna(std_reward) else 0
        row["Results"] = mean_reward - std_reward
        row["Mean Reward"] = mean_reward
        row["Std Reward"] = std_reward
        data.append(row)

    ranked_dataframe = rank_dataframe(pd.DataFrame.from_records(data))
    new_history = ranked_dataframe
    file_path = path + "/" + rl_env + ".csv"
    new_history.to_csv(file_path, index=False)

    return ranked_dataframe

def download_leaderboard_dataset():
    path = snapshot_download(repo_id=DATASET_REPO_ID, repo_type="dataset")
    return path

def get_data(rl_env, path) -> pd.DataFrame:
    """
    Get data from rl_env
    :return: data as a pandas DataFrame
    """
    csv_path = path + "/" + rl_env + ".csv"
    data = pd.read_csv(csv_path)

    for index, row in data.iterrows():
        user_id = row["User"]
        data.loc[index, "User"] = make_clickable_user(user_id)
        model_id = row["Model"]
        data.loc[index, "Model"] = make_clickable_model(model_id)
        
    return data

def get_data_no_html(rl_env, path) -> pd.DataFrame:
    """
    Get data from rl_env
    :return: data as a pandas DataFrame
    """
    csv_path = path + "/" + rl_env + ".csv"
    data = pd.read_csv(csv_path)

    return data
    
def rank_dataframe(dataframe):
    dataframe = dataframe.sort_values(by=['Results', 'User', 'Model'], ascending=False)
    if not 'Ranking' in dataframe.columns:
        dataframe.insert(0, 'Ranking', [i for i in range(1,len(dataframe)+1)])
    else:
        dataframe['Ranking'] =   [i for i in range(1,len(dataframe)+1)]
    return dataframe


def run_update_dataset():
    path_ = download_leaderboard_dataset()
    for i in range(0, len(rl_envs)):
        rl_env = rl_envs[i]
        update_leaderboard_dataset_parallel(rl_env["rl_env"], path_)

    api.upload_folder(
    folder_path=path_,
    repo_id="huggingface-projects/drlc-leaderboard-data",
    repo_type="dataset",
    commit_message="Update dataset")

def filter_data(rl_env, path, user_id):
    data_df = get_data_no_html(rl_env, path)
    models = []
    models = data_df[data_df["User"] == user_id]

    for index, row in models.iterrows():
        user_id = row["User"]
        models.loc[index, "User"] = make_clickable_user(user_id)
        model_id = row["Model"]
        models.loc[index, "Model"] = make_clickable_model(model_id)
        

    return models

run_update_dataset()

with block:
    gr.Markdown(f"""
    # 🏆 The Deep Reinforcement Learning Course Leaderboard 🏆 
    
    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.
    
    ### We only display the best 100 models
    If you want to **find yours, type your user id and click on Search my models.**
    You **can click on the model's name** to be redirected to its model card, including documentation.
    
    ### How are the results calculated?
    We use **lower bound result to sort the models: mean_reward - std_reward.**

    ### I can't find my model 😭
    The leaderboard is **updated every two hours** if you can't find your models, just wait for the next update.
    
    ### The Deep RL Course
    🤖 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>.
        
    🔧 There is an **environment missing?** Please open an issue.
    """)
    path_ = download_leaderboard_dataset()

    for i in range(0, len(rl_envs)):
        rl_env = rl_envs[i]
        with gr.TabItem(rl_env["rl_env_beautiful"]) as rl_tab:
            with gr.Row():
                markdown = """
                    # {name_leaderboard}
                    
                    """.format(name_leaderboard = rl_env["rl_env_beautiful"], video_link = rl_env["video_link"])
                gr.Markdown(markdown)
                
            
            with gr.Row():
                gr.Markdown("""
                    ## Search your models
                    Simply type your user id to find your models
                    """)
                
            with gr.Row():
                user_id = gr.Textbox(label= "Your user id")
                search_btn = gr.Button("Search my models 🔎")
                reset_btn = gr.Button("Clear my search")
                env = gr.Variable(rl_env["rl_env"])
                grpath = gr.Variable(path_)
            with gr.Row():
                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'))
             
            with gr.Row():
                #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)
                search_btn.click(fn=filter_data, inputs=[env, grpath, user_id], outputs=gr_dataframe, api_name="filter_data")

            with gr.Row():
                search_btn.click(fn=filter_data, inputs=[env, grpath, user_id], outputs=gr_dataframe, api_name="filter_data")
                reset_btn.click(fn=get_data, inputs=[env, grpath], outputs=gr_dataframe, api_name="get_data")
    """
    block.load(
        download_leaderboard_dataset,
        inputs=[],
        outputs=[
            grpath
        ],
    )
    """


scheduler = BackgroundScheduler()
# Refresh every hour
#scheduler.add_job(func=run_update_dataset, trigger="interval", seconds=3600)
#scheduler.add_job(download_leaderboard_dataset, 'interval', seconds=3600)
#scheduler.add_job(run_update_dataset, 'interval', seconds=3600)
scheduler.add_job(restart, 'interval', seconds=7200)
scheduler.start()

block.launch()