add-support-for-new-vidore-result-format

#2
by tonywu71 - opened
Files changed (2) hide show
  1. data/model_handler.py +34 -17
  2. ruff.toml +7 -0
data/model_handler.py CHANGED
@@ -1,12 +1,15 @@
1
  import json
2
  import os
3
- from typing import Dict
4
- from huggingface_hub import HfApi, hf_hub_download, metadata_load
5
  import pandas as pd
6
- from .dataset_handler import get_datasets_nickname, VIDORE_DATASETS_KEYWORDS
 
 
7
 
8
  BLOCKLIST = ["impactframes"]
9
 
 
10
  class ModelHandler:
11
  def __init__(self, model_infos_path="model_infos.json"):
12
  self.api = HfApi()
@@ -23,26 +26,40 @@ class ModelHandler:
23
  with open(self.model_infos_path, "w") as f:
24
  json.dump(self.model_infos, f)
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def get_vidore_data(self, metric="ndcg_at_5"):
27
  models = self.api.list_models(filter="vidore")
28
  repositories = [model.modelId for model in models] # type: ignore
29
 
30
  for repo_id in repositories:
31
- org_name = repo_id.split('/')[0]
32
  if org_name in BLOCKLIST:
33
  continue
34
-
35
- files = [f for f in self.api.list_repo_files(repo_id) if f.endswith('_metrics.json') or f == 'results.json']
36
 
37
-
 
38
  if len(files) == 0:
39
  continue
40
  else:
41
  for file in files:
42
- if file.endswith('results.json'):
43
- model_name = repo_id.replace('/', '_')
44
  else:
45
- model_name = file.split('_metrics.json')[0]
46
 
47
  if model_name not in self.model_infos:
48
  readme_path = hf_hub_download(repo_id, filename="README.md")
@@ -53,15 +70,15 @@ class ModelHandler:
53
  with open(result_path) as f:
54
  results = json.load(f)
55
 
56
- for dataset in results:
57
- results[dataset] = {key: value for key, value in results[dataset].items()}
58
 
59
  self.model_infos[model_name] = {"meta": meta, "results": results}
60
  except Exception as e:
61
  print(f"Error loading {model_name} - {e}")
62
  continue
63
 
64
- #self._save_model_infos()
65
 
66
  model_res = {}
67
  if len(self.model_infos) > 0:
@@ -69,7 +86,7 @@ class ModelHandler:
69
  res = self.model_infos[model]["results"]
70
  dataset_res = {}
71
  for dataset in res.keys():
72
- #for each keyword check if it is in the dataset name if not continue
73
  if not any(keyword in dataset for keyword in VIDORE_DATASETS_KEYWORDS):
74
  print(f"{dataset} not found in ViDoRe datasets. Skipping ...")
75
  continue
@@ -77,9 +94,9 @@ class ModelHandler:
77
  dataset_nickname = get_datasets_nickname(dataset)
78
  dataset_res[dataset_nickname] = res[dataset][metric]
79
  model_res[model] = dataset_res
80
-
81
  df = pd.DataFrame(model_res).T
82
-
83
  return df
84
  return pd.DataFrame()
85
 
@@ -104,7 +121,7 @@ class ModelHandler:
104
  df.insert(len(df.columns) - len(cols_to_rank), "Average", df[cols_to_rank].mean(axis=1, skipna=False))
105
  df.sort_values("Average", ascending=False, inplace=True)
106
  df.insert(0, "Rank", list(range(1, len(df) + 1)))
107
- #multiply values by 100 if they are floats and round to 1 decimal place
108
  for col in df.columns:
109
  if df[col].dtype == "float64":
110
  df[col] = df[col].apply(lambda x: round(x * 100, 1))
 
1
  import json
2
  import os
3
+ from typing import Any, Dict, Tuple
4
+
5
  import pandas as pd
6
+ from huggingface_hub import HfApi, hf_hub_download, metadata_load
7
+
8
+ from .dataset_handler import VIDORE_DATASETS_KEYWORDS, get_datasets_nickname
9
 
10
  BLOCKLIST = ["impactframes"]
11
 
12
+
13
  class ModelHandler:
14
  def __init__(self, model_infos_path="model_infos.json"):
15
  self.api = HfApi()
 
26
  with open(self.model_infos_path, "w") as f:
27
  json.dump(self.model_infos, f)
28
 
29
+ def _are_results_in_new_vidore_format(self, results: Dict[str, Any]) -> bool:
30
+ return "metadata" in results and "metrics" in results
31
+
32
+ @staticmethod
33
+ def convert_new_vidore_results_format(results: Dict[str, Any]) -> Tuple[str, str, Dict[str, Any]]:
34
+ if "metadata" not in results:
35
+ raise KeyError("results does not contain a 'metadata' key.")
36
+ if "metrics" not in results:
37
+ raise KeyError("results does not contain a 'metrics' key.")
38
+
39
+ metadata = results["metadata"]
40
+ metrics = results["metrics"]
41
+
42
+ return metadata["timestamp"], metadata["vidore_benchmark_hash"], metrics
43
+
44
  def get_vidore_data(self, metric="ndcg_at_5"):
45
  models = self.api.list_models(filter="vidore")
46
  repositories = [model.modelId for model in models] # type: ignore
47
 
48
  for repo_id in repositories:
49
+ org_name = repo_id.split("/")[0]
50
  if org_name in BLOCKLIST:
51
  continue
 
 
52
 
53
+ files = [f for f in self.api.list_repo_files(repo_id) if f.endswith("_metrics.json") or f == "results.json"]
54
+
55
  if len(files) == 0:
56
  continue
57
  else:
58
  for file in files:
59
+ if file.endswith("results.json"):
60
+ model_name = repo_id.replace("/", "_")
61
  else:
62
+ model_name = file.split("_metrics.json")[0]
63
 
64
  if model_name not in self.model_infos:
65
  readme_path = hf_hub_download(repo_id, filename="README.md")
 
70
  with open(result_path) as f:
71
  results = json.load(f)
72
 
73
+ if self._are_results_in_new_vidore_format(results):
74
+ timestamp, vidore_hash, results = self.convert_new_vidore_results_format(results)
75
 
76
  self.model_infos[model_name] = {"meta": meta, "results": results}
77
  except Exception as e:
78
  print(f"Error loading {model_name} - {e}")
79
  continue
80
 
81
+ # self._save_model_infos()
82
 
83
  model_res = {}
84
  if len(self.model_infos) > 0:
 
86
  res = self.model_infos[model]["results"]
87
  dataset_res = {}
88
  for dataset in res.keys():
89
+ # for each keyword check if it is in the dataset name if not continue
90
  if not any(keyword in dataset for keyword in VIDORE_DATASETS_KEYWORDS):
91
  print(f"{dataset} not found in ViDoRe datasets. Skipping ...")
92
  continue
 
94
  dataset_nickname = get_datasets_nickname(dataset)
95
  dataset_res[dataset_nickname] = res[dataset][metric]
96
  model_res[model] = dataset_res
97
+
98
  df = pd.DataFrame(model_res).T
99
+
100
  return df
101
  return pd.DataFrame()
102
 
 
121
  df.insert(len(df.columns) - len(cols_to_rank), "Average", df[cols_to_rank].mean(axis=1, skipna=False))
122
  df.sort_values("Average", ascending=False, inplace=True)
123
  df.insert(0, "Rank", list(range(1, len(df) + 1)))
124
+ # multiply values by 100 if they are floats and round to 1 decimal place
125
  for col in df.columns:
126
  if df[col].dtype == "float64":
127
  df[col] = df[col].apply(lambda x: round(x * 100, 1))
ruff.toml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ line-length = 120
2
+
3
+ [lint]
4
+ select = ["E", "F", "W", "I", "N"]
5
+
6
+ [lint.per-file-ignores]
7
+ "__init__.py" = ["F401"]