albertvillanova HF staff commited on
Commit
841e241
1 Parent(s): 15c8167

Create src.details module

Browse files
Files changed (2) hide show
  1. app.py +4 -175
  2. src/details.py +81 -0
app.py CHANGED
@@ -1,185 +1,14 @@
1
- import json
2
-
3
  import gradio as gr
4
- import pandas as pd
5
  from huggingface_hub import HfFileSystem
6
 
7
- from src.constants import DETAILS_DATASET_ID, DETAILS_FILENAME, SUBTASKS, TASKS
 
 
8
  from src.results import fetch_result_paths, filter_latest_result_path_per_model, update_load_results_component, \
9
  load_results_dataframes, display_results, update_tasks_component, clear_results
10
 
11
- fs = HfFileSystem()
12
-
13
-
14
- def fetch_result_paths():
15
- paths = fs.glob(f"{RESULTS_DATASET_ID}/**/**/*.json")
16
- return paths
17
-
18
-
19
- def filter_latest_result_path_per_model(paths):
20
- from collections import defaultdict
21
-
22
- d = defaultdict(list)
23
- for path in paths:
24
- model_id, _ = path[len(RESULTS_DATASET_ID) + 1:].rsplit("/", 1)
25
- d[model_id].append(path)
26
- return {model_id: max(paths) for model_id, paths in d.items()}
27
-
28
-
29
- def get_result_path_from_model(model_id, result_path_per_model):
30
- return result_path_per_model[model_id]
31
-
32
-
33
- def update_load_results_component():
34
- return gr.Button("Load Results", interactive=True)
35
-
36
-
37
- def load_data(result_path) -> pd.DataFrame:
38
- with fs.open(result_path, "r") as f:
39
- data = json.load(f)
40
- return data
41
-
42
-
43
- def load_results_dataframe(model_id):
44
- if not model_id:
45
- return
46
- result_path = get_result_path_from_model(model_id, latest_result_path_per_model)
47
- data = load_data(result_path)
48
- model_name = data.get("model_name", "Model")
49
- df = pd.json_normalize([{key: value for key, value in data.items()}])
50
- # df.columns = df.columns.str.split(".") # .split return a list instead of a tuple
51
- return df.set_index(pd.Index([model_name])).reset_index()
52
-
53
-
54
- def load_results_dataframes(*model_ids):
55
- return [load_results_dataframe(model_id) for model_id in model_ids]
56
-
57
-
58
- def display_results(task, *dfs):
59
- dfs = [df.set_index("index") for df in dfs if "index" in df.columns]
60
- if not dfs:
61
- return None, None
62
- df = pd.concat(dfs)
63
- df = df.T.rename_axis(columns=None)
64
- return display_tab("results", df, task), display_tab("configs", df, task)
65
-
66
-
67
- def display_tab(tab, df, task):
68
- df = df.style.format(na_rep="")
69
- df.hide(
70
- [
71
- row
72
- for row in df.index
73
- if (
74
- not row.startswith(f"{tab}.")
75
- or row.startswith(f"{tab}.leaderboard.")
76
- or row.endswith(".alias")
77
- or (not row.startswith(f"{tab}.{task}") if task != "All" else False)
78
- )
79
- ],
80
- axis="index",
81
- )
82
- start = len(f"{tab}.leaderboard_") if task == "All" else len(f"{tab}.{task} ")
83
- df.format_index(lambda idx: idx[start:].removesuffix(",none"), axis="index")
84
- return df.to_html()
85
-
86
 
87
- def update_tasks_component():
88
- return gr.Radio(
89
- ["All"] + list(TASKS.values()),
90
- label="Tasks",
91
- info="Evaluation tasks to be displayed",
92
- value="All",
93
- interactive=True,
94
- )
95
-
96
-
97
- def clear_results():
98
- # model_id_1, model_id_2, dataframe_1, dataframe_2, task
99
- return (
100
- None, None, None, None,
101
- gr.Radio(
102
- ["All"] + list(TASKS.values()),
103
- label="Tasks",
104
- info="Evaluation tasks to be displayed",
105
- value="All",
106
- interactive=False,
107
- ),
108
- )
109
-
110
-
111
- def update_subtasks_component(task):
112
- return gr.Radio(
113
- SUBTASKS.get(task),
114
- info="Evaluation subtasks to be displayed",
115
- value=None,
116
- )
117
-
118
-
119
- def update_load_details_component(model_id_1, model_id_2, subtask):
120
- if (model_id_1 or model_id_2) and subtask:
121
- return gr.Button("Load Details", interactive=True)
122
- else:
123
- return gr.Button("Load Details", interactive=False)
124
-
125
-
126
- def load_details_dataframe(model_id, subtask):
127
- if not model_id or not subtask:
128
- return
129
- model_name_sanitized = model_id.replace("/", "__")
130
- paths = fs.glob(
131
- f"{DETAILS_DATASET_ID}/**/{DETAILS_FILENAME}".format(
132
- model_name_sanitized=model_name_sanitized, subtask=subtask
133
- )
134
- )
135
- if not paths:
136
- return
137
- path = max(paths)
138
- with fs.open(path, "r") as f:
139
- data = [json.loads(line) for line in f]
140
- df = pd.json_normalize(data)
141
- # df = df.rename_axis("Parameters", axis="columns")
142
- df["model_name"] = model_id # Keep model_name
143
- return df
144
- # return df.set_index(pd.Index([model_id])).reset_index()
145
-
146
-
147
- def load_details_dataframes(subtask, *model_ids):
148
- return [load_details_dataframe(model_id, subtask) for model_id in model_ids]
149
-
150
-
151
- def display_details(sample_idx, *dfs):
152
- rows = [df.iloc[sample_idx] for df in dfs if "model_name" in df.columns and sample_idx < len(df)]
153
- if not rows:
154
- return
155
- # Pop model_name and add it to the column name
156
- df = pd.concat([row.rename(row.pop("model_name")) for row in rows], axis="columns")
157
- return (
158
- df.style
159
- .format(na_rep="")
160
- # .hide(axis="index")
161
- .to_html()
162
- )
163
-
164
-
165
- def update_sample_idx_component(*dfs):
166
- maximum = max([len(df) - 1 for df in dfs])
167
- return gr.Number(
168
- label="Sample Index",
169
- info="Index of the sample to be displayed",
170
- value=0,
171
- minimum=0,
172
- maximum=maximum,
173
- visible=True,
174
- )
175
-
176
-
177
- def clear_details():
178
- # model_id_1, model_id_2, details_dataframe_1, details_dataframe_2, details_task, subtask, sample_idx
179
- return (
180
- None, None, None, None, None, None,
181
- gr.Number(label="Sample Index", info="Index of the sample to be displayed", value=0, minimum=0,visible=False),
182
- )
183
 
184
 
185
  # if __name__ == "__main__":
 
 
 
1
  import gradio as gr
 
2
  from huggingface_hub import HfFileSystem
3
 
4
+ from src.constants import SUBTASKS, TASKS
5
+ from src.details import update_subtasks_component, update_load_details_component, load_details_dataframes, \
6
+ display_details, update_sample_idx_component, clear_details
7
  from src.results import fetch_result_paths, filter_latest_result_path_per_model, update_load_results_component, \
8
  load_results_dataframes, display_results, update_tasks_component, clear_results
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ fs = HfFileSystem()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
 
14
  # if __name__ == "__main__":
src/details.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ import gradio as gr
4
+ import pandas as pd
5
+
6
+ from app import fs
7
+ from src.constants import SUBTASKS, DETAILS_DATASET_ID, DETAILS_FILENAME
8
+
9
+
10
+ def update_subtasks_component(task):
11
+ return gr.Radio(
12
+ SUBTASKS.get(task),
13
+ info="Evaluation subtasks to be displayed",
14
+ value=None,
15
+ )
16
+
17
+
18
+ def update_load_details_component(model_id_1, model_id_2, subtask):
19
+ if (model_id_1 or model_id_2) and subtask:
20
+ return gr.Button("Load Details", interactive=True)
21
+ else:
22
+ return gr.Button("Load Details", interactive=False)
23
+
24
+
25
+ def load_details_dataframe(model_id, subtask):
26
+ if not model_id or not subtask:
27
+ return
28
+ model_name_sanitized = model_id.replace("/", "__")
29
+ paths = fs.glob(
30
+ f"{DETAILS_DATASET_ID}/**/{DETAILS_FILENAME}".format(
31
+ model_name_sanitized=model_name_sanitized, subtask=subtask
32
+ )
33
+ )
34
+ if not paths:
35
+ return
36
+ path = max(paths)
37
+ with fs.open(path, "r") as f:
38
+ data = [json.loads(line) for line in f]
39
+ df = pd.json_normalize(data)
40
+ # df = df.rename_axis("Parameters", axis="columns")
41
+ df["model_name"] = model_id # Keep model_name
42
+ return df
43
+ # return df.set_index(pd.Index([model_id])).reset_index()
44
+
45
+
46
+ def load_details_dataframes(subtask, *model_ids):
47
+ return [load_details_dataframe(model_id, subtask) for model_id in model_ids]
48
+
49
+
50
+ def display_details(sample_idx, *dfs):
51
+ rows = [df.iloc[sample_idx] for df in dfs if "model_name" in df.columns and sample_idx < len(df)]
52
+ if not rows:
53
+ return
54
+ # Pop model_name and add it to the column name
55
+ df = pd.concat([row.rename(row.pop("model_name")) for row in rows], axis="columns")
56
+ return (
57
+ df.style
58
+ .format(na_rep="")
59
+ # .hide(axis="index")
60
+ .to_html()
61
+ )
62
+
63
+
64
+ def update_sample_idx_component(*dfs):
65
+ maximum = max([len(df) - 1 for df in dfs])
66
+ return gr.Number(
67
+ label="Sample Index",
68
+ info="Index of the sample to be displayed",
69
+ value=0,
70
+ minimum=0,
71
+ maximum=maximum,
72
+ visible=True,
73
+ )
74
+
75
+
76
+ def clear_details():
77
+ # model_id_1, model_id_2, details_dataframe_1, details_dataframe_2, details_task, subtask, sample_idx
78
+ return (
79
+ None, None, None, None, None, None,
80
+ gr.Number(label="Sample Index", info="Index of the sample to be displayed", value=0, minimum=0,visible=False),
81
+ )