OnyxMunk's picture
πŸ› Fix backend startup: Set correct working directory for uvicorn
23038c3
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()