Upload spaces_dev_mode_controller.py with huggingface_hub
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283"""Hugging Face Spaces Dev Mode Automation SubstrateSupports:- 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_tokenimport requestsimport 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 SpaceFROM python:3.11-slim
# 1. Install mandatory packages for SSH, VS Code Web, and Git persistenceRUN 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 installCOPY requirements.txt /app/requirements.txtRUN pip install --no-cache-dir -r requirements.txt
# 3. Copy application filesCOPY --link ./ /app
# 4. Set ownership to UID 1000 and switch userRUN chown -R 1000:1000 /appUSER 1000
# 5. Mandatory CMD instructionCMD ["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] + "...")spaces_dev_mode_controller.pyAdded+83