Spaces:
Paused
Paused
| #!/usr/bin/env python3 | |
| """ | |
| FaceFusion 3.5.0 - HuggingFace Spaces Launcher | |
| """ | |
| import subprocess | |
| import sys | |
| import os | |
| from pathlib import Path | |
| def detect_device(): | |
| """ | |
| Detect GPU availability and return appropriate settings. | |
| Returns: | |
| tuple: (onnxruntime_provider, execution_provider) | |
| - onnxruntime_provider: For install.py ("cuda" or "default") | |
| - execution_provider: For facefusion.py ("cuda" or "cpu") | |
| """ | |
| try: | |
| import torch | |
| if torch.cuda.is_available(): | |
| gpu_name = torch.cuda.get_device_name(0) | |
| gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1024**3 | |
| cuda_version = torch.version.cuda | |
| print(f"✅ [INFO] GPU detected: {gpu_name}") | |
| print(f"✅ [INFO] GPU memory: {gpu_memory:.2f} GB") | |
| print(f"✅ [INFO] CUDA version: {cuda_version}") | |
| print("✅ [INFO] Using GPU acceleration (CUDA)") | |
| return "cuda", "cuda" | |
| else: | |
| print("ℹ️ [INFO] No GPU detected") | |
| print("ℹ️ [INFO] Using CPU mode") | |
| return "default", "cpu" | |
| except ImportError: | |
| print("⚠️ [WARNING] PyTorch not available, cannot detect GPU") | |
| print("ℹ️ [INFO] Falling back to CPU mode") | |
| return "default", "cpu" | |
| except Exception as e: | |
| print(f"⚠️ [WARNING] Error detecting device: {e}") | |
| print("ℹ️ [INFO] Falling back to CPU mode") | |
| return "default", "cpu" | |
| def main(): | |
| """ | |
| Main function to launch FaceFusion 3.5.0 on HuggingFace Spaces | |
| """ | |
| print("=" * 80) | |
| print("FaceFusion 3.5.0 - HuggingFace Spaces Launcher") | |
| print("=" * 80) | |
| # Change directory to FaceFusion installation | |
| facefusion_dir = Path("Original_facefusion-3.5.0") | |
| if not facefusion_dir.exists(): | |
| print(f"[ERROR] FaceFusion directory not found: {facefusion_dir}") | |
| print("[ERROR] Please ensure Original_facefusion-3.5.0 is uploaded to the Space") | |
| sys.exit(1) | |
| print(f"[INFO] Changing directory to: {facefusion_dir}") | |
| os.chdir(facefusion_dir) | |
| # Detect device (GPU or CPU) | |
| print("\n" + "=" * 80) | |
| print("DEVICE DETECTION") | |
| print("=" * 80) | |
| onnxruntime_provider, execution_provider = detect_device() | |
| # Step 1: Update pip (FaceFusion 3.5.0 recommends pip >= 25.0) | |
| print("\n" + "=" * 80) | |
| print("STEP 1: Updating pip") | |
| print("=" * 80) | |
| try: | |
| subprocess.run( | |
| [sys.executable, "-m", "pip", "install", "--upgrade", "pip>=25.0"], | |
| check=True, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT, | |
| text=True | |
| ) | |
| print("[SUCCESS] pip update completed") | |
| except subprocess.CalledProcessError as e: | |
| print(f"[ERROR] Failed to update pip: {e}") | |
| print(f"[ERROR] Output: {e.stdout if hasattr(e, 'stdout') else 'N/A'}") | |
| sys.exit(1) | |
| # Step 2: Run install.py with detected provider | |
| print("\n" + "=" * 80) | |
| print(f"STEP 2: Installing FaceFusion dependencies (onnxruntime: {onnxruntime_provider})") | |
| print("=" * 80) | |
| try: | |
| result = subprocess.run( | |
| [sys.executable, "install.py", "--onnxruntime", onnxruntime_provider, "--skip-conda"], | |
| check=True, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT, | |
| text=True | |
| ) | |
| print(result.stdout) | |
| print("[SUCCESS] FaceFusion installation completed") | |
| except subprocess.CalledProcessError as e: | |
| print(f"[ERROR] Failed to install FaceFusion: {e}") | |
| print(f"[ERROR] Output: {e.stdout if hasattr(e, 'stdout') else 'N/A'}") | |
| sys.exit(1) | |
| # Step 3: Launch FaceFusion UI with detected provider | |
| print("\n" + "=" * 80) | |
| print(f"STEP 3: Launching FaceFusion UI (execution-providers: {execution_provider})") | |
| print("=" * 80) | |
| try: | |
| # Run FaceFusion with detected execution provider | |
| subprocess.run( | |
| [ | |
| sys.executable, | |
| "facefusion.py", | |
| "run", | |
| "--execution-providers", execution_provider | |
| ], | |
| check=True | |
| ) | |
| except subprocess.CalledProcessError as e: | |
| print(f"[ERROR] Failed to launch FaceFusion: {e}") | |
| sys.exit(1) | |
| except KeyboardInterrupt: | |
| print("\n[INFO] FaceFusion stopped by user") | |
| sys.exit(0) | |
| if __name__ == "__main__": | |
| main() | |