Spaces:
Running
Running
File size: 11,692 Bytes
d6979e5 511bdf3 d6979e5 511bdf3 d6979e5 511bdf3 026906f d6979e5 |
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 |
import os
import json
import time
import shutil
import logging
import zipfile
from typing import List, Optional
from collections import defaultdict
from air_benchmark.tasks.tasks import check_benchmark_version
from air_benchmark.evaluation_utils.data_loader import DataLoader
from air_benchmark.evaluation_utils.evaluator import Evaluator
from src.envs import (
API,
LOG_DIR, ZIP_CACHE_DIR,
SEARCH_RESULTS_REPO, RESULTS_REPO
)
log_file = os.path.join(LOG_DIR, f"backend_{time.strftime('%Y-%m-%d_%H-%M-%S')}.log")
logger = logging.getLogger(__name__)
logging.basicConfig(
filename=log_file,
filemode='w',
level=logging.WARNING,
datefmt='%Y-%m-%d %H:%M:%S',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
def compute_metrics(
benchmark_version: str,
search_results_save_dir: str,
k_values: List[int] = [1, 3, 5, 10, 50, 100, 1000],
cache_dir: Optional[str] = None,
):
data_loader = DataLoader(benchmark_version, cache_dir=cache_dir)
evaluator = Evaluator(data_loader)
eval_results = evaluator.evaluate_results(search_results_save_dir, k_values=k_values)
return eval_results
def save_evaluation_results(
eval_results: dict,
save_path: str,
model_name: str,
reranker_name: str,
model_link: Optional[str] = None,
reranker_link: Optional[str] = None,
is_anonymous: bool = False,
timestamp: str = None,
revision: str = None,
):
results = defaultdict(list)
configs = {}
for task_type, task_type_results in eval_results.items():
for domain, domain_results in task_type_results.items():
for lang, lang_results in domain_results.items():
for dataset_name, task_results in lang_results.items():
for metric, metric_val in task_results.items():
_key = f"{model_name}_{reranker_name}_{task_type}_{metric}"
results[_key].append({
"domain": domain,
"lang": lang,
"dataset": dataset_name,
"value": metric_val,
})
configs[_key] = {
"retrieval_model": model_name,
"retrieval_model_link": model_link,
"reranking_model": reranker_name,
"reranking_model_link": reranker_link,
"task": task_type,
"metric": metric,
"timestamp": timestamp,
"is_anonymous": is_anonymous,
"revision": revision,
}
results_list = []
for k, result in results.items():
config = configs[k]
results_list.append({
"config": config,
"results": result
})
with open(save_path, 'w', encoding='utf-8') as f:
json.dump(results_list, f, ensure_ascii=False, indent=4)
def get_file_list(dir_path: str, allowed_suffixes: List[str] = None) -> List[str]:
file_paths = set()
if os.path.exists(dir_path) and os.path.isdir(dir_path):
for root, _, files in os.walk(dir_path):
for file in files:
if allowed_suffixes is None or any(
file.endswith(suffix) for suffix in allowed_suffixes
):
file_paths.add(os.path.abspath(os.path.join(root, file)))
return file_paths
def get_zip_file_path(zip_file_name: str):
zip_file_path = None
for root, _, files in os.walk(ZIP_CACHE_DIR):
for file in files:
if file == zip_file_name:
zip_file_path = os.path.abspath(os.path.join(root, file))
break
return zip_file_path
def pull_search_results(
hf_search_results_repo_dir: str,
hf_eval_results_repo_dir: str,
unzip_target_dir: str,
k_values: List[int] = [1, 3, 5, 10, 50, 100, 1000],
cache_dir: str = None,
time_duration: int = 1800,
start_commit_id: str = None
):
print("Start from commit:", start_commit_id)
if start_commit_id is not None:
API.snapshot_download(
repo_id=SEARCH_RESULTS_REPO,
repo_type="dataset",
revision=start_commit_id,
local_dir=hf_search_results_repo_dir,
etag_timeout=30,
allow_patterns=['*.json']
)
cur_file_paths = get_file_list(hf_search_results_repo_dir, allowed_suffixes=['.json'])
else:
cur_file_paths = get_file_list(hf_search_results_repo_dir, allowed_suffixes=['.json'])
print("Start to pull new search results ...")
while True:
os.makedirs(ZIP_CACHE_DIR, exist_ok=True)
os.makedirs(unzip_target_dir, exist_ok=True)
try:
API.snapshot_download(
repo_id=RESULTS_REPO,
repo_type="dataset",
local_dir=hf_eval_results_repo_dir,
etag_timeout=30
)
API.snapshot_download(
repo_id=SEARCH_RESULTS_REPO,
repo_type="dataset",
local_dir=hf_search_results_repo_dir,
etag_timeout=30,
allow_patterns=['*.json']
)
except Exception as e:
logger.error(f"Failed to download the search results or evaluation results: {e}")
logger.error(f"Wait for {time_duration} seconds for the next update ...")
time.sleep(time_duration)
continue
commit_infos_dict = defaultdict(list)
new_file_paths = get_file_list(hf_search_results_repo_dir, allowed_suffixes=['.json'])
added_file_paths = new_file_paths - cur_file_paths
for metadata_file_path in sorted(list(added_file_paths)):
with open(metadata_file_path, 'r', encoding='utf-8') as f:
metadata = json.load(f)
model_name = metadata['model_name']
model_link = None if not metadata['model_url'] else metadata['model_url']
reranker_name = metadata['reranker_name']
reranker_link = None if not metadata['reranker_url'] else metadata['reranker_url']
benchmark_version = metadata['version']
try:
check_benchmark_version(benchmark_version)
except ValueError:
logger.error(f"Invalid benchmark version `{benchmark_version}` in file `{metadata_file_path}`. Skip this commit.")
continue
file_name = os.path.basename(metadata_file_path).split('.')[0]
zip_file_name = f"{file_name}.zip"
try:
API.snapshot_download(
repo_id=SEARCH_RESULTS_REPO,
repo_type="dataset",
local_dir=ZIP_CACHE_DIR,
etag_timeout=30,
allow_patterns=[zip_file_name]
)
zip_file_path = get_zip_file_path(zip_file_name)
assert zip_file_path is not None
except Exception as e:
logger.error(f"Failed to download the zip file `{zip_file_name}`: {e}")
continue
unzip_target_path = os.path.join(unzip_target_dir, benchmark_version, file_name)
os.makedirs(unzip_target_path, exist_ok=True)
try:
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
zip_ref.extractall(unzip_target_path)
except Exception as e:
logger.error(f"Failed to unzip the search results `{file_name}`: {e}")
continue
commit_infos_dict[benchmark_version].append({
"model_name": model_name,
"model_link": model_link,
"reranker_name": reranker_name,
"reranker_link": reranker_link,
"is_anonymous": metadata['is_anonymous'],
"file_name": file_name,
"timestamp": metadata['timestamp'],
"revision": metadata['revision'],
"search_results_dir": unzip_target_path
})
# Sort the search results by timestamp
for benchmark_version in commit_infos_dict:
commit_infos_dict[benchmark_version].sort(key=lambda x: int(os.path.basename(x["search_results_dir"]).split('-')[0]))
# Save the evaluation results
update_flag = False
new_models_set = set()
for benchmark_version, commit_infos in commit_infos_dict.items():
eval_results_dir = os.path.join(hf_eval_results_repo_dir, benchmark_version)
os.makedirs(eval_results_dir, exist_ok=True)
for commit_info in commit_infos:
try:
eval_results = compute_metrics(
benchmark_version,
commit_info['search_results_dir'],
k_values=k_values,
cache_dir=cache_dir,
)
except KeyError as e:
logger.error(f"KeyError: {e}. Skip this commit: {commit_info['file_name']}")
continue
save_dir = os.path.join(eval_results_dir, commit_info['model_name'], commit_info['reranker_name'])
os.makedirs(save_dir, exist_ok=True)
results_save_path = os.path.join(save_dir, f"results_{commit_info['file_name']}.json")
save_evaluation_results(eval_results,
results_save_path,
commit_info['model_name'],
commit_info['reranker_name'],
model_link=commit_info['model_link'],
reranker_link=commit_info['reranker_link'],
is_anonymous=commit_info['is_anonymous'],
timestamp=commit_info['timestamp'],
revision=commit_info['revision'])
new_models_set.add(f"{commit_info['model_name']}_{commit_info['reranker_name']}")
update_flag = True
# Commit the updated evaluation results
if update_flag:
commit_message = "Update evaluation results\nNew models added in this update:\n"
for new_model in new_models_set:
commit_message += f"\t- {new_model}\n"
API.upload_folder(
repo_id=RESULTS_REPO,
folder_path=hf_eval_results_repo_dir,
path_in_repo=None,
commit_message=commit_message,
repo_type="dataset"
)
logger.warning("Evaluation results updated and pushed to the remote repository.")
# Print the new models
logger.warning("=====================================")
logger.warning("New models added in this update:")
for new_model in new_models_set:
logger.warning("\t" + new_model)
# Clean the cache
shutil.rmtree(ZIP_CACHE_DIR)
shutil.rmtree(unzip_target_dir)
# Wait for the next update
logger.warning(f"Wait for {time_duration} seconds for the next update ...")
cur_file_paths = new_file_paths
time.sleep(time_duration)
|