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()