File size: 3,943 Bytes
8edefd6 | 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 | """
Pre-download all HuggingFace model repos used by:
- MedEvalKit/eval_scripts/grounding/diff_region/*.sh (grounding diff-region analysis)
Downloads go to a custom HuggingFace cache directory
(/home/jinhua/ExplainableVQA/checkpoint/models) instead of the default
~/.cache/huggingface. As long as HF_HOME points to this same directory at
load time, `from_pretrained(repo_id, ...)` will transparently hit this cache
instead of re-downloading - it behaves exactly like the normal HF cache,
just relocated.
Each repo is retried several times with resume (snapshot_download skips files
already fully downloaded and resumes partial ones), since mirrors like
hf-mirror.com often time out near the end of large files.
Usage:
python predownload_models_to_checkpoint.py
"""
import os
import time
# Custom cache location - from_pretrained(repo_id) will read from here as
# long as HF_HOME is set to the same path when the model is actually loaded.
#os.environ.setdefault("HF_HOME", "/home/jinhua/ExplainableVQA/checkpoint/models")
# Use the hf-mirror.com mirror (much faster from China than huggingface.co directly).
#os.environ.setdefault("HF_ENDPOINT", "https://hf-mirror.com")
# Raise the default 10s timeout - hf-mirror.com often stalls on the final
# etag/HEAD check for large files.
os.environ.setdefault("HF_HUB_DOWNLOAD_TIMEOUT", "60")
# Disable the Xet backend: it generates a fresh random temp file on every
# resume instead of continuing the previous .incomplete file, so interrupted
# downloads restart from 0 instead of resuming.
os.environ.setdefault("HF_HUB_DISABLE_XET", "1")
from huggingface_hub import snapshot_download
REPO_IDS = [
# ---- MedEvalKit/eval_scripts/grounding/diff_region/*.sh ----
# "Qwen/Qwen2.5-VL-3B-Instruct", # Qwen2.5VL-3B
# "Qwen/Qwen2.5-VL-7B-Instruct", # Qwen2.5VL-7B
# "Qwen/Qwen2.5-VL-32B-Instruct", # Qwen2.5VL-32B
# "llava-hf/llava-1.5-7b-hf", # LLaVA-7B
# "llava-hf/llava-1.5-13b-hf", # LLaVA-13B
# "microsoft/llava-med-v1.5-mistral-7b", # LLaVA-Med-7B
# "FreedomIntelligence/HuatuoGPT-Vision-7B", # HuatuoGPT-V-7B
# "FreedomIntelligence/HuatuoGPT-Vision-34B", # HuatuoGPT-V-34B
# "google/medgemma-4b-it", # MedGemma-4B-IT
# "google/medgemma-27b-it", # MedGemma-27B-IT (gated repo, multimodal variant)
"Qwen/Qwen3-VL-2B-Instruct", # Qwen3VL-2B
"Qwen/Qwen3-VL-4B-Instruct", # Qwen3VL-4B
"Qwen/Qwen3-VL-8B-Instruct", # Qwen3VL-8B
]
DATASET_REPO_IDS = []
MAX_RETRIES = 10
RETRY_DELAY_SECONDS = 5
def download_with_retries(repo_id, repo_type="model"):
for attempt in range(1, MAX_RETRIES + 1):
try:
snapshot_download(repo_id=repo_id, repo_type=repo_type, resume_download=True)
return True
except Exception as e:
print(f" attempt {attempt}/{MAX_RETRIES} failed: {e}")
if attempt < MAX_RETRIES:
time.sleep(RETRY_DELAY_SECONDS)
return False
def main():
#print(f"HF_HOME = {os.environ['HF_HOME']}")
failed = []
for repo_id in REPO_IDS:
print(f"==> Downloading {repo_id}")
if not download_with_retries(repo_id):
failed.append(repo_id)
print(f" GAVE UP: {repo_id}")
for repo_id in DATASET_REPO_IDS:
print(f"==> Downloading dataset {repo_id}")
if not download_with_retries(repo_id, repo_type="dataset"):
failed.append(repo_id)
print(f" GAVE UP: {repo_id}")
print("\n==== Summary ====")
if failed:
print("Failed repos (re-run this script to resume them):")
for repo_id in failed:
print(f" - {repo_id}")
else:
print("All repos downloaded successfully.")
if __name__ == "__main__":
main()
|