MogensR commited on
Commit
3c3f63e
·
1 Parent(s): f11fb39

Create scripts/setup.sh

Browse files
Files changed (1) hide show
  1. scripts/setup.sh +364 -0
scripts/setup.sh ADDED
@@ -0,0 +1,364 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ # Setup script for BackgroundFX Pro
3
+ # Handles environment setup, dependency installation, and initial configuration
4
+
5
+ set -e
6
+
7
+ # Colors for output
8
+ RED='\033[0;31m'
9
+ GREEN='\033[0;32m'
10
+ YELLOW='\033[1;33m'
11
+ BLUE='\033[0;34m'
12
+ NC='\033[0m' # No Color
13
+
14
+ # Configuration
15
+ PYTHON_VERSION="3.10"
16
+ VENV_NAME="venv"
17
+ PROJECT_ROOT=$(dirname $(dirname $(realpath $0)))
18
+ MODELS_DIR="$HOME/.backgroundfx/models"
19
+ CONFIG_DIR="$HOME/.backgroundfx/config"
20
+
21
+ # Functions
22
+ print_header() {
23
+ echo -e "\n${BLUE}========================================${NC}"
24
+ echo -e "${BLUE}$1${NC}"
25
+ echo -e "${BLUE}========================================${NC}\n"
26
+ }
27
+
28
+ print_success() {
29
+ echo -e "${GREEN}✓ $1${NC}"
30
+ }
31
+
32
+ print_warning() {
33
+ echo -e "${YELLOW}⚠ $1${NC}"
34
+ }
35
+
36
+ print_error() {
37
+ echo -e "${RED}✗ $1${NC}"
38
+ }
39
+
40
+ check_command() {
41
+ if command -v $1 &> /dev/null; then
42
+ return 0
43
+ else
44
+ return 1
45
+ fi
46
+ }
47
+
48
+ # Check system requirements
49
+ check_requirements() {
50
+ print_header "Checking System Requirements"
51
+
52
+ # Check Python
53
+ if check_command python$PYTHON_VERSION; then
54
+ print_success "Python $PYTHON_VERSION found"
55
+ else
56
+ print_error "Python $PYTHON_VERSION not found"
57
+ echo "Please install Python $PYTHON_VERSION or later"
58
+ exit 1
59
+ fi
60
+
61
+ # Check pip
62
+ if check_command pip3; then
63
+ print_success "pip found"
64
+ else
65
+ print_error "pip not found"
66
+ exit 1
67
+ fi
68
+
69
+ # Check git
70
+ if check_command git; then
71
+ print_success "git found"
72
+ else
73
+ print_warning "git not found - some features may not work"
74
+ fi
75
+
76
+ # Check FFmpeg
77
+ if check_command ffmpeg; then
78
+ print_success "FFmpeg found"
79
+ else
80
+ print_warning "FFmpeg not found - video processing will fail"
81
+ echo "Install with:"
82
+ echo " Ubuntu/Debian: sudo apt-get install ffmpeg"
83
+ echo " macOS: brew install ffmpeg"
84
+ echo " Windows: Download from https://ffmpeg.org"
85
+ fi
86
+
87
+ # Check CUDA (optional)
88
+ if check_command nvidia-smi; then
89
+ print_success "NVIDIA GPU detected"
90
+ nvidia-smi --query-gpu=name,memory.total --format=csv,noheader
91
+ CUDA_AVAILABLE=true
92
+ else
93
+ print_warning "No NVIDIA GPU detected - will use CPU"
94
+ CUDA_AVAILABLE=false
95
+ fi
96
+
97
+ # Check available memory
98
+ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
99
+ MEM_GB=$(free -g | awk '/^Mem:/{print $2}')
100
+ elif [[ "$OSTYPE" == "darwin"* ]]; then
101
+ MEM_GB=$(($(sysctl -n hw.memsize) / 1024 / 1024 / 1024))
102
+ else
103
+ MEM_GB=0
104
+ fi
105
+
106
+ if [ $MEM_GB -ge 8 ]; then
107
+ print_success "Memory: ${MEM_GB}GB (recommended: 8GB+)"
108
+ else
109
+ print_warning "Memory: ${MEM_GB}GB (recommended: 8GB+)"
110
+ fi
111
+
112
+ # Check disk space
113
+ DISK_GB=$(df -BG . | awk 'NR==2 {print $4}' | sed 's/G//')
114
+ if [ $DISK_GB -ge 20 ]; then
115
+ print_success "Disk space: ${DISK_GB}GB available"
116
+ else
117
+ print_warning "Disk space: ${DISK_GB}GB (recommended: 20GB+)"
118
+ fi
119
+ }
120
+
121
+ # Create virtual environment
122
+ setup_virtualenv() {
123
+ print_header "Setting Up Virtual Environment"
124
+
125
+ cd "$PROJECT_ROOT"
126
+
127
+ if [ -d "$VENV_NAME" ]; then
128
+ print_warning "Virtual environment already exists"
129
+ read -p "Recreate? (y/n): " -n 1 -r
130
+ echo
131
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
132
+ rm -rf "$VENV_NAME"
133
+ else
134
+ return
135
+ fi
136
+ fi
137
+
138
+ python$PYTHON_VERSION -m venv "$VENV_NAME"
139
+ print_success "Virtual environment created"
140
+
141
+ # Activate virtual environment
142
+ source "$VENV_NAME/bin/activate"
143
+
144
+ # Upgrade pip
145
+ pip install --upgrade pip setuptools wheel
146
+ print_success "pip upgraded"
147
+ }
148
+
149
+ # Install dependencies
150
+ install_dependencies() {
151
+ print_header "Installing Dependencies"
152
+
153
+ source "$VENV_NAME/bin/activate"
154
+
155
+ # Install PyTorch
156
+ if [ "$CUDA_AVAILABLE" = true ]; then
157
+ print_success "Installing PyTorch with CUDA support..."
158
+ pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
159
+ else
160
+ print_success "Installing PyTorch (CPU only)..."
161
+ pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
162
+ fi
163
+
164
+ # Install other requirements
165
+ if [ -f "requirements.txt" ]; then
166
+ print_success "Installing requirements..."
167
+ pip install -r requirements.txt
168
+ fi
169
+
170
+ # Install development dependencies
171
+ if [ -f "requirements-dev.txt" ]; then
172
+ print_success "Installing development dependencies..."
173
+ pip install -r requirements-dev.txt
174
+ fi
175
+
176
+ print_success "All dependencies installed"
177
+ }
178
+
179
+ # Create directories
180
+ setup_directories() {
181
+ print_header "Creating Directory Structure"
182
+
183
+ # Create model directory
184
+ mkdir -p "$MODELS_DIR"
185
+ print_success "Created models directory: $MODELS_DIR"
186
+
187
+ # Create config directory
188
+ mkdir -p "$CONFIG_DIR"
189
+ print_success "Created config directory: $CONFIG_DIR"
190
+
191
+ # Create local directories
192
+ mkdir -p "$PROJECT_ROOT/uploads"
193
+ mkdir -p "$PROJECT_ROOT/outputs"
194
+ mkdir -p "$PROJECT_ROOT/logs"
195
+ mkdir -p "$PROJECT_ROOT/temp"
196
+
197
+ print_success "Created project directories"
198
+ }
199
+
200
+ # Download models
201
+ download_models() {
202
+ print_header "Model Download"
203
+
204
+ echo "Would you like to download the AI models now? (Recommended)"
205
+ echo "This will download approximately 2-3GB of data."
206
+ read -p "Download models? (y/n): " -n 1 -r
207
+ echo
208
+
209
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
210
+ source "$VENV_NAME/bin/activate"
211
+
212
+ # Create model download script
213
+ cat > download_models.py << 'EOF'
214
+ import sys
215
+ sys.path.append('.')
216
+ from models import create_model_manager
217
+
218
+ print("Downloading essential models...")
219
+ manager = create_model_manager()
220
+
221
+ # Download essential models
222
+ models_to_download = ['rmbg-1.4', 'u2netp', 'modnet']
223
+
224
+ for model_id in models_to_download:
225
+ print(f"Downloading {model_id}...")
226
+ success = manager.downloader.download_model(model_id)
227
+ if success:
228
+ print(f"✓ {model_id} downloaded")
229
+ else:
230
+ print(f"✗ Failed to download {model_id}")
231
+
232
+ print("\nModel download complete!")
233
+ print(f"Models stored in: {manager.models_dir}")
234
+ EOF
235
+
236
+ python download_models.py
237
+ rm download_models.py
238
+
239
+ print_success "Models downloaded"
240
+ else
241
+ print_warning "Skipping model download - models will be downloaded on first use"
242
+ fi
243
+ }
244
+
245
+ # Create configuration
246
+ create_config() {
247
+ print_header "Creating Configuration"
248
+
249
+ # Create default config
250
+ cat > "$CONFIG_DIR/config.json" << EOF
251
+ {
252
+ "device": "auto",
253
+ "quality_preset": "high",
254
+ "models_dir": "$MODELS_DIR",
255
+ "max_memory_gb": 8,
256
+ "enable_cache": true,
257
+ "default_background": "blur",
258
+ "output_format": "mp4",
259
+ "log_level": "INFO"
260
+ }
261
+ EOF
262
+
263
+ print_success "Configuration created: $CONFIG_DIR/config.json"
264
+
265
+ # Create .env file
266
+ if [ ! -f "$PROJECT_ROOT/.env" ]; then
267
+ cp "$PROJECT_ROOT/docker/.env.example" "$PROJECT_ROOT/.env" 2>/dev/null || \
268
+ cat > "$PROJECT_ROOT/.env" << EOF
269
+ # BackgroundFX Pro Environment Configuration
270
+ DEVICE=auto
271
+ MODEL_CACHE_DIR=$MODELS_DIR
272
+ LOG_LEVEL=INFO
273
+ GRADIO_SERVER_NAME=0.0.0.0
274
+ GRADIO_SERVER_PORT=7860
275
+ EOF
276
+ print_success "Environment file created: .env"
277
+ fi
278
+ }
279
+
280
+ # Setup CLI
281
+ setup_cli() {
282
+ print_header "Setting Up CLI"
283
+
284
+ source "$VENV_NAME/bin/activate"
285
+
286
+ # Install CLI in development mode
287
+ pip install -e .
288
+
289
+ if check_command bgfx; then
290
+ print_success "CLI installed successfully"
291
+ echo "You can now use: bgfx --help"
292
+ else
293
+ print_warning "CLI installation may have failed"
294
+ fi
295
+ }
296
+
297
+ # Test installation
298
+ test_installation() {
299
+ print_header "Testing Installation"
300
+
301
+ source "$VENV_NAME/bin/activate"
302
+
303
+ # Test imports
304
+ python -c "
305
+ import torch
306
+ import cv2
307
+ import numpy
308
+ print('✓ Core libraries imported successfully')
309
+ "
310
+
311
+ # Test custom modules
312
+ python -c "
313
+ from api import ProcessingPipeline
314
+ from models import ModelRegistry
315
+ print('✓ Custom modules imported successfully')
316
+ " 2>/dev/null || print_warning "Some custom modules may not be available"
317
+
318
+ # Test GPU if available
319
+ if [ "$CUDA_AVAILABLE" = true ]; then
320
+ python -c "
321
+ import torch
322
+ if torch.cuda.is_available():
323
+ print(f'✓ CUDA available: {torch.cuda.get_device_name(0)}')
324
+ else:
325
+ print('✗ CUDA not available in PyTorch')
326
+ "
327
+ fi
328
+ }
329
+
330
+ # Main setup flow
331
+ main() {
332
+ print_header "BackgroundFX Pro Setup"
333
+ echo "This script will set up your development environment"
334
+ echo "Project root: $PROJECT_ROOT"
335
+ echo
336
+
337
+ check_requirements
338
+ setup_virtualenv
339
+ install_dependencies
340
+ setup_directories
341
+ download_models
342
+ create_config
343
+ setup_cli
344
+ test_installation
345
+
346
+ print_header "Setup Complete!"
347
+ echo -e "${GREEN}BackgroundFX Pro is ready to use!${NC}"
348
+ echo
349
+ echo "To activate the environment:"
350
+ echo " source $VENV_NAME/bin/activate"
351
+ echo
352
+ echo "To run the application:"
353
+ echo " python app.py"
354
+ echo
355
+ echo "To use the CLI:"
356
+ echo " bgfx --help"
357
+ echo
358
+ echo "To run tests:"
359
+ echo " pytest"
360
+ echo
361
+ }
362
+
363
+ # Run main function
364
+ main