File size: 2,384 Bytes
ac38d57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
PneumoOps β€” Upload Models Directly to Hugging Face Space
=========================================================
Uploads local model files to your Hugging Face Space repo, 
bypassing GitHub since the large model files are ignored in .gitignore.

Usage:
    export HF_SPACE_REPO="your-username/pneumoops-space"
    python scripts/upload_model_to_space.py
"""

import os
from pathlib import Path
from huggingface_hub import HfApi

# ─── Config ───────────────────────────────────────────────────────────────────
ROOT = Path(__file__).resolve().parents[1]
MODEL_DIR = ROOT / "models" / "chestmnist_mobilenetv3"

HF_TOKEN = os.getenv("HF_TOKEN")  # optional if already logged in via CLI
SPACE_REPO = os.getenv("HF_SPACE_REPO", "")

if not SPACE_REPO:
    print("\n⚠️  Please set HF_SPACE_REPO environment variable, e.g.:")
    print('   export HF_SPACE_REPO="your-hf-username/pneumoops-space"')
    raise SystemExit(1)

# ─── Main ─────────────────────────────────────────────────────────────────────

def main():
    api = HfApi(token=HF_TOKEN)

    if not MODEL_DIR.exists():
        print(f"\n❌ Error: Model directory {MODEL_DIR} not found.")
        print("Please ensure you have trained or downloaded the models first.")
        raise SystemExit(1)

    print(f"\nπŸ“¦ Verifying space repository: {SPACE_REPO}")
    # Ensure the space exists (this will create it if it doesn't, but typically you already have one)
    api.create_repo(
        repo_id=SPACE_REPO,
        repo_type="space",
        space_sdk="docker",
        exist_ok=True,
        token=HF_TOKEN,
    )

    print(f"\n⬆️  Uploading models to https://huggingface.co/spaces/{SPACE_REPO}")
    
    api.upload_folder(
        folder_path=str(MODEL_DIR),
        path_in_repo="models/chestmnist_mobilenetv3",
        repo_id=SPACE_REPO,
        repo_type="space",
        commit_message="Upload models to Space",
        token=HF_TOKEN,
    )

    print(f"\nβœ… All models uploaded successfully!")
    print(f"   View your space at: https://huggingface.co/spaces/{SPACE_REPO}")

if __name__ == "__main__":
    main()