|
| 1 | +# Copyright 2026 The HuggingFace Team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import os |
| 15 | +import sys |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +from huggingface_hub import HfApi, constants, scan_cache_dir, snapshot_download |
| 19 | + |
| 20 | + |
| 21 | +sys.path.insert(0, str(Path(__file__).parents[1] / "openvino")) |
| 22 | + |
| 23 | +from utils_tests import MODEL_NAMES |
| 24 | + |
| 25 | + |
| 26 | +# github actions repo cache limit is 10 GB so using 9 GB |
| 27 | +CACHE_LIMIT = 9 * 10**9 |
| 28 | +# exclude model larger than 100 MB |
| 29 | +MODEL_SIZE_THRESHOLD = 100 * 10**6 |
| 30 | + |
| 31 | + |
| 32 | +def hub_size(api: HfApi, repo_id: str) -> int: |
| 33 | + try: |
| 34 | + info = api.model_info(repo_id, files_metadata=True) |
| 35 | + return sum(sib.size or 0 for sib in (info.siblings or [])) |
| 36 | + except Exception as e: |
| 37 | + print(f"Size query failed for {repo_id}: {e}") |
| 38 | + return -1 |
| 39 | + |
| 40 | + |
| 41 | +def main() -> None: |
| 42 | + api = HfApi() |
| 43 | + candidates = {} |
| 44 | + |
| 45 | + for name, repo_id in MODEL_NAMES.items(): |
| 46 | + if not isinstance(repo_id, str) or not repo_id or repo_id.startswith("/"): |
| 47 | + continue |
| 48 | + candidates[repo_id] = name |
| 49 | + |
| 50 | + if os.path.exists(constants.HF_HUB_CACHE): |
| 51 | + cache_info = scan_cache_dir() |
| 52 | + cached_repos = {repo.repo_id for repo in cache_info.repos} |
| 53 | + sum_repo_size = cache_info.size_on_disk |
| 54 | + else: |
| 55 | + cached_repos = set() |
| 56 | + sum_repo_size = 0 |
| 57 | + |
| 58 | + downloaded = [] |
| 59 | + skipped_too_large = [] |
| 60 | + skipped_cache_full = [] |
| 61 | + failed = [] |
| 62 | + force_update = os.environ.get("FORCE_DOWNLOAD", "").lower() in ("1", "true", "yes") |
| 63 | + for repo_id, name in candidates.items(): |
| 64 | + if not force_update and repo_id in cached_repos: |
| 65 | + continue |
| 66 | + |
| 67 | + size = hub_size(api, repo_id) |
| 68 | + if size < 0: |
| 69 | + failed.append(name) |
| 70 | + continue |
| 71 | + |
| 72 | + if size > MODEL_SIZE_THRESHOLD: |
| 73 | + skipped_too_large.append(name) |
| 74 | + continue |
| 75 | + |
| 76 | + if sum_repo_size + size > CACHE_LIMIT: |
| 77 | + skipped_cache_full.append(name) |
| 78 | + continue |
| 79 | + |
| 80 | + try: |
| 81 | + snapshot_download(repo_id) |
| 82 | + sum_repo_size += size |
| 83 | + downloaded.append(name) |
| 84 | + except Exception as e: |
| 85 | + print(f"[FAIL] {name}: {e}", file=sys.stderr) |
| 86 | + failed.append(repo_id) |
| 87 | + |
| 88 | + print(f"Downloaded : {len(downloaded)} ({', '.join(downloaded)})") |
| 89 | + print(f"Skipped (too large) : {len(skipped_too_large)} ({', '.join(skipped_too_large)})") |
| 90 | + print(f"Skipped (cache full): {len(skipped_cache_full)} ({', '.join(skipped_cache_full)})") |
| 91 | + print(f"Loading failed : {len(failed)} ({', '.join(failed)})") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments