akhaliq HF Staff commited on
Commit
f7c3e69
Β·
1 Parent(s): 3f78831
Files changed (2) hide show
  1. README.md +4 -0
  2. app.py +61 -13
README.md CHANGED
@@ -9,6 +9,10 @@ app_file: app.py
9
  pinned: false
10
  disable_embedding: true
11
  hf_oauth: true
 
 
 
 
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
9
  pinned: false
10
  disable_embedding: true
11
  hf_oauth: true
12
+ hf_oauth_scopes:
13
+ - "openid" # Always included: ID token
14
+ - "profile" # Always included: user profile info
15
+ - "write-repos" # Needed to create/upload to Spaces on behalf of the user
16
  ---
17
 
18
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -22,6 +22,8 @@ import urllib.parse
22
  import gradio as gr
23
  from huggingface_hub import InferenceClient
24
  from tavily import TavilyClient
 
 
25
 
26
  # Gradio supported languages for syntax highlighting
27
  GRADIO_SUPPORTED_LANGUAGES = [
@@ -1157,13 +1159,15 @@ with gr.Blocks(
1157
  last_login_state = gr.State(None)
1158
 
1159
  with gr.Sidebar():
1160
- # Remove Hugging Face Login Button and login-required message
1161
- # login_button = gr.LoginButton(
1162
- # value="Sign in with Hugging Face",
1163
- # variant="huggingface",
1164
- # size="lg"
1165
- # )
1166
- # login_required_msg = gr.Markdown("**Please sign in with Hugging Face to use the app.**", visible=True)
 
 
1167
  input = gr.Textbox(
1168
  label="What would you like to build?",
1169
  placeholder="Describe your application...",
@@ -1209,7 +1213,6 @@ with gr.Blocks(
1209
  label="Model",
1210
  visible=True # Always visible
1211
  )
1212
- # Remove provider_choices and provider_dropdown, set provider_state to 'auto' only
1213
  provider_state = gr.State("auto")
1214
  gr.Markdown("**Quick start**", visible=True)
1215
  with gr.Column(visible=True) as quick_examples_col:
@@ -1239,6 +1242,8 @@ with gr.Blocks(
1239
  outputs=[current_model, image_input]
1240
  )
1241
  # Remove the Advanced accordion and system prompt editing UI
 
 
1242
 
1243
  with gr.Column():
1244
  with gr.Tabs():
@@ -1249,8 +1254,6 @@ with gr.Blocks(
1249
  interactive=False,
1250
  label="Generated code"
1251
  )
1252
- # Rename button to 'πŸš€ Deploy App'
1253
- deploy_btn = gr.Button("πŸš€ Deploy App", variant="primary", size="sm", visible=True)
1254
  with gr.Tab("Preview"):
1255
  sandbox = gr.HTML(label="Live preview")
1256
  with gr.Tab("History"):
@@ -1280,11 +1283,56 @@ with gr.Blocks(
1280
 
1281
  # Deploy to Spaces logic
1282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1283
  deploy_btn.click(
1284
- fn=lambda code: deploy_to_spaces_static(code),
1285
- inputs=code_output,
1286
- outputs=None
1287
  )
 
 
1288
 
1289
  if __name__ == "__main__":
1290
  demo.queue(api_open=False, default_concurrency_limit=20).launch(ssr_mode=True, mcp_server=False, show_api=False)
 
22
  import gradio as gr
23
  from huggingface_hub import InferenceClient
24
  from tavily import TavilyClient
25
+ from huggingface_hub import HfApi
26
+ import tempfile
27
 
28
  # Gradio supported languages for syntax highlighting
29
  GRADIO_SUPPORTED_LANGUAGES = [
 
1159
  last_login_state = gr.State(None)
1160
 
1161
  with gr.Sidebar():
1162
+ login_button = gr.LoginButton()
1163
+ space_name_input = gr.Textbox(
1164
+ label="Space name (e.g. my-cool-space)",
1165
+ placeholder="Enter your Space name",
1166
+ lines=1,
1167
+ visible=True
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...",
 
1213
  label="Model",
1214
  visible=True # Always visible
1215
  )
 
1216
  provider_state = gr.State("auto")
1217
  gr.Markdown("**Quick start**", visible=True)
1218
  with gr.Column(visible=True) as quick_examples_col:
 
1242
  outputs=[current_model, image_input]
1243
  )
1244
  # Remove the Advanced accordion and system prompt editing UI
1245
+ # login_button.render()
1246
+ # space_name_input.render()
1247
 
1248
  with gr.Column():
1249
  with gr.Tabs():
 
1254
  interactive=False,
1255
  label="Generated code"
1256
  )
 
 
1257
  with gr.Tab("Preview"):
1258
  sandbox = gr.HTML(label="Live preview")
1259
  with gr.Tab("History"):
 
1283
 
1284
  # Deploy to Spaces logic
1285
 
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)
1314
+ temp_path = f.name
1315
+ # Upload the file
1316
+ try:
1317
+ api.upload_file(
1318
+ path_or_fileobj=temp_path,
1319
+ path_in_repo="index.html",
1320
+ repo_id=repo_id,
1321
+ repo_type="space"
1322
+ )
1323
+ space_url = f"https://huggingface.co/spaces/{repo_id}"
1324
+ return gr.update(value=f"βœ… Deployed! [Open your Space here]({space_url})", visible=True)
1325
+ except Exception as e:
1326
+ return gr.update(value=f"Error uploading file: {e}", visible=True)
1327
+
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)