File size: 9,272 Bytes
3c3f63e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/bin/bash
# Setup script for BackgroundFX Pro
# Handles environment setup, dependency installation, and initial configuration

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Configuration
PYTHON_VERSION="3.10"
VENV_NAME="venv"
PROJECT_ROOT=$(dirname $(dirname $(realpath $0)))
MODELS_DIR="$HOME/.backgroundfx/models"
CONFIG_DIR="$HOME/.backgroundfx/config"

# Functions
print_header() {
    echo -e "\n${BLUE}========================================${NC}"
    echo -e "${BLUE}$1${NC}"
    echo -e "${BLUE}========================================${NC}\n"
}

print_success() {
    echo -e "${GREEN}βœ“ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠ $1${NC}"
}

print_error() {
    echo -e "${RED}βœ— $1${NC}"
}

check_command() {
    if command -v $1 &> /dev/null; then
        return 0
    else
        return 1
    fi
}

# Check system requirements
check_requirements() {
    print_header "Checking System Requirements"
    
    # Check Python
    if check_command python$PYTHON_VERSION; then
        print_success "Python $PYTHON_VERSION found"
    else
        print_error "Python $PYTHON_VERSION not found"
        echo "Please install Python $PYTHON_VERSION or later"
        exit 1
    fi
    
    # Check pip
    if check_command pip3; then
        print_success "pip found"
    else
        print_error "pip not found"
        exit 1
    fi
    
    # Check git
    if check_command git; then
        print_success "git found"
    else
        print_warning "git not found - some features may not work"
    fi
    
    # Check FFmpeg
    if check_command ffmpeg; then
        print_success "FFmpeg found"
    else
        print_warning "FFmpeg not found - video processing will fail"
        echo "Install with:"
        echo "  Ubuntu/Debian: sudo apt-get install ffmpeg"
        echo "  macOS: brew install ffmpeg"
        echo "  Windows: Download from https://ffmpeg.org"
    fi
    
    # Check CUDA (optional)
    if check_command nvidia-smi; then
        print_success "NVIDIA GPU detected"
        nvidia-smi --query-gpu=name,memory.total --format=csv,noheader
        CUDA_AVAILABLE=true
    else
        print_warning "No NVIDIA GPU detected - will use CPU"
        CUDA_AVAILABLE=false
    fi
    
    # Check available memory
    if [[ "$OSTYPE" == "linux-gnu"* ]]; then
        MEM_GB=$(free -g | awk '/^Mem:/{print $2}')
    elif [[ "$OSTYPE" == "darwin"* ]]; then
        MEM_GB=$(($(sysctl -n hw.memsize) / 1024 / 1024 / 1024))
    else
        MEM_GB=0
    fi
    
    if [ $MEM_GB -ge 8 ]; then
        print_success "Memory: ${MEM_GB}GB (recommended: 8GB+)"
    else
        print_warning "Memory: ${MEM_GB}GB (recommended: 8GB+)"
    fi
    
    # Check disk space
    DISK_GB=$(df -BG . | awk 'NR==2 {print $4}' | sed 's/G//')
    if [ $DISK_GB -ge 20 ]; then
        print_success "Disk space: ${DISK_GB}GB available"
    else
        print_warning "Disk space: ${DISK_GB}GB (recommended: 20GB+)"
    fi
}

# Create virtual environment
setup_virtualenv() {
    print_header "Setting Up Virtual Environment"
    
    cd "$PROJECT_ROOT"
    
    if [ -d "$VENV_NAME" ]; then
        print_warning "Virtual environment already exists"
        read -p "Recreate? (y/n): " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            rm -rf "$VENV_NAME"
        else
            return
        fi
    fi
    
    python$PYTHON_VERSION -m venv "$VENV_NAME"
    print_success "Virtual environment created"
    
    # Activate virtual environment
    source "$VENV_NAME/bin/activate"
    
    # Upgrade pip
    pip install --upgrade pip setuptools wheel
    print_success "pip upgraded"
}

# Install dependencies
install_dependencies() {
    print_header "Installing Dependencies"
    
    source "$VENV_NAME/bin/activate"
    
    # Install PyTorch
    if [ "$CUDA_AVAILABLE" = true ]; then
        print_success "Installing PyTorch with CUDA support..."
        pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
    else
        print_success "Installing PyTorch (CPU only)..."
        pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
    fi
    
    # Install other requirements
    if [ -f "requirements.txt" ]; then
        print_success "Installing requirements..."
        pip install -r requirements.txt
    fi
    
    # Install development dependencies
    if [ -f "requirements-dev.txt" ]; then
        print_success "Installing development dependencies..."
        pip install -r requirements-dev.txt
    fi
    
    print_success "All dependencies installed"
}

# Create directories
setup_directories() {
    print_header "Creating Directory Structure"
    
    # Create model directory
    mkdir -p "$MODELS_DIR"
    print_success "Created models directory: $MODELS_DIR"
    
    # Create config directory
    mkdir -p "$CONFIG_DIR"
    print_success "Created config directory: $CONFIG_DIR"
    
    # Create local directories
    mkdir -p "$PROJECT_ROOT/uploads"
    mkdir -p "$PROJECT_ROOT/outputs"
    mkdir -p "$PROJECT_ROOT/logs"
    mkdir -p "$PROJECT_ROOT/temp"
    
    print_success "Created project directories"
}

# Download models
download_models() {
    print_header "Model Download"
    
    echo "Would you like to download the AI models now? (Recommended)"
    echo "This will download approximately 2-3GB of data."
    read -p "Download models? (y/n): " -n 1 -r
    echo
    
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        source "$VENV_NAME/bin/activate"
        
        # Create model download script
        cat > download_models.py << 'EOF'
import sys
sys.path.append('.')
from models import create_model_manager

print("Downloading essential models...")
manager = create_model_manager()

# Download essential models
models_to_download = ['rmbg-1.4', 'u2netp', 'modnet']

for model_id in models_to_download:
    print(f"Downloading {model_id}...")
    success = manager.downloader.download_model(model_id)
    if success:
        print(f"βœ“ {model_id} downloaded")
    else:
        print(f"βœ— Failed to download {model_id}")

print("\nModel download complete!")
print(f"Models stored in: {manager.models_dir}")
EOF
        
        python download_models.py
        rm download_models.py
        
        print_success "Models downloaded"
    else
        print_warning "Skipping model download - models will be downloaded on first use"
    fi
}

# Create configuration
create_config() {
    print_header "Creating Configuration"
    
    # Create default config
    cat > "$CONFIG_DIR/config.json" << EOF
{
    "device": "auto",
    "quality_preset": "high",
    "models_dir": "$MODELS_DIR",
    "max_memory_gb": 8,
    "enable_cache": true,
    "default_background": "blur",
    "output_format": "mp4",
    "log_level": "INFO"
}
EOF
    
    print_success "Configuration created: $CONFIG_DIR/config.json"
    
    # Create .env file
    if [ ! -f "$PROJECT_ROOT/.env" ]; then
        cp "$PROJECT_ROOT/docker/.env.example" "$PROJECT_ROOT/.env" 2>/dev/null || \
        cat > "$PROJECT_ROOT/.env" << EOF
# BackgroundFX Pro Environment Configuration
DEVICE=auto
MODEL_CACHE_DIR=$MODELS_DIR
LOG_LEVEL=INFO
GRADIO_SERVER_NAME=0.0.0.0
GRADIO_SERVER_PORT=7860
EOF
        print_success "Environment file created: .env"
    fi
}

# Setup CLI
setup_cli() {
    print_header "Setting Up CLI"
    
    source "$VENV_NAME/bin/activate"
    
    # Install CLI in development mode
    pip install -e .
    
    if check_command bgfx; then
        print_success "CLI installed successfully"
        echo "You can now use: bgfx --help"
    else
        print_warning "CLI installation may have failed"
    fi
}

# Test installation
test_installation() {
    print_header "Testing Installation"
    
    source "$VENV_NAME/bin/activate"
    
    # Test imports
    python -c "
import torch
import cv2
import numpy
print('βœ“ Core libraries imported successfully')
"
    
    # Test custom modules
    python -c "
from api import ProcessingPipeline
from models import ModelRegistry
print('βœ“ Custom modules imported successfully')
" 2>/dev/null || print_warning "Some custom modules may not be available"
    
    # Test GPU if available
    if [ "$CUDA_AVAILABLE" = true ]; then
        python -c "
import torch
if torch.cuda.is_available():
    print(f'βœ“ CUDA available: {torch.cuda.get_device_name(0)}')
else:
    print('βœ— CUDA not available in PyTorch')
"
    fi
}

# Main setup flow
main() {
    print_header "BackgroundFX Pro Setup"
    echo "This script will set up your development environment"
    echo "Project root: $PROJECT_ROOT"
    echo
    
    check_requirements
    setup_virtualenv
    install_dependencies
    setup_directories
    download_models
    create_config
    setup_cli
    test_installation
    
    print_header "Setup Complete!"
    echo -e "${GREEN}BackgroundFX Pro is ready to use!${NC}"
    echo
    echo "To activate the environment:"
    echo "  source $VENV_NAME/bin/activate"
    echo
    echo "To run the application:"
    echo "  python app.py"
    echo
    echo "To use the CLI:"
    echo "  bgfx --help"
    echo
    echo "To run tests:"
    echo "  pytest"
    echo
}

# Run main function
main