bbkdevops commited on
Commit
4faddf2
·
verified ·
1 Parent(s): 7c6e14d

Upload spaces_dev_mode_controller.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. spaces_dev_mode_controller.py +83 -0
spaces_dev_mode_controller.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Hugging Face Spaces Dev Mode Automation Substrate
3
+ Supports:
4
+ - Programmatic Dev Mode toggling via Hugging Face REST API
5
+ - SSH Subdomain resolution & connection helper
6
+ - Auto-generation of Dev-Mode-compliant Dockerfiles
7
+ """
8
+
9
+ from huggingface_hub import HfApi, get_token
10
+ import requests
11
+ import json
12
+
13
+ class SpacesDevModeController:
14
+ def __init__(self, token: str = None):
15
+ self.token = token or get_token()
16
+ self.api = HfApi(token=self.token)
17
+
18
+ def _headers(self):
19
+ return {
20
+ "Authorization": f"Bearer {self.token}",
21
+ "Content-Type": "application/json"
22
+ }
23
+
24
+ def set_dev_mode(self, space_id: str, enabled: bool = True) -> dict:
25
+ """Toggle Dev Mode on a Space via the official Hub API."""
26
+ url = f"https://huggingface.co/api/spaces/{space_id}/dev-mode"
27
+ payload = {"enabled": enabled}
28
+ res = requests.post(url, headers=self._headers(), json=payload)
29
+ return {
30
+ "status_code": res.status_code,
31
+ "response": res.text,
32
+ "enabled": enabled
33
+ }
34
+
35
+ def get_ssh_connection_string(self, space_id: str) -> str:
36
+ """Resolve Space subdomain and format SSH terminal command."""
37
+ try:
38
+ info = self.api.space_info(space_id)
39
+ subdomain = getattr(info, "subdomain", None)
40
+ if subdomain:
41
+ return f"ssh {subdomain}@ssh.hf.space"
42
+ return f"hf spaces ssh {space_id}"
43
+ except Exception as e:
44
+ return f"hf spaces ssh {space_id} (fallback due to: {e})"
45
+
46
+ @staticmethod
47
+ def generate_dev_mode_dockerfile() -> str:
48
+ """Generate a pristine Dockerfile complying with all Dev Mode requirements."""
49
+ return """# Dev Mode Compliant Dockerfile for Fiber-MoE Space
50
+ FROM python:3.11-slim
51
+
52
+ # 1. Install mandatory packages for SSH, VS Code Web, and Git persistence
53
+ RUN apt-get update && \\
54
+ apt-get install -y \\
55
+ bash \\
56
+ git git-lfs \\
57
+ wget curl procps \\
58
+ htop vim nano && \\
59
+ rm -rf /var/lib/apt/lists/*
60
+
61
+ WORKDIR /app
62
+
63
+ # 2. Copy dependencies and install
64
+ COPY requirements.txt /app/requirements.txt
65
+ RUN pip install --no-cache-dir -r requirements.txt
66
+
67
+ # 3. Copy application files
68
+ COPY --link ./ /app
69
+
70
+ # 4. Set ownership to UID 1000 and switch user
71
+ RUN chown -R 1000:1000 /app
72
+ USER 1000
73
+
74
+ # 5. Mandatory CMD instruction
75
+ CMD ["python", "app_zerogpu.py"]
76
+ """
77
+
78
+ if __name__ == "__main__":
79
+ controller = SpacesDevModeController()
80
+ print("SpacesDevModeController initialized!")
81
+ sample_dockerfile = controller.generate_dev_mode_dockerfile()
82
+ print("Sample Compliant Dockerfile Preview:")
83
+ print(sample_dockerfile[:250] + "...")