Spaces:
Sleeping
Fix backend_deploy: Let HuggingFace auto-create README, only add anycoder tag
Browse filesIssue: Previously was creating README with hardcoded SDK version (4.44.1)
Fix: Follow the original deploy.py pattern
Changes:
- Removed create_minimal_readme() function - not needed
- Don't manually create or upload README.md during deployment
- Let HuggingFace automatically generate README.md with proper defaults
- After deployment, only modify the auto-generated README to add:
* tags: anycoder (preserves all existing content)
* app_port: 7860 (only for Docker spaces)
Deployment flow now:
1. Parse code based on language (html, gradio, streamlit, react, etc.)
2. Write code files to temp directory (no README)
3. Create space with create_repo() - HF auto-generates README
4. Upload files with upload_folder()
5. Wait 2 seconds for README generation
6. Call add_anycoder_tag_to_readme() to modify existing README
7. Preserves all HF defaults (versions, metadata, etc.)
This ensures:
- No hardcoded versions (HF uses latest compatible versions)
- Existing README content is preserved
- Only adds anycoder attribution tag
- Works with all SDK types (static, gradio, docker)
134 unmodified lines13513613713813914014114214314414514614714814915015115215315415515615715815916016116216316494 unmodified lines2592602612622632641 unmodified line26626726826927027127227327427527627727827928028128228328428528628728828929029113 unmodified lines3053063073083093103113123133146 unmodified lines3213223233243253263273283293304 unmodified lines33533633733833934034134234334422 unmodified lines367368369370371372134 unmodified linesreturn "gradio" # Defaultdef create_space_readme(space_name: str, sdk: str, description: str = None) -> str:"""Create README.md for HuggingFace Space"""readme = f"""---title: {space_name}emoji: 🚀colorFrom: bluecolorTo: purplesdk: {sdk}sdk_version: 4.44.1app_file: app.pypinned: false"""if sdk == "docker":readme += "app_port: 7860\n"readme += "---\n\n"if description:readme += f"{description}\n\n"else:readme += f"# {space_name}\n\nBuilt with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)\n"return readmedef create_dockerfile_for_streamlit(space_name: str) -> str:94 unmodified linestemp_path = Path(temp_dir)# Parse code based on languageif language == "transformers.js":files = parse_transformers_js_output(code)1 unmodified linefor filename, content in files.items():(temp_path / filename).write_text(content, encoding='utf-8')# Create READMEreadme = create_space_readme(space_name, "static", description)(temp_path / "README.md").write_text(readme, encoding='utf-8')elif language == "html":html_code = parse_html_code(code)(temp_path / "index.html").write_text(html_code, encoding='utf-8')# Create READMEreadme = create_space_readme(space_name, "static", description)(temp_path / "README.md").write_text(readme, encoding='utf-8')elif language == "comfyui":# ComfyUI is JSON, wrap in HTML viewer(temp_path / "index.html").write_text(code, encoding='utf-8')# Create READMEreadme = create_space_readme(space_name, "static", description)(temp_path / "README.md").write_text(readme, encoding='utf-8')elif language in ["gradio", "streamlit"]:files = parse_multi_file_python_output(code)13 unmodified linesif language == "streamlit":dockerfile = create_dockerfile_for_streamlit(space_name)(temp_path / "Dockerfile").write_text(dockerfile, encoding='utf-8')# Create READMEreadme = create_space_readme(space_name, sdk, description)(temp_path / "README.md").write_text(readme, encoding='utf-8')elif language == "react":# For React, we'd need package.json and other files6 unmodified lines# Create Dockerfiledockerfile = create_dockerfile_for_react(space_name)(temp_path / "Dockerfile").write_text(dockerfile, encoding='utf-8')# Create READMEreadme = create_space_readme(space_name, "docker", description)(temp_path / "README.md").write_text(readme, encoding='utf-8')else:# Default: treat as Gradio app4 unmodified linesif "requirements.txt" not in files:(temp_path / "requirements.txt").write_text("gradio>=4.0.0\n", encoding='utf-8')# Create READMEreadme = create_space_readme(space_name, "gradio", description)(temp_path / "README.md").write_text(readme, encoding='utf-8')# Create the spacetry:22 unmodified linesexcept Exception as e:return False, f"Failed to upload files: {str(e)}", Nonespace_url = f"https://huggingface.co/spaces/{repo_id}"return True, f"✅ Successfully deployed to {repo_id}!", space_url134 unmodified lines13513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121294 unmodified lines3073083093103113123133141 unmodified line31631731831932032132232332432532632732832913 unmodified lines3433443453463473483496 unmodified lines3563573583593603613624 unmodified lines36736836937037137237337437522 unmodified lines398399400401402403404405406407408409410411412413414134 unmodified linesreturn "gradio" # Defaultdef add_anycoder_tag_to_readme(api, repo_id: str, app_port: Optional[int] = None) -> None:"""Download existing README, add anycoder tag and app_port if needed, and upload back.Preserves all existing README content and frontmatter.Args:api: HuggingFace API clientrepo_id: Repository ID (username/space-name)app_port: Optional port number to set for Docker spaces (e.g., 7860)"""try:import tempfileimport re# Download the existing READMEreadme_path = api.hf_hub_download(repo_id=repo_id,filename="README.md",repo_type="space")# Read the existing README contentwith open(readme_path, 'r', encoding='utf-8') as f:content = f.read()# Parse frontmatter and contentif content.startswith('---'):# Split frontmatter and bodyparts = content.split('---', 2)if len(parts) >= 3:frontmatter = parts[1].strip()body = parts[2] if len(parts) > 2 else ""# Check if tags already existif 'tags:' in frontmatter:# Add anycoder to existing tags if not presentif '- anycoder' not in frontmatter:frontmatter = re.sub(r'(tags:\s*\n(?:\s*-\s*[^\n]+\n)*)', r'\1- anycoder\n', frontmatter)else:# Add tags section with anycoderfrontmatter += '\ntags:\n- anycoder'# Add app_port if specified and not already presentif app_port is not None and 'app_port:' not in frontmatter:frontmatter += f'\napp_port: {app_port}'# Reconstruct the READMEnew_content = f"---\n{frontmatter}\n---{body}"else:# Malformed frontmatter, just add tags at the end of frontmatternew_content = content.replace('---', '---\ntags:\n- anycoder\n---', 1)else:# No frontmatter, add it at the beginningapp_port_line = f'\napp_port: {app_port}' if app_port else ''new_content = f"---\ntags:\n- anycoder{app_port_line}\n---\n\n{content}"# Upload the modified READMEwith tempfile.NamedTemporaryFile("w", suffix=".md", delete=False, encoding='utf-8') as f:f.write(new_content)temp_path = f.nameapi.upload_file(path_or_fileobj=temp_path,path_in_repo="README.md",repo_id=repo_id,repo_type="space")os.unlink(temp_path)except Exception as e:print(f"Warning: Could not modify README.md to add anycoder tag: {e}")def create_dockerfile_for_streamlit(space_name: str) -> str:94 unmodified linestemp_path = Path(temp_dir)# 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)1 unmodified linefor 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')elif language == "comfyui":# ComfyUI is JSON, wrap in HTML viewer(temp_path / "index.html").write_text(code, encoding='utf-8')elif language in ["gradio", "streamlit"]:files = parse_multi_file_python_output(code)13 unmodified linesif language == "streamlit":dockerfile = 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 files6 unmodified lines# 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 app4 unmodified linesif "requirements.txt" not in files:(temp_path / "requirements.txt").write_text("gradio>=4.0.0\n", encoding='utf-8')# Don't create README - HuggingFace will auto-generate it# We'll add the anycoder tag after deployment# Create the spacetry:22 unmodified linesexcept Exception as e:return False, f"Failed to upload files: {str(e)}", None# After successful upload, modify the auto-generated README to add anycoder tag# HuggingFace automatically creates README.md when space is created# Wait a moment for it to be generated, then modify ittry:import timetime.sleep(2) # Give HF time to generate READMEadd_anycoder_tag_to_readme(api, repo_id, app_port)except Exception as e:# Don't fail deployment if README modification failsprint(f"Warning: Could not add anycoder tag to README: {e}")space_url = f"https://huggingface.co/spaces/{repo_id}"return True, f"✅ Successfully deployed to {repo_id}!", space_url