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 lines3483493503513513523533543533553563573588 unmodified lines36736836937037137237037137337437537637737837938038138238338438538638737538838937737837939039139239339439539639739839940038740140240340447 unmodified lines45245345444145545644345745845946046146246346446546646746846947047147247344747447547647711 unmodified lines4894904914924934944951 unmodified line497498499500501502503347 unmodified lines elif language in ["gradio", "streamlit"]: files = parse_multi_file_python_output(code) # Write Python files # Write Python files (create subdirectories if needed) for filename, content in files.items(): file_path = temp_path / filename file_path.parent.mkdir(parents=True, exist_ok=True) (temp_path / filename).write_text(content, encoding='utf-8') file_path.write_text(content, encoding='utf-8') # Ensure requirements.txt exists if "requirements.txt" not in files:8 unmodified lines dockerfile = create_dockerfile_for_streamlit(space_name) (temp_path / "Dockerfile").write_text(dockerfile, encoding='utf-8') app_port = 7860 # Set app_port for Docker spaces use_individual_uploads = True # Streamlit uses individual file uploads elif language == "react": # For React, we'd need package.json and other files # This is more complex, so for now just create a placeholder # 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 template if '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 / filename file_path.parent.mkdir(parents=True, exist_ok=True) (temp_path / filename).write_text(content, encoding='utf-8') file_path.write_text(content, encoding='utf-8') # Create Dockerfile dockerfile = create_dockerfile_for_react(space_name) (temp_path / "Dockerfile").write_text(dockerfile, encoding='utf-8') app_port = 7860 # Set app_port for Docker spaces use_individual_uploads = True # React uses individual file uploads else: # Default: treat as Gradio app files = parse_multi_file_python_output(code) # Write files (create subdirectories if needed) for filename, content in files.items(): file_path = temp_path / filename file_path.parent.mkdir(parents=True, exist_ok=True) (temp_path / filename).write_text(content, encoding='utf-8') 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 lines try: if use_individual_uploads: # For transformers.js, upload each file individually (matches original deploy.py) # For transformers.js, React, Streamlit: upload each file individually (matches original deploy.py) import time files_to_upload = ["index.html", "index.js", "style.css"] # Get list of files to upload from temp directory files_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", None print(f"[Deploy] Uploading {len(files_to_upload)} files individually: {files_to_upload}") max_attempts = 3 for filename in files_to_upload: # Convert back to Path for filesystem operations file_path = temp_path / filename file_path = temp_path / filename.replace('/', os.sep) if not file_path.exists(): return False, f"Failed to upload: {filename} not found", None 11 unmodified lines commit_message=f"{commit_message} - {filename}" ) success = True print(f"[Deploy] Successfully uploaded {filename}") break except Exception as e: last_error = e1 unmodified line 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 print(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