Spaces:
Running
Running
File size: 11,705 Bytes
5e8f045 4a43fed 5e8f045 4a43fed 5e8f045 4a43fed 5e8f045 4a43fed 5e8f045 4a43fed 5e8f045 4a43fed 5e8f045 4a43fed 5e8f045 4a43fed 5e8f045 4a43fed 5e8f045 cd13f52 4a43fed cd13f52 4a43fed cd13f52 4a43fed cd13f52 4a43fed |
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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
"""
๋ฆฌ๋๋ณด๋ ๊ด๋ฆฌ ๋ชจ๋
๋ฆฌ๋๋ณด๋ ๋ฐ์ดํฐ์ ๋ก๋, ์ ์ฅ, ํ์ ์ค๋น๋ฅผ ๋ด๋นํฉ๋๋ค.
- ๋ก์ปฌ CSV: ํ๋ก์ ํธ ๋ฃจํธ์ data/leaderboard_results.csv
- ์ ํ์ HF ์ฐ๋:
- repo_id: Config.FRESHQA_DATA_REPO_ID
- token : Config.HF_TOKEN
- ํ์ผ๋ช
: leaderboard_results.csv (repo ๋ฃจํธ)
- Config.UPLOAD_LEADERBOARD_TO_HF == True ์ผ ๋๋ง HF๋ฅผ ์ฝ๊ณ /์ด๋ค.
"""
import os
import time
import tempfile
from typing import Optional
import pandas as pd
from huggingface_hub import HfApi, hf_hub_download
from config import Config
from src.utils import file_lock
# -------------------------
# ์์ ๋ฐ ์ค์
# -------------------------
HF_LEADERBOARD_FILENAME = "leaderboard_results.csv" # HF dataset ๋ด ํ์ผ๋ช
(๋ฃจํธ)
LOCAL_LEADERBOARD_FILENAME = "leaderboard_results.csv" # ๋ก์ปฌ data ํด๋ ๋ด ํ์ผ๋ช
(๊ธฐ์กด ์ ์ง)
HF_REPO_ID = Config.FRESHQA_DATA_REPO_ID
HF_ADMIN_TOKEN = Config.HF_TOKEN
UPLOAD_LEADERBOARD_TO_HF = Config.UPLOAD_LEADERBOARD_TO_HF
hf_api = HfApi()
# -------------------------
# ๊ฒฝ๋ก/์ด๊ธฐ ์คํค๋ง/์ ๊ทํ ํฌํผ
# -------------------------
def _get_local_leaderboard_path() -> str:
"""ํ๋ก์ ํธ ๋ฃจํธ ๊ธฐ์ค ๋ก์ปฌ ๋ฆฌ๋๋ณด๋ CSV ๊ฒฝ๋ก ๋ฐํ."""
current_dir = os.path.dirname(os.path.abspath(__file__)) # src/ ํด๋
project_root = os.path.dirname(current_dir) # ํ๋ก์ ํธ ๋ฃจํธ
return os.path.join(project_root, "data", LOCAL_LEADERBOARD_FILENAME)
def _init_empty_leaderboard_df() -> pd.DataFrame:
"""์ด๊ธฐ ๋น ๋ฆฌ๋๋ณด๋ ์คํค๋ง DataFrame."""
return pd.DataFrame({
"id": [],
"model": [],
"description": [],
"accuracy": [],
"fast_changing_accuracy": [],
"slow_changing_accuracy": [],
"never_changing_accuracy": [],
"acc_vp": [],
"acc_fp": [],
"acc_vp_one_hop": [],
"acc_vp_two_hop": [],
"acc_fp_one_hop": [],
"acc_fp_two_hop": [],
"acc_vp_old": [],
"acc_vp_new": [],
"acc_fp_old": [],
"acc_fp_new": [],
"acc_politics": [],
"acc_sports": [],
"acc_entertainment": [],
"acc_weather": [],
"acc_world": [],
"acc_economy": [],
"acc_society": [],
"acc_it_science": [],
"acc_life_culture": [],
"acc_unknown": [],
"total_questions": [],
"evaluation_date": [],
"evaluation_mode": [],
})
def _normalize_leaderboard_df(df: pd.DataFrame) -> pd.DataFrame:
"""
๋ฆฌ๋๋ณด๋ DF๋ฅผ ์คํค๋ง/์ ๋ ฌ/์ปฌ๋ผ ์์ ๊ธฐ์ค์ ๋ง์ถฐ ์ ๊ทํํ๋ค.
(๊ธฐ์กด load_leaderboard_data์ ๋ก์ง์ ํจ์๋ก ๋ถ๋ฆฌ)
"""
if df is None or df.empty:
return _init_empty_leaderboard_df()
df = df.copy()
# evaluation_mode๊ฐ ์์ผ๋ฉด ์ถ๊ฐ
if "evaluation_mode" not in df.columns:
df["evaluation_mode"] = "Unknown"
# ํ
์คํธ ์ปฌ๋ผ ๋ณด์
text_columns = ["model", "description"]
for col in text_columns:
if col not in df.columns:
df[col] = pd.Series(dtype="object")
# ์์ธ ๋ถ์ ์ปฌ๋ผ ์์ผ๋ฉด ์ถ๊ฐ
detailed_columns = [
"acc_test", "acc_dev", "acc_vp", "acc_fp", "acc_vp_one_hop", "acc_vp_two_hop",
"acc_fp_one_hop", "acc_fp_two_hop", "acc_vp_old", "acc_vp_new", "acc_fp_old", "acc_fp_new",
]
for col in detailed_columns:
if col not in df.columns:
df[col] = 0.0
# ๋๋ฉ์ธ๋ณ ์ ํ๋ ์ปฌ๋ผ ์์ผ๋ฉด ์ถ๊ฐ
domain_columns = [
"acc_politics", "acc_sports", "acc_entertainment",
"acc_weather", "acc_world", "acc_economy",
"acc_society", "acc_it_science", "acc_life_culture", "acc_unknown",
]
for col in domain_columns:
if col not in df.columns:
df[col] = 0.0
# accuracy ๊ธฐ์ค ์ ๋ ฌ
if "accuracy" in df.columns and not df.empty:
df = df.sort_values("accuracy", ascending=False).reset_index(drop=True)
# ์ปฌ๋ผ ์์ ์ ๋ ฌ (rank ์ ์ธ)
column_order = [
"id", "model", "description", "accuracy", "fast_changing_accuracy",
"slow_changing_accuracy", "never_changing_accuracy", "acc_vp", "acc_fp",
"acc_vp_one_hop", "acc_vp_two_hop", "acc_fp_one_hop", "acc_fp_two_hop",
"acc_vp_old", "acc_vp_new", "acc_fp_old", "acc_fp_new",
"acc_politics", "acc_sports", "acc_entertainment", "acc_weather",
"acc_world", "acc_economy", "acc_society", "acc_it_science",
"acc_life_culture", "acc_unknown", "total_questions",
"evaluation_date", "evaluation_mode",
]
available_columns = [col for col in column_order if col in df.columns]
df = df[available_columns]
return df
def _load_local_leaderboard_df() -> pd.DataFrame:
"""๋ก์ปฌ CSV์์ ๋ฆฌ๋๋ณด๋ ๋ก๋ (์์ผ๋ฉด ๋น ์คํค๋ง)."""
data_path = _get_local_leaderboard_path()
try:
df = pd.read_csv(data_path)
return _normalize_leaderboard_df(df)
except FileNotFoundError:
return _init_empty_leaderboard_df()
except Exception as e:
print(f"โ ๏ธ ๋ก์ปฌ ๋ฆฌ๋๋ณด๋ ๋ก๋ ์คํจ: {e}")
return _init_empty_leaderboard_df()
# -------------------------
# HF ์ฐ๋ ํฌํผ
# -------------------------
def _can_use_hf() -> bool:
"""HF ์ฐ๋์ด ๊ฐ๋ฅํ ์ํ์ธ์ง ์ฌ๋ถ (Config ๊ธฐ๋ฐ)."""
if not UPLOAD_LEADERBOARD_TO_HF:
return False
if not HF_REPO_ID or not HF_ADMIN_TOKEN:
# ์ค์ ์ด ์์ผ๋ฉด HF๋ ๊ฑด๋๋
return False
return True
def _load_leaderboard_from_hf(retries: int = 3, delay: float = 1.0) -> Optional[pd.DataFrame]:
"""
HF dataset์์ ๋ฆฌ๋๋ณด๋ CSV๋ฅผ ๋ค์ด๋ก๋ํ์ฌ DataFrame์ผ๋ก ๋ฐํ.
์คํจ ์ None ๋ฐํ. ์ฌ์๋ ๋ก์ง ํฌํจ.
"""
if not _can_use_hf():
return None
last_err: Optional[Exception] = None
for attempt in range(1, retries + 1):
try:
with tempfile.TemporaryDirectory() as tmpdir:
file_path = hf_hub_download(
repo_id=HF_REPO_ID,
filename=HF_LEADERBOARD_FILENAME,
repo_type="dataset",
local_dir=tmpdir,
token=HF_ADMIN_TOKEN,
)
df = pd.read_csv(file_path)
return _normalize_leaderboard_df(df)
except Exception as e:
last_err = e
print(f"โ ๏ธ HF ๋ฆฌ๋๋ณด๋ ๋ก๋ ์คํจ (์๋ {attempt}/{retries}): {e}")
if attempt < retries:
time.sleep(delay)
delay *= 2
print("โ HF ๋ฆฌ๋๋ณด๋ ๋ก๋ ์ฌ์๋ ๋ชจ๋ ์คํจ")
return None
def _save_leaderboard_to_hf(df: pd.DataFrame, retries: int = 3, delay: float = 1.0) -> bool:
"""
HF dataset์ ๋ฆฌ๋๋ณด๋ CSV ์
๋ก๋.
์คํจ ์ False ๋ฐํ. ์ฌ์๋ ๋ก์ง ํฌํจ.
"""
if not _can_use_hf():
return False
df = _normalize_leaderboard_df(df)
last_err: Optional[Exception] = None
for attempt in range(1, retries + 1):
try:
with tempfile.NamedTemporaryFile(
mode="w",
encoding="utf-8",
suffix=".csv",
delete=False,
) as tmpfile:
df.to_csv(tmpfile.name, index=False)
tmp_path = tmpfile.name
hf_api.upload_file(
path_or_fileobj=tmp_path,
path_in_repo=HF_LEADERBOARD_FILENAME,
repo_id=HF_REPO_ID,
repo_type="dataset",
token=HF_ADMIN_TOKEN,
commit_message="Update leaderboard results",
)
os.unlink(tmp_path)
return True
except Exception as e:
last_err = e
print(f"โ ๏ธ HF ๋ฆฌ๋๋ณด๋ ์
๋ก๋ ์คํจ (์๋ {attempt}/{retries}): {e}")
if attempt < retries:
time.sleep(delay)
delay *= 2
print(f"โ HF ๋ฆฌ๋๋ณด๋ ์
๋ก๋ ์ฌ์๋ ๋ชจ๋ ์คํจ: {last_err}")
return False
# -------------------------
# ๊ณต๊ฐ API: ๋ก๋ / ์ถ๊ฐ
# -------------------------
def load_leaderboard_data() -> pd.DataFrame:
"""
๋ฆฌ๋๋ณด๋ ๋ฐ์ดํฐ ๋ก๋.
๋์ ์ฐ์ ์์:
1) Config.UPLOAD_LEADERBOARD_TO_HF == True && HF ์ค์ OK:
- HF์์ ์ต์ ๋ฆฌ๋๋ณด๋ ๋ก๋ ์๋
- ์ฑ๊ณต ์: ๊ทธ ๋ด์ฉ์ ๋ก์ปฌ CSV์ ๋ฎ์ด์ด ๋ค ๋ฐํ
- ์คํจ ์: ๋ก์ปฌ CSV๋ฅผ ์ฌ์ฉ (์์ผ๋ฉด ๋น ์คํค๋ง)
2) ๊ทธ ์ธ:
- ๋ก์ปฌ CSV๋ง ์ฌ์ฉ (์์ผ๋ฉด ๋น ์คํค๋ง)
"""
data_path = _get_local_leaderboard_path()
lock_path = data_path + ".lock"
# HF๋ฅผ ์ฌ์ฉํ ์ ์๋ ๊ฒฝ์ฐ์๋ง HF ์ฐ์ ์๋
if _can_use_hf():
with file_lock(lock_path):
hf_df = _load_leaderboard_from_hf()
if hf_df is not None:
# HF๊ฐ ์์ค ์ค๋ธ ํธ๋ฃจ์ค: ๋ก์ปฌ CSV๋ HF ๊ธฐ์ค์ผ๋ก ๋๊ธฐํ
try:
os.makedirs(os.path.dirname(data_path), exist_ok=True)
hf_df.to_csv(data_path, index=False)
except Exception as e:
print(f"โ ๏ธ ๋ก์ปฌ ๋ฆฌ๋๋ณด๋ ๋๊ธฐํ ์คํจ: {e}")
return hf_df
# HF์์ ๋ชป ๊ฐ์ ธ์ค๋ฉด ๋ก์ปฌ๋ก ํด๋ฐฑ
local_df = _load_local_leaderboard_df()
return local_df
# HF๋ฅผ ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ: ๋ก์ปฌ๋ง
return _load_local_leaderboard_df()
def append_to_leaderboard_data(new_data_list):
"""
๋ฆฌ๋๋ณด๋ ๋ฐ์ดํฐ์ ์๋ก์ด ๊ฒฐ๊ณผ ์ถ๊ฐ (ํ์ผ ์ ๊ธ ์ฌ์ฉ).
- ํญ์ ๋ก์ปฌ CSV๋ฅผ ์
๋ฐ์ดํธ
- Config.UPLOAD_LEADERBOARD_TO_HF == True ์ด๊ณ HF ์ค์ ์ด ์ ํจํ๋ฉด,
์
๋ฐ์ดํธ๋ ์ ์ฒด DF๋ฅผ HF์๋ ์
๋ก๋ (์ฌ์๋ ํฌํจ).
"""
data_path = _get_local_leaderboard_path()
lock_path = data_path + ".lock"
with file_lock(lock_path):
# 1) ๋ก์ปฌ ๊ธฐ์กด ๋ฐ์ดํฐ ๋ก๋
if os.path.exists(data_path):
try:
existing_df = pd.read_csv(data_path)
except Exception as e:
print(f"โ ๏ธ ๋ก์ปฌ ๋ฆฌ๋๋ณด๋ ์ฝ๊ธฐ ์คํจ, ๋น ์คํค๋ง๋ก ์งํ: {e}")
existing_df = _init_empty_leaderboard_df()
else:
existing_df = _init_empty_leaderboard_df()
existing_df = _normalize_leaderboard_df(existing_df)
# 2) ์๋ก์ด ๋ฐ์ดํฐ ์ถ๊ฐ
new_df = pd.DataFrame(new_data_list)
if not new_df.empty:
new_df = _normalize_leaderboard_df(new_df)
frames_to_concat = []
if not existing_df.empty:
frames_to_concat.append(existing_df)
if not new_df.empty:
frames_to_concat.append(new_df)
if len(frames_to_concat) == 0:
combined_df = existing_df.copy()
elif len(frames_to_concat) == 1:
combined_df = frames_to_concat[0].copy()
else:
combined_df = pd.concat(frames_to_concat, ignore_index=True)
combined_df = _normalize_leaderboard_df(combined_df)
# 3) ๋ก์ปฌ ์ ์ฅ
try:
os.makedirs(os.path.dirname(data_path), exist_ok=True)
combined_df.to_csv(data_path, index=False)
except Exception as e:
print(f"โ ๋ก์ปฌ ๋ฆฌ๋๋ณด๋ ์ ์ฅ ์คํจ: {e}")
# 4) HF์๋ ์
๋ก๋ (์ต์
)
if _can_use_hf():
ok = _save_leaderboard_to_hf(combined_df)
if not ok:
print("โ ๏ธ ๋ฆฌ๋๋ณด๋ HF ์
๋ก๋ ์คํจ (๋ก์ปฌ์๋ ์ ์ฅ๋จ)")
return combined_df |