chrisjay commited on
Commit
059d8f0
β€’
1 Parent(s): c519aa8

initial files

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +132 -0
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: πŸ“š
4
  colorFrom: green
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 2.9.4
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: green
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 2.9b23
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+ import pandas as pd
4
+ from tqdm.auto import tqdm
5
+
6
+ import gradio as gr
7
+ #import streamlit as st
8
+ from huggingface_hub import HfApi, hf_hub_download
9
+ from huggingface_hub.repocard import metadata_load
10
+ #import streamlit.components.v1 as components
11
+
12
+ # Based on Omar Sanseviero work
13
+ # Make model clickable link
14
+ def make_clickable_model(model_name):
15
+ link = "https://huggingface.co/" + model_name
16
+ return f'<a target="_blank" href="{link}">{model_name}</a>'
17
+
18
+ # Make user clickable link
19
+ def make_clickable_user(user_id):
20
+ link = "https://huggingface.co/" + user_id
21
+ return f'<a target="_blank" href="{link}">{user_id}</a>'
22
+
23
+ def get_model_ids(rl_env):
24
+ api = HfApi()
25
+ models = api.list_models(filter=rl_env)
26
+ model_ids = [x.modelId for x in models]
27
+ return model_ids
28
+
29
+ def get_metadata(model_id):
30
+ try:
31
+ readme_path = hf_hub_download(model_id, filename="README.md")
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
+ print("ACCURACY", accuracy)
44
+ return accuracy
45
+
46
+ # We keep the worst case episode
47
+ def parse_rewards(accuracy):
48
+ if accuracy != None:
49
+ parsed = accuracy.split(' +/- ')
50
+ mean_reward = float(parsed[0])
51
+ std_reward = float(parsed[1])
52
+ else:
53
+ mean_reward = -1000
54
+ std_reward = -1000
55
+ return mean_reward, std_reward
56
+
57
+ def get_data(rl_env):
58
+ data = []
59
+ model_ids = get_model_ids(rl_env)
60
+ for model_id in tqdm(model_ids):
61
+ meta = get_metadata(model_id)
62
+ if meta is None:
63
+ continue
64
+ user_id = model_id.split('/')[0]
65
+ row = {}
66
+ row["User"] = user_id
67
+ row["Model"] = model_id
68
+ accuracy = parse_metrics_accuracy(meta)
69
+ print("RETURNED ACCURACY", accuracy)
70
+ mean_reward, std_reward = parse_rewards(accuracy)
71
+ print("MEAN REWARD", mean_reward)
72
+ row["Results"] = mean_reward - std_reward
73
+ row["Mean Reward"] = mean_reward
74
+ row["Std Reward"] = std_reward
75
+ data.append(row)
76
+ return pd.DataFrame.from_records(data)
77
+
78
+
79
+ def get_data_per_env(rl_env):
80
+ dataframe = get_data(rl_env)
81
+ dataframe = dataframe.fillna("")
82
+
83
+ #import pdb; pdb.set_trace()
84
+ if not dataframe.empty:
85
+ # turn the model ids into clickable links
86
+ dataframe["User"] = dataframe["User"].apply(make_clickable_user)
87
+ dataframe["Model"] = dataframe["Model"].apply(make_clickable_model)
88
+ dataframe = dataframe.sort_values(by=['Results'], ascending=False)
89
+ table_html = dataframe.to_html(escape=False, index=False)
90
+ table_html = table_html.replace("<th>", '<th align="left">') # left-align the headers
91
+ return table_html,dataframe,dataframe.empty
92
+ else:
93
+ html = """<div style="color: green">
94
+ <p> βŒ› Please wait. Results will be out soon... </p>
95
+ </div>
96
+ """
97
+ return html,dataframe,dataframe.empty
98
+
99
+
100
+
101
+ RL_ENVS = ['CarRacing-v0','MountainCar-v0','LunarLander-v2']
102
+ RL_DETAILS ={'CarRacing-v0':{'title':" The Car Racing πŸŒ• Leaderboard πŸš€",'data':get_data_per_env('CarRacing-v0')},
103
+ 'MountainCar-v0':{'title':"The Mountain Car πŸŒ• Leaderboard πŸš€",'data':get_data_per_env('MountainCar-v0')},
104
+ 'LunarLander-v2':{'title':" The Lunar Lander πŸŒ• Leaderboard πŸš€",'data':get_data_per_env('LunarLander-v2')}
105
+ }
106
+
107
+
108
+ block = gr.Blocks()
109
+ with block:
110
+
111
+ with gr.Tabs():
112
+ for rl_env in RL_ENVS:
113
+ with gr.TabItem(rl_env):
114
+ data_html,data_dataframe,is_empty = RL_DETAILS[rl_env]['data']
115
+
116
+ markdown = """
117
+ # {name_leaderboard}
118
+
119
+ This is a leaderboard of {len_dataframe}** agents playing {env_name} πŸ‘©β€πŸš€.
120
+
121
+ We use lower bound result to sort the models: mean_reward - std_reward.
122
+
123
+ You can click on the model's name to be redirected to its model card which includes documentation.
124
+
125
+ You want to try your model? Read this Unit 1 of Deep Reinforcement Learning Class: https://github.com/huggingface/deep-rl-class/blob/Unit1/unit1/README.md.
126
+
127
+ """.format(len_dataframe = len(data_dataframe),env_name = rl_env,name_leaderboard = RL_DETAILS[rl_env]['title'])
128
+ gr.Markdown(markdown)
129
+ gr.HTML(data_html)
130
+
131
+
132
+ block.launch()