akhaliq HF Staff commited on
Commit
31713bf
Β·
1 Parent(s): 4134e29
Files changed (2) hide show
  1. backend_deploy.py +51 -36
  2. frontend/src/app/page.tsx +29 -7
backend_deploy.py CHANGED
@@ -461,30 +461,42 @@ 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
- # 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",
@@ -492,18 +504,21 @@ def deploy_to_huggingface_space(
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:
 
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",
 
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:
frontend/src/app/page.tsx CHANGED
@@ -167,18 +167,28 @@ export default function Home() {
167
  let existingSpace: string | null = null;
168
 
169
  // Look for previous deployment or imported space in history
 
 
 
 
 
170
  if (messages.length > 0 && username) {
171
- console.log('[Deploy] Scanning message history for existing deployments...');
172
 
173
  for (let i = messages.length - 1; i >= 0; i--) {
174
  const msg = messages[i];
 
 
 
 
 
175
 
176
  // Check for deployment messages
177
  if (msg.role === 'assistant' && msg.content.includes('βœ… Deployed')) {
178
  const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
179
  if (match) {
180
  existingSpace = match[1];
181
- console.log('[Deploy] Found previous deployment:', existingSpace);
182
  break;
183
  }
184
  }
@@ -188,30 +198,42 @@ export default function Home() {
188
  const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
189
  if (match) {
190
  existingSpace = match[1];
191
- console.log('[Deploy] Found previous update:', existingSpace);
192
  break;
193
  }
194
  }
195
 
196
  // Check for imported space messages - THE KEY PART!
197
  if (msg.role === 'user' && msg.content.startsWith('Imported Space from')) {
 
198
  const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
 
199
  if (match) {
200
  const importedSpace = match[1];
 
 
 
 
201
  // Only use imported space if user owns it (can update it)
202
  if (importedSpace.startsWith(`${username}/`)) {
203
  existingSpace = importedSpace;
204
- console.log('[Deploy] Found imported space (user owns it):', existingSpace);
205
  break;
206
  } else {
207
- console.log('[Deploy] Found imported space but user does not own it:', importedSpace);
208
- // If user doesn't own the imported space, we'll create a new one
209
- // (existingSpace remains null, triggering new deployment)
210
  }
 
 
211
  }
212
  }
213
  }
 
 
 
 
214
  }
 
215
 
216
  // Auto-generate space name (never prompt user)
217
  let spaceName = undefined; // undefined = backend will auto-generate
 
167
  let existingSpace: string | null = null;
168
 
169
  // Look for previous deployment or imported space in history
170
+ console.log('[Deploy] ========== DEBUG START ==========');
171
+ console.log('[Deploy] Total messages in history:', messages.length);
172
+ console.log('[Deploy] Current username:', username);
173
+ console.log('[Deploy] Messages:', JSON.stringify(messages, null, 2));
174
+
175
  if (messages.length > 0 && username) {
176
+ console.log('[Deploy] Scanning message history...');
177
 
178
  for (let i = messages.length - 1; i >= 0; i--) {
179
  const msg = messages[i];
180
+ console.log(`[Deploy] Checking message ${i}:`, {
181
+ role: msg.role,
182
+ contentPreview: msg.content.substring(0, 100),
183
+ startsWithImported: msg.content.startsWith('Imported Space from')
184
+ });
185
 
186
  // Check for deployment messages
187
  if (msg.role === 'assistant' && msg.content.includes('βœ… Deployed')) {
188
  const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
189
  if (match) {
190
  existingSpace = match[1];
191
+ console.log('[Deploy] βœ… Found "βœ… Deployed" message:', existingSpace);
192
  break;
193
  }
194
  }
 
198
  const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
199
  if (match) {
200
  existingSpace = match[1];
201
+ console.log('[Deploy] βœ… Found "βœ… Updated" message:', existingSpace);
202
  break;
203
  }
204
  }
205
 
206
  // Check for imported space messages - THE KEY PART!
207
  if (msg.role === 'user' && msg.content.startsWith('Imported Space from')) {
208
+ console.log('[Deploy] 🎯 Found "Imported Space from" message!');
209
  const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
210
+ console.log('[Deploy] Regex match result:', match);
211
  if (match) {
212
  const importedSpace = match[1];
213
+ console.log('[Deploy] Extracted space:', importedSpace);
214
+ console.log('[Deploy] Username to check:', username);
215
+ console.log('[Deploy] Starts with username?', importedSpace.startsWith(`${username}/`));
216
+
217
  // Only use imported space if user owns it (can update it)
218
  if (importedSpace.startsWith(`${username}/`)) {
219
  existingSpace = importedSpace;
220
+ console.log('[Deploy] βœ…βœ…βœ… USER OWNS THIS SPACE! Will update:', existingSpace);
221
  break;
222
  } else {
223
+ console.log('[Deploy] ⚠️ User does not own this space:', importedSpace);
224
+ console.log('[Deploy] Expected to start with:', `${username}/`);
 
225
  }
226
+ } else {
227
+ console.log('[Deploy] ❌ Regex did not match URL in message');
228
  }
229
  }
230
  }
231
+
232
+ console.log('[Deploy] Final existingSpace value:', existingSpace);
233
+ } else {
234
+ console.log('[Deploy] Skipping scan - no messages or no username');
235
  }
236
+ console.log('[Deploy] ========== DEBUG END ==========');
237
 
238
  // Auto-generate space name (never prompt user)
239
  let spaceName = undefined; // undefined = backend will auto-generate