philippds commited on
Commit
3c478c7
Β·
1 Parent(s): 74f1f58
Files changed (3) hide show
  1. app.py +213 -0
  2. requirements.txt +5 -0
  3. utils.py +14 -0
app.py ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 tqdm.contrib.concurrent import thread_map
12
+
13
+ from utils import make_clickable_model, make_clickable_user
14
+
15
+ DATASET_REPO_URL = (
16
+ "https://huggingface.co/datasets/hivex-research/hivex-leaderboard-data"
17
+ )
18
+ DATASET_REPO_ID = "hivex-research/hivex-leaderboard-data"
19
+ HF_TOKEN = os.environ.get("HF_TOKEN")
20
+
21
+ block = gr.Blocks()
22
+ api = HfApi(token=HF_TOKEN)
23
+
24
+ hivex_envs = [
25
+ {
26
+ "hivex_env": "hivex-wind-farm-control",
27
+ },
28
+ {
29
+ "hivex_env": "hivex-wildfire-resource-management",
30
+ },
31
+ {
32
+ "hivex_env": "hivex-drone-based-reforestation",
33
+ },
34
+ {
35
+ "hivex_env": "hivex-ocean-plastic-collection",
36
+ },
37
+ {
38
+ "hivex_env": "hivex-aerial-wildfire-suppression",
39
+ },
40
+ ]
41
+
42
+
43
+ def restart():
44
+ print("RESTART")
45
+ api.restart_space(repo_id="hivex-research/hivex-leaderboard")
46
+
47
+
48
+ def download_leaderboard_dataset():
49
+ path = snapshot_download(repo_id=DATASET_REPO_ID, repo_type="dataset")
50
+ return path
51
+
52
+
53
+ def get_model_ids(hivex_env):
54
+ api = HfApi()
55
+ models = api.list_models(filter=hivex_env)
56
+ model_ids = [x.modelId for x in models]
57
+ return model_ids
58
+
59
+
60
+ def get_metadata(model_id):
61
+ try:
62
+ readme_path = hf_hub_download(model_id, filename="README.md", etag_timeout=180)
63
+ return metadata_load(readme_path)
64
+ except requests.exceptions.HTTPError:
65
+ # 404 README.md not found
66
+ return None
67
+
68
+
69
+ # def parse_metrics_accuracy(meta):
70
+ # if "model-index" not in meta:
71
+ # return None
72
+ # result = meta["model-index"][0]["results"]
73
+ # metrics = result[0]["metrics"]
74
+ # accuracy = metrics[0]["value"]
75
+ # return accuracy
76
+
77
+
78
+ # def parse_rewards(accuracy):
79
+ # default_std = -1000
80
+ # default_reward = -1000
81
+ # if accuracy != None:
82
+ # accuracy = str(accuracy)
83
+ # parsed = accuracy.split("+/-")
84
+ # if len(parsed) > 1:
85
+ # mean_reward = float(parsed[0].strip())
86
+ # std_reward = float(parsed[1].strip())
87
+ # elif len(parsed) == 1: # only mean reward
88
+ # mean_reward = float(parsed[0].strip())
89
+ # std_reward = float(0)
90
+ # else:
91
+ # mean_reward = float(default_std)
92
+ # std_reward = float(default_reward)
93
+
94
+ # else:
95
+ # mean_reward = float(default_std)
96
+ # std_reward = float(default_reward)
97
+ # return mean_reward, std_reward
98
+
99
+
100
+ def rank_dataframe(dataframe):
101
+ dataframe = dataframe.sort_values(
102
+ by=["Cumulative Reward", "User", "Model"], ascending=False
103
+ )
104
+ if not "Ranking" in dataframe.columns:
105
+ dataframe.insert(0, "Ranking", [i for i in range(1, len(dataframe) + 1)])
106
+ else:
107
+ dataframe["Ranking"] = [i for i in range(1, len(dataframe) + 1)]
108
+ return dataframe
109
+
110
+
111
+ def update_leaderboard_dataset_parallel(hivex_env, path):
112
+ # Get model ids associated with hivex_env
113
+ model_ids = get_model_ids(hivex_env)
114
+
115
+ def process_model(model_id):
116
+ meta = get_metadata(model_id)
117
+ # LOADED_MODEL_METADATA[model_id] = meta if meta is not None else ''
118
+ if meta is None:
119
+ return None
120
+ user_id = model_id.split("/")[0]
121
+ row = {}
122
+ row["User"] = user_id
123
+ row["Model"] = model_id
124
+ # accuracy = parse_metrics_accuracy(meta)
125
+ # mean_reward, std_reward = parse_rewards(accuracy)
126
+ # mean_reward = mean_reward if not pd.isna(mean_reward) else 0
127
+ # std_reward = std_reward if not pd.isna(std_reward) else 0
128
+ # row["Results"] = mean_reward - std_reward
129
+ # row["Mean Reward"] = mean_reward
130
+ # row["Std Reward"] = std_reward
131
+ results = meta["model-index"][0]["results"][0]["metrics"]
132
+
133
+ for result in results:
134
+ row[result["name"]] = float(result["value"].split("+/-")[0].strip())
135
+
136
+ return row
137
+
138
+ data = list(thread_map(process_model, model_ids, desc="Processing models"))
139
+
140
+ # Filter out None results (models with no metadata)
141
+ data = [row for row in data if row is not None]
142
+
143
+ ranked_dataframe = rank_dataframe(pd.DataFrame.from_records(data))
144
+ new_history = ranked_dataframe
145
+ file_path = path + "/" + hivex_env + ".csv"
146
+ new_history.to_csv(file_path, index=False)
147
+
148
+ return ranked_dataframe
149
+
150
+
151
+ def run_update_dataset():
152
+ path_ = download_leaderboard_dataset()
153
+ for i in range(0, len(hivex_envs)):
154
+ hivex_env = hivex_envs[i]
155
+ update_leaderboard_dataset_parallel(hivex_env["hivex_env"], path_)
156
+
157
+ api.upload_folder(
158
+ folder_path=path_,
159
+ repo_id="hivex-research/hivex-leaderboard-data",
160
+ repo_type="dataset",
161
+ commit_message="Update dataset",
162
+ )
163
+
164
+
165
+ def get_data(rl_env, path) -> pd.DataFrame:
166
+ """
167
+ Get data from rl_env
168
+ :return: data as a pandas DataFrame
169
+ """
170
+ csv_path = path + "/" + rl_env + ".csv"
171
+ data = pd.read_csv(csv_path)
172
+
173
+ for index, row in data.iterrows():
174
+ user_id = row["User"]
175
+ data.loc[index, "User"] = make_clickable_user(user_id)
176
+ model_id = row["Model"]
177
+ data.loc[index, "Model"] = make_clickable_model(model_id)
178
+
179
+ return data
180
+
181
+
182
+ def get_data_no_html(rl_env, path) -> pd.DataFrame:
183
+ """
184
+ Get data from rl_env
185
+ :return: data as a pandas DataFrame
186
+ """
187
+ csv_path = path + "/" + rl_env + ".csv"
188
+ data = pd.read_csv(csv_path)
189
+
190
+ return data
191
+
192
+
193
+ run_update_dataset()
194
+
195
+ main_block = gr.Blocks()
196
+ with main_block:
197
+ with gr.Row(elem_id="header-row"):
198
+ # TITLE + "<p>Total models: " + str(len(HARD_LEADERBOARD_DF))+ "</p>"
199
+ gr.HTML("<h1>Leaderboard</h1>")
200
+
201
+ # gr.Markdown(INTRODUCTION_TEXT, elem_classes="markdown-text")
202
+ with gr.Tabs(elem_classes="tab-buttons") as tabs:
203
+ with gr.Tab("πŸ’Ž Hard Set") as hard_tabs:
204
+ with gr.TabItem(
205
+ "πŸ… Benchmark", elem_id="llm-benchmark-tab-table", id="hard_bench"
206
+ ):
207
+ gr.DataTable(
208
+ get_data(
209
+ "hivex-wind-farm-control", "datasets/hivex-leaderboard-data"
210
+ ),
211
+ elem_id="hard_benchmark_table",
212
+ elem_classes="table",
213
+ )
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # pip install -r requirements.txt
2
+ APScheduler==3.10.1
3
+ gradio==4.0
4
+ httpx==0.24.0
5
+ tqdm
utils.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Based on Omar Sanseviero work
2
+ # Make model clickable link
3
+ def make_clickable_model(model_name):
4
+ # remove user from model name
5
+ model_name_show = " ".join(model_name.split("/")[1:])
6
+
7
+ link = "https://huggingface.co/" + model_name
8
+ return f'<a target="_blank" href="{link}">{model_name_show}</a>'
9
+
10
+
11
+ # Make user clickable link
12
+ def make_clickable_user(user_id):
13
+ link = "https://huggingface.co/" + user_id
14
+ return f'<a target="_blank" href="{link}">{user_id}</a>'