#!/usr/bin/env python import os import sys import subprocess from pathlib import Path import shutil def setup_environment(): """Set up the environment for the application.""" print("Setting up environment...") # Create .env file if it doesn't exist if not os.path.exists('.env'): shutil.copy('.env.example', '.env') print("Created .env file from .env.example. Please update with your settings.") # Create instance directory instance_path = os.getenv('INSTANCE_PATH', '/tmp/instance') Path(instance_path).mkdir(parents=True, exist_ok=True) print(f"Created instance directory at {instance_path}") # Create models directory if it doesn't exist models_dir = Path('app/models') models_dir.mkdir(parents=True, exist_ok=True) print("Created models directory") return True def setup_database(): """Set up the database for the application.""" print("Setting up database...") try: subprocess.run([sys.executable, 'manage.py', 'init-db'], check=True) print("Database initialized successfully.") return True except subprocess.CalledProcessError: print("Error initializing database. Please check your database configuration.") return False def download_yolo_models(): """Download YOLOv4 models for object detection.""" print("Downloading YOLOv4 models...") models_dir = Path('app/models') # Check if models already exist weights_path = models_dir / "yolov4.weights" config_path = models_dir / "yolov4.cfg" if weights_path.exists() and config_path.exists(): print("YOLOv4 models already exist. Skipping download.") return True try: # Download weights subprocess.run([ "wget", "https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights", "-O", str(weights_path) ], check=True) # Download config subprocess.run([ "wget", "https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4.cfg", "-O", str(config_path) ], check=True) print("YOLOv4 models downloaded successfully.") return True except subprocess.CalledProcessError: print("Error downloading YOLOv4 models. Please download them manually.") return False def main(): """Main setup function.""" print("Setting up Facebook Ad Analytics...") # Setup environment if not setup_environment(): print("Error setting up environment. Exiting.") return # Setup database if not setup_database(): print("Error setting up database. Exiting.") return # Download YOLO models if not download_yolo_models(): print("Error downloading YOLO models. You may need to download them manually.") print("\nSetup completed successfully!") print("To run the application, use: python manage.py run") print("For production deployment, use Docker: docker-compose up -d") if __name__ == "__main__": main()