File size: 7,741 Bytes
6f2c7f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

#!/bin/bash

# MIMO Installation Script
# Compatible with conda environments and HuggingFace Spaces
#
# USAGE:
# 1. First activate conda environment: conda activate mimo
# 2. Then run this script: ./install.sh
#
# Requirements:
# - Python 3.10 (recommended via conda environment 'mimo')
# - CUDA-capable GPU (for local development)

set -e  # Exit on any error

echo "πŸš€ Starting MIMO installation..."

# Install system dependencies first
echo "πŸ”§ Installing system dependencies..."
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS - check for ffmpeg
    if ! command -v ffmpeg &> /dev/null; then
        echo "πŸ“¦ Installing ffmpeg via Homebrew..."
        if command -v brew &> /dev/null; then
            brew install ffmpeg
        else
            echo "⚠️  Please install Homebrew first: https://brew.sh/"
            echo "    Then run: brew install ffmpeg"
            echo "    Or run: ./install_system_deps.sh"
        fi
    else
        echo "βœ… ffmpeg already installed"
    fi
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
    # Linux - attempt to install ffmpeg
    if ! command -v ffmpeg &> /dev/null; then
        echo "πŸ“¦ Installing ffmpeg..."
        if command -v apt-get &> /dev/null; then
            sudo apt-get update && sudo apt-get install -y ffmpeg
        elif command -v yum &> /dev/null; then
            sudo yum install -y ffmpeg
        else
            echo "⚠️  Please install ffmpeg manually for your Linux distribution"
        fi
    else
        echo "βœ… ffmpeg already installed"
    fi
fi

# Check if conda environment is activated
if [[ -n "$CONDA_DEFAULT_ENV" ]]; then
    echo "🐍 Conda environment detected: $CONDA_DEFAULT_ENV"
    if [[ "$CONDA_DEFAULT_ENV" == "mimo" ]]; then
        echo "βœ… Using MIMO conda environment"
        ENVIRONMENT="local"
        PYTHON_CMD="python"
        PIP_CMD="pip"
    else
        echo "⚠️  Warning: Expected 'mimo' conda environment, but found '$CONDA_DEFAULT_ENV'"
        ENVIRONMENT="local"
        PYTHON_CMD="python"
        PIP_CMD="pip"
    fi
elif [[ -n "$SPACE_ID" ]] || [[ -n "$HF_HOME" ]]; then
    echo "πŸ“¦ Detected HuggingFace Spaces environment"
    ENVIRONMENT="hf"
    PYTHON_CMD="python3"
    PIP_CMD="pip3"
else
    echo "πŸ’» Detected local development environment (non-conda)"
    ENVIRONMENT="local"
    PYTHON_CMD="python3"
    PIP_CMD="pip3"
fi

# Verify Python version
echo "πŸ” Checking Python version..."
$PYTHON_CMD --version

# Fix typing-extensions first to resolve dependency conflicts
echo "πŸ”§ Fixing typing-extensions version conflicts..."
$PIP_CMD install --upgrade "typing-extensions>=4.12.0"

# Install environment-specific PyTorch
if [[ "$ENVIRONMENT" == "local" ]]; then
    # For local development, we'll use versions from requirements_stable.txt
    # Skip separate PyTorch installation as it will be handled by requirements
    echo "⚑ PyTorch will be installed from requirements file for better compatibility"
else
    # HuggingFace Spaces - CPU optimized versions
    echo "☁️  Installing PyTorch for HuggingFace Spaces..."
    $PIP_CMD install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
fi

# Use stable requirements for better compatibility
echo "πŸ“š Installing core dependencies..."

# First, completely clean any conflicting packages
echo "🧹 Cleaning existing installations to prevent conflicts..."
set +e
$PIP_CMD uninstall -y torch torchvision torchaudio tensorboard tensorflow accelerate diffusers transformers huggingface-hub 2>/dev/null || true
set -e

if [[ -f "requirements.txt" ]]; then
    echo "Using main requirements (MIMO-compatible versions)..."
    $PIP_CMD install -r requirements.txt
elif [[ -f "requirements_stable.txt" ]]; then
    echo "Using stable requirements..."
    $PIP_CMD install -r requirements_stable.txt
else
    echo "No requirements file found!"
    exit 1
fi

# Force MIMO-compatible HuggingFace ecosystem versions
echo "πŸ”§ Ensuring MIMO-compatible HuggingFace ecosystem versions..."
$PIP_CMD install huggingface-hub==0.17.3 diffusers==0.23.1 transformers==4.35.2 accelerate==0.20.3 --force-reinstall

# Remove conflicting packages detection section since we already cleaned
# Everything will be handled by requirements_stable.txt

# Install additional dependencies based on environment
if [[ "$ENVIRONMENT" == "local" ]]; then
    # Local development - only install xformers if not Apple Silicon
    echo "οΏ½ Installing optional performance enhancements..."
    set +e  # Allow this to fail gracefully

    # Skip xformers on Apple Silicon due to compilation issues
    if [[ $(uname -m) == "arm64" && $(uname -s) == "Darwin" ]]; then
        echo "⚠️  Skipping xformers on Apple Silicon (known compilation issues)"
        echo "   MIMO will work without xformers, just with slightly slower performance"
    else
        # Try to install xformers for other platforms
        $PIP_CMD install xformers==0.0.16
        if [ $? -eq 0 ]; then
            echo "βœ… xformers installed successfully"
        else
            echo "⚠️  xformers installation failed, continuing without it"
            echo "   This is optional - MIMO will work fine without it"
        fi
    fi
    set -e

    # Skip TensorFlow to avoid conflicts - it's not essential for MIMO
    echo "πŸ“Š Installing TensorFlow (required for human segmentation)..."
    set +e
    $PIP_CMD install tensorflow==2.13.0
    if [ $? -eq 0 ]; then
        echo "βœ… TensorFlow installed successfully"
    else
        echo "⚠️  TensorFlow installation failed - MIMO may not work fully without it"
    fi
    set -e
else
    # HuggingFace Spaces specific
    echo "🌐 Installing HuggingFace Spaces dependencies..."
    $PIP_CMD install spaces --upgrade
    # Ensure compatible gradio version for HF Spaces
    $PIP_CMD install gradio>=3.40.0 --upgrade
fi

# Verify installation
echo "βœ… Verifying installation..."
$PYTHON_CMD -c "import torch; print(f'PyTorch version: {torch.__version__}'); print(f'CUDA available: {torch.cuda.is_available()}')"
$PYTHON_CMD -c "import transformers; print(f'Transformers version: {transformers.__version__}')"
$PYTHON_CMD -c "import diffusers; print(f'Diffusers version: {diffusers.__version__}')"
$PYTHON_CMD -c "import gradio; print(f'Gradio version: {gradio.__version__}')"
$PYTHON_CMD -c "import accelerate; print(f'Accelerate version: {accelerate.__version__}')"

# Optional: Check xformers
set +e
$PYTHON_CMD -c "
try:
    import xformers
    print(f'xformers version: {xformers.__version__}')
except ImportError:
    print('xformers: Not installed (optional)')
"
set -e

echo "πŸŽ‰ Installation completed successfully!"
echo "Environment: $ENVIRONMENT"

# Optional: Download MIMO model
echo ""
echo "πŸ“₯ Do you want to download the MIMO model from ModelScope? (y/n)"
read -r download_model
if [[ "$download_model" =~ ^[Yy]$ ]]; then
    echo "πŸ”½ Downloading MIMO model..."
    $PYTHON_CMD -c "
from modelscope import snapshot_download
import os
print('Downloading MIMO model from ModelScope...')
model_dir = snapshot_download(model_id='iic/MIMO', cache_dir='./pretrained_weights')
print(f'Model downloaded successfully to: {model_dir}')
"
    if [ $? -eq 0 ]; then
        echo "βœ… Model download completed!"
    else
        echo "⚠️  Model download failed. You can download it later using:"
        echo "   python -c \"from modelscope import snapshot_download; snapshot_download(model_id='iic/MIMO', cache_dir='./pretrained_weights')\""
    fi
else
    echo "⏭️  Skipping model download. You can download it later using:"
    echo "   python -c \"from modelscope import snapshot_download; snapshot_download(model_id='iic/MIMO', cache_dir='./pretrained_weights')\""
fi