Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# — USER INFO & MODEL LISTING —
|
6 |
|
@@ -27,7 +34,6 @@ def enable_create(
|
|
27 |
profile: gr.OAuthProfile | None,
|
28 |
oauth_token: gr.OAuthToken | None
|
29 |
):
|
30 |
-
# Called with no inputs→Gradio auto‑injects profile & token
|
31 |
return gr.update(interactive=profile is not None)
|
32 |
|
33 |
def enable_repo_actions(
|
@@ -35,7 +41,6 @@ def enable_repo_actions(
|
|
35 |
profile: gr.OAuthProfile | None,
|
36 |
oauth_token: gr.OAuthToken | None
|
37 |
):
|
38 |
-
# Called with inputs=[session_id]→Gradio injects profile & token
|
39 |
return gr.update(interactive=bool(repo_id and profile and oauth_token))
|
40 |
|
41 |
# — CORE ACTIONS —
|
@@ -83,30 +88,57 @@ def upload_file_to_space(
|
|
83 |
)
|
84 |
return f"✅ Uploaded `{path_in_repo}` to `{repo_id}`"
|
85 |
|
86 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
repo_id: str,
|
88 |
profile: gr.OAuthProfile | None,
|
89 |
oauth_token: gr.OAuthToken | None
|
90 |
) -> str:
|
91 |
-
if not profile
|
92 |
return "⚠️ Please log in and create a Space first."
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
100 |
|
101 |
# — BUILD THE UI —
|
102 |
|
103 |
-
with gr.Blocks(title="HF OAuth + Space Manager") as demo:
|
104 |
gr.Markdown(
|
105 |
"## Sign in with Hugging Face + Manage Your Space\n\n"
|
106 |
"1. Sign in\n"
|
107 |
"2. Create a Space (Gradio/Streamlit)\n"
|
108 |
"3. Upload files to it\n"
|
109 |
-
"4. Fetch
|
110 |
"---"
|
111 |
)
|
112 |
|
@@ -132,13 +164,12 @@ with gr.Blocks(title="HF OAuth + Space Manager") as demo:
|
|
132 |
create_logs = gr.Textbox(label="Create Logs", interactive=False, lines=3)
|
133 |
preview = gr.HTML("<p>No Space created yet.</p>")
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
login_btn.click(enable_create, inputs=None, outputs=[create_btn])
|
138 |
|
139 |
create_btn.click(
|
140 |
fn=create_space,
|
141 |
-
inputs=[repo_name, sdk_selector],
|
142 |
outputs=[session_id, create_logs, preview]
|
143 |
)
|
144 |
|
@@ -148,16 +179,15 @@ with gr.Blocks(title="HF OAuth + Space Manager") as demo:
|
|
148 |
upload_btn = gr.Button("Upload File", interactive=False)
|
149 |
upload_logs = gr.Textbox(label="Upload Logs", interactive=False, lines=2)
|
150 |
|
151 |
-
# enable upload_btn once space exists & logged in
|
152 |
demo.load(enable_repo_actions,
|
153 |
-
inputs=[session_id],
|
154 |
outputs=[upload_btn])
|
155 |
-
session_id.change(enable_repo_actions,
|
156 |
-
inputs=[session_id],
|
157 |
-
outputs=[upload_btn])
|
158 |
login_btn.click(enable_repo_actions,
|
159 |
inputs=[session_id],
|
160 |
outputs=[upload_btn])
|
|
|
|
|
|
|
161 |
|
162 |
upload_btn.click(
|
163 |
fn=upload_file_to_space,
|
@@ -165,24 +195,33 @@ with gr.Blocks(title="HF OAuth + Space Manager") as demo:
|
|
165 |
outputs=[upload_logs]
|
166 |
)
|
167 |
|
168 |
-
# — FETCH
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
inputs=[session_id],
|
185 |
-
outputs=[
|
186 |
)
|
187 |
|
188 |
if __name__ == "__main__":
|
|
|
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 |
|
|
|
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(
|
|
|
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 —
|
|
|
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 |
|
|
|
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 |
|
|
|
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,
|
|
|
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__":
|