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 lines32432532632732832933033133233333433533633733833934034169 unmodified lines411412413414415416417418419420421422423424425426427428323 unmodified linesuse_individual_uploads = False # Flag for transformers.jsif language == "transformers.js":files = parse_transformers_js_output(code)# Validate all three files are presentif 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 filesfor 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 = Trueelif language == "html":html_code = parse_html_code(code)69 unmodified linestry:if language == "transformers.js":# For transformers.js, duplicate the template spacefrom huggingface_hub import duplicate_spacetry:duplicate_space(from_id="static-templates/transformers.js",to_id=repo_id,token=token,exist_ok=True)except Exception as e:# If template duplication fails, fall back to regular createprint(f"[Deploy] Template duplication failed, creating regular static space: {e}")api.create_repo(repo_id=repo_id,repo_type="space",323 unmodified lines32432532632732832933033133233333433533633733833934034134234334434534634734834935069 unmodified lines420421422423424425426427428429430431432433434435436437438439440441442443444323 unmodified linesuse_individual_uploads = False # Flag for transformers.jsif language == "transformers.js":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 filesfor 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 = Trueexcept Exception as e:print(f"[Deploy] Error parsing transformers.js: {e}")import tracebacktraceback.print_exc()return False, f"Error parsing transformers.js output: {str(e)}", Noneelif language == "html":html_code = parse_html_code(code)69 unmodified linestry:if language == "transformers.js":# For transformers.js, duplicate the template spaceprint(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 usernameprint(f"[Deploy] Attempting to duplicate template space to: {space_name}")result = duplicate_space(from_id="static-templates/transformers.js",to_id=space_name, # Just the space name, not username/space-nametoken=token,exist_ok=True)print(f"[Deploy] Template duplication result: {result}")except Exception as e:# If template duplication fails, fall back to regular createprint(f"[Deploy] Template duplication failed, creating regular static space: {e}")import tracebacktraceback.print_exc()api.create_repo(repo_id=repo_id,repo_type="space",