File size: 4,410 Bytes
a4bd58d 52279ac a4bd58d 52279ac a4bd58d 52279ac a4bd58d 52279ac a4bd58d 52279ac a4bd58d | 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 | #!/usr/bin/env python3
"""
Bootstrap module - Downloads ver20 from private Space.
mbok_dev / mbok-dev → DLPO/habadashi_dev
mbok (その他) → DLPO/habadashi
"""
import os
from pathlib import Path
from huggingface_hub import snapshot_download
def _resolve_repo_id() -> str:
"""SPACE_ID 環境変数から参照先 private Space の repo_id を決定する。"""
space_id = os.environ.get("SPACE_ID", "").lower()
if "mbok_dev" in space_id or "mbok-dev" in space_id:
return "DLPO/habadashi_dev"
return "DLPO/habadashi"
def get_hf_token():
"""Get HF_TOKEN from environment variable"""
token = os.environ.get("HF_TOKEN")
if not token:
print("[BOOTSTRAP_ERROR] HF_TOKEN not found in environment")
raise ValueError(
"HF_TOKEN not found. Please set HF_TOKEN environment variable "
"in HF Space Secrets to access private repository."
)
print(f"[BOOTSTRAP] HF_TOKEN found (length: {len(token)}, 末尾3文字: ...{token[-3:]})")
return token
def download_private_app(force_download: bool = False):
"""
Download ver20 application from private Space.
参照先は SPACE_ID 環境変数で決定:
mbok_dev / mbok-dev → DLPO/habadashi_dev
mbok (その他) → DLPO/habadashi
Args:
force_download: If True, re-download even if already cached
Returns:
Path: Local directory containing downloaded ver20 files
"""
repo_id = _resolve_repo_id()
repo_type = "space"
local_dir = Path("./private_app")
print(f"[BOOTSTRAP] Starting download")
print(f"[BOOTSTRAP] repo_id={repo_id} repo_type={repo_type}")
print(f"[BOOTSTRAP] local_dir={local_dir} force_download={force_download}")
print(f"[BOOTSTRAP] local_dir.exists()={local_dir.exists()}")
# Check if already downloaded (skip if exists and not forced)
if local_dir.exists() and not force_download:
# Verify essential files exist
app_py = local_dir / "app.py"
print(f"[BOOTSTRAP] Checking cache: app.py exists={app_py.exists()}")
if app_py.exists():
print(f"[BOOTSTRAP] Using cached download: {local_dir}")
# Verify structure
_verify_downloaded_structure(local_dir)
return local_dir
else:
print(f"[BOOTSTRAP] Cache invalid (app.py missing), will re-download")
print(f"[BOOTSTRAP] Starting fresh download from {repo_id}...")
try:
token = get_hf_token()
# Download entire Space repository (removed local_dir_use_symlinks - deprecated)
print(f"[BOOTSTRAP] Calling snapshot_download...")
snapshot_download(
repo_id=repo_id,
repo_type=repo_type,
local_dir=str(local_dir),
token=token,
)
print(f"[BOOTSTRAP] Download complete: {local_dir}")
# Verify essential structure
_verify_downloaded_structure(local_dir)
return local_dir
except Exception as e:
print(f"[BOOTSTRAP_ERROR] Download failed: {e}")
import traceback
print(f"[TRACEBACK]\n{traceback.format_exc()}")
raise
def _verify_downloaded_structure(local_dir: Path):
"""Verify downloaded structure has essential components"""
print(f"[BOOTSTRAP_VERIFY] Checking downloaded structure...")
essential_files = [
"app.py",
"requirements.txt",
]
essential_dirs = [
"lib",
"exec",
"core",
"presentation",
]
for file_name in essential_files:
file_path = local_dir / file_name
exists = file_path.exists()
print(f"[BOOTSTRAP_VERIFY] {file_name}: {'EXISTS' if exists else 'MISSING'}")
if not exists and file_name == "app.py":
raise FileNotFoundError(
f"Critical file missing: {file_name}. "
f"Expected at: {file_path}"
)
for dir_name in essential_dirs:
dir_path = local_dir / dir_name
exists = dir_path.exists() and dir_path.is_dir()
print(f"[BOOTSTRAP_VERIFY] {dir_name}/: {'EXISTS' if exists else 'MISSING'}")
print(f"[BOOTSTRAP_VERIFY] Verification complete")
if __name__ == "__main__":
# Test download
download_private_app()
print("🎉 Bootstrap test successful!")
|