akhaliq HF Staff commited on
Commit
dcd2ebe
·
1 Parent(s): 106a0d6
Files changed (1) hide show
  1. backend_deploy.py +59 -44
backend_deploy.py CHANGED
@@ -461,64 +461,79 @@ def deploy_to_huggingface_space(
461
  # Don't create README - HuggingFace will auto-generate it
462
  # We'll add the anycoder tag after deployment
463
 
464
- # ONLY create repo for NEW deployments (not updates!)
465
- # This matches the Gradio version logic
466
- if not is_update:
467
- print(f"[Deploy] Creating NEW space: {repo_id}")
 
468
  try:
469
- if language == "transformers.js":
470
- # For NEW transformers.js spaces, try to duplicate the template
471
- print(f"[Deploy] Creating new transformers.js space: {repo_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
472
  try:
473
- from huggingface_hub import duplicate_space
474
-
475
- # IMPORTANT: duplicate_space expects just the space name, not the full repo_id
476
- # It will automatically prepend the username
477
- print(f"[Deploy] Attempting to duplicate template space to: {space_name}")
478
- result = duplicate_space(
479
- from_id="static-templates/transformers.js",
480
- to_id=space_name, # Just the space name, not username/space-name
481
- token=token,
482
- exist_ok=True
483
- )
484
- print(f"[Deploy] Template duplication result: {result}")
485
- except Exception as e:
486
- # If template duplication fails, fall back to regular create
487
- print(f"[Deploy] Template duplication failed, creating regular static space: {e}")
488
- import traceback
489
- traceback.print_exc()
490
  api.create_repo(
491
  repo_id=repo_id,
492
  repo_type="space",
493
  space_sdk=sdk,
494
  private=private,
495
- exist_ok=True # Don't fail if exists
496
  )
497
- elif sdk != "docker":
498
- # For non-Docker SDKs, create the repo normally
499
- print(f"[Deploy] Creating new {sdk} space: {repo_id}")
500
- api.create_repo(
501
- repo_id=repo_id,
502
- repo_type="space",
503
- space_sdk=sdk,
504
- private=private,
505
- exist_ok=True # Don't fail if exists
506
- )
507
- else:
508
- # For Docker (React/Streamlit), create_repo is handled differently
509
- print(f"[Deploy] Creating new Docker space: {repo_id}")
510
- from huggingface_hub import create_repo
511
- create_repo(
 
 
 
512
  repo_id=repo_id,
513
  repo_type="space",
514
  space_sdk="docker",
515
  token=token,
516
  exist_ok=True
517
  )
518
- except Exception as e:
519
- return False, f"Failed to create space: {str(e)}", None
520
- else:
521
- print(f"[Deploy] UPDATING existing space: {repo_id} (skipping create_repo)")
522
 
523
  # Upload files
524
  if not commit_message:
 
461
  # Don't create README - HuggingFace will auto-generate it
462
  # We'll add the anycoder tag after deployment
463
 
464
+ # ONLY create repo for NEW deployments of non-Docker, non-transformers.js spaces
465
+ # Docker and transformers.js handle repo creation separately below
466
+ # This matches the Gradio version logic (line 1256 in ui.py)
467
+ if not is_update and sdk != "docker" and language not in ["transformers.js"]:
468
+ print(f"[Deploy] Creating NEW {sdk} space: {repo_id}")
469
  try:
470
+ api.create_repo(
471
+ repo_id=repo_id,
472
+ repo_type="space",
473
+ space_sdk=sdk,
474
+ private=private,
475
+ exist_ok=True
476
+ )
477
+ except Exception as e:
478
+ return False, f"Failed to create space: {str(e)}", None
479
+ elif is_update:
480
+ print(f"[Deploy] UPDATING existing space: {repo_id} (skipping create_repo)")
481
+
482
+ # Handle transformers.js spaces (create repo via duplicate_space)
483
+ if language == "transformers.js":
484
+ if not is_update:
485
+ print(f"[Deploy] Creating NEW transformers.js space via template duplication")
486
+ try:
487
+ from huggingface_hub import duplicate_space
488
+
489
+ # duplicate_space expects just the space name, not the full repo_id
490
+ print(f"[Deploy] Attempting to duplicate template space to: {space_name}")
491
+ result = duplicate_space(
492
+ from_id="static-templates/transformers.js",
493
+ to_id=space_name,
494
+ token=token,
495
+ exist_ok=True
496
+ )
497
+ print(f"[Deploy] Template duplication result: {result}")
498
+ except Exception as e:
499
+ # If template duplication fails, fall back to regular create
500
+ print(f"[Deploy] Template duplication failed, creating regular static space: {e}")
501
+ import traceback
502
+ traceback.print_exc()
503
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
504
  api.create_repo(
505
  repo_id=repo_id,
506
  repo_type="space",
507
  space_sdk=sdk,
508
  private=private,
509
+ exist_ok=True
510
  )
511
+ except Exception as e2:
512
+ return False, f"Failed to create transformers.js space: {str(e2)}", None
513
+ else:
514
+ # For updates, verify we can access the existing space
515
+ try:
516
+ space_info = api.space_info(repo_id)
517
+ if not space_info:
518
+ return False, f"Could not access space {repo_id} for update", None
519
+ except Exception as e:
520
+ return False, f"Cannot update space {repo_id}: {str(e)}", None
521
+
522
+ # Handle Docker spaces (React/Streamlit) - create repo separately
523
+ elif sdk == "docker" and language in ["streamlit", "react"]:
524
+ if not is_update:
525
+ print(f"[Deploy] Creating NEW Docker space for {language}: {repo_id}")
526
+ try:
527
+ from huggingface_hub import create_repo as hf_create_repo
528
+ hf_create_repo(
529
  repo_id=repo_id,
530
  repo_type="space",
531
  space_sdk="docker",
532
  token=token,
533
  exist_ok=True
534
  )
535
+ except Exception as e:
536
+ return False, f"Failed to create Docker space: {str(e)}", None
 
 
537
 
538
  # Upload files
539
  if not commit_message: