| |
| """ |
| 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()}") |
| |
| |
| if local_dir.exists() and not force_download: |
| |
| 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_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() |
| |
| |
| 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_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__": |
| |
| download_private_app() |
| print("🎉 Bootstrap test successful!") |
|
|