File size: 3,247 Bytes
092e58d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/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()