Upload backend/hue_portal/hue-portal-backendDocker/push_code_to_space.py with huggingface_hub
Browse files
backend/hue_portal/hue-portal-backendDocker/push_code_to_space.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Script to push code directly to Hugging Face Space repository.
|
| 4 |
+
This will upload updated files to trigger a rebuild.
|
| 5 |
+
"""
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from huggingface_hub import HfApi, login
|
| 10 |
+
from huggingface_hub.utils import HfFolder
|
| 11 |
+
|
| 12 |
+
DEFAULT_SPACE_ID = "davidtran999/hue-portal-backend"
|
| 13 |
+
|
| 14 |
+
def get_hf_token() -> str:
|
| 15 |
+
"""Get HF token from environment or cache."""
|
| 16 |
+
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN")
|
| 17 |
+
if token:
|
| 18 |
+
return token
|
| 19 |
+
|
| 20 |
+
# Try to read from cache
|
| 21 |
+
try:
|
| 22 |
+
token = HfFolder.get_token()
|
| 23 |
+
if token:
|
| 24 |
+
return token
|
| 25 |
+
except:
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
def upload_file_to_space(api: HfApi, space_id: str, local_path: Path, repo_path: str) -> bool:
|
| 31 |
+
"""Upload a file to Space repository."""
|
| 32 |
+
try:
|
| 33 |
+
print(f"📤 Uploading {local_path.name} to {repo_path}...")
|
| 34 |
+
api.upload_file(
|
| 35 |
+
path_or_fileobj=str(local_path),
|
| 36 |
+
path_in_repo=repo_path,
|
| 37 |
+
repo_id=space_id,
|
| 38 |
+
repo_type="space",
|
| 39 |
+
)
|
| 40 |
+
print(f"✅ Successfully uploaded {local_path.name}")
|
| 41 |
+
return True
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"❌ Failed to upload {local_path.name}: {e}")
|
| 44 |
+
return False
|
| 45 |
+
|
| 46 |
+
def push_code_to_space(space_id: str = DEFAULT_SPACE_ID) -> bool:
|
| 47 |
+
"""Push updated code files to Space to trigger rebuild."""
|
| 48 |
+
hf_token = get_hf_token()
|
| 49 |
+
if not hf_token:
|
| 50 |
+
print("❌ No Hugging Face token found.")
|
| 51 |
+
print(" Please set HF_TOKEN or HUGGINGFACE_HUB_TOKEN environment variable,")
|
| 52 |
+
print(" or run: huggingface-cli login")
|
| 53 |
+
return False
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
login(token=hf_token)
|
| 57 |
+
api = HfApi(token=hf_token)
|
| 58 |
+
print(f"✅ Authenticated with Hugging Face Hub")
|
| 59 |
+
except Exception as e:
|
| 60 |
+
print(f"❌ Failed to authenticate: {e}")
|
| 61 |
+
return False
|
| 62 |
+
|
| 63 |
+
# Get project root
|
| 64 |
+
script_dir = Path(__file__).parent
|
| 65 |
+
project_root = script_dir.parent
|
| 66 |
+
backend_dir = project_root / "backend"
|
| 67 |
+
hue_portal_backend_dir = script_dir
|
| 68 |
+
|
| 69 |
+
print(f"\n📦 Preparing to push code to Space: {space_id}")
|
| 70 |
+
print(f" Project root: {project_root}")
|
| 71 |
+
print(f" Backend dir: {backend_dir}")
|
| 72 |
+
print(f" Hue portal backend dir: {hue_portal_backend_dir}\n")
|
| 73 |
+
|
| 74 |
+
# Files to upload (relative to Space repo root)
|
| 75 |
+
files_to_upload = [
|
| 76 |
+
# Dockerfile
|
| 77 |
+
(hue_portal_backend_dir / "Dockerfile", "Dockerfile"),
|
| 78 |
+
# README
|
| 79 |
+
(hue_portal_backend_dir / "README.md", "README.md"),
|
| 80 |
+
# Requirements.txt (from backend)
|
| 81 |
+
(backend_dir / "requirements.txt", "requirements.txt"),
|
| 82 |
+
]
|
| 83 |
+
|
| 84 |
+
# Upload backend code (all Python files)
|
| 85 |
+
backend_code_dir = backend_dir / "hue_portal"
|
| 86 |
+
if backend_code_dir.exists():
|
| 87 |
+
print(f"\n📤 Uploading backend code from {backend_code_dir}...")
|
| 88 |
+
|
| 89 |
+
# Upload all Python files recursively
|
| 90 |
+
python_files = list(backend_code_dir.rglob("*.py"))
|
| 91 |
+
print(f" Found {len(python_files)} Python files to upload...")
|
| 92 |
+
|
| 93 |
+
uploaded_count = 0
|
| 94 |
+
for local_file in python_files:
|
| 95 |
+
# Skip __pycache__ and test files if needed
|
| 96 |
+
if "__pycache__" in str(local_file) or ".pyc" in str(local_file):
|
| 97 |
+
continue
|
| 98 |
+
|
| 99 |
+
# Calculate relative path
|
| 100 |
+
rel_path = local_file.relative_to(backend_code_dir)
|
| 101 |
+
repo_path = f"backend/{rel_path}"
|
| 102 |
+
|
| 103 |
+
if upload_file_to_space(api, space_id, local_file, repo_path):
|
| 104 |
+
uploaded_count += 1
|
| 105 |
+
|
| 106 |
+
print(f"✅ Uploaded {uploaded_count}/{len(python_files)} Python files")
|
| 107 |
+
|
| 108 |
+
# Upload main files
|
| 109 |
+
print(f"\n📤 Uploading main files...")
|
| 110 |
+
success_count = 0
|
| 111 |
+
for local_path, repo_path in files_to_upload:
|
| 112 |
+
if local_path.exists():
|
| 113 |
+
if upload_file_to_space(api, space_id, local_path, repo_path):
|
| 114 |
+
success_count += 1
|
| 115 |
+
else:
|
| 116 |
+
print(f"⚠️ File not found: {local_path}")
|
| 117 |
+
|
| 118 |
+
print(f"\n✅ Uploaded {success_count}/{len(files_to_upload)} main files")
|
| 119 |
+
print(f"\n🔄 Space should automatically rebuild with the new code.")
|
| 120 |
+
print(f" Check build status at: https://huggingface.co/spaces/{space_id}")
|
| 121 |
+
|
| 122 |
+
return success_count > 0
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
space_id = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_SPACE_ID
|
| 126 |
+
success = push_code_to_space(space_id)
|
| 127 |
+
sys.exit(0 if success else 1)
|
| 128 |
+
|