File size: 2,673 Bytes
4faddf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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] + "...")