|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Model download script for Qwen-Image-Edit with LightX2V. |
| 4 | +
|
| 5 | +Downloads models to network volume for caching across cold starts. |
| 6 | +Supports both RunPod network volumes and local fallback. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | +from huggingface_hub import snapshot_download |
| 13 | + |
| 14 | + |
| 15 | +def get_model_paths(): |
| 16 | + """Determine model paths based on available storage.""" |
| 17 | + # Check for RunPod network volume first |
| 18 | + if Path("/runpod-volume").exists() and os.access("/runpod-volume", os.W_OK): |
| 19 | + base_path = Path("/runpod-volume/models") |
| 20 | + cache_path = Path("/runpod-volume/.cache/huggingface") |
| 21 | + print("Using RunPod network volume for model storage") |
| 22 | + else: |
| 23 | + # Fallback to local storage (container ephemeral storage) |
| 24 | + base_path = Path("/models") |
| 25 | + cache_path = Path("/root/.cache/huggingface") |
| 26 | + print("WARNING: No network volume found, using ephemeral storage") |
| 27 | + print("Models will be re-downloaded on each cold start!") |
| 28 | + |
| 29 | + base_path.mkdir(parents=True, exist_ok=True) |
| 30 | + cache_path.mkdir(parents=True, exist_ok=True) |
| 31 | + |
| 32 | + # Set HF cache location |
| 33 | + os.environ["HF_HOME"] = str(cache_path) |
| 34 | + |
| 35 | + return { |
| 36 | + "base_path": base_path, |
| 37 | + "model_path": base_path / "qwen-edit", |
| 38 | + "fp8_path": base_path / "qwen-edit-fp8", |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +def check_model_exists(path: Path, min_files: int = 5) -> bool: |
| 43 | + """Check if model directory has enough files to be considered complete.""" |
| 44 | + if not path.exists(): |
| 45 | + return False |
| 46 | + files = list(path.glob("*")) |
| 47 | + return len(files) >= min_files |
| 48 | + |
| 49 | + |
| 50 | +def download_base_model(model_path: Path) -> bool: |
| 51 | + """Download Qwen-Image-Edit-2511 base model.""" |
| 52 | + if check_model_exists(model_path, min_files=10): |
| 53 | + print(f"Base model already exists at {model_path}") |
| 54 | + return True |
| 55 | + |
| 56 | + print("Downloading Qwen-Image-Edit-2511 base model (~20GB)...") |
| 57 | + print("This may take 5-10 minutes on first run.") |
| 58 | + |
| 59 | + try: |
| 60 | + snapshot_download( |
| 61 | + repo_id="Qwen/Qwen-Image-Edit-2511", |
| 62 | + local_dir=str(model_path), |
| 63 | + ignore_patterns=["*.md", "*.txt", ".gitattributes"], |
| 64 | + ) |
| 65 | + print("Base model downloaded successfully") |
| 66 | + return True |
| 67 | + except Exception as e: |
| 68 | + print(f"ERROR downloading base model: {e}") |
| 69 | + return False |
| 70 | + |
| 71 | + |
| 72 | +def download_fp8_weights(fp8_path: Path) -> bool: |
| 73 | + """Download FP8 quantized Lightning weights.""" |
| 74 | + if check_model_exists(fp8_path, min_files=3): |
| 75 | + print(f"FP8 weights already exist at {fp8_path}") |
| 76 | + return True |
| 77 | + |
| 78 | + print("Downloading FP8 Lightning weights (~10GB)...") |
| 79 | + |
| 80 | + try: |
| 81 | + snapshot_download( |
| 82 | + repo_id="lightx2v/Qwen-Image-Edit-2511-Lightning", |
| 83 | + local_dir=str(fp8_path), |
| 84 | + ignore_patterns=["*.md", "*.txt", ".gitattributes"], |
| 85 | + ) |
| 86 | + print("FP8 weights downloaded successfully") |
| 87 | + return True |
| 88 | + except Exception as e: |
| 89 | + print(f"ERROR downloading FP8 weights: {e}") |
| 90 | + return False |
| 91 | + |
| 92 | + |
| 93 | +def ensure_models_downloaded() -> dict: |
| 94 | + """ |
| 95 | + Ensure all required models are downloaded. |
| 96 | +
|
| 97 | + Returns dict with model paths if successful, raises exception if not. |
| 98 | + """ |
| 99 | + paths = get_model_paths() |
| 100 | + |
| 101 | + # Download base model |
| 102 | + if not download_base_model(paths["model_path"]): |
| 103 | + raise RuntimeError("Failed to download base model") |
| 104 | + |
| 105 | + # Download FP8 weights |
| 106 | + if not download_fp8_weights(paths["fp8_path"]): |
| 107 | + raise RuntimeError("Failed to download FP8 weights") |
| 108 | + |
| 109 | + print(f"\nAll models ready:") |
| 110 | + print(f" Base model: {paths['model_path']}") |
| 111 | + print(f" FP8 weights: {paths['fp8_path']}") |
| 112 | + |
| 113 | + return paths |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + # Can be run standalone to pre-download models |
| 118 | + try: |
| 119 | + paths = ensure_models_downloaded() |
| 120 | + print("\nModel download complete!") |
| 121 | + sys.exit(0) |
| 122 | + except Exception as e: |
| 123 | + print(f"\nModel download failed: {e}") |
| 124 | + sys.exit(1) |
0 commit comments