akhaliq HF Staff commited on
Commit
e5de22c
·
1 Parent(s): 5493f2d

add load space button

Browse files
Files changed (1) hide show
  1. app.py +68 -19
app.py CHANGED
@@ -1157,6 +1157,7 @@ with gr.Blocks(
1157
  current_model = gr.State(AVAILABLE_MODELS[0]) # Moonshot Kimi-K2
1158
  open_panel = gr.State(None)
1159
  last_login_state = gr.State(None)
 
1160
 
1161
  with gr.Sidebar():
1162
  login_button = gr.LoginButton()
@@ -1168,6 +1169,18 @@ with gr.Blocks(
1168
  )
1169
  deploy_btn = gr.Button("🚀 Deploy App", variant="primary")
1170
  deploy_status = gr.Markdown(visible=False, label="Deploy status")
 
 
 
 
 
 
 
 
 
 
 
 
1171
  input = gr.Textbox(
1172
  label="What would you like to build?",
1173
  placeholder="Describe your application...",
@@ -1286,28 +1299,30 @@ with gr.Blocks(
1286
  def deploy_to_user_space(
1287
  code,
1288
  space_name,
1289
- profile: gr.OAuthProfile | None = None,
1290
- token: gr.OAuthToken | None = None
 
1291
  ):
1292
  if not code or not code.strip():
1293
  return gr.update(value="No code to deploy.", visible=True)
1294
  if profile is None or token is None:
1295
- # Fallback to old method if not logged in
1296
- return gr.update(value="Please log in with your Hugging Face account to deploy to your own Space. Otherwise, use the default deploy (opens in new tab).", visible=True)
1297
- username = profile.username
1298
- repo_id = f"{username}/{space_name.strip()}"
1299
- api = HfApi(token=token.token)
1300
- # Create the Space if it doesn't exist
1301
- try:
1302
- # Fix create_repo call: use repo_id, not name
1303
- api.create_repo(
1304
- repo_id=repo_id, # e.g. username/space_name
1305
- repo_type="space",
1306
- space_sdk="static", # or "gradio" if you want a Gradio Space
1307
- exist_ok=True # Don't error if it already exists
1308
- )
1309
- except Exception as e:
1310
- return gr.update(value=f"Error creating Space: {e}", visible=True)
 
1311
  # Save code to a temporary file
1312
  with tempfile.NamedTemporaryFile("w", suffix=".html", delete=False) as f:
1313
  f.write(code)
@@ -1328,11 +1343,45 @@ with gr.Blocks(
1328
  # Connect the deploy button to the new function
1329
  deploy_btn.click(
1330
  deploy_to_user_space,
1331
- inputs=[code_output, space_name_input],
1332
  outputs=deploy_status
1333
  )
1334
  # Keep the old deploy method as fallback (if not logged in, user can still use the old method)
1335
  # Optionally, you can keep the old deploy_btn.click for the default method as a secondary button.
1336
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1337
  if __name__ == "__main__":
1338
  demo.queue(api_open=False, default_concurrency_limit=20).launch(ssr_mode=True, mcp_server=False, show_api=False)
 
1157
  current_model = gr.State(AVAILABLE_MODELS[0]) # Moonshot Kimi-K2
1158
  open_panel = gr.State(None)
1159
  last_login_state = gr.State(None)
1160
+ loaded_space_id = gr.State(None)
1161
 
1162
  with gr.Sidebar():
1163
  login_button = gr.LoginButton()
 
1169
  )
1170
  deploy_btn = gr.Button("🚀 Deploy App", variant="primary")
1171
  deploy_status = gr.Markdown(visible=False, label="Deploy status")
1172
+
1173
+ # --- Load Space UI ---
1174
+ load_space_id = gr.Textbox(
1175
+ label="Load Space (username/space_name)",
1176
+ placeholder="e.g. huggingface-projects/hello-static",
1177
+ lines=1,
1178
+ visible=True
1179
+ )
1180
+ load_space_btn = gr.Button("Load Space", variant="secondary")
1181
+ load_space_status = gr.Markdown(visible=False)
1182
+ # --- End Load Space UI ---
1183
+
1184
  input = gr.Textbox(
1185
  label="What would you like to build?",
1186
  placeholder="Describe your application...",
 
1299
  def deploy_to_user_space(
1300
  code,
1301
  space_name,
1302
+ profile, # OAuth profile from LoginButton
1303
+ token, # OAuth token from LoginButton
1304
+ loaded_space_id=None
1305
  ):
1306
  if not code or not code.strip():
1307
  return gr.update(value="No code to deploy.", visible=True)
1308
  if profile is None or token is None:
1309
+ return gr.update(value="Please log in with your Hugging Face account to deploy to your own Space.", visible=True)
1310
+ api = HfApi(token=token)
1311
+ if loaded_space_id:
1312
+ repo_id = loaded_space_id
1313
+ else:
1314
+ username = profile.username
1315
+ repo_id = f"{username}/{space_name.strip()}"
1316
+ # Create the Space if it doesn't exist
1317
+ try:
1318
+ api.create_repo(
1319
+ repo_id=repo_id,
1320
+ repo_type="space",
1321
+ space_sdk="static",
1322
+ exist_ok=True
1323
+ )
1324
+ except Exception as e:
1325
+ return gr.update(value=f"Error creating Space: {e}", visible=True)
1326
  # Save code to a temporary file
1327
  with tempfile.NamedTemporaryFile("w", suffix=".html", delete=False) as f:
1328
  f.write(code)
 
1343
  # Connect the deploy button to the new function
1344
  deploy_btn.click(
1345
  deploy_to_user_space,
1346
+ inputs=[code_output, space_name_input, login_button, login_button, loaded_space_id],
1347
  outputs=deploy_status
1348
  )
1349
  # Keep the old deploy method as fallback (if not logged in, user can still use the old method)
1350
  # Optionally, you can keep the old deploy_btn.click for the default method as a secondary button.
1351
 
1352
+ # --- Load Space logic ---
1353
+ from huggingface_hub import HfApi, hf_hub_download
1354
+ def load_space_index_html(space_id, history):
1355
+ api = HfApi()
1356
+ try:
1357
+ files = api.list_repo_files(space_id, repo_type="space")
1358
+ if "index.html" not in files:
1359
+ return gr.update(value="No index.html found in this Space.", visible=True), gr.update(value="", language="html"), None, "", gr.update(visible=True), history, None
1360
+ # Download the file
1361
+ local_path = hf_hub_download(repo_id=space_id, filename="index.html", repo_type="space")
1362
+ with open(local_path, "r", encoding="utf-8") as f:
1363
+ html_code = f.read()
1364
+ # Set as last assistant message in history
1365
+ new_history = history.copy() if history else []
1366
+ # Use a synthetic user message for clarity
1367
+ new_history.append([f"Loaded index.html from {space_id}", html_code])
1368
+ return (
1369
+ gr.update(value=f"Loaded index.html from {space_id}", visible=True),
1370
+ gr.update(value=html_code, language="html"),
1371
+ send_to_sandbox(html_code),
1372
+ history_to_chatbot_messages(new_history),
1373
+ gr.update(visible=True),
1374
+ new_history,
1375
+ space_id
1376
+ )
1377
+ except Exception as e:
1378
+ return gr.update(value=f"Error: {e}", visible=True), gr.update(value="", language="html"), None, "", gr.update(visible=True), history, None
1379
+ load_space_btn.click(
1380
+ load_space_index_html,
1381
+ inputs=[load_space_id, history],
1382
+ outputs=[load_space_status, code_output, sandbox, history_output, load_space_status, history, loaded_space_id]
1383
+ )
1384
+ # --- End Load Space logic ---
1385
+
1386
  if __name__ == "__main__":
1387
  demo.queue(api_open=False, default_concurrency_limit=20).launch(ssr_mode=True, mcp_server=False, show_api=False)