polyglot-backend-quant / download_code.py
mutisya's picture
Deploy Polyglot backend with quantized models
d7086f8 verified
#!/usr/bin/env python3
"""
Download application code from private HuggingFace Space
"""
import os
import sys
from huggingface_hub import snapshot_download
from pathlib import Path
def download_code(code_space_id, token):
"""
Download app and data from the private code space
Args:
code_space_id: Full space ID (e.g., "mutisya/polyglot-backend-code")
token: HuggingFace token for authentication
"""
print(f"Downloading code from: {code_space_id}")
try:
# Download the entire space to a temporary directory
download_path = snapshot_download(
repo_id=code_space_id,
repo_type="space",
token=token,
local_dir="/tmp/code_download",
local_dir_use_symlinks=False
)
print(f"Code downloaded to: {download_path}")
# Copy app and data to the correct locations
import shutil
# Copy app directory
if Path("/tmp/code_download/app").exists():
# Remove existing app directory if present
if Path("/app/app").exists():
shutil.rmtree("/app/app")
# Copy the entire app directory
shutil.copytree("/tmp/code_download/app", "/app/app")
print("OK app/ directory copied")
else:
print("WARNING: app/ directory not found in code space")
# Copy data directory
if Path("/tmp/code_download/data").exists():
# Remove existing data directory if present
if Path("/app/data").exists():
shutil.rmtree("/app/data")
# Copy the entire data directory
shutil.copytree("/tmp/code_download/data", "/app/data")
print("OK data/ directory copied")
else:
print("WARNING: data/ directory not found in code space")
# Clean up
if Path("/tmp/code_download").exists():
shutil.rmtree("/tmp/code_download")
print("OK Code download complete")
return True
except Exception as e:
print(f"ERROR downloading code: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python download_code.py <code_space_id> [token]")
sys.exit(1)
code_space_id = sys.argv[1]
token = sys.argv[2] if len(sys.argv) > 2 else os.getenv("HUGGING_FACE_HUB_TOKEN")
if not token:
print("ERROR: No HuggingFace token provided")
sys.exit(1)
success = download_code(code_space_id, token)
sys.exit(0 if success else 1)