Spaces:
Sleeping
Sleeping
| import subprocess | |
| import os | |
| import signal | |
| import sys | |
| import time | |
| def run_services(): | |
| """ | |
| Runs the FastAPI backend and Next.js frontend services for Hugging Face Spaces. | |
| """ | |
| # Environment variables for the services | |
| env = os.environ.copy() | |
| env["PORT"] = "7860" | |
| env["HOSTNAME"] = "0.0.0.0" | |
| print("π΅ Starting AudioForge DAW Services...") | |
| print("π§ Backend: FastAPI (Port 8000)") | |
| print("π¨ Frontend: Next.js (Port 7860)") | |
| print("=" * 50) | |
| # 1. Start Backend (FastAPI) | |
| print("π Starting FastAPI backend...") | |
| backend_process = subprocess.Popen( | |
| [sys.executable, "-m", "uvicorn", "backend.main:app", | |
| "--host", "127.0.0.1", | |
| "--port", "8000", | |
| "--workers", "1", | |
| "--loop", "asyncio"], | |
| env=env, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE, | |
| cwd="/app" # Set working directory to project root | |
| ) | |
| # Give backend time to start | |
| time.sleep(3) | |
| if backend_process.poll() is None: | |
| print(f"β Backend started successfully (PID: {backend_process.pid})") | |
| else: | |
| print("β Backend failed to start") | |
| stdout, stderr = backend_process.communicate() | |
| print("Backend stdout:", stdout.decode()) | |
| print("Backend stderr:", stderr.decode()) | |
| return | |
| # 2. Start Frontend (Next.js Standalone) | |
| print("π Starting Next.js frontend...") | |
| frontend_process = subprocess.Popen( | |
| ["node", "server.js"], | |
| env=env, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE | |
| ) | |
| # Give frontend time to start | |
| time.sleep(3) | |
| if frontend_process.poll() is None: | |
| print(f"β Frontend started successfully (PID: {frontend_process.pid})") | |
| print("=" * 50) | |
| print("π AudioForge is now running!") | |
| print("π Access your DAW at: https://your-space-name.hf.space") | |
| print("π API Documentation: https://your-space-name.hf.space/docs") | |
| print("=" * 50) | |
| else: | |
| print("β Frontend failed to start") | |
| stdout, stderr = frontend_process.communicate() | |
| print("Frontend stdout:", stdout.decode()) | |
| print("Frontend stderr:", stderr.decode()) | |
| return | |
| def signal_handler(sig, frame): | |
| print("\nπ Shutting down AudioForge services...") | |
| backend_process.terminate() | |
| frontend_process.terminate() | |
| print("β Services stopped") | |
| sys.exit(0) | |
| signal.signal(signal.SIGINT, signal_handler) | |
| signal.signal(signal.SIGTERM, signal_handler) | |
| # Wait for processes | |
| backend_process.wait() | |
| frontend_process.wait() | |
| if __name__ == "__main__": | |
| run_services() | |