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 lines32132232332432532632732832933033133233333452 unmodified lines3873883893903913923933943953963973983991 unmodified line401402403404405406407408409410411412413414415416417320 unmodified lines# Parse code based on languageapp_port = None # Track if we need app_port for Docker spacesif language == "transformers.js":files = parse_transformers_js_output(code)# Write transformers.js filesfor filename, content in files.items():(temp_path / filename).write_text(content, encoding='utf-8')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: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 lineelse:return False, f"Failed to create space: {str(e)}", None# Upload all filesif not commit_message:commit_message = "Update from anycoder" if is_update else "Deploy from anycoder"try: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)}", None320 unmodified lines32132232332432532632732832933033133233333433533633733833934034134252 unmodified lines3953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294301 unmodified line432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485320 unmodified lines# Parse code based on languageapp_port = None # Track if we need app_port for Docker spacesuse_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)(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 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",space_sdk=sdk,private=private,exist_ok=False)else:# For other languages, create space normallyapi.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 lineelse:return False, f"Failed to create space: {str(e)}", None# Upload filesif 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 timefiles_to_upload = ["index.html", "index.js", "style.css"]max_attempts = 3for filename in files_to_upload:file_path = temp_path / filenameif not file_path.exists():return False, f"Failed to upload: {filename} not found", None# Upload with retry logic (like original)success = Falselast_error = Nonefor 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 = Truebreakexcept Exception as e:last_error = eif "403" in str(e) or "Forbidden" in str(e):return False, f"Permission denied uploading {filename}. Check your token has write access.", Noneif attempt < max_attempts - 1:time.sleep(2) # Wait before retryif not success:return False, f"Failed to upload {filename} after {max_attempts} attempts: {last_error}", Noneelse:# For other languages, use upload_folderapi.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