Spaces:
Build error
Build error
| #!/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) | |