akhaliq HF Staff commited on
Commit
e8d64af
·
1 Parent(s): 7f3ae70

add sdk options

Browse files
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -1166,6 +1166,18 @@ with gr.Blocks(
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(
@@ -1286,37 +1298,47 @@ 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)
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
  )
@@ -1328,7 +1350,7 @@ 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)
 
1166
  lines=1,
1167
  visible=True
1168
  )
1169
+ # Add SDK selection dropdown
1170
+ sdk_choices = [
1171
+ ("Gradio (Python)", "gradio"),
1172
+ ("Streamlit (Python)", "streamlit"),
1173
+ ("Static (HTML)", "static")
1174
+ ]
1175
+ sdk_dropdown = gr.Dropdown(
1176
+ choices=[x[0] for x in sdk_choices],
1177
+ value="Gradio (Python)",
1178
+ label="App SDK",
1179
+ visible=True
1180
+ )
1181
  deploy_btn = gr.Button("🚀 Deploy App", variant="primary")
1182
  deploy_status = gr.Markdown(visible=False, label="Deploy status")
1183
  input = gr.Textbox(
 
1298
  def deploy_to_user_space(
1299
  code,
1300
  space_name,
1301
+ sdk_name, # new argument
1302
  profile: gr.OAuthProfile | None = None,
1303
  token: gr.OAuthToken | None = None
1304
  ):
1305
  if not code or not code.strip():
1306
  return gr.update(value="No code to deploy.", visible=True)
1307
  if profile is None or token is None:
 
1308
  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)
1309
  username = profile.username
1310
  repo_id = f"{username}/{space_name.strip()}"
1311
+ # Map SDK name to HF SDK slug
1312
+ sdk_map = {
1313
+ "Gradio (Python)": "gradio",
1314
+ "Streamlit (Python)": "streamlit",
1315
+ "Static (HTML)": "static"
1316
+ }
1317
+ sdk = sdk_map.get(sdk_name, "gradio")
1318
  api = HfApi(token=token.token)
1319
  # Create the Space if it doesn't exist
1320
  try:
 
1321
  api.create_repo(
1322
  repo_id=repo_id, # e.g. username/space_name
1323
  repo_type="space",
1324
+ space_sdk=sdk, # Use selected SDK
1325
  exist_ok=True # Don't error if it already exists
1326
  )
1327
  except Exception as e:
1328
  return gr.update(value=f"Error creating Space: {e}", visible=True)
1329
  # Save code to a temporary file
1330
+ if sdk == "static":
1331
+ file_name = "index.html"
1332
+ else:
1333
+ file_name = "app.py"
1334
+ with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:
1335
  f.write(code)
1336
  temp_path = f.name
1337
  # Upload the file
1338
  try:
1339
  api.upload_file(
1340
  path_or_fileobj=temp_path,
1341
+ path_in_repo=file_name,
1342
  repo_id=repo_id,
1343
  repo_type="space"
1344
  )
 
1350
  # Connect the deploy button to the new function
1351
  deploy_btn.click(
1352
  deploy_to_user_space,
1353
+ inputs=[code_output, space_name_input, sdk_dropdown],
1354
  outputs=deploy_status
1355
  )
1356
  # Keep the old deploy method as fallback (if not logged in, user can still use the old method)