Spaces:
Running
Running
Add uto generation for model files
Browse files
src/leaderboard/build_leaderboard.py
CHANGED
|
@@ -2,12 +2,13 @@ import json
|
|
| 2 |
import logging
|
| 3 |
import os
|
| 4 |
import time
|
|
|
|
| 5 |
from typing import Any
|
| 6 |
|
| 7 |
import pandas as pd
|
| 8 |
-
from huggingface_hub import snapshot_download
|
| 9 |
|
| 10 |
-
from src.envs import H4_TOKEN, RESULTS_PATH, RESULTS_REPO
|
| 11 |
|
| 12 |
# Configure logging
|
| 13 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
@@ -68,11 +69,140 @@ def download_dataset(
|
|
| 68 |
logging.error("Failed to download %s after %s attempts", repo_id, max_attempts)
|
| 69 |
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def download_results() -> None:
|
| 72 |
"""Download model evaluation results from HuggingFace RESULTS_REPO."""
|
| 73 |
try:
|
| 74 |
download_dataset(RESULTS_REPO, RESULTS_PATH)
|
| 75 |
logging.info("Successfully downloaded model evaluation results")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
logging.error(f"Failed to download model evaluation results: {e}")
|
| 78 |
|
|
|
|
| 2 |
import logging
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
+
from io import BytesIO
|
| 6 |
from typing import Any
|
| 7 |
|
| 8 |
import pandas as pd
|
| 9 |
+
from huggingface_hub import hf_hub_download, snapshot_download
|
| 10 |
|
| 11 |
+
from src.envs import API, H4_TOKEN, RESULTS_PATH, RESULTS_REPO
|
| 12 |
|
| 13 |
# Configure logging
|
| 14 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
|
| 69 |
logging.error("Failed to download %s after %s attempts", repo_id, max_attempts)
|
| 70 |
|
| 71 |
|
| 72 |
+
def create_safe_filename(model_name: str) -> str:
|
| 73 |
+
"""
|
| 74 |
+
Create safe filename from model name.
|
| 75 |
+
|
| 76 |
+
Args:
|
| 77 |
+
model_name: Full model name (e.g., "username/model-name")
|
| 78 |
+
|
| 79 |
+
Returns:
|
| 80 |
+
Safe filename (e.g., "username_model-name.json")
|
| 81 |
+
"""
|
| 82 |
+
# Extract username and model parts
|
| 83 |
+
parts = model_name.split("/")
|
| 84 |
+
if len(parts) >= 2:
|
| 85 |
+
username = parts[0]
|
| 86 |
+
modelname = "_".join(parts[1:])
|
| 87 |
+
else:
|
| 88 |
+
username = "unknown"
|
| 89 |
+
modelname = model_name
|
| 90 |
+
|
| 91 |
+
# Replace invalid characters
|
| 92 |
+
safe_name = f"{username}_{modelname}".replace("/", "_").replace(" ", "_")
|
| 93 |
+
return f"{safe_name}.json"
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def generate_individual_files_from_leaderboard() -> None:
|
| 97 |
+
"""
|
| 98 |
+
Generate individual model files from leaderboard.json backup.
|
| 99 |
+
Only creates missing files, doesn't overwrite existing ones.
|
| 100 |
+
Uploads new files to RESULTS_REPO.
|
| 101 |
+
"""
|
| 102 |
+
try:
|
| 103 |
+
# Download leaderboard.json from RESULTS_REPO
|
| 104 |
+
logging.info("Checking for leaderboard.json in RESULTS_REPO")
|
| 105 |
+
leaderboard_path = hf_hub_download(
|
| 106 |
+
repo_id=RESULTS_REPO,
|
| 107 |
+
filename="leaderboard.json",
|
| 108 |
+
repo_type="dataset",
|
| 109 |
+
token=H4_TOKEN,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
with open(leaderboard_path, encoding="utf-8") as f:
|
| 113 |
+
leaderboard_data = json.load(f)
|
| 114 |
+
|
| 115 |
+
if not leaderboard_data:
|
| 116 |
+
logging.info("leaderboard.json is empty, skipping generation")
|
| 117 |
+
return
|
| 118 |
+
|
| 119 |
+
logging.info(f"Found leaderboard.json with {len(leaderboard_data)} models")
|
| 120 |
+
|
| 121 |
+
# Check existing files
|
| 122 |
+
external_dir = "./m_data/model_data/external/"
|
| 123 |
+
os.makedirs(external_dir, exist_ok=True)
|
| 124 |
+
existing_files = set(os.listdir(external_dir))
|
| 125 |
+
logging.info(f"Existing files in external/: {len(existing_files)}")
|
| 126 |
+
|
| 127 |
+
# Process each model in leaderboard
|
| 128 |
+
created_count = 0
|
| 129 |
+
skipped_count = 0
|
| 130 |
+
error_count = 0
|
| 131 |
+
|
| 132 |
+
for entry in leaderboard_data:
|
| 133 |
+
try:
|
| 134 |
+
# Get model_name
|
| 135 |
+
model_name = entry.get("model_name") or entry.get("model")
|
| 136 |
+
if not model_name:
|
| 137 |
+
logging.warning(f"Skipping entry without model_name: {entry}")
|
| 138 |
+
error_count += 1
|
| 139 |
+
continue
|
| 140 |
+
|
| 141 |
+
# Create safe filename
|
| 142 |
+
safe_filename = create_safe_filename(model_name)
|
| 143 |
+
|
| 144 |
+
# Skip if file already exists
|
| 145 |
+
if safe_filename in existing_files:
|
| 146 |
+
skipped_count += 1
|
| 147 |
+
continue
|
| 148 |
+
|
| 149 |
+
# Prepare model data
|
| 150 |
+
model_data = {
|
| 151 |
+
"model_name": model_name,
|
| 152 |
+
"score": float(entry.get("score", 0.0)),
|
| 153 |
+
"math_score": float(entry.get("math_score", 0.0)),
|
| 154 |
+
"physics_score": float(entry.get("physics_score", 0.0)),
|
| 155 |
+
"total_tokens": int(entry.get("total_tokens", 0)),
|
| 156 |
+
"evaluation_time": float(entry.get("evaluation_time", 0.0)),
|
| 157 |
+
"system_prompt": entry.get(
|
| 158 |
+
"system_prompt",
|
| 159 |
+
"Вы - полезный помощник по математике и физике. Ответьте на русском языке.",
|
| 160 |
+
),
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
# Save locally
|
| 164 |
+
local_path = os.path.join(external_dir, safe_filename)
|
| 165 |
+
with open(local_path, "w", encoding="utf-8") as f:
|
| 166 |
+
json.dump(model_data, f, ensure_ascii=False, indent=2)
|
| 167 |
+
|
| 168 |
+
# Upload to RESULTS_REPO
|
| 169 |
+
buf = BytesIO()
|
| 170 |
+
buf.write(json.dumps(model_data, ensure_ascii=False).encode("utf-8"))
|
| 171 |
+
|
| 172 |
+
API.upload_file(
|
| 173 |
+
path_or_fileobj=buf.getvalue(),
|
| 174 |
+
path_in_repo=f"model_data/external/{safe_filename}",
|
| 175 |
+
repo_id=RESULTS_REPO,
|
| 176 |
+
repo_type="dataset",
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
logging.info(f"Created: {safe_filename}")
|
| 180 |
+
created_count += 1
|
| 181 |
+
|
| 182 |
+
except Exception as e:
|
| 183 |
+
logging.error(f"Failed to process entry {entry.get('model_name', 'unknown')}: {e}")
|
| 184 |
+
error_count += 1
|
| 185 |
+
continue
|
| 186 |
+
|
| 187 |
+
logging.info(
|
| 188 |
+
f"Generation complete: {created_count} files created, {skipped_count} skipped, {error_count} errors"
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
except FileNotFoundError:
|
| 192 |
+
logging.warning("leaderboard.json not found in RESULTS_REPO, skipping generation")
|
| 193 |
+
except Exception as e:
|
| 194 |
+
logging.error(f"Failed to generate files from leaderboard.json: {e}")
|
| 195 |
+
|
| 196 |
+
|
| 197 |
def download_results() -> None:
|
| 198 |
"""Download model evaluation results from HuggingFace RESULTS_REPO."""
|
| 199 |
try:
|
| 200 |
download_dataset(RESULTS_REPO, RESULTS_PATH)
|
| 201 |
logging.info("Successfully downloaded model evaluation results")
|
| 202 |
+
|
| 203 |
+
# Generate individual files from leaderboard.json if needed
|
| 204 |
+
generate_individual_files_from_leaderboard()
|
| 205 |
+
|
| 206 |
except Exception as e:
|
| 207 |
logging.error(f"Failed to download model evaluation results: {e}")
|
| 208 |
|