Fix transformers.js deployment: Upload all files (html, js, css) individually
Browse filesIssue: Transformers.js apps were not deploying correctly - CSS and JavaScript
files were missing from the deployed space.
Root cause: backend_deploy.py was using upload_folder() for all file types,
but transformers.js requires individual file uploads like the original deploy.py.
Changes to backend_deploy.py:
1. Added validation for transformers.js files:
- Check all three files exist (index.html, index.js, style.css)
- Return error if any file is missing
2. Template space duplication (NEW):
- Use duplicate_space() from static-templates/transformers.js template
- Matches original deploy.py behavior
- Falls back to create_repo() if duplication fails
3. Individual file upload for transformers.js:
- Added use_individual_uploads flag
- Upload each file separately using api.upload_file()
- NOT using upload_folder() for transformers.js
4. Retry logic with error handling:
- 3 attempts per file
- 2-second delay between retries
- Proper permission error detection
- User-friendly error messages
5. Separate commit messages:
- Each file gets its own commit: "{message} - {filename}"
- Preserves git history for each file
Upload flow for transformers.js:
1. Parse code to extract index.html, index.js, style.css
2. Validate all three files are present
3. Duplicate template space (or create static space)
4. Upload index.html individually (with retry)
5. Upload index.js individually (with retry)
6. Upload style.css individually (with retry)
7. Add anycoder tag to README
This matches the original deploy.py pattern where transformers.js files
are uploaded one at a time rather than as a folder.
Other languages (html, gradio, streamlit, react) continue to use
upload_folder() which works fine for them.
320 unmodified lines32132232332432532632732832933033133233333433533633733833934034134252 unmodified lines3953963973983994004014024034044054064074084094104113903913923933943953964124134144154164174184194204214224234244254264274284294301 unmodified line432433434404435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476409410411412413414477478479480481482483484485320 unmodified lines # Parse code based on language app_port = None # Track if we need app_port for Docker spaces 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 elif language == "html": html_code = parse_html_code(code) (temp_path / "index.html").write_text(html_code, encoding='utf-8')52 unmodified lines # Create the space (only for new deployments) if not is_update: try: if language == "transformers.js": # For transformers.js, duplicate the template space from huggingface_hub import duplicate_space try: 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 create print(f"[Deploy] Template duplication failed, creating regular static space: {e}") api.create_repo( repo_id=repo_id, repo_type="space", space_sdk=sdk, private=private, exist_ok=False ) api.create_repo( repo_id=repo_id, repo_type="space", space_sdk=sdk, private=private, exist_ok=False ) else: # For other languages, create space normally api.create_repo( repo_id=repo_id, repo_type="space", space_sdk=sdk, private=private, exist_ok=False ) except Exception as e: if "already exists" in str(e).lower(): # Space exists, treat as update1 unmodified line else: return False, f"Failed to create space: {str(e)}", None # Upload all files # Upload files if not commit_message: commit_message = "Update from anycoder" if is_update else "Deploy from anycoder" try: if use_individual_uploads: # For transformers.js, upload each file individually (matches original deploy.py) import time files_to_upload = ["index.html", "index.js", "style.css"] max_attempts = 3 for filename in files_to_upload: file_path = temp_path / filename if not file_path.exists(): return False, f"Failed to upload: {filename} not found", None # Upload with retry logic (like original) success = False last_error = None for attempt in range(max_attempts): try: api.upload_file( path_or_fileobj=str(file_path), path_in_repo=filename, repo_id=repo_id, repo_type="space", commit_message=f"{commit_message} - {filename}" ) success = True break except Exception as e: last_error = e if "403" in str(e) or "Forbidden" in str(e): return False, f"Permission denied uploading {filename}. Check your token has write access.", None if attempt < max_attempts - 1: time.sleep(2) # Wait before retry if not success: return False, f"Failed to upload {filename} after {max_attempts} attempts: {last_error}", None else: # For other languages, use upload_folder api.upload_folder( folder_path=str(temp_path), repo_id=repo_id, repo_type="space", commit_message=commit_message ) api.upload_folder( folder_path=str(temp_path), repo_id=repo_id, repo_type="space", commit_message=commit_message ) except Exception as e: return False, f"Failed to upload files: {str(e)}", None