akhaliq HF Staff commited on
Commit
4134e29
Β·
1 Parent(s): 47565e8
Files changed (2) hide show
  1. backend_deploy.py +37 -40
  2. frontend/src/app/page.tsx +3 -3
backend_deploy.py CHANGED
@@ -461,52 +461,49 @@ 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
- # Create the space (only for new deployments)
465
- if not is_update:
466
- try:
467
- if language == "transformers.js":
468
- # For transformers.js, duplicate the template space
469
- print(f"[Deploy] Creating transformers.js space: {repo_id}")
470
- try:
471
- from huggingface_hub import duplicate_space
472
-
473
- # IMPORTANT: duplicate_space expects just the space name, not the full repo_id
474
- # It will automatically prepend the username
475
- print(f"[Deploy] Attempting to duplicate template space to: {space_name}")
476
- result = duplicate_space(
477
- from_id="static-templates/transformers.js",
478
- to_id=space_name, # Just the space name, not username/space-name
479
- token=token,
480
- exist_ok=True
481
- )
482
- print(f"[Deploy] Template duplication result: {result}")
483
- except Exception as e:
484
- # If template duplication fails, fall back to regular create
485
- print(f"[Deploy] Template duplication failed, creating regular static space: {e}")
486
- import traceback
487
- traceback.print_exc()
488
- api.create_repo(
489
- repo_id=repo_id,
490
- repo_type="space",
491
- space_sdk=sdk,
492
- private=private,
493
- exist_ok=False
494
- )
495
- else:
496
- # For other languages, create space normally
497
  api.create_repo(
498
  repo_id=repo_id,
499
  repo_type="space",
500
  space_sdk=sdk,
501
  private=private,
502
- exist_ok=False
503
  )
504
- except Exception as e:
505
- if "already exists" in str(e).lower():
506
- # Space exists, treat as update
507
- is_update = True
508
- else:
509
- return False, f"Failed to create space: {str(e)}", None
 
 
 
 
 
 
510
 
511
  # Upload files
512
  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
+ # ALWAYS create/ensure repo exists (with exist_ok=True)
465
+ # This works for both new spaces and updates!
466
+ try:
467
+ if language == "transformers.js" and not is_update:
468
+ # For NEW transformers.js spaces, try to duplicate the template
469
+ print(f"[Deploy] Creating new transformers.js space: {repo_id}")
470
+ try:
471
+ from huggingface_hub import duplicate_space
472
+
473
+ # IMPORTANT: duplicate_space expects just the space name, not the full repo_id
474
+ # It will automatically prepend the username
475
+ print(f"[Deploy] Attempting to duplicate template space to: {space_name}")
476
+ result = duplicate_space(
477
+ from_id="static-templates/transformers.js",
478
+ to_id=space_name, # Just the space name, not username/space-name
479
+ token=token,
480
+ exist_ok=True
481
+ )
482
+ print(f"[Deploy] Template duplication result: {result}")
483
+ except Exception as e:
484
+ # If template duplication fails, fall back to regular create
485
+ print(f"[Deploy] Template duplication failed, creating regular static space: {e}")
486
+ import traceback
487
+ traceback.print_exc()
 
 
 
 
 
 
 
 
 
488
  api.create_repo(
489
  repo_id=repo_id,
490
  repo_type="space",
491
  space_sdk=sdk,
492
  private=private,
493
+ exist_ok=True # Don't fail if exists
494
  )
495
+ else:
496
+ # For all other cases (new or update), ensure repo exists
497
+ print(f"[Deploy] Ensuring repo exists: {repo_id} (is_update={is_update})")
498
+ api.create_repo(
499
+ repo_id=repo_id,
500
+ repo_type="space",
501
+ space_sdk=sdk,
502
+ private=private if not is_update else None, # Only set private for new repos
503
+ exist_ok=True # Don't fail if repo already exists
504
+ )
505
+ except Exception as e:
506
+ return False, f"Failed to create/access space: {str(e)}", None
507
 
508
  # Upload files
509
  if not commit_message:
frontend/src/app/page.tsx CHANGED
@@ -254,12 +254,12 @@ export default function Home() {
254
  }
255
  }
256
 
257
- // Add deployment message to chat (matches Gradio format)
258
  const deployMessage: Message = {
259
  role: 'assistant',
260
  content: existingSpace
261
- ? `βœ… Updated! View at: ${response.space_url}`
262
- : `βœ… Deployed! View at: ${response.space_url}`,
263
  timestamp: new Date().toISOString(),
264
  };
265
  setMessages((prev) => [...prev, deployMessage]);
 
254
  }
255
  }
256
 
257
+ // Add deployment message to chat (EXACT Gradio format with markdown link)
258
  const deployMessage: Message = {
259
  role: 'assistant',
260
  content: existingSpace
261
+ ? `βœ… Updated! [Open your Space here](${response.space_url})`
262
+ : `βœ… Deployed! [Open your Space here](${response.space_url})`,
263
  timestamp: new Date().toISOString(),
264
  };
265
  setMessages((prev) => [...prev, deployMessage]);