Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| """ | |
| ColorCraft SDXL Space Deployment Script | |
| Helps deploy your SDXL pipeline to HuggingFace Spaces | |
| """ | |
| import os | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| def check_dependencies(): | |
| """Check if required tools are installed""" | |
| try: | |
| import huggingface_hub | |
| print("β huggingface_hub is installed") | |
| except ImportError: | |
| print("β Installing huggingface_hub...") | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", "huggingface_hub"]) | |
| try: | |
| subprocess.run(["git", "--version"], capture_output=True, check=True) | |
| print("β Git is available") | |
| except (subprocess.CalledProcessError, FileNotFoundError): | |
| print("β Git is required. Please install Git first.") | |
| sys.exit(1) | |
| def setup_space(username, space_name, hf_token): | |
| """Create and setup HuggingFace Space""" | |
| from huggingface_hub import HfApi, create_repo | |
| # Initialize HF API | |
| api = HfApi(token=hf_token) | |
| # Create repository | |
| repo_id = f"{username}/{space_name}" | |
| try: | |
| print(f"π Creating HuggingFace Space: {repo_id}") | |
| create_repo( | |
| repo_id=repo_id, | |
| token=hf_token, | |
| repo_type="space", | |
| space_sdk="gradio", | |
| space_hardware="t4-medium", # GPU for SDXL | |
| private=False | |
| ) | |
| print(f"β Space created successfully!") | |
| except Exception as e: | |
| if "already exists" in str(e): | |
| print(f"βΉοΈ Space {repo_id} already exists, updating...") | |
| else: | |
| print(f"β Error creating space: {e}") | |
| sys.exit(1) | |
| return repo_id | |
| def deploy_files(repo_id, hf_token): | |
| """Deploy files to HuggingFace Space""" | |
| from huggingface_hub import HfApi | |
| api = HfApi(token=hf_token) | |
| space_dir = Path(__file__).parent | |
| files_to_upload = [ | |
| ("app.py", "app.py"), | |
| ("requirements.txt", "requirements.txt"), | |
| ("README.md", "README.md") | |
| ] | |
| print("π Uploading files to Space...") | |
| for local_file, remote_file in files_to_upload: | |
| local_path = space_dir / local_file | |
| if local_path.exists(): | |
| print(f" π€ Uploading {local_file}...") | |
| api.upload_file( | |
| path_or_fileobj=str(local_path), | |
| path_in_repo=remote_file, | |
| repo_id=repo_id, | |
| repo_type="space", | |
| token=hf_token | |
| ) | |
| else: | |
| print(f" β οΈ File not found: {local_file}") | |
| print("β All files uploaded successfully!") | |
| def update_backend_config(repo_id): | |
| """Update backend to use the deployed Space""" | |
| backend_dir = Path(__file__).parent.parent | |
| hf_client_path = backend_dir / "app" / "huggingface_client.py" | |
| if hf_client_path.exists(): | |
| print("π§ Updating backend configuration...") | |
| # Read current file | |
| with open(hf_client_path, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| # Replace placeholder URL with actual Space URL | |
| space_url = f"https://{repo_id.replace('/', '-')}.hf.space/api/predict" | |
| content = content.replace( | |
| "https://YOUR_USERNAME-colorcraft-sdxl.hf.space/api/predict", | |
| space_url | |
| ) | |
| # Write back | |
| with open(hf_client_path, 'w', encoding='utf-8') as f: | |
| f.write(content) | |
| print(f"β Backend updated to use: {space_url}") | |
| else: | |
| print("β οΈ Backend HuggingFace client not found") | |
| def main(): | |
| print("π¨ ColorCraft SDXL Space Deployment") | |
| print("=" * 50) | |
| # Check dependencies | |
| check_dependencies() | |
| # Get user input | |
| print("\nπ Configuration:") | |
| username = input("Enter your HuggingFace username: ").strip() | |
| space_name = input("Enter Space name (e.g., 'colorcraft-sdxl'): ").strip() | |
| hf_token = input("Enter your HuggingFace token: ").strip() | |
| if not all([username, space_name, hf_token]): | |
| print("β All fields are required!") | |
| sys.exit(1) | |
| # Setup and deploy | |
| try: | |
| repo_id = setup_space(username, space_name, hf_token) | |
| deploy_files(repo_id, hf_token) | |
| update_backend_config(repo_id) | |
| space_url = f"https://{repo_id.replace('/', '-')}.hf.space" | |
| print("\nπ Deployment Complete!") | |
| print("=" * 50) | |
| print(f"π Space URL: {space_url}") | |
| print(f"π Space Dashboard: https://huggingface.co/spaces/{repo_id}") | |
| print("\nπ Next Steps:") | |
| print("1. Visit your Space URL to verify it's working") | |
| print("2. Wait for the Space to build (5-10 minutes)") | |
| print("3. Test image processing through the interface") | |
| print("4. Your backend will now use SDXL quality!") | |
| print("\nπ Your ColorCraft app now has cloud SDXL processing!") | |
| except Exception as e: | |
| print(f"β Deployment failed: {e}") | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() | |