|
#!/bin/bash |
|
|
|
# Main launcher script for IOPaint |
|
# This script handles the virtual environment and launches IOPaint with the user's configuration |
|
|
|
# Get the directory where this script is located |
|
Help() { |
|
echo "Usage: $0 [OPTIONS]" |
|
echo |
|
echo "Options:" |
|
echo " /? Show this help message" |
|
echo " --edit-config Edit the iopaint-user.sh configuration file, check the #comments" |
|
echo " --gpu VENDOR Set GPU vendor for this session only (nvidia, amd, cpu)" |
|
echo " --set-perma-gpu VENDOR Permanently set GPU vendor in config (nvidia, amd, cpu)" |
|
echo " --show-config Show current configuration file location and GPU setting" |
|
echo " --download-config Download default configuration from official URL" |
|
echo |
|
echo "GPU Vendors:" |
|
echo " nvidia - Use NVIDIA GPU with CUDA" |
|
echo " amd - Use AMD GPU with ROCm" |
|
echo " cpu - Use CPU only" |
|
echo |
|
echo "How to use Models?" |
|
echo " MODEL=\"lama\" (default, recommended)" |
|
echo |
|
echo "Recommended models:" |
|
echo " lama, mat, migan" |
|
echo |
|
echo "Supported other models:" |
|
echo " ldm, zits, fcf, manga" |
|
echo |
|
echo "Stable Diffusion Inpaint / normal models:" |
|
echo " inpainting" |
|
echo " runwayml/stable-diffusion-inpainting" |
|
echo " diffusers/stable-diffusion-xl-1.0-inpainting-0.1" |
|
echo " diffusionbee/fooocus_inpainting" |
|
echo " andregn/Realistic_Vision_V3.0-inpainting" |
|
echo " Lykon/dreamshaper-8-inpainting" |
|
echo " Sanster/anything-4.0-inpainting" |
|
echo " Sanster/PowerPaint-V1-stable-diffusion-inpainting" |
|
echo " Fantasy-Studio/Paint-by-Example" |
|
echo " kandinsky-community/kandinsky-2-2-decoder-inpaint (use 768x768 img dimensions)" |
|
echo |
|
echo "Other specialized models:" |
|
echo " Brushnet - turns any sd1.5 model into an inpainting model." |
|
echo " brushnet_segmentation_mask: maintains consistency with the mask shape." |
|
echo " brushnet_random_mask: provides a general ckpt for random mask shapes." |
|
echo |
|
echo "This script launches IOPaint with user settings and handles the Python environment." |
|
echo |
|
echo "refer to https://www.iopaint.com/models" |
|
echo |
|
exit 0 |
|
} |
|
|
|
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
|
|
|
# Default configuration URL |
|
CONFIG_URL="https://gist.githubusercontent.com/airborne-commando/38d059e31274546e5bc9060516c735e9/raw/8e420885088c2f50f261b3f88d68b3d562063e53/iopaint-user.sh" |
|
|
|
# Parse command line arguments |
|
SESSION_GPU="" |
|
SET_PERMA_GPU="" |
|
SHOW_CONFIG=false |
|
EDIT_CONFIG=false |
|
DOWNLOAD_CONFIG=false |
|
|
|
while [[ $# -gt 0 ]]; do |
|
case $1 in |
|
/\?) |
|
Help |
|
;; |
|
--edit-config) |
|
EDIT_CONFIG=true |
|
shift |
|
;; |
|
--gpu) |
|
SESSION_GPU="$2" |
|
shift 2 |
|
;; |
|
--set-perma-gpu) |
|
SET_PERMA_GPU="$2" |
|
shift 2 |
|
;; |
|
--show-config) |
|
SHOW_CONFIG=true |
|
shift |
|
;; |
|
--download-config) |
|
DOWNLOAD_CONFIG=true |
|
shift |
|
;; |
|
*) |
|
shift |
|
;; |
|
esac |
|
done |
|
|
|
# Determine config file location |
|
# Priority: 1. XDG_CONFIG_HOME, 2. ~/.config/, 3. script directory |
|
if [ -n "$XDG_CONFIG_HOME" ]; then |
|
CONFIG_DIR="$XDG_CONFIG_HOME/iopaint" |
|
else |
|
CONFIG_DIR="${HOME}/.config/iopaint" |
|
fi |
|
|
|
USER_CONFIG="$CONFIG_DIR/iopaint-user.sh" |
|
|
|
# Create config directory if it doesn't exist |
|
if [ ! -d "$CONFIG_DIR" ]; then |
|
mkdir -p "$CONFIG_DIR" |
|
echo "Created config directory: $CONFIG_DIR" |
|
fi |
|
|
|
# Handle --download-config |
|
if [ "$DOWNLOAD_CONFIG" = true ]; then |
|
echo "Downloading default configuration from: $CONFIG_URL" |
|
echo "Target: $USER_CONFIG" |
|
|
|
# Check for available download tools |
|
if command -v curl &> /dev/null; then |
|
if curl -fsSL "$CONFIG_URL" -o "$USER_CONFIG"; then |
|
echo "โ
Successfully downloaded configuration using curl" |
|
chmod +x "$USER_CONFIG" |
|
else |
|
echo "โ Failed to download configuration using curl" |
|
exit 1 |
|
fi |
|
elif command -v wget &> /dev/null; then |
|
if wget -q "$CONFIG_URL" -O "$USER_CONFIG"; then |
|
echo "โ
Successfully downloaded configuration using wget" |
|
chmod +x "$USER_CONFIG" |
|
else |
|
echo "โ Failed to download configuration using wget" |
|
exit 1 |
|
fi |
|
else |
|
echo "โ Error: Neither curl nor wget is available for downloading" |
|
echo "Please install curl or wget, or create the configuration manually" |
|
exit 1 |
|
fi |
|
|
|
# Verify the download was successful |
|
if [ -f "$USER_CONFIG" ] && [ -s "$USER_CONFIG" ]; then |
|
echo "Configuration successfully downloaded to: $USER_CONFIG" |
|
echo "You can now edit it with: $0 --edit-config" |
|
exit 0 |
|
else |
|
echo "โ Error: Downloaded file is empty or missing" |
|
exit 1 |
|
fi |
|
fi |
|
|
|
# Copy default config if no user config exists |
|
if [ ! -f "$USER_CONFIG" ]; then |
|
echo "No user configuration found at: $USER_CONFIG" |
|
echo "You can:" |
|
echo " 1. Run '$0 --download-config' to download the default configuration" |
|
echo " 2. Run '$0 --edit-config' to create and edit a blank configuration" |
|
echo " 3. Create the file manually at: $USER_CONFIG" |
|
echo "" |
|
read -p "Would you like to download the default configuration now? (y/N): " -n 1 -r |
|
echo |
|
if [[ $REPLY =~ ^[Yy]$ ]]; then |
|
# Use self-call to download config |
|
exec "$0" --download-config |
|
else |
|
echo "Please create a configuration file first." |
|
exit 1 |
|
fi |
|
fi |
|
|
|
# Handle --show-config |
|
if [ "$SHOW_CONFIG" = true ]; then |
|
echo "Configuration file: $USER_CONFIG" |
|
if [ -f "$USER_CONFIG" ]; then |
|
echo "File size: $(wc -l < "$USER_CONFIG") lines" |
|
echo "" |
|
echo "Current GPU setting:" |
|
grep "GPU_VENDOR=" "$USER_CONFIG" || echo "GPU_VENDOR not set (using default: cpu)" |
|
echo "" |
|
echo "Current MODEL setting:" |
|
grep "MODEL=" "$USER_CONFIG" | head -1 || echo "MODEL not set (using default: lama)" |
|
fi |
|
exit 0 |
|
fi |
|
|
|
# Handle --edit-config |
|
if [ "$EDIT_CONFIG" = true ]; then |
|
${EDITOR:-nano} "$USER_CONFIG" |
|
exit 0 |
|
fi |
|
|
|
# Source the user configuration file |
|
if ! source "$USER_CONFIG"; then |
|
echo "โ Error: Failed to load configuration file: $USER_CONFIG" |
|
echo "The file may contain syntax errors. Please check or download a fresh copy with:" |
|
echo " $0 --download-config" |
|
exit 1 |
|
fi |
|
|
|
# Handle permanent GPU setting |
|
if [ -n "$SET_PERMA_GPU" ]; then |
|
case $SET_PERMA_GPU in |
|
nvidia|amd|cpu) |
|
# Create backup |
|
cp "$USER_CONFIG" "$USER_CONFIG.bak.$(date +%Y%m%d_%H%M%S)" |
|
|
|
if grep -q "GPU_VENDOR=" "$USER_CONFIG"; then |
|
# Update existing GPU_VENDOR line |
|
sed -i "s/GPU_VENDOR=.*/GPU_VENDOR=\"$SET_PERMA_GPU\"/" "$USER_CONFIG" |
|
else |
|
# Add GPU_VENDOR line after the first comments |
|
sed -i "0,/^[^#]/s//GPU_VENDOR=\"$SET_PERMA_GPU\"\n&/" "$USER_CONFIG" |
|
fi |
|
|
|
echo "โ
Permanently set GPU vendor to: $SET_PERMA_GPU" |
|
echo "๐ Configuration updated in: $USER_CONFIG" |
|
echo "๐พ Backup saved as: $USER_CONFIG.bak.*" |
|
|
|
# Reload the updated config |
|
source "$USER_CONFIG" |
|
;; |
|
*) |
|
echo "โ Error: Invalid GPU vendor. Use: nvidia, amd, or cpu" |
|
exit 1 |
|
;; |
|
esac |
|
fi |
|
|
|
# Handle session GPU override |
|
if [ -n "$SESSION_GPU" ]; then |
|
case $SESSION_GPU in |
|
nvidia|amd|cpu) |
|
echo "๐ง Using GPU vendor for this session: $SESSION_GPU" |
|
GPU_VENDOR="$SESSION_GPU" |
|
;; |
|
*) |
|
echo "โ Error: Invalid GPU vendor. Use: nvidia, amd, or cpu" |
|
exit 1 |
|
;; |
|
esac |
|
fi |
|
|
|
# Check if Python 3.10 is available |
|
if ! command -v python3.10 &> /dev/null; then |
|
echo "โ Python 3.10 is required but not installed." |
|
exit 1 |
|
fi |
|
|
|
# Create virtual environment if it doesn't exist |
|
if [ ! -d "$VENV_DIR" ]; then |
|
echo "๐ Creating virtual environment in $VENV_DIR..." |
|
echo "๐ฎ Using GPU vendor: $GPU_VENDOR" |
|
python3.10 -m venv "$VENV_DIR" |
|
source "$VENV_DIR/bin/activate" |
|
|
|
echo "๐ฆ Installing PyTorch for $GPU_VENDOR..." |
|
case "$GPU_VENDOR" in |
|
nvidia) |
|
pip3 install torch==2.1.2 torchvision==0.16.2 --index-url https://download.pytorch.org/whl/cu118 |
|
;; |
|
amd) |
|
pip3 install torch==2.1.2 torchvision==0.16.2 --index-url https://download.pytorch.org/whl/rocm5.6 |
|
;; |
|
cpu|*) |
|
echo "Installing default PyTorch (CPU version)" |
|
pip3 install torch==2.1.2 torchvision==0.16.2 |
|
;; |
|
esac |
|
|
|
pip install --upgrade pip |
|
pip install iopaint |
|
|
|
# Install plugin dependencies if needed |
|
if [ "$ENABLE_REMOVE_BG" = true ] || [ "$ENABLE_ANIME_SEG" = true ] || [ "$ENABLE_INTERACTIVE_SEG" = true ]; then |
|
echo "๐ Installing plugin dependencies (onnxruntime, rembg)..." |
|
pip install onnxruntime rembg |
|
fi |
|
|
|
deactivate |
|
echo "โ
Virtual environment created and iopaint installed." |
|
fi |
|
|
|
# For existing environments, check if we need to install additional dependencies |
|
source "$VENV_DIR/bin/activate" |
|
if [ "$ENABLE_REMOVE_BG" = true ] || [ "$ENABLE_ANIME_SEG" = true ] || [ "$ENABLE_INTERACTIVE_SEG" = true ]; then |
|
if ! pip show onnxruntime rembg &> /dev/null; then |
|
echo "๐ Installing missing plugin dependencies (onnxruntime, rembg)..." |
|
pip install onnxruntime rembg |
|
fi |
|
fi |
|
deactivate |
|
|
|
# Build the command |
|
CMD="iopaint start --model=\"$MODEL\" --device=\"$DEVICE\" --port=\"$PORT\" --host=\"$HOST\" --model-dir=\"$MODEL_DIR\" --quality=\"$QUALITY\"" |
|
|
|
# Add boolean flags |
|
[ "$IN_BROWSER" = true ] && CMD="$CMD --inbrowser" |
|
[ "$LOW_MEM" = true ] && CMD="$CMD --low-mem" |
|
[ "$NO_HALF" = true ] && CMD="$CMD --no-half" |
|
[ "$CPU_OFFLOAD" = true ] && CMD="$CMD --cpu-offload" |
|
[ "$DISABLE_NSFW_CHECKER" = true ] && CMD="$CMD --disable-nsfw-checker" |
|
[ "$CPU_TEXTENCODER" = true ] && CMD="$CMD --cpu-textencoder" |
|
[ "$LOCAL_FILES_ONLY" = true ] && CMD="$CMD --local-files-only" |
|
[ "$ENABLE_INTERACTIVE_SEG" = true ] && CMD="$CMD --enable-interactive-seg" |
|
[ "$ENABLE_REMOVE_BG" = true ] && CMD="$CMD --enable-remove-bg" |
|
[ "$ENABLE_ANIME_SEG" = true ] && CMD="$CMD --enable-anime-seg" |
|
[ "$ENABLE_REALESRGAN" = true ] && CMD="$CMD --enable-realesrgan" |
|
[ "$ENABLE_GFPGAN" = true ] && CMD="$CMD --enable-gfpgan" |
|
[ "$ENABLE_RESTOREFORMER" = true ] && CMD="$CMD --enable-restoreformer" |
|
|
|
# Add other options |
|
[ -n "$INTERACTIVE_SEG_MODEL" ] && CMD="$CMD --interactive-seg-model=\"$INTERACTIVE_SEG_MODEL\"" |
|
[ -n "$INTERACTIVE_SEG_DEVICE" ] && CMD="$CMD --interactive-seg-device=\"$INTERACTIVE_SEG_DEVICE\"" |
|
[ -n "$REMOVE_BG_DEVICE" ] && CMD="$CMD --remove-bg-device=\"$REMOVE_BG_DEVICE\"" |
|
[ -n "$REMOVE_BG_MODEL" ] && CMD="$CMD --remove-bg-model=\"$REMOVE_BG_MODEL\"" |
|
[ -n "$REALESRGAN_DEVICE" ] && CMD="$CMD --realesrgan-device=\"$REALESRGAN_DEVICE\"" |
|
[ -n "$REALESRGAN_MODEL" ] && CMD="$CMD --realesrgan-model=\"$REALESRGAN_MODEL\"" |
|
[ -n "$GFPGAN_DEVICE" ] && CMD="$CMD --gfpgan-device=\"$GFPGAN_DEVICE\"" |
|
[ -n "$RESTOREFORMER_DEVICE" ] && CMD="$CMD --restoreformer-device=\"$RESTOREFORMER_DEVICE\"" |
|
[ -n "$INPUT" ] && CMD="$CMD --input=\"$INPUT\"" |
|
[ -n "$MASK_DIR" ] && CMD="$CMD --mask-dir=\"$MASK_DIR\"" |
|
[ -n "$OUTPUT_DIR" ] && CMD="$CMD --output-dir=\"$OUTPUT_DIR\"" |
|
|
|
# Activate and run |
|
echo "๐ Starting IOPaint with GPU vendor: $GPU_VENDOR" |
|
echo "๐ Configuration: $USER_CONFIG" |
|
echo "๐ง Command: $CMD" |
|
source "$VENV_DIR/bin/activate" |
|
eval "$CMD" |
|
deactivate |