wuhp commited on
Commit
5716b08
·
verified ·
1 Parent(s): 6146397

Create hfmaker.py

Browse files
Files changed (1) hide show
  1. hfmaker.py +228 -0
hfmaker.py ADDED
@@ -0,0 +1,228 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ from huggingface_hub import (
5
+ create_repo,
6
+ list_models,
7
+ upload_file,
8
+ constants,
9
+ )
10
+ from huggingface_hub.utils import build_hf_headers, get_session, hf_raise_for_status
11
+
12
+ # — USER INFO & MODEL LISTING —
13
+
14
+ def show_profile(profile: gr.OAuthProfile | None) -> str:
15
+ if profile is None:
16
+ return "*Not logged in.*"
17
+ return f"✅ Logged in as **{profile.username}**"
18
+
19
+ def list_private_models(
20
+ profile: gr.OAuthProfile | None,
21
+ oauth_token: gr.OAuthToken | None
22
+ ) -> str:
23
+ if profile is None or oauth_token is None:
24
+ return "Please log in to see your models."
25
+ models = [
26
+ f"{m.id} ({'private' if m.private else 'public'})"
27
+ for m in list_models(author=profile.username, token=oauth_token.token)
28
+ ]
29
+ return "No models found." if not models else "Models:\n\n" + "\n - ".join(models)
30
+
31
+ # — BUTTON‑ENABLING HELPERS —
32
+
33
+ def enable_create(
34
+ profile: gr.OAuthProfile | None,
35
+ oauth_token: gr.OAuthToken | None
36
+ ):
37
+ return gr.update(interactive=profile is not None)
38
+
39
+ def enable_repo_actions(
40
+ repo_id: str,
41
+ profile: gr.OAuthProfile | None,
42
+ oauth_token: gr.OAuthToken | None
43
+ ):
44
+ return gr.update(interactive=bool(repo_id and profile and oauth_token))
45
+
46
+ # — CORE ACTIONS —
47
+
48
+ def create_space(
49
+ repo_name: str,
50
+ sdk: str,
51
+ profile: gr.OAuthProfile | None,
52
+ oauth_token: gr.OAuthToken | None
53
+ ) -> tuple[str, str, str]:
54
+ if not profile or not oauth_token:
55
+ return "", "⚠️ Please log in first.", "<p>No Space created yet.</p>"
56
+ repo_id = f"{profile.username}/{repo_name}"
57
+ create_repo(
58
+ repo_id=repo_id,
59
+ token=oauth_token.token,
60
+ exist_ok=True,
61
+ repo_type="space",
62
+ space_sdk=sdk
63
+ )
64
+ url = f"https://huggingface.co/spaces/{repo_id}"
65
+ logmsg = f"✅ Space ready: {url} (SDK: {sdk})"
66
+ iframe = f'<iframe src="{url}" width="100%" height="500px"></iframe>'
67
+ return repo_id, logmsg, iframe
68
+
69
+ def upload_file_to_space(
70
+ file,
71
+ path_in_repo: str,
72
+ repo_id: str,
73
+ profile: gr.OAuthProfile | None,
74
+ oauth_token: gr.OAuthToken | None
75
+ ) -> str:
76
+ if not profile or not oauth_token:
77
+ return "⚠️ Please log in first."
78
+ if not repo_id:
79
+ return "⚠️ Please create a Space first."
80
+ if not file:
81
+ return "⚠️ No file selected."
82
+ upload_file(
83
+ path_or_fileobj=file.name,
84
+ path_in_repo=path_in_repo,
85
+ repo_id=repo_id,
86
+ token=oauth_token.token,
87
+ repo_type="space"
88
+ )
89
+ return f"✅ Uploaded `{path_in_repo}` to `{repo_id}`"
90
+
91
+ def _fetch_space_logs_level(repo_id: str, level: str) -> str:
92
+ # 1) Get SSE JWT
93
+ jwt_url = f"{constants.ENDPOINT}/api/spaces/{repo_id}/jwt"
94
+ r = get_session().get(jwt_url, headers=build_hf_headers())
95
+ hf_raise_for_status(r)
96
+ jwt = r.json()["token"]
97
+ # 2) Stream logs
98
+ logs_url = f"https://api.hf.space/v1/{repo_id}/logs/{level}"
99
+ lines = []
100
+ with get_session().get(logs_url, headers=build_hf_headers(token=jwt), stream=True) as resp:
101
+ hf_raise_for_status(resp)
102
+ for raw in resp.iter_lines():
103
+ if not raw.startswith(b"data: "):
104
+ continue
105
+ payload = raw[len(b"data: "):]
106
+ try:
107
+ event = json.loads(payload.decode())
108
+ except json.JSONDecodeError:
109
+ continue
110
+ ts = event.get("timestamp", "")
111
+ txt = event.get("data", "")
112
+ lines.append(f"[{ts}] {txt}")
113
+ return "\n".join(lines)
114
+
115
+ def get_build_logs(
116
+ repo_id: str,
117
+ profile: gr.OAuthProfile | None,
118
+ oauth_token: gr.OAuthToken | None
119
+ ) -> str:
120
+ if not (profile and oauth_token and repo_id):
121
+ return "⚠️ Please log in and create a Space first."
122
+ return _fetch_space_logs_level(repo_id, "build")
123
+
124
+ def get_container_logs(
125
+ repo_id: str,
126
+ profile: gr.OAuthProfile | None,
127
+ oauth_token: gr.OAuthToken | None
128
+ ) -> str:
129
+ if not (profile and oauth_token and repo_id):
130
+ return "⚠️ Please log in and create a Space first."
131
+ return _fetch_space_logs_level(repo_id, "run")
132
+
133
+ # — BUILD THE UI —
134
+
135
+ with gr.Blocks(title="HF OAuth + Space Manager with Logs") as demo:
136
+ gr.Markdown(
137
+ "## Sign in with Hugging Face + Manage Your Space\n\n"
138
+ "1. Sign in\n"
139
+ "2. Create a Space (Gradio/Streamlit)\n"
140
+ "3. Upload files to it\n"
141
+ "4. Fetch build and container logs\n\n"
142
+ "---"
143
+ )
144
+
145
+ # — LOGIN & MODEL LIST —
146
+ login_btn = gr.LoginButton(variant="huggingface", size="lg")
147
+ status_md = gr.Markdown("*Not logged in.*")
148
+ models_md = gr.Markdown()
149
+ demo.load(show_profile, inputs=None, outputs=status_md)
150
+ login_btn.click(show_profile, inputs=None, outputs=status_md)
151
+ demo.load(list_private_models, inputs=None, outputs=models_md)
152
+ login_btn.click(list_private_models,
153
+ inputs=None, outputs=models_md)
154
+
155
+ # — CREATE SPACE —
156
+ repo_name = gr.Textbox(label="New Space name", placeholder="my-space")
157
+ sdk_selector = gr.Radio(
158
+ choices=["gradio","streamlit"],
159
+ value="gradio",
160
+ label="Space template (SDK)"
161
+ )
162
+ create_btn = gr.Button("Create Space", interactive=False)
163
+ session_id = gr.Textbox(visible=False)
164
+ create_logs = gr.Textbox(label="Create Logs", interactive=False, lines=3)
165
+ preview = gr.HTML("<p>No Space created yet.</p>")
166
+
167
+ demo.load(enable_create, inputs=None, outputs=[create_btn])
168
+ login_btn.click(enable_create, inputs=None, outputs=[create_btn])
169
+
170
+ create_btn.click(
171
+ fn=create_space,
172
+ inputs=[repo_name, sdk_selector],
173
+ outputs=[session_id, create_logs, preview]
174
+ )
175
+
176
+ # — UPLOAD FILES —
177
+ path_in_repo = gr.Textbox(label="Path in Space", value="app.py")
178
+ file_uploader = gr.File(label="Select file")
179
+ upload_btn = gr.Button("Upload File", interactive=False)
180
+ upload_logs = gr.Textbox(label="Upload Logs", interactive=False, lines=2)
181
+
182
+ demo.load(enable_repo_actions,
183
+ inputs=[session_id],
184
+ outputs=[upload_btn])
185
+ login_btn.click(enable_repo_actions,
186
+ inputs=[session_id],
187
+ outputs=[upload_btn])
188
+ session_id.change(enable_repo_actions,
189
+ inputs=[session_id],
190
+ outputs=[upload_btn])
191
+
192
+ upload_btn.click(
193
+ fn=upload_file_to_space,
194
+ inputs=[file_uploader, path_in_repo, session_id],
195
+ outputs=[upload_logs]
196
+ )
197
+
198
+ # — FETCH BUILD & CONTAINER LOGS —
199
+ build_logs_btn = gr.Button("Get Build Logs", interactive=False)
200
+ container_logs_btn = gr.Button("Get Container Logs", interactive=False)
201
+ build_logs_md = gr.Textbox(label="Build Logs", interactive=False, lines=10)
202
+ container_logs_md = gr.Textbox(label="Container Logs", interactive=False, lines=10)
203
+
204
+ # enable both log buttons
205
+ for btn in (build_logs_btn, container_logs_btn):
206
+ demo.load(enable_repo_actions,
207
+ inputs=[session_id],
208
+ outputs=[btn])
209
+ login_btn.click(enable_repo_actions,
210
+ inputs=[session_id],
211
+ outputs=[btn])
212
+ session_id.change(enable_repo_actions,
213
+ inputs=[session_id],
214
+ outputs=[btn])
215
+
216
+ build_logs_btn.click(
217
+ fn=get_build_logs,
218
+ inputs=[session_id],
219
+ outputs=[build_logs_md]
220
+ )
221
+ container_logs_btn.click(
222
+ fn=get_container_logs,
223
+ inputs=[session_id],
224
+ outputs=[container_logs_md]
225
+ )
226
+
227
+ if __name__ == "__main__":
228
+ demo.launch()