akhaliq HF Staff commited on
Commit
1230bba
·
1 Parent(s): a47f768

Fix transformers.js deployment: Use space name not repo_id for duplicate_space()

Browse files

Issue: 500 error when deploying transformers.js apps
Root cause: duplicate_space() was receiving 'username/space-name' but expects just 'space-name'

Key fix in backend_deploy.py:
- Changed: duplicate_space(to_id=repo_id, ...)
- To: duplicate_space(to_id=space_name, ...)

The huggingface_hub duplicate_space() function automatically prepends
the username from the token, so it expects just the space name, not
the full repo_id format.

Additional improvements:
1. Better error handling and logging for transformers.js parsing
2. Added detailed print statements to track duplication process
3. Match original deploy.py validation logic exactly:
- Use dict access (files['index.html']) instead of .get()
- Check all three files with 'or' conditions
4. Added traceback printing for easier debugging

Error flow now matches original:
1. Parse transformers.js output → extract 3 files
2. Validate all 3 files exist (KeyError if missing)
3. Write files to temp directory
4. Duplicate template space (to_id=space_name only)
5. Upload each file individually with retry logic
6. Add anycoder tag to README

This matches the exact pattern used in the original deploy.py at line 1506-1508.

Files changed (1) hide show
  1. backend_deploy.py +32 -16
backend_deploy.py CHANGED
@@ -324,18 +324,27 @@ def deploy_to_huggingface_space(
324
  use_individual_uploads = False # Flag for transformers.js
325
 
326
  if language == "transformers.js":
327
- files = parse_transformers_js_output(code)
328
-
329
- # Validate all three files are present
330
- if not files.get('index.html') or not files.get('index.js') or not files.get('style.css'):
331
- return False, "Error: Could not parse transformers.js output. Missing index.html, index.js, or style.css", None
332
-
333
- # Write transformers.js files
334
- for filename, content in files.items():
335
- (temp_path / filename).write_text(content, encoding='utf-8')
336
-
337
- # For transformers.js, we'll upload files individually (not via upload_folder)
338
- use_individual_uploads = True
 
 
 
 
 
 
 
 
 
339
 
340
  elif language == "html":
341
  html_code = parse_html_code(code)
@@ -411,18 +420,25 @@ def deploy_to_huggingface_space(
411
  try:
412
  if language == "transformers.js":
413
  # For transformers.js, duplicate the template space
414
- from huggingface_hub import duplicate_space
415
-
416
  try:
417
- duplicate_space(
 
 
 
 
 
418
  from_id="static-templates/transformers.js",
419
- to_id=repo_id,
420
  token=token,
421
  exist_ok=True
422
  )
 
423
  except Exception as e:
424
  # If template duplication fails, fall back to regular create
425
  print(f"[Deploy] Template duplication failed, creating regular static space: {e}")
 
 
426
  api.create_repo(
427
  repo_id=repo_id,
428
  repo_type="space",
 
324
  use_individual_uploads = False # Flag for transformers.js
325
 
326
  if language == "transformers.js":
327
+ try:
328
+ files = parse_transformers_js_output(code)
329
+ print(f"[Deploy] Parsed transformers.js files: {list(files.keys())}")
330
+
331
+ # Validate all three files are present (match original deploy.py check)
332
+ if not files['index.html'] or not files['index.js'] or not files['style.css']:
333
+ return False, "Error: Could not parse transformers.js output. Please regenerate the code.", None
334
+
335
+ # Write transformers.js files
336
+ for filename, content in files.items():
337
+ print(f"[Deploy] Writing {filename} ({len(content)} chars)")
338
+ (temp_path / filename).write_text(content, encoding='utf-8')
339
+
340
+ # For transformers.js, we'll upload files individually (not via upload_folder)
341
+ use_individual_uploads = True
342
+
343
+ except Exception as e:
344
+ print(f"[Deploy] Error parsing transformers.js: {e}")
345
+ import traceback
346
+ traceback.print_exc()
347
+ return False, f"Error parsing transformers.js output: {str(e)}", None
348
 
349
  elif language == "html":
350
  html_code = parse_html_code(code)
 
420
  try:
421
  if language == "transformers.js":
422
  # For transformers.js, duplicate the template space
423
+ print(f"[Deploy] Creating transformers.js space: {repo_id}")
 
424
  try:
425
+ from huggingface_hub import duplicate_space
426
+
427
+ # IMPORTANT: duplicate_space expects just the space name, not the full repo_id
428
+ # It will automatically prepend the username
429
+ print(f"[Deploy] Attempting to duplicate template space to: {space_name}")
430
+ result = duplicate_space(
431
  from_id="static-templates/transformers.js",
432
+ to_id=space_name, # Just the space name, not username/space-name
433
  token=token,
434
  exist_ok=True
435
  )
436
+ print(f"[Deploy] Template duplication result: {result}")
437
  except Exception as e:
438
  # If template duplication fails, fall back to regular create
439
  print(f"[Deploy] Template duplication failed, creating regular static space: {e}")
440
+ import traceback
441
+ traceback.print_exc()
442
  api.create_repo(
443
  repo_id=repo_id,
444
  repo_type="space",