Add standalone backend_deploy.py without Gradio dependencies
Browse filesCreated backend_deploy.py as a standalone deployment utility that can be
used by the backend API without any Gradio dependencies. This module
provides clean deployment functions for HuggingFace Spaces.
Key features:
- deploy_to_huggingface_space(): Main deployment function supporting all
language types (HTML, Gradio, Streamlit, React, Transformers.js, ComfyUI)
- Automatic SDK detection and appropriate file structure generation
- Multi-file support (README, Dockerfile, requirements.txt, etc.)
- update_space_file(): Update individual files in existing spaces
- delete_space(): Delete spaces
- list_user_spaces(): List all user spaces
Code parsing utilities:
- parse_html_code(): Extract HTML from various formats
- parse_transformers_js_output(): Parse multi-file transformers.js output
- parse_python_requirements(): Extract requirements.txt
- parse_multi_file_python_output(): Parse Gradio/Streamlit multi-file apps
- is_streamlit_code() / is_gradio_code(): Code type detection
- detect_sdk_from_code(): Auto-detect appropriate HF Space SDK
File generators:
- create_space_readme(): Generate README.md with proper frontmatter
- create_dockerfile_for_streamlit(): Streamlit Dockerfile
- create_dockerfile_for_react(): React Dockerfile
Updated backend_api.py:
- Simplified /api/deploy endpoint to use backend_deploy module
- Reduced code duplication and improved maintainability
- Better error handling with user-friendly messages
Updated Dockerfile:
- Added COPY for backend_deploy.py
This completes the backend modularization, making deployment logic
reusable and maintainable without heavy UI dependencies.
Files changed (3)
- Dockerfile+1
- backend_api.py+36-122
- backend_deploy.pyAdded+501
54 unmodified lines5556575859606154 unmodified linesCOPY --chown=user:user anycoder_app/ ./anycoder_app/COPY --chown=user:user backend_api.py .COPY --chown=user:user backend_prompts.py .COPY --chown=user:user backend_deploy.py .COPY --chown=user:user app.py .
# Copy built frontend from builder stage510 unmodified lines51151251351451451551651751851952052152252352452552651751851952022 unmodified lines5435445455555565575465475485493 unmodified lines5535545555675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246255565575585595605615625635645655666276286296306316326336346356366376386396406416426436446456466476486496506516526536545675685695706585715726606616625735745755765775785795805815825835845855865875885895905915924 unmodified lines597598599673674675676677678679680681682683684685686687688689600601602603604605606510 unmodified lines # Check if this is dev mode (no real token) if auth.token and auth.token.startswith("dev_token_"): # In dev mode, open HF Spaces creation page import urllib.parse from backend_deploy import detect_sdk_from_code base_url = "https://huggingface.co/new-space" # Map language to SDK language_to_sdk = { "gradio": "gradio", "streamlit": "docker", "react": "docker", "html": "static", "transformers.js": "static", "comfyui": "static" } sdk = language_to_sdk.get(request.language, "gradio") sdk = detect_sdk_from_code(request.code, request.language) params = urllib.parse.urlencode({ "name": request.space_name or "my-anycoder-app",22 unmodified lines # Production mode with real OAuth token try: from huggingface_hub import HfApi import tempfile import uuid from backend_deploy import deploy_to_huggingface_space # Get user token - should be the access_token from OAuth session user_token = auth.token if auth.token else os.getenv("HF_TOKEN")3 unmodified lines print(f"[Deploy] Attempting deployment with token (first 10 chars): {user_token[:10]}...") # Create API client api = HfApi(token=user_token) # Get the actual username from HuggingFace API try: user_info = api.whoami() print(f"[Deploy] User info from HF API: {user_info}") username = user_info.get("name") or user_info.get("preferred_username") or auth.username or "user" except Exception as e: print(f"[Deploy] Failed to get user info from HF API: {e}") # Fallback to auth username if available username = auth.username if not username: raise HTTPException( status_code=401, detail="Failed to verify HuggingFace account. Please sign in again." ) # Generate space name if not provided space_name = request.space_name or f"anycoder-{uuid.uuid4().hex[:8]}" repo_id = f"{username}/{space_name}" print(f"[Deploy] Creating/updating space: {repo_id}") # Map language to SDK language_to_sdk = { "gradio": "gradio", "streamlit": "docker", "react": "docker", "html": "static", "transformers.js": "static", "comfyui": "static" } sdk = language_to_sdk.get(request.language, "gradio") # Create the space try: api.create_repo( repo_id=repo_id, repo_type="space", space_sdk=sdk, exist_ok=False ) except Exception as e: if "already exists" in str(e).lower(): # Space exists, we'll update it pass else: raise # Upload the code file if request.language in ["html", "transformers.js", "comfyui"]: file_name = "index.html" else: file_name = "app.py" with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f: f.write(request.code) temp_path = f.name # Use the standalone deployment function success, message, space_url = deploy_to_huggingface_space( code=request.code, language=request.language, space_name=request.space_name, token=user_token, username=auth.username, description=request.description if hasattr(request, 'description') else None, private=False ) try: api.upload_file( path_or_fileobj=temp_path, path_in_repo=file_name, repo_id=repo_id, repo_type="space" ) # For Gradio apps, also upload requirements.txt if needed if request.language == "gradio": # Simple requirements for basic Gradio app requirements = "gradio>=4.0.0\n" with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as req_f: req_f.write(requirements) req_temp_path = req_f.name try: api.upload_file( path_or_fileobj=req_temp_path, path_in_repo="requirements.txt", repo_id=repo_id, repo_type="space" ) finally: os.unlink(req_temp_path) space_url = f"https://huggingface.co/spaces/{repo_id}" if success: return { "success": True, "space_url": space_url, "message": f"✅ Deployed successfully to {repo_id}!" "message": message } finally: os.unlink(temp_path) else: # Provide user-friendly error message based on the error if "401" in message or "Unauthorized" in message: raise HTTPException( status_code=401, detail="Authentication failed. Please sign in again with HuggingFace." ) elif "403" in message or "Forbidden" in message or "Permission" in message: raise HTTPException( status_code=403, detail="Permission denied. Your HuggingFace token may not have the required permissions (manage-repos scope)." ) else: raise HTTPException( status_code=500, detail=message ) except HTTPException: # Re-raise HTTP exceptions as-is4 unmodified lines error_details = traceback.format_exc() print(f"[Deploy] Deployment error: {error_details}") # Provide user-friendly error message error_msg = str(e) if "401" in error_msg or "Unauthorized" in error_msg: raise HTTPException( status_code=401, detail="Authentication failed. Please sign in again with HuggingFace." ) elif "403" in error_msg or "Forbidden" in error_msg: raise HTTPException( status_code=403, detail="Permission denied. Your HuggingFace token may not have the required permissions (manage-repos scope)." ) else: raise HTTPException( status_code=500, detail=f"Deployment failed: {error_msg}" ) raise HTTPException( status_code=500, detail=f"Deployment failed: {str(e)}" )
@app.websocket("/ws/generate")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501"""Standalone deployment utilities for publishing to HuggingFace Spaces.No Gradio dependencies - can be used in backend API."""import osimport reimport jsonimport uuidimport tempfileimport shutilfrom typing import Dict, List, Optional, Tuplefrom pathlib import Path
from huggingface_hub import HfApi
def parse_html_code(code: str) -> str: """Extract HTML code from various formats""" code = code.strip() # If already clean HTML, return as-is if code.startswith('<!DOCTYPE') or code.startswith('<html'): return code # Try to extract from code blocks if '```html' in code: match = re.search(r'```html\s*(.*?)\s*```', code, re.DOTALL) if match: return match.group(1).strip() if '```' in code: match = re.search(r'```\s*(.*?)\s*```', code, re.DOTALL) if match: return match.group(1).strip() return code
def parse_transformers_js_output(code: str) -> Dict[str, str]: """Parse transformers.js output into separate files""" files = {} # Pattern to match file sections pattern = r'===\s*(\S+\.(?:html|js|css))\s*===\s*(.*?)(?====|$)' matches = re.finditer(pattern, code, re.DOTALL | re.IGNORECASE) for match in matches: filename = match.group(1).strip() content = match.group(2).strip() # Clean up code blocks if present content = re.sub(r'^```\w*\s*', '', content, flags=re.MULTILINE) content = re.sub(r'```\s*$', '', content, flags=re.MULTILINE) files[filename] = content # If no files were parsed, try to extract as single HTML file if not files: html_content = parse_html_code(code) if html_content: files['index.html'] = html_content return files
def parse_python_requirements(code: str) -> Optional[str]: """Extract requirements.txt content from code if present""" # Look for requirements.txt section req_pattern = r'===\s*requirements\.txt\s*===\s*(.*?)(?====|$)' match = re.search(req_pattern, code, re.DOTALL | re.IGNORECASE) if match: requirements = match.group(1).strip() # Clean up code blocks requirements = re.sub(r'^```\w*\s*', '', requirements, flags=re.MULTILINE) requirements = re.sub(r'```\s*$', '', requirements, flags=re.MULTILINE) return requirements return None
def parse_multi_file_python_output(code: str) -> Dict[str, str]: """Parse multi-file Python output (e.g., Gradio, Streamlit)""" files = {} # Pattern to match file sections pattern = r'===\s*(\S+\.(?:py|txt))\s*===\s*(.*?)(?====|$)' matches = re.finditer(pattern, code, re.DOTALL | re.IGNORECASE) for match in matches: filename = match.group(1).strip() content = match.group(2).strip() # Clean up code blocks if present content = re.sub(r'^```\w*\s*', '', content, flags=re.MULTILINE) content = re.sub(r'```\s*$', '', content, flags=re.MULTILINE) files[filename] = content # If no files were parsed, treat as single app.py if not files: # Clean up code blocks clean_code = re.sub(r'^```\w*\s*', '', code, flags=re.MULTILINE) clean_code = re.sub(r'```\s*$', '', clean_code, flags=re.MULTILINE) files['app.py'] = clean_code.strip() return files
def is_streamlit_code(code: str) -> bool: """Check if code is Streamlit""" return 'import streamlit' in code or 'streamlit.run' in code
def is_gradio_code(code: str) -> bool: """Check if code is Gradio""" return 'import gradio' in code or 'gr.' in code
def detect_sdk_from_code(code: str, language: str) -> str: """Detect the appropriate SDK from code and language""" if language == "html": return "static" elif language == "transformers.js": return "static" elif language == "comfyui": return "static" elif language == "react": return "docker" elif language == "streamlit" or is_streamlit_code(code): return "docker" elif language == "gradio" or is_gradio_code(code): return "gradio" else: return "gradio" # Default
def 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 readme
def create_dockerfile_for_streamlit(space_name: str) -> str: """Create Dockerfile for Streamlit app""" return f"""FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 7860
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]"""
def create_dockerfile_for_react(space_name: str) -> str: """Create Dockerfile for React app""" return f"""FROM node:18-slim
# Use existing node userUSER nodeENV HOME=/home/nodeENV PATH=/home/node/.local/bin:$PATH
WORKDIR /home/node/app
COPY --chown=node:node package*.json ./RUN npm install
COPY --chown=node:node . .RUN npm run build
EXPOSE 7860
CMD ["npm", "start", "--", "-p", "7860"]"""
def deploy_to_huggingface_space( code: str, language: str, space_name: Optional[str] = None, token: Optional[str] = None, username: Optional[str] = None, description: Optional[str] = None, private: bool = False) -> Tuple[bool, str, Optional[str]]: """ Deploy code to HuggingFace Spaces Args: code: Generated code to deploy language: Target language/framework (html, gradio, streamlit, react, transformers.js, comfyui) space_name: Name for the space (auto-generated if None) token: HuggingFace API token username: HuggingFace username description: Space description private: Whether to make the space private Returns: Tuple of (success: bool, message: str, space_url: Optional[str]) """ if not token: token = os.getenv("HF_TOKEN") if not token: return False, "No HuggingFace token provided", None try: api = HfApi(token=token) # Get username if not provided if not username: try: user_info = api.whoami() username = user_info.get("name") or user_info.get("preferred_username") or "user" except Exception as e: return False, f"Failed to get user info: {str(e)}", None # Generate space name if not provided if not space_name: space_name = f"anycoder-{uuid.uuid4().hex[:8]}" # Clean space name (no spaces, lowercase, alphanumeric + hyphens) space_name = re.sub(r'[^a-z0-9-]', '-', space_name.lower()) space_name = re.sub(r'-+', '-', space_name).strip('-') repo_id = f"{username}/{space_name}" # Detect SDK sdk = detect_sdk_from_code(code, language) # Create temporary directory for files with tempfile.TemporaryDirectory() as temp_dir: temp_path = Path(temp_dir) # Parse code based on language if language == "transformers.js": files = parse_transformers_js_output(code) # Write transformers.js files for filename, content in files.items(): (temp_path / filename).write_text(content, encoding='utf-8') # Create README readme = 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 README readme = 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 README readme = 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) # Write Python files for filename, content in files.items(): (temp_path / filename).write_text(content, encoding='utf-8') # Ensure requirements.txt exists if "requirements.txt" not in files: if language == "gradio": (temp_path / "requirements.txt").write_text("gradio>=4.0.0\n", encoding='utf-8') elif language == "streamlit": (temp_path / "requirements.txt").write_text("streamlit>=1.30.0\n", encoding='utf-8') # Create Dockerfile if needed if sdk == "docker": if language == "streamlit": dockerfile = create_dockerfile_for_streamlit(space_name) (temp_path / "Dockerfile").write_text(dockerfile, encoding='utf-8') # Create README readme = 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 files # This is more complex, so for now just create a placeholder files = parse_multi_file_python_output(code) for filename, content in files.items(): (temp_path / filename).write_text(content, encoding='utf-8') # Create Dockerfile dockerfile = create_dockerfile_for_react(space_name) (temp_path / "Dockerfile").write_text(dockerfile, encoding='utf-8') # Create README readme = create_space_readme(space_name, "docker", description) (temp_path / "README.md").write_text(readme, encoding='utf-8') else: # Default: treat as Gradio app files = 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') # Create README readme = create_space_readme(space_name, "gradio", description) (temp_path / "README.md").write_text(readme, encoding='utf-8') # Create the space 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, we'll update it pass else: return False, f"Failed to create space: {str(e)}", None # Upload all files try: api.upload_folder( folder_path=str(temp_path), repo_id=repo_id, repo_type="space", commit_message="Deploy from anycoder" ) except Exception as e: return False, f"Failed to upload files: {str(e)}", None space_url = f"https://huggingface.co/spaces/{repo_id}" return True, f"✅ Successfully deployed to {repo_id}!", space_url except Exception as e: return False, f"Deployment error: {str(e)}", None
def update_space_file( repo_id: str, file_path: str, content: str, token: Optional[str] = None, commit_message: Optional[str] = None) -> Tuple[bool, str]: """ Update a single file in an existing HuggingFace Space Args: repo_id: Full repo ID (username/space-name) file_path: Path of file to update (e.g., "app.py") content: New file content token: HuggingFace API token commit_message: Commit message (default: "Update {file_path}") Returns: Tuple of (success: bool, message: str) """ if not token: token = os.getenv("HF_TOKEN") if not token: return False, "No HuggingFace token provided" try: api = HfApi(token=token) if not commit_message: commit_message = f"Update {file_path}" # Create temporary file with tempfile.NamedTemporaryFile(mode='w', suffix=f'.{file_path.split(".")[-1]}', delete=False) as f: f.write(content) temp_path = f.name try: api.upload_file( path_or_fileobj=temp_path, path_in_repo=file_path, repo_id=repo_id, repo_type="space", commit_message=commit_message ) return True, f"✅ Successfully updated {file_path}" finally: os.unlink(temp_path) except Exception as e: return False, f"Failed to update file: {str(e)}"
def delete_space( repo_id: str, token: Optional[str] = None) -> Tuple[bool, str]: """ Delete a HuggingFace Space Args: repo_id: Full repo ID (username/space-name) token: HuggingFace API token Returns: Tuple of (success: bool, message: str) """ if not token: token = os.getenv("HF_TOKEN") if not token: return False, "No HuggingFace token provided" try: api = HfApi(token=token) api.delete_repo(repo_id=repo_id, repo_type="space") return True, f"✅ Successfully deleted {repo_id}" except Exception as e: return False, f"Failed to delete space: {str(e)}"
def list_user_spaces( username: Optional[str] = None, token: Optional[str] = None) -> Tuple[bool, str, Optional[List[Dict]]]: """ List all spaces for a user Args: username: HuggingFace username (gets from token if None) token: HuggingFace API token Returns: Tuple of (success: bool, message: str, spaces: Optional[List[Dict]]) """ if not token: token = os.getenv("HF_TOKEN") if not token: return False, "No HuggingFace token provided", None try: api = HfApi(token=token) # Get username if not provided if not username: user_info = api.whoami() username = user_info.get("name") or user_info.get("preferred_username") # List spaces spaces = api.list_spaces(author=username) space_list = [] for space in spaces: space_list.append({ "id": space.id, "author": space.author, "name": getattr(space, 'name', space.id.split('/')[-1]), "sdk": getattr(space, 'sdk', 'unknown'), "private": getattr(space, 'private', False), "url": f"https://huggingface.co/spaces/{space.id}" }) return True, f"Found {len(space_list)} spaces", space_list except Exception as e: return False, f"Failed to list spaces: {str(e)}", None