Download predownload_models.py from Master-Shi/ExplainableVQA: direct link, hf CLI and curl.
- Browser
- Download file 3.94 kB
-
https://huggingface.co/datasets/Master-Shi/ExplainableVQA/resolve/main/predownload_models.py
- Command line
-
hf download hf://datasets/Master-Shi/ExplainableVQA/predownload_models.py
-
curl -L -o predownload_models.py https://huggingface.co/datasets/Master-Shi/ExplainableVQA/resolve/main/predownload_models.py
3.94 kB
| """ | |
| 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() | |