# HALE API — Hugging Face Spaces Deployment # # This script prepares and deploys the HALE backend to HF Spaces. # Your API will be live at: https://-hale-api.hf.space # # Prerequisites: # pip install huggingface-hub # huggingface-cli login import os import shutil import subprocess import sys import tempfile SPACE_NAME = "hale-api" # Change this to your desired space name HF_USERNAME = None # Will be auto-detected from huggingface-cli def run(cmd, check=True): print(f" → {cmd}") return subprocess.run(cmd, shell=True, check=check, capture_output=True, text=True) def get_hf_username(): result = run("huggingface-cli whoami", check=False) if result.returncode != 0: print("āŒ Not logged in to Hugging Face. Run: huggingface-cli login") sys.exit(1) username = result.stdout.strip().split("\n")[0] print(f" āœ“ Logged in as: {username}") return username def main(): global HF_USERNAME print("\nšŸš€ HALE API → Hugging Face Spaces Deployer\n") # 1. Check HF login print("[1/5] Checking Hugging Face login...") HF_USERNAME = get_hf_username() space_id = f"{HF_USERNAME}/{SPACE_NAME}" # 2. Create Space (if not exists) print(f"\n[2/5] Creating Space: {space_id}...") try: from huggingface_hub import HfApi api = HfApi() api.create_repo( repo_id=space_id, repo_type="space", space_sdk="docker", private=False, exist_ok=True, ) print(f" āœ“ Space ready: https://huggingface.co/spaces/{space_id}") except Exception as e: print(f" ⚠ Space creation: {e}") # 3. Prepare deploy directory print("\n[3/5] Preparing deploy files...") deploy_dir = os.path.join(tempfile.gettempdir(), "hale-deploy") if os.path.exists(deploy_dir): shutil.rmtree(deploy_dir) # Clone the space repo run(f"git clone https://huggingface.co/spaces/{space_id} \"{deploy_dir}\"", check=False) if not os.path.exists(deploy_dir): os.makedirs(deploy_dir) run(f"git init", check=True) # Copy project files project_dir = os.path.dirname(os.path.abspath(__file__)) ignore_patterns = {'.git', '__pycache__', '.pytest_cache', 'hale-frontend', 'node_modules', '.env', 'data', 'logs', '.planning', 'Dockerfile.spaces'} for item in os.listdir(project_dir): if item in ignore_patterns: continue src = os.path.join(project_dir, item) dst = os.path.join(deploy_dir, item) if os.path.isdir(src): shutil.copytree(src, dst, dirs_exist_ok=True, ignore=shutil.ignore_patterns('__pycache__', '*.pyc', '.pytest_cache')) else: shutil.copy2(src, dst) # Copy Dockerfile.spaces as Dockerfile shutil.copy2( os.path.join(project_dir, "Dockerfile.spaces"), os.path.join(deploy_dir, "Dockerfile") ) # Create .env for spaces (no secrets — those go in HF Settings) with open(os.path.join(deploy_dir, ".env"), "w") as f: f.write("APP_ENV=production\n") f.write("DATABASE_URL=sqlite:///./data/hale.db\n") f.write("CORS_ORIGINS=*\n") f.write("LOG_LEVEL=INFO\n") # Create README.md with HF Spaces metadata with open(os.path.join(deploy_dir, "README.md"), "w") as f: f.write(f"""--- title: HALE - Adaptive Learning Engine API emoji: 🧠 colorFrom: purple colorTo: blue sdk: docker app_port: 7860 pinned: true --- # 🧠 HALE — Adaptive Learning Engine AI-powered adaptive learning API with Reinforcement Learning behavioral engine. **API Docs**: [/docs](https://{HF_USERNAME}-{SPACE_NAME}.hf.space/docs) ## Endpoints - `POST /api/auth/register` — Register - `POST /api/auth/login` — Login - `GET /api/plan` — Get today's adaptive plan - `POST /api/complete-task` — Mark task done - `GET /api/stats` — User analytics """) print(f" āœ“ Deploy files ready at: {deploy_dir}") # 4. Set secrets print("\n[4/5] Setting secrets...") print(" ⚠ You need to set GEMINI_API_KEYS and SECRET_KEY in HF Space Settings:") print(f" → https://huggingface.co/spaces/{space_id}/settings") print(" → Add these as 'Repository secrets':") print(" GEMINI_API_KEYS = your,comma,separated,keys") print(" SECRET_KEY = some-random-64-char-string") # 5. Push to HF print(f"\n[5/5] Pushing to HF Spaces...") os.chdir(deploy_dir) run("git add -A") run('git commit -m "Deploy HALE API v3.1"', check=False) run(f"git push https://huggingface.co/spaces/{space_id} main --force", check=False) print(f""" ╔══════════════════════════════════════════════════════════╗ ā•‘ āœ… DEPLOYMENT COMPLETE! ā•‘ ā•‘ ā•‘ ā•‘ 🌐 API URL: https://{HF_USERNAME}-{SPACE_NAME}.hf.space ā•‘ ā•‘ šŸ“š Docs: https://{HF_USERNAME}-{SPACE_NAME}.hf.space/docs ā•‘ ā•‘ āš™ļø Settings: https://huggingface.co/spaces/{space_id}/settings ā•‘ ā•‘ ā•‘ ā•‘ āš ļø Don't forget to add secrets in Settings! ā•‘ ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā• """) if __name__ == "__main__": main()