Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
from huggingface_hub import create_repo, list_models, upload_file
|
4 |
|
|
|
|
|
5 |
def show_profile(profile: gr.OAuthProfile | None) -> str:
|
6 |
if profile is None:
|
7 |
return "*Not logged in.*"
|
@@ -9,7 +11,7 @@ def show_profile(profile: gr.OAuthProfile | None) -> str:
|
|
9 |
|
10 |
def list_private_models(
|
11 |
profile: gr.OAuthProfile | None,
|
12 |
-
oauth_token: gr.OAuthToken
|
13 |
) -> str:
|
14 |
if profile is None or oauth_token is None:
|
15 |
return "Please log in to see your models."
|
@@ -17,14 +19,37 @@ def list_private_models(
|
|
17 |
f"{m.id} ({'private' if m.private else 'public'})"
|
18 |
for m in list_models(author=profile.username, token=oauth_token.token)
|
19 |
]
|
20 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def create_space(
|
23 |
repo_name: str,
|
24 |
sdk: str,
|
25 |
profile: gr.OAuthProfile | None,
|
26 |
-
oauth_token: gr.OAuthToken
|
27 |
-
) -> tuple[str,
|
28 |
if profile is None or oauth_token is None:
|
29 |
return "", "⚠️ Please log in first.", "<p>No Space created yet.</p>"
|
30 |
repo_id = f"{profile.username}/{repo_name}"
|
@@ -40,20 +65,14 @@ def create_space(
|
|
40 |
iframe = f'<iframe src="{url}" width="100%" height="500px"></iframe>'
|
41 |
return repo_id, logmsg, iframe
|
42 |
|
43 |
-
def enable_button(
|
44 |
-
profile: gr.OAuthProfile | None,
|
45 |
-
oauth_token: gr.OAuthToken | None,
|
46 |
-
repo_id: str
|
47 |
-
):
|
48 |
-
return gr.update(interactive=bool(profile and oauth_token and repo_id))
|
49 |
-
|
50 |
def upload_file_to_space(
|
51 |
-
file, #
|
52 |
path_in_repo: str,
|
53 |
repo_id: str,
|
54 |
-
|
|
|
55 |
) -> str:
|
56 |
-
if oauth_token is None:
|
57 |
return "⚠️ Please log in first."
|
58 |
if not repo_id:
|
59 |
return "⚠️ Please create a Space first."
|
@@ -71,17 +90,19 @@ def upload_file_to_space(
|
|
71 |
def get_space_logs(
|
72 |
repo_id: str,
|
73 |
profile: gr.OAuthProfile | None,
|
74 |
-
oauth_token: gr.OAuthToken
|
75 |
) -> str:
|
76 |
-
if profile is None or oauth_token is None:
|
77 |
return "⚠️ Please log in and create a Space first."
|
78 |
endpoint = f"https://huggingface.co/api/spaces/{repo_id}/logs"
|
79 |
-
headers
|
80 |
-
resp
|
81 |
if resp.status_code != 200:
|
82 |
return f"❌ Failed to fetch logs ({resp.status_code})"
|
83 |
-
entries
|
84 |
-
return "\n".join(e.get("text",
|
|
|
|
|
85 |
|
86 |
with gr.Blocks(title="HF OAuth + Space Manager") as demo:
|
87 |
gr.Markdown(
|
@@ -94,37 +115,34 @@ with gr.Blocks(title="HF OAuth + Space Manager") as demo:
|
|
94 |
)
|
95 |
|
96 |
# — LOGIN & MODEL LIST —
|
97 |
-
login_btn
|
98 |
-
status_md
|
99 |
-
models_md
|
100 |
-
demo.load(show_profile, inputs=None,
|
101 |
-
login_btn.click(show_profile, inputs=None,
|
102 |
-
demo.load(list_private_models, inputs=None,
|
103 |
-
login_btn.click(list_private_models, inputs=None,
|
104 |
|
105 |
# — CREATE SPACE —
|
106 |
repo_name = gr.Textbox(label="New Space name", placeholder="my-space")
|
107 |
sdk_selector = gr.Radio(
|
108 |
-
choices=["gradio",
|
109 |
value="gradio",
|
110 |
label="Space template (SDK)"
|
111 |
)
|
112 |
create_btn = gr.Button("Create Space", interactive=False)
|
113 |
session_id = gr.Textbox(visible=False)
|
114 |
-
|
115 |
preview = gr.HTML("<p>No Space created yet.</p>")
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
login_btn.click(enable_button,
|
121 |
-
inputs=[login_btn, login_btn, session_id],
|
122 |
-
outputs=[create_btn])
|
123 |
|
124 |
create_btn.click(
|
125 |
fn=create_space,
|
126 |
-
inputs=[repo_name, sdk_selector, login_btn
|
127 |
-
outputs=[session_id,
|
128 |
)
|
129 |
|
130 |
# — UPLOAD FILES —
|
@@ -133,16 +151,16 @@ with gr.Blocks(title="HF OAuth + Space Manager") as demo:
|
|
133 |
upload_btn = gr.Button("Upload File", interactive=False)
|
134 |
upload_logs = gr.Textbox(label="Upload Logs", interactive=False, lines=2)
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
|
147 |
upload_btn.click(
|
148 |
fn=upload_file_to_space,
|
@@ -154,20 +172,19 @@ with gr.Blocks(title="HF OAuth + Space Manager") as demo:
|
|
154 |
logs_btn = gr.Button("Get Space Logs", interactive=False)
|
155 |
space_logs_md = gr.Textbox(label="Space Logs", interactive=False, lines=10)
|
156 |
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
)
|
167 |
|
168 |
logs_btn.click(
|
169 |
fn=get_space_logs,
|
170 |
-
inputs=[session_id, login_btn
|
171 |
outputs=[space_logs_md]
|
172 |
)
|
173 |
|
|
|
2 |
import requests
|
3 |
from huggingface_hub import create_repo, list_models, upload_file
|
4 |
|
5 |
+
# — USER INFO & MODEL LISTING —
|
6 |
+
|
7 |
def show_profile(profile: gr.OAuthProfile | None) -> str:
|
8 |
if profile is None:
|
9 |
return "*Not logged in.*"
|
|
|
11 |
|
12 |
def list_private_models(
|
13 |
profile: gr.OAuthProfile | None,
|
14 |
+
oauth_token: gr.OAuthToken | None
|
15 |
) -> str:
|
16 |
if profile is None or oauth_token is None:
|
17 |
return "Please log in to see your models."
|
|
|
19 |
f"{m.id} ({'private' if m.private else 'public'})"
|
20 |
for m in list_models(author=profile.username, token=oauth_token.token)
|
21 |
]
|
22 |
+
return (
|
23 |
+
"No models found."
|
24 |
+
if not models
|
25 |
+
else "Models:\n\n" + "\n - ".join(models)
|
26 |
+
)
|
27 |
+
|
28 |
+
# — BUTTON‑ENABLING HELPERS —
|
29 |
+
|
30 |
+
def enable_create(
|
31 |
+
profile: gr.OAuthProfile | None,
|
32 |
+
oauth_token: gr.OAuthToken | None
|
33 |
+
) -> gr.Update:
|
34 |
+
"""Enable Create Space once logged in."""
|
35 |
+
return gr.update(interactive=profile is not None)
|
36 |
+
|
37 |
+
def enable_repo_actions(
|
38 |
+
repo_id: str,
|
39 |
+
profile: gr.OAuthProfile | None,
|
40 |
+
oauth_token: gr.OAuthToken | None
|
41 |
+
) -> gr.Update:
|
42 |
+
"""Enable Upload & Get Logs once space exists and logged in."""
|
43 |
+
return gr.update(interactive=bool(repo_id and profile and oauth_token))
|
44 |
+
|
45 |
+
# — CORE ACTIONS —
|
46 |
|
47 |
def create_space(
|
48 |
repo_name: str,
|
49 |
sdk: str,
|
50 |
profile: gr.OAuthProfile | None,
|
51 |
+
oauth_token: gr.OAuthToken | None
|
52 |
+
) -> tuple[str,str,str]:
|
53 |
if profile is None or oauth_token is None:
|
54 |
return "", "⚠️ Please log in first.", "<p>No Space created yet.</p>"
|
55 |
repo_id = f"{profile.username}/{repo_name}"
|
|
|
65 |
iframe = f'<iframe src="{url}" width="100%" height="500px"></iframe>'
|
66 |
return repo_id, logmsg, iframe
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
def upload_file_to_space(
|
69 |
+
file, # Gradio passes a Python tempfile-like object
|
70 |
path_in_repo: str,
|
71 |
repo_id: str,
|
72 |
+
profile: gr.OAuthProfile | None,
|
73 |
+
oauth_token: gr.OAuthToken | None
|
74 |
) -> str:
|
75 |
+
if profile is None or oauth_token is None:
|
76 |
return "⚠️ Please log in first."
|
77 |
if not repo_id:
|
78 |
return "⚠️ Please create a Space first."
|
|
|
90 |
def get_space_logs(
|
91 |
repo_id: str,
|
92 |
profile: gr.OAuthProfile | None,
|
93 |
+
oauth_token: gr.OAuthToken | None
|
94 |
) -> str:
|
95 |
+
if profile is None or oauth_token is None or not repo_id:
|
96 |
return "⚠️ Please log in and create a Space first."
|
97 |
endpoint = f"https://huggingface.co/api/spaces/{repo_id}/logs"
|
98 |
+
headers = {"Authorization": f"Bearer {oauth_token.token}"}
|
99 |
+
resp = requests.get(endpoint, headers=headers)
|
100 |
if resp.status_code != 200:
|
101 |
return f"❌ Failed to fetch logs ({resp.status_code})"
|
102 |
+
entries = resp.json()
|
103 |
+
return "\n".join(e.get("text","") for e in entries)
|
104 |
+
|
105 |
+
# — BUILD THE UI —
|
106 |
|
107 |
with gr.Blocks(title="HF OAuth + Space Manager") as demo:
|
108 |
gr.Markdown(
|
|
|
115 |
)
|
116 |
|
117 |
# — LOGIN & MODEL LIST —
|
118 |
+
login_btn = gr.LoginButton(variant="huggingface", size="lg")
|
119 |
+
status_md = gr.Markdown("*Not logged in.*")
|
120 |
+
models_md = gr.Markdown()
|
121 |
+
demo.load(show_profile, inputs=None, outputs=status_md)
|
122 |
+
login_btn.click(show_profile, inputs=None, outputs=status_md)
|
123 |
+
demo.load(list_private_models, inputs=None, outputs=models_md)
|
124 |
+
login_btn.click(list_private_models, inputs=None, outputs=models_md)
|
125 |
|
126 |
# — CREATE SPACE —
|
127 |
repo_name = gr.Textbox(label="New Space name", placeholder="my-space")
|
128 |
sdk_selector = gr.Radio(
|
129 |
+
choices=["gradio","streamlit"],
|
130 |
value="gradio",
|
131 |
label="Space template (SDK)"
|
132 |
)
|
133 |
create_btn = gr.Button("Create Space", interactive=False)
|
134 |
session_id = gr.Textbox(visible=False)
|
135 |
+
create_logs = gr.Textbox(label="Create Logs", interactive=False, lines=3)
|
136 |
preview = gr.HTML("<p>No Space created yet.</p>")
|
137 |
|
138 |
+
# enable create_btn after login
|
139 |
+
demo.load(enable_create, inputs=[login_btn], outputs=[create_btn])
|
140 |
+
login_btn.click(enable_create, inputs=[login_btn], outputs=[create_btn])
|
|
|
|
|
|
|
141 |
|
142 |
create_btn.click(
|
143 |
fn=create_space,
|
144 |
+
inputs=[repo_name, sdk_selector, login_btn],
|
145 |
+
outputs=[session_id, create_logs, preview]
|
146 |
)
|
147 |
|
148 |
# — UPLOAD FILES —
|
|
|
151 |
upload_btn = gr.Button("Upload File", interactive=False)
|
152 |
upload_logs = gr.Textbox(label="Upload Logs", interactive=False, lines=2)
|
153 |
|
154 |
+
# enable upload_btn after space exists & login
|
155 |
+
demo.load(enable_repo_actions,
|
156 |
+
inputs=[session_id, login_btn],
|
157 |
+
outputs=[upload_btn])
|
158 |
+
login_btn.click(enable_repo_actions,
|
159 |
+
inputs=[session_id, login_btn],
|
160 |
+
outputs=[upload_btn])
|
161 |
+
session_id.change(enable_repo_actions,
|
162 |
+
inputs=[session_id, login_btn],
|
163 |
+
outputs=[upload_btn])
|
164 |
|
165 |
upload_btn.click(
|
166 |
fn=upload_file_to_space,
|
|
|
172 |
logs_btn = gr.Button("Get Space Logs", interactive=False)
|
173 |
space_logs_md = gr.Textbox(label="Space Logs", interactive=False, lines=10)
|
174 |
|
175 |
+
demo.load(enable_repo_actions,
|
176 |
+
inputs=[session_id, login_btn],
|
177 |
+
outputs=[logs_btn])
|
178 |
+
login_btn.click(enable_repo_actions,
|
179 |
+
inputs=[session_id, login_btn],
|
180 |
+
outputs=[logs_btn])
|
181 |
+
session_id.change(enable_repo_actions,
|
182 |
+
inputs=[session_id, login_btn],
|
183 |
+
outputs=[logs_btn])
|
|
|
184 |
|
185 |
logs_btn.click(
|
186 |
fn=get_space_logs,
|
187 |
+
inputs=[session_id, login_btn],
|
188 |
outputs=[space_logs_md]
|
189 |
)
|
190 |
|