#!/usr/bin/env python3 """ AudioForge Environment Setup Script Helps configure .env file with Hugging Face token and other settings """ import os import sys from pathlib import Path from typing import Optional def get_input(prompt: str, default: Optional[str] = None, required: bool = False) -> str: """Get user input with optional default value.""" if default: prompt = f"{prompt} [{default}]: " else: prompt = f"{prompt}: " while True: value = input(prompt).strip() if not value and default: return default if not value and required: print("āŒ This field is required!") continue return value def generate_secret_key() -> str: """Generate a secure random secret key.""" import secrets return secrets.token_urlsafe(32) def main(): """Main setup function.""" print("šŸŽµ AudioForge Environment Setup") print("=" * 60) print() # Determine paths script_dir = Path(__file__).parent project_root = script_dir.parent backend_dir = project_root / "backend" env_file = backend_dir / ".env" env_example = backend_dir / ".env.example" # Check if .env already exists if env_file.exists(): print(f"āš ļø .env file already exists at: {env_file}") overwrite = get_input("Do you want to overwrite it? (yes/no)", default="no") if overwrite.lower() not in ["yes", "y"]: print("āŒ Setup cancelled.") sys.exit(0) print() print("šŸ“ Let's configure your environment variables...") print() # ═══════════════════════════════════════════════════════════ # Hugging Face Token (MOST IMPORTANT) # ═══════════════════════════════════════════════════════════ print("šŸ¤— HUGGING FACE TOKEN (REQUIRED)") print("-" * 60) print("You need a Hugging Face token to download AI models.") print("Get your token from: https://huggingface.co/settings/tokens") print() hf_token = get_input( "Enter your Hugging Face token", required=True ) print() # ═══════════════════════════════════════════════════════════ # Environment Type # ═══════════════════════════════════════════════════════════ print("šŸŒ ENVIRONMENT TYPE") print("-" * 60) environment = get_input( "Environment (development/staging/production)", default="development" ) print() # ═══════════════════════════════════════════════════════════ # Database Configuration # ═══════════════════════════════════════════════════════════ print("šŸ—„ļø DATABASE CONFIGURATION") print("-" * 60) if environment == "production": # Production: User should provide their production database URL # Default shown is for reference only database_url = get_input( "Database URL", default="postgresql+asyncpg://postgres:postgres@localhost:5432/audioforge", required=True ) else: # Development uses Docker port 5433 (mapped from container's 5432) database_url = "postgresql+asyncpg://postgres:postgres@localhost:5433/audioforge" print(f"Using default: {database_url}") print(" (Docker container exposes PostgreSQL on port 5433)") print() # ═══════════════════════════════════════════════════════════ # Redis Configuration # ═══════════════════════════════════════════════════════════ print("šŸ“¦ REDIS CONFIGURATION") print("-" * 60) redis_url = get_input( "Redis URL", default="redis://localhost:6379/0" ) print() # ═══════════════════════════════════════════════════════════ # Device Configuration # ═══════════════════════════════════════════════════════════ print("šŸ–„ļø DEVICE CONFIGURATION") print("-" * 60) print("Device options: cpu, cuda (NVIDIA GPU), mps (Apple Silicon)") # Check if CUDA is available try: import torch if torch.cuda.is_available(): print("āœ… CUDA detected! GPU acceleration available.") default_device = "cuda" else: print("ā„¹ļø No CUDA detected. Using CPU.") default_device = "cpu" except ImportError: print("ā„¹ļø PyTorch not installed yet. Defaulting to CPU.") default_device = "cpu" device = get_input( "Device for AI models", default=default_device ) print() # ═══════════════════════════════════════════════════════════ # CORS Configuration # ═══════════════════════════════════════════════════════════ print("🌐 CORS CONFIGURATION") print("-" * 60) if environment == "production": allowed_origins = get_input( "Allowed origins (comma-separated)", default="https://yourdomain.com" ) else: allowed_origins = "http://localhost:3000,http://localhost:3001" print(f"Using default: {allowed_origins}") print() # ═══════════════════════════════════════════════════════════ # Secret Key # ═══════════════════════════════════════════════════════════ print("šŸ” SECRET KEY") print("-" * 60) secret_key = generate_secret_key() print(f"Generated secure secret key: {secret_key[:20]}...") print() # ═══════════════════════════════════════════════════════════ # Generate .env file # ═══════════════════════════════════════════════════════════ print("šŸ“ Generating .env file...") env_content = f"""# ═══════════════════════════════════════════════════════════ # AudioForge Backend Environment Configuration # Generated by setup_env.py on {Path(__file__).stat().st_mtime} # ═══════════════════════════════════════════════════════════ # ───────────────────────────────────────────────────────── # Application Settings # ───────────────────────────────────────────────────────── DEBUG={'true' if environment == 'development' else 'false'} ENVIRONMENT={environment} LOG_LEVEL={'DEBUG' if environment == 'development' else 'INFO'} SECRET_KEY={secret_key} # ───────────────────────────────────────────────────────── # Database Configuration # ───────────────────────────────────────────────────────── DATABASE_URL={database_url} # ───────────────────────────────────────────────────────── # Redis Configuration # ───────────────────────────────────────────────────────── REDIS_URL={redis_url} # ───────────────────────────────────────────────────────── # AI Models Configuration # ───────────────────────────────────────────────────────── # Hugging Face Token (REQUIRED) HUGGINGFACE_TOKEN={hf_token} HF_TOKEN={hf_token} # Device configuration MUSICGEN_DEVICE={device} BARK_DEVICE={device} DEMUCS_DEVICE={device} # Model versions MUSICGEN_MODEL=facebook/musicgen-small BARK_MODEL=suno/bark-small DEMUCS_MODEL=htdemucs # ───────────────────────────────────────────────────────── # API Configuration # ───────────────────────────────────────────────────────── API_V1_PREFIX=/api/v1 ALLOWED_ORIGINS={allowed_origins} # ───────────────────────────────────────────────────────── # Audio Processing Settings # ───────────────────────────────────────────────────────── MAX_AUDIO_DURATION=300 DEFAULT_AUDIO_DURATION=30 AUDIO_SAMPLE_RATE=32000 # ───────────────────────────────────────────────────────── # Feature Flags # ───────────────────────────────────────────────────────── ENABLE_VOCALS=true ENABLE_MASTERING=true ENABLE_STEM_SEPARATION=true # ───────────────────────────────────────────────────────── # Monitoring (Optional) # ───────────────────────────────────────────────────────── ENABLE_METRICS=true # Sentry (uncomment and configure if needed) # SENTRY_DSN=your-sentry-dsn # SENTRY_ENVIRONMENT={environment} """ # Write .env file env_file.write_text(env_content, encoding="utf-8") print("āœ… .env file created successfully!") print() print("=" * 60) print("šŸŽ‰ Setup Complete!") print("=" * 60) print() print(f"šŸ“ Configuration saved to: {env_file}") print() print("šŸ“‹ Next Steps:") print(" 1. Review the .env file and adjust if needed") print(" 2. Install dependencies: cd backend && pip install -e '.[dev]'") print(" 3. Initialize database: python scripts/init_db.py") print(" 4. Start the backend: uvicorn app.main:app --reload") print() print("šŸ¤— Your Hugging Face token is configured!") print(" Models will download automatically on first use.") print() print("🐼⚔ Ready to forge some audio!") if __name__ == "__main__": try: main() except KeyboardInterrupt: print("\n\nāŒ Setup cancelled by user.") sys.exit(1) except Exception as e: print(f"\n\nāŒ Error during setup: {e}") sys.exit(1)