Skip to content

Instantly share code, notes, and snippets.

@thinkyhead
Last active January 27, 2023 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkyhead/eef24dc490550765c3a3c9ceffede719 to your computer and use it in GitHub Desktop.
Save thinkyhead/eef24dc490550765c3a3c9ceffede719 to your computer and use it in GitHub Desktop.
Start Stable Diffusion + InvokeAI on macOS
#!/usr/bin/env zsh
#
# StableDiffusion.command
#
# - Start Stable Diffusion - InvokeAI in the default Terminal.
# - When ready, open InvokeAI in the default Browser.
#
# author: @thinkyhead
# created: 2022-11-10
# revised: 2022-11-18 - allow for multiple project folders
# - prevent multiple instances (crudely)
# 2022-12-01 - allow for a non-default data folder
# 2022-12-27 - Set SDDATA from INVOKEAI_ROOT if possible
# based on: https://medium.com/gft-engineering/macbook-m1-how-to-install-and-run-stable-diffusion-7bfb2f802b1a
# https://github.com/invoke-ai/InvokeAI
#
# Change these based on your install:
SDHOME=$HOME/Projects/StableDiffusion/InvokeAI
# Data to use for this .command file. Set to img-samples for the last-used set.
# Comment out the line below to pick up SDOUT from the name of the .command file.
SDOUT=img-samples
# SDDATA should match INVOKEAI_ROOT, if set
[[ -n $INVOKEAI_ROOT ]] && SDDATA=$INVOKEAI_ROOT
[[ -z $SDDATA ]] && SDDATA=$SDHOME
# Use this .command file's name for the data folder
[[ -z $SDOUT && $0 =~ '[^-]+-(.*)\.command' ]] && SDOUT=img-samples-$match[1]
# Comment out if you want to run multiple instances:
RUNONCE=1
# Check for a previous instance
[[ $RUNONCE > 0 && -n "$( ps | grep -E "(invoke|webui).py" | grep -v grep )" ]] && {
echo
echo "***************************************"
echo "** Stable Diffusion already running! **"
echo "***************************************"
exit
}
opensite() {
echo "Opening InvokeAI in the browser..."
open $1
}
# Alternative data just for this .command file?
SDBASE=outputs/img-samples
if [[ $SDBASE != "outputs/$SDOUT" ]]; then
cd $SDDATA
if [[ -L $SDBASE ]]; then
rm -f $SDBASE ; # Remove an existing symlink
elif [[ -d $SDBASE ]]; then
mv $SDBASE outputs/$SDOUT ; # Dir? First time so rename it
fi
[[ -e outputs/$SDOUT ]] || mkdir -p outputs/$SDOUT ; # Create dir if needed
ln -s $SDOUT $SDBASE
fi
# Activate the invokeai environment to run InvokeAI Web UI
cd $SDHOME
#source $(conda info --base)/etc/profile.d/conda.sh
eval "$(conda shell.zsh hook)"
conda activate invokeai
#export PKG_CONFIG_PATH=/opt/local/lib/opencv4/pkgconfig
echo Starting Invoke AI Web Server...
# Shut down the server / diffusion with CTRL-C
python3 -u scripts/invoke.py --web | {
while IFS= read -r line
do
echo "$line"
[[ $line =~ "Point your browser at (.+)" ]] && opensite $match[1]
done
}
@thinkyhead
Copy link
Author

Note: You'll need to chmod u+x StableDiffusion.command from the Terminal before it will run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment