""" Hugging Face Spaces Dev Mode Automation Substrate Supports: - Programmatic Dev Mode toggling via Hugging Face REST API - SSH Subdomain resolution & connection helper - Auto-generation of Dev-Mode-compliant Dockerfiles """ from huggingface_hub import HfApi, get_token import requests import json class SpacesDevModeController: def __init__(self, token: str = None): self.token = token or get_token() self.api = HfApi(token=self.token) def _headers(self): return { "Authorization": f"Bearer {self.token}", "Content-Type": "application/json" } def set_dev_mode(self, space_id: str, enabled: bool = True) -> dict: """Toggle Dev Mode on a Space via the official Hub API.""" url = f"https://huggingface.co/api/spaces/{space_id}/dev-mode" payload = {"enabled": enabled} res = requests.post(url, headers=self._headers(), json=payload) return { "status_code": res.status_code, "response": res.text, "enabled": enabled } def get_ssh_connection_string(self, space_id: str) -> str: """Resolve Space subdomain and format SSH terminal command.""" try: info = self.api.space_info(space_id) subdomain = getattr(info, "subdomain", None) if subdomain: return f"ssh {subdomain}@ssh.hf.space" return f"hf spaces ssh {space_id}" except Exception as e: return f"hf spaces ssh {space_id} (fallback due to: {e})" @staticmethod def generate_dev_mode_dockerfile() -> str: """Generate a pristine Dockerfile complying with all Dev Mode requirements.""" return """# Dev Mode Compliant Dockerfile for Fiber-MoE Space FROM python:3.11-slim # 1. Install mandatory packages for SSH, VS Code Web, and Git persistence RUN apt-get update && \\ apt-get install -y \\ bash \\ git git-lfs \\ wget curl procps \\ htop vim nano && \\ rm -rf /var/lib/apt/lists/* WORKDIR /app # 2. Copy dependencies and install COPY requirements.txt /app/requirements.txt RUN pip install --no-cache-dir -r requirements.txt # 3. Copy application files COPY --link ./ /app # 4. Set ownership to UID 1000 and switch user RUN chown -R 1000:1000 /app USER 1000 # 5. Mandatory CMD instruction CMD ["python", "app_zerogpu.py"] """ if __name__ == "__main__": controller = SpacesDevModeController() print("SpacesDevModeController initialized!") sample_dockerfile = controller.generate_dev_mode_dockerfile() print("Sample Compliant Dockerfile Preview:") print(sample_dockerfile[:250] + "...")