Spaces:
Paused
Paused
File size: 3,660 Bytes
5a81b95 | 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #!/usr/bin/env python3
"""Direct HuggingFace upload using Python API"""
from huggingface_hub import HfApi, login
import os
from pathlib import Path
import shutil
# Configuration
HF_TOKEN = os.environ.get("HF_TOKEN")
SPACE_NAME = "Kraft102/widgetdc-cortex"
DEPLOY_DIR = "hf-deploy-python"
print("π Direct HuggingFace Deployment via Python API")
print("=" * 50)
# Step 1: Login
print("\n1οΈβ£ Logging in to HuggingFace...")
login(token=HF_TOKEN)
print("β
Logged in")
# Step 2: Initialize API
print("\n2οΈβ£ Initializing HF API...")
api = HfApi()
print("β
API ready")
# Step 3: Create Space if doesn't exist
print(f"\n3οΈβ£ Creating Space (if needed): {SPACE_NAME}...")
try:
api.create_repo(
repo_id=SPACE_NAME,
repo_type="space",
space_sdk="docker",
exist_ok=True
)
print("β
Space ready")
except Exception as e:
print(f"β οΈ Space creation: {e} (might already exist)")
# Step 4: Prepare files
print(f"\n4οΈβ£ Preparing deployment files...")
if Path(DEPLOY_DIR).exists():
shutil.rmtree(DEPLOY_DIR)
Path(DEPLOY_DIR).mkdir()
# Copy backend
print(" Copying backend...")
shutil.copytree("apps/backend/src", f"{DEPLOY_DIR}/apps/backend/src")
shutil.copytree("apps/backend/prisma", f"{DEPLOY_DIR}/apps/backend/prisma")
shutil.copy("apps/backend/package.json", f"{DEPLOY_DIR}/apps/backend/")
shutil.copy("apps/backend/tsconfig.json", f"{DEPLOY_DIR}/apps/backend/")
# Copy packages
print(" Copying packages...")
shutil.copytree("packages/domain-types/src", f"{DEPLOY_DIR}/packages/domain-types/src")
shutil.copy("packages/domain-types/package.json", f"{DEPLOY_DIR}/packages/domain-types/")
shutil.copy("packages/domain-types/tsconfig.json", f"{DEPLOY_DIR}/packages/domain-types/")
shutil.copytree("packages/mcp-types/src", f"{DEPLOY_DIR}/packages/mcp-types/src")
shutil.copy("packages/mcp-types/package.json", f"{DEPLOY_DIR}/packages/mcp-types/")
shutil.copy("packages/mcp-types/tsconfig.json", f"{DEPLOY_DIR}/packages/mcp-types/")
# Copy root files
print(" Copying root files...")
shutil.copy("package.json", f"{DEPLOY_DIR}/")
if Path("tsconfig.json").exists():
shutil.copy("tsconfig.json", f"{DEPLOY_DIR}/")
# Copy Dockerfile
if Path("scripts/hf-backend.dockerfile").exists():
shutil.copy("scripts/hf-backend.dockerfile", f"{DEPLOY_DIR}/Dockerfile")
# Create README
print(" Creating README...")
readme_content = """---
title: WidgeTDC Cortex
emoji: π§
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860
hardware: t4-small
---
# WidgeTDC Cortex - Neural Backend
Enterprise AI backend with GPU-accelerated embeddings and MCP agents.
## Features
- GPU-accelerated embeddings via sentence-transformers
- MCP (Model Context Protocol) tool execution
- Real-time WebSocket communication
- Health monitoring at /health endpoint
"""
Path(f"{DEPLOY_DIR}/README.md").write_text(readme_content)
print("β
Files prepared")
# Step 5: Upload to Space
print(f"\n5οΈβ£ Uploading to HuggingFace Space: {SPACE_NAME}...")
try:
api.upload_folder(
folder_path=DEPLOY_DIR,
repo_id=SPACE_NAME,
repo_type="space",
commit_message=f"Deploy from Python API - {Path.cwd().name}"
)
print("β
Upload complete!")
except Exception as e:
print(f"β Upload failed: {e}")
raise
# Success
print("\n" + "=" * 50)
print("π DEPLOYMENT SUCCESSFUL!")
print(f"π Space URL: https://huggingface.co/spaces/{SPACE_NAME}")
print(f"π API Endpoint: https://{SPACE_NAME.replace('/', '-')}.hf.space/")
print("\nβ³ Space is now building Docker image (5-10 minutes)")
print("π Don't forget to add environment variables in Space settings!")
|