Spaces:
Paused
Paused
File size: 3,772 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 |
import os
import shutil
from huggingface_hub import HfApi
HF_TOKEN = os.environ.get("HF_TOKEN")
REPO_ID = "Kraft102/widgetdc-cortex"
STAGING_DIR = "temp_deploy_backend"
def ignore_patterns(path, names):
# Ignore .env files, node_modules, dist, .git, etc
ignore = set()
for name in names:
if name.startswith(".env"):
ignore.add(name)
if name in ["node_modules", "dist", ".git", ".DS_Store", "__pycache__"]:
ignore.add(name)
return ignore
def prepare_staging():
print(f"π§Ή Clearing staging directory: {STAGING_DIR}")
if os.path.exists(STAGING_DIR):
shutil.rmtree(STAGING_DIR)
os.makedirs(STAGING_DIR)
# 1. Structure: Layout needs to match Monorepo subset
# Root package.json
print("π¦ Copying root config...")
shutil.copy("package.json", os.path.join(STAGING_DIR, "package.json"))
if os.path.exists("tsconfig.json"):
shutil.copy("tsconfig.json", os.path.join(STAGING_DIR, "tsconfig.json"))
# apps/backend
print("π¦ Copying backend app...")
backend_dest = os.path.join(STAGING_DIR, "apps", "backend")
os.makedirs(backend_dest)
# Copy src, package.json, tsconfig.json, prisma
shutil.copytree("apps/backend/src", os.path.join(backend_dest, "src"), ignore=ignore_patterns)
shutil.copy("apps/backend/package.json", os.path.join(backend_dest, "package.json"))
if os.path.exists("apps/backend/tsconfig.json"):
shutil.copy("apps/backend/tsconfig.json", os.path.join(backend_dest, "tsconfig.json"))
if os.path.exists("apps/backend/prisma"):
shutil.copytree("apps/backend/prisma", os.path.join(backend_dest, "prisma"), ignore=ignore_patterns)
# packages/
print("π¦ Copying shared packages...")
pkgs_dest = os.path.join(STAGING_DIR, "packages")
os.makedirs(pkgs_dest)
if os.path.exists("packages/domain-types"):
shutil.copytree("packages/domain-types", os.path.join(pkgs_dest, "domain-types"), ignore=ignore_patterns)
if os.path.exists("packages/mcp-types"):
shutil.copytree("packages/mcp-types", os.path.join(pkgs_dest, "mcp-types"), ignore=ignore_patterns)
# Dockerfile
print("π³ Configuring Dockerfile for Hugging Face...")
if os.path.exists("hf-deploy-python/Dockerfile"):
shutil.copy("hf-deploy-python/Dockerfile", os.path.join(STAGING_DIR, "Dockerfile"))
elif os.path.exists("apps/backend/Dockerfile.hf"):
shutil.copy("apps/backend/Dockerfile.hf", os.path.join(STAGING_DIR, "Dockerfile"))
else:
print("β οΈ WARNING: Could not find optimized Dockerfile, using default if available")
# Readme (Metadata)
print("π Generating README.md with Metadata...")
readme_content = """---
title: WidgeTDC Cortex Backend
emoji: π§
colorFrom: purple
colorTo: blue
sdk: docker
pinned: false
license: mit
app_port: 7860
---
# WidgeTDC Cortex Backend
Enterprise-grade autonomous intelligence platform backend.
## Environment Check
Deployed via Omni-Tool V2.
"""
with open(os.path.join(STAGING_DIR, "README.md"), "w", encoding="utf-8") as f:
f.write(readme_content)
def deploy():
print(f"π Deploying Backend to {REPO_ID}...")
api = HfApi(token=HF_TOKEN)
try:
api.upload_folder(
folder_path=STAGING_DIR,
repo_id=REPO_ID,
repo_type="space",
path_in_repo=".",
token=HF_TOKEN,
commit_message="Deploy Clean Backend (No .env pollution)"
)
print("β
Upload Complete!")
print(f"π Space URL: https://huggingface.co/spaces/{REPO_ID}")
except Exception as e:
print(f"β Upload Failed: {e}")
if __name__ == "__main__":
prepare_staging()
deploy()
|