Spaces:
Runtime error
Fix transformers.js deployment: Use space name not repo_id for duplicate_space()
Browse filesIssue: 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.
323 unmodified lines32432532632732832933033133233333433533633733832732832933033133233333433533633733833934034134234334434534634734834935069 unmodified lines420421422414415423424425426427428429417430431419432433434435436437438439440441442443444323 unmodified lines use_individual_uploads = False # Flag for transformers.js if language == "transformers.js": files = parse_transformers_js_output(code) # Validate all three files are present if not files.get('index.html') or not files.get('index.js') or not files.get('style.css'): return False, "Error: Could not parse transformers.js output. Missing index.html, index.js, or style.css", None # Write transformers.js files for filename, content in files.items(): (temp_path / filename).write_text(content, encoding='utf-8') # For transformers.js, we'll upload files individually (not via upload_folder) use_individual_uploads = True try: files = parse_transformers_js_output(code) print(f"[Deploy] Parsed transformers.js files: {list(files.keys())}") # Validate all three files are present (match original deploy.py check) if not files['index.html'] or not files['index.js'] or not files['style.css']: return False, "Error: Could not parse transformers.js output. Please regenerate the code.", None # Write transformers.js files for filename, content in files.items(): print(f"[Deploy] Writing {filename} ({len(content)} chars)") (temp_path / filename).write_text(content, encoding='utf-8') # For transformers.js, we'll upload files individually (not via upload_folder) use_individual_uploads = True except Exception as e: print(f"[Deploy] Error parsing transformers.js: {e}") import traceback traceback.print_exc() return False, f"Error parsing transformers.js output: {str(e)}", None elif language == "html": html_code = parse_html_code(code)69 unmodified lines try: if language == "transformers.js": # For transformers.js, duplicate the template space from huggingface_hub import duplicate_space print(f"[Deploy] Creating transformers.js space: {repo_id}") try: from huggingface_hub import duplicate_space # IMPORTANT: duplicate_space expects just the space name, not the full repo_id # It will automatically prepend the username print(f"[Deploy] Attempting to duplicate template space to: {space_name}") duplicate_space( result = duplicate_space( from_id="static-templates/transformers.js", to_id=repo_id, to_id=space_name, # Just the space name, not username/space-name token=token, exist_ok=True ) print(f"[Deploy] Template duplication result: {result}") except Exception as e: # If template duplication fails, fall back to regular create print(f"[Deploy] Template duplication failed, creating regular static space: {e}") import traceback traceback.print_exc() api.create_repo( repo_id=repo_id, repo_type="space",