File size: 6,326 Bytes
ea9bdf0
6d68931
 
 
 
d0a592f
 
 
 
 
efa5772
 
d0a592f
 
 
6d68931
ea9bdf0
 
 
 
 
 
 
 
 
 
 
d0a592f
ea9bdf0
d0a592f
 
ea9bdf0
 
d0a592f
ea9bdf0
6d68931
 
d0a592f
6d68931
 
d0a592f
 
 
 
 
 
 
 
 
 
 
efa5772
d0a592f
 
 
 
 
 
 
 
 
 
 
 
 
 
6d68931
 
ea9bdf0
d0a592f
6d68931
 
 
d0a592f
 
 
 
 
6d68931
 
 
 
d0a592f
6d68931
 
 
 
d0a592f
 
 
 
efa5772
d0a592f
 
 
 
efa5772
d0a592f
 
efa5772
 
 
 
 
 
 
 
 
d0a592f
efa5772
d0a592f
 
 
 
6d68931
 
ea9bdf0
d0a592f
6d68931
ea9bdf0
 
d0a592f
 
ea9bdf0
 
6d68931
 
 
d0a592f
ea9bdf0
6d68931
d0a592f
 
 
6d68931
 
 
d0a592f
ea9bdf0
6d68931
d0a592f
 
efa5772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
import os

from huggingface_hub import HfApi, hf_hub_download

from src.envs import (
    ON_SPACE,
    RESULTS_DATASET_REPO,
    RESULTS_DIR,
    SPACE_REPO,
    SUBMISSIONS_DIR,
    SUBMISSIONS_REPO_PREFIX,
    TOKEN,
    USE_HUB_STORAGE,
)

logger = logging.getLogger(__name__)


def _api() -> HfApi:
    return HfApi(token=TOKEN)


def log_storage_status() -> None:
    if not ON_SPACE:
        return
    if USE_HUB_STORAGE:
        print(f"Results dataset storage enabled for {RESULTS_DATASET_REPO}")
        return
    if not RESULTS_DATASET_REPO:
        print("Results storage disabled: could not resolve results dataset repo id")
        return
    print(
        f"Results storage disabled: set HF_TOKEN secret with write access to {RESULTS_DATASET_REPO}"
    )


def _ensure_results_dataset() -> None:
    if not USE_HUB_STORAGE:
        return
    try:
        _api().create_repo(
            repo_id=RESULTS_DATASET_REPO,
            repo_type="dataset",
            exist_ok=True,
            token=TOKEN,
        )
    except Exception as exc:
        logger.warning("Could not ensure results dataset exists: %s", exc)


def _download_dataset_file(path_in_repo: str) -> None:
    hf_hub_download(
        repo_id=RESULTS_DATASET_REPO,
        repo_type="dataset",
        filename=path_in_repo,
        local_dir=RESULTS_DIR,
        local_dir_use_symlinks=False,
        token=TOKEN,
    )


def _sync_legacy_space_results() -> None:
    """One-time compatibility: pull results/*.json committed to the Space repo."""
    if not SPACE_REPO or not TOKEN:
        return

    try:
        repo_files = _api().list_repo_files(repo_id=SPACE_REPO, repo_type="space")
        legacy_files = [
            path for path in repo_files
            if path.startswith("results/") and path.endswith(".json")
        ]
        for path_in_repo in legacy_files:
            filename = os.path.basename(path_in_repo)
            local_path = os.path.join(RESULTS_DIR, filename)
            if os.path.isfile(local_path):
                continue
            hf_hub_download(
                repo_id=SPACE_REPO,
                repo_type="space",
                filename=path_in_repo,
                local_dir=os.path.dirname(RESULTS_DIR),
                local_dir_use_symlinks=False,
                token=TOKEN,
            )
    except Exception as exc:
        logger.warning("Could not sync legacy Space results: %s", exc)


def sync_results_from_hub() -> None:
    """Pull submission JSON and ZIP files from the results dataset (and legacy Space repo)."""
    if not USE_HUB_STORAGE:
        return

    os.makedirs(RESULTS_DIR, exist_ok=True)
    os.makedirs(SUBMISSIONS_DIR, exist_ok=True)
    try:
        repo_files = _api().list_repo_files(repo_id=RESULTS_DATASET_REPO, repo_type="dataset")
        result_files = [
            path
            for path in repo_files
            if path.endswith(".json")
            or (
                path.startswith(f"{SUBMISSIONS_REPO_PREFIX}/")
                and path.endswith(".zip")
            )
        ]
        for path_in_repo in result_files:
            _download_dataset_file(path_in_repo)
    except Exception as exc:
        logger.exception("Could not sync results from dataset: %s", exc)

    _sync_legacy_space_results()


def upload_result_to_hub(local_path: str, model: str) -> str | None:
    """Persist a results JSON file to the results dataset (no Space restart)."""
    if not USE_HUB_STORAGE:
        if ON_SPACE:
            return (
                "Results were saved locally but not uploaded. "
                f"Add an HF_TOKEN secret with write access to {RESULTS_DATASET_REPO}."
            )
        return None

    filename = os.path.basename(local_path)
    try:
        _ensure_results_dataset()
        _api().upload_file(
            path_or_fileobj=local_path,
            path_in_repo=filename,
            repo_id=RESULTS_DATASET_REPO,
            repo_type="dataset",
            commit_message=f"Update leaderboard results for {model}",
            token=TOKEN,
        )
        logger.info("Uploaded %s to dataset %s", filename, RESULTS_DATASET_REPO)
        return None
    except Exception as exc:
        logger.exception("Could not upload %s to results dataset", filename)
        return f"Could not upload results to the dataset repo: {exc}"


def upload_submission_zip_to_hub(local_path: str, model: str) -> str | None:
    """Persist the full submission ZIP to the results dataset."""
    if not USE_HUB_STORAGE:
        if ON_SPACE:
            return (
                "Submission archive was saved locally but not uploaded. "
                f"Add an HF_TOKEN secret with write access to {RESULTS_DATASET_REPO}."
            )
        return None

    filename = os.path.basename(local_path)
    path_in_repo = f"{SUBMISSIONS_REPO_PREFIX}/{filename}"
    try:
        _ensure_results_dataset()
        _api().upload_file(
            path_or_fileobj=local_path,
            path_in_repo=path_in_repo,
            repo_id=RESULTS_DATASET_REPO,
            repo_type="dataset",
            commit_message=f"Update submission archive for {model}",
            token=TOKEN,
        )
        logger.info("Uploaded %s to dataset %s", path_in_repo, RESULTS_DATASET_REPO)
        return None
    except Exception as exc:
        logger.exception("Could not upload %s to results dataset", path_in_repo)
        return f"Could not upload submission archive to the dataset repo: {exc}"


def ensure_submission_zip_local(model: str) -> str | None:
    """Ensure the submission zip for *model* exists locally; download from Hub if needed."""
    from src.leaderboard.store import (
        model_to_zip_filename,
        submission_zip_path_for_model,
    )

    local_path = submission_zip_path_for_model(model)
    if os.path.isfile(local_path):
        return local_path

    if not USE_HUB_STORAGE:
        return None

    path_in_repo = f"{SUBMISSIONS_REPO_PREFIX}/{model_to_zip_filename(model)}"
    try:
        os.makedirs(SUBMISSIONS_DIR, exist_ok=True)
        _download_dataset_file(path_in_repo)
    except Exception as exc:
        logger.warning("Could not download submission archive %s: %s", path_in_repo, exc)
        return None

    return local_path if os.path.isfile(local_path) else None