Spaces:
Runtime error
Fix React and Streamlit deployment: Upload files individually like transformers.js
Browse filesIssue: React and Streamlit apps were using upload_folder() which doesn't match
the original deploy.py behavior, potentially causing issues with multi-file apps.
Root cause analysis from original deploy.py:
- Transformers.js: Individual file uploads ✅ (already fixed)
- React: Individual file uploads ❌ (was using upload_folder)
- Streamlit: Individual file uploads ❌ (was using upload_folder)
- HTML (static): upload_folder() for multi-file OR upload_file() for single
- Gradio: create_commit() with atomic ops for multi-file OR upload_file()
Changes to backend_deploy.py:
1. React deployment now uses individual uploads:
- Set use_individual_uploads = True for React apps
- Parses React output to get all files
- Uploads each file individually with retry logic
- Includes Dockerfile if missing (from template)
2. Streamlit deployment now uses individual uploads:
- Set use_individual_uploads = True for Streamlit (Docker mode)
- Uploads each file individually with retry logic
- Includes Dockerfile and requirements.txt
3. Enhanced subdirectory support:
- All file writes now create parent directories if needed
- Handles nested structures like pages/, components/, styles/
- Path handling works correctly on Windows and Unix
4. Improved individual upload logic:
- Dynamically scans temp directory for all files
- Handles subdirectories with proper path separators
- Converts backslashes to forward slashes for repo paths
- Works for any number of files (not hardcoded list)
5. Better logging:
- Shows how many files are being uploaded
- Lists all file paths being uploaded
- Per-file success messages
- Retry attempt logging
Upload behavior by language:
- transformers.js: Individual uploads (index.html, index.js, style.css)
- React: Individual uploads (all parsed files + Dockerfile)
- Streamlit: Individual uploads (all parsed files + Dockerfile + requirements.txt)
- HTML: upload_folder() (works fine for static sites)
- Gradio: upload_folder() (works fine for Gradio SDK)
- ComfyUI: upload_folder() (works fine for static)
This ensures React and Streamlit apps with multiple files, nested directories,
and Dockerfiles deploy correctly, matching the original deploy.py behavior.
347 unmodified lines3483493503513523533543553568 unmodified lines36536636736836937037137237337437537637737837938038138238338438538638738838939047 unmodified lines43843944044144244344444544644744844945011 unmodified lines4624634644654664671 unmodified line469470471472473474347 unmodified lineselif language in ["gradio", "streamlit"]:files = parse_multi_file_python_output(code)# Write Python filesfor filename, content in files.items():(temp_path / filename).write_text(content, encoding='utf-8')# Ensure requirements.txt existsif "requirements.txt" not in files:8 unmodified linesdockerfile = create_dockerfile_for_streamlit(space_name)(temp_path / "Dockerfile").write_text(dockerfile, encoding='utf-8')app_port = 7860 # Set app_port for Docker spaceselif language == "react":# For React, we'd need package.json and other files# This is more complex, so for now just create a placeholderfiles = parse_multi_file_python_output(code)for filename, content in files.items():(temp_path / filename).write_text(content, encoding='utf-8')# Create Dockerfiledockerfile = create_dockerfile_for_react(space_name)(temp_path / "Dockerfile").write_text(dockerfile, encoding='utf-8')app_port = 7860 # Set app_port for Docker spaceselse:# Default: treat as Gradio appfiles = parse_multi_file_python_output(code)for filename, content in files.items():(temp_path / filename).write_text(content, encoding='utf-8')if "requirements.txt" not in files:(temp_path / "requirements.txt").write_text("gradio>=4.0.0\n", encoding='utf-8')47 unmodified linestry: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", None11 unmodified linescommit_message=f"{commit_message} - {filename}")success = Truebreakexcept Exception as e:last_error = e1 unmodified linereturn 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}", None347 unmodified lines3483493503513523533543553563573588 unmodified lines36736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340447 unmodified lines45245345445545645745845946046146246346446546646746846947047147247347447547647711 unmodified lines4894904914924934944951 unmodified line497498499500501502503347 unmodified lineselif language in ["gradio", "streamlit"]:files = parse_multi_file_python_output(code)# Write Python files (create subdirectories if needed)for filename, content in files.items():file_path = temp_path / filenamefile_path.parent.mkdir(parents=True, exist_ok=True)file_path.write_text(content, encoding='utf-8')# Ensure requirements.txt existsif "requirements.txt" not in files:8 unmodified linesdockerfile = create_dockerfile_for_streamlit(space_name)(temp_path / "Dockerfile").write_text(dockerfile, encoding='utf-8')app_port = 7860 # Set app_port for Docker spacesuse_individual_uploads = True # Streamlit uses individual file uploadselif language == "react":# Parse React output to get all files (uses same multi-file format as Python)files = parse_multi_file_python_output(code)if not files:return False, "Error: Could not parse React output", None# If Dockerfile is missing, use templateif 'Dockerfile' not in files:dockerfile = create_dockerfile_for_react(space_name)files['Dockerfile'] = dockerfile# Write all React files (create subdirectories if needed)for filename, content in files.items():file_path = temp_path / filenamefile_path.parent.mkdir(parents=True, exist_ok=True)file_path.write_text(content, encoding='utf-8')app_port = 7860 # Set app_port for Docker spacesuse_individual_uploads = True # React uses individual file uploadselse:# Default: treat as Gradio appfiles = parse_multi_file_python_output(code)# Write files (create subdirectories if needed)for filename, content in files.items():file_path = temp_path / filenamefile_path.parent.mkdir(parents=True, exist_ok=True)file_path.write_text(content, encoding='utf-8')if "requirements.txt" not in files:(temp_path / "requirements.txt").write_text("gradio>=4.0.0\n", encoding='utf-8')47 unmodified linestry:if use_individual_uploads:# For transformers.js, React, Streamlit: upload each file individually (matches original deploy.py)import time# Get list of files to upload from temp directoryfiles_to_upload = []for file_path in temp_path.rglob('*'):if file_path.is_file():# Get relative path from temp directory (use forward slashes for repo paths)rel_path = file_path.relative_to(temp_path)files_to_upload.append(str(rel_path).replace('\\', '/'))if not files_to_upload:return False, "No files to upload", Noneprint(f"[Deploy] Uploading {len(files_to_upload)} files individually: {files_to_upload}")max_attempts = 3for filename in files_to_upload:# Convert back to Path for filesystem operationsfile_path = temp_path / filename.replace('/', os.sep)if not file_path.exists():return False, f"Failed to upload: {filename} not found", None11 unmodified linescommit_message=f"{commit_message} - {filename}")success = Trueprint(f"[Deploy] Successfully uploaded {filename}")breakexcept Exception as e:last_error = e1 unmodified linereturn False, f"Permission denied uploading {filename}. Check your token has write access.", Noneif attempt < max_attempts - 1:time.sleep(2) # Wait before retryprint(f"[Deploy] Retry {attempt + 1}/{max_attempts} for {filename}")if not success:return False, f"Failed to upload {filename} after {max_attempts} attempts: {last_error}", None