at_test / 1.Setup.sh
corsair4090's picture
Upload 1.Setup.sh
d3b4657 verified
#!/bin/bash
set -e
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
echo "Starting the script..."
# Install Python 3.10.11 if not present
if ! command_exists python3.10; then
echo "Python 3.10 not found. Installing..."
apt update
apt install -y software-properties-common
add-apt-repository -y ppa:deadsnakes/ppa
apt update
apt install -y python3.10 python3.10-venv python3.10-distutils
apt install -y python3-pip
else
echo "Python 3.10 is already installed."
fi
# Check pip for Python 3.10
if ! python3.10 -m pip --version >/dev/null 2>&1; then
echo "pip not found for Python 3.10. Installing..."
python3.10 -m ensurepip --upgrade
python3.10 -m pip install --upgrade pip
else
echo "pip is already installed for Python 3.10."
fi
# Clone the repository if it doesn't exist
if [ ! -d "sd-scripts" ]; then
echo "Cloning the sd-scripts repository..."
git clone -b sd3 https://github.com/kohya-ss/sd-scripts.git
else
echo "The sd-scripts directory already exists. Skipping cloning."
fi
# Copy sample_prompts.txt if it exists
if [ -f "sample_prompts.txt" ]; then
echo "Copying sample_prompts.txt to the sd-scripts directory..."
cp sample_prompts.txt sd-scripts/
else
echo "Warning: sample_prompts.txt not found. Skipping copy."
fi
# Enter the repository directory
cd sd-scripts || { echo "Error: Could not enter the sd-scripts directory."; exit 1; }
# Create a virtual environment with Python 3.10 if it doesn't exist
if [ ! -d "venv" ]; then
echo "Creating the virtual environment with Python 3.10..."
python3.10 -m venv venv
else
echo "The virtual environment already exists. Skipping creation."
fi
# Activate the virtual environment in a compatible way
if [ -f "venv/bin/activate" ]; then
echo "Activating the virtual environment..."
source venv/bin/activate || { echo "Error: Could not activate the virtual environment."; exit 1; }
else
echo "Error: Could not find the virtual environment activation script."
exit 1
fi
# Upgrade pip and setuptools inside the virtual environment
echo "Upgrading pip and setuptools inside the virtual environment..."
pip install --upgrade pip setuptools
# Install dependencies from requirements.txt if it exists
if [ -f "requirements.txt" ]; then
echo "Installing dependencies from requirements.txt..."
pip install -r requirements.txt
else
echo "Warning: requirements.txt not found. Skipping dependency installation."
fi
# Install additional packages
echo "Installing toml and diffusers..."
apt install -y git
apt update
pip install toml diffusers
pip3 install --upgrade diffusers
pip3 install huggingface-hub==0.11.1
pip3 install --upgrade huggingface-hub
pip3 install --upgrade torch torchvision
pip3 uninstall -y torchvision
pip3 install torchvision
pip3 uninstall -y opencv-python
pip3 install opencv-python
pip3 install --upgrade numpy opencv-python pybind11
pip3 uninstall -y opencv-python opencv-python-headless
pip3 install opencv-python-headless
echo "Installing PyTorch and other libraries..."
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
# Always reinstall accelerate
echo "Reinstalling accelerate..."
pip install --upgrade --force-reinstall accelerate
# Configure accelerate
echo "Configuring accelerate..."
# Attempt a non-interactive configuration (if possible)
accelerate config || {
echo "Error: Accelerate configuration failed."
deactivate
exit 1
}
echo "Script completed successfully."