#!/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()