Skip to content

Instantly share code, notes, and snippets.

@erancihan
Last active August 4, 2023 13:17
Show Gist options
  • Save erancihan/222d529d6847a24b81b5daa5a6018ad0 to your computer and use it in GitHub Desktop.
Save erancihan/222d529d6847a24b81b5daa5a6018ad0 to your computer and use it in GitHub Desktop.
A script to setup AUTOMATIC1111's "stable-diffusion-webui"
#!/bin/bash
# This is a script to both install and run the Stable Diffusion WebUI by AUTOMATIC1111.
# Script will first check if the webui is installed, if not it will install it.
# Then it will check for updates and run the webui's webui.sh script.
#
# Usage:
# -p [value], --path [value] :
# Install Automatic1111's Stable Diffusion Webui repo to a custom path.
# You will not need to set this path every time you want to run this script,
# as the path will be added to config file stored at `~/.config/sdf.cfg`
# -u, --update :
# check for repo updates on automatic1111 repo
# -l, --with-lycoris :
# does a fresh installation of lycoris extension
# -e, --use-erancihan-k-diffusion-repo :
# Update environment configurations to use erancihan/k-diffusion repo's latest version.
#
# Example Command:
# `./automatic1111.sh -ul -e -p "$HOME/sd-webui"`
#
# get latest stable-diffusion-webui release tag, and update the repo
get_latest_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"([^"]+)".*/\1/'
}
get_latest_sha() {
curl --silent "https://api.github.com/repos/$1/git/ref/tags/$2" | jq -r '.object.sha'
}
__CONFIG_PATH="$HOME/.config/sdf.cfg"
# read values from config file or set defaults
function __config() {
val=$(grep -E "^$1=" $__CONFIG_PATH 2>/dev/null || echo "$1=__DEFAULT__" | head -n 1 | cut -d '=' -f 2-)
if [[ $val == __DEFAULT__ ]]
then
case $1 in
STABLE_DIFFUSION_WEBUI_PATH)
echo -n "\"$HOME/stable-diffusion-webui\""
;;
esac
else
echo -n "$(sed "s/^$1=//" <<< $val)"
fi
}
# set config values
function __set_config(){
sed -i '' "s|^\($1\s*=\s*\).*|\1\"$2\"|g" $__CONFIG_PATH
}
# cold start
# initialize config file if it doesn't exist
if [ ! -e "${__CONFIG_PATH}" ] ; then
# Set default variable value
touch $__CONFIG_PATH
# assign default values
# --
echo "STABLE_DIFFUSION_WEBUI_PATH=$(__config STABLE_DIFFUSION_WEBUI_PATH)" | tee -a $__CONFIG_PATH
# --
fi
REPO_UPDATE=false
WITH_LYCORIS=false
STABLE_DIFFUSION_WEBUI_PATH="$(__config STABLE_DIFFUSION_WEBUI_PATH)"
case "$(uname -s)" in
Linux*)
VALID_ARGS=$(getopt -o ulp:e --long update,with-lycoris,path:,use-erancihan-k-diffusion-repo -- "$@")
if [[ $? -ne 0 ]]; then
exit 2;
fi
;;
Darwin*)
echo "Please note that long name args are not supported on macOS for this script."
echo ""
VALID_ARGS=`getopt ulp:e $*`
if [[ $? -ne 0 ]]; then
exit 2;
fi
;;
esac
eval set -- "$VALID_ARGS"
while [ : ]; do
case $1 in
-u | --update)
REPO_UPDATE=true # update the webui
shift
;;
-l | --with-lycoris)
WITH_LYCORIS=true # install lycoris
shift
;;
-p | --path)
STABLE_DIFFUSION_WEBUI_PATH="$2"
echo "Setting Stable Diffusion WebUI path to: $STABLE_DIFFUSION_WEBUI_PATH"
__set_config "STABLE_DIFFUSION_WEBUI_PATH" "$STABLE_DIFFUSION_WEBUI_PATH"
shift 2
;;
-e | --use-erancihan-k-diffusion-repo)
FLAG_USE_ERANCIHAN_K_DIFFUSION_REPO=true
shift
;;
--) shift;
break
;;
esac
done
# sanitize path
STABLE_DIFFUSION_WEBUI_PATH=`sed -e 's/^"//' -e 's/"$//' <<<"$STABLE_DIFFUSION_WEBUI_PATH"`
cd $STABLE_DIFFUSION_WEBUI_PATH
echo "Stable Diffusion WebUI path is set to: $STABLE_DIFFUSION_WEBUI_PATH"
if [ -d "$STABLE_DIFFUSION_WEBUI_PATH" ]
then
# Stable Diffusion WebUI is installed, check for updates
echo "Stable Diffusion WebUI is installed"
else
# Stable Diffusion WebUI is not installed, install it
echo "Stable Diffusion WebUI is not installed, installing..."
# Install Homebrew if not installed
if ! command -v brew --version &> /dev/null
then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# Install Homebrew packages
brew install cmake protobuf rust [email protected] git wget
# Clone automatic1111 repo
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui "$STABLE_DIFFUSION_WEBUI_PATH"
fi
cd "$STABLE_DIFFUSION_WEBUI_PATH"
if [[ $WITH_LYCORIS == true ]];
then
LYCORIS_PATH="$STABLE_DIFFUSION_WEBUI_PATH/extensions/a1111-sd-webui-lycoris"
# if Lycoris exists, just update it
if [ -d "$LYCORIS_PATH/.git" ]
then
echo "Lycoris is installed, updating..."
cd "$LYCORIS_PATH"
git fetch --all --tags --prune
git pull
cd -
else
echo "Lycoris is not installed, installing..."
rm -rf "$LYCORIS_PATH"
git clone https://github.com/KohakuBlueleaf/a1111-sd-webui-lycoris.git "$LYCORIS_PATH"
fi
fi
# Run the following commands to update and run the webui
if [[ $REPO_UPDATE == true ]];
then
latest_tag=$(get_latest_release "AUTOMATIC1111/stable-diffusion-webui")
git fetch --all --tags --prune
git checkout $latest_tag
fi
if [[ $FLAG_USE_ERANCIHAN_K_DIFFUSION_REPO == true ]];
then
latest_tag=$(get_latest_release "erancihan/k-diffusion")
latest_sha=$(get_latest_sha "erancihan/k-diffusion" $latest_tag)
# update webui scripts to include commit hash of latest k-diffusion release
# check if webui-user.sh and webui-macos-env.sh contain K_DIFFUSION_REPO and K_DIFFUSION_COMMIT_HASH
if grep -q "K_DIFFUSION_REPO" "$STABLE_DIFFUSION_WEBUI_PATH/webui-user.sh";
then
# K_DIFFUSION_REPO is already in webui-user.sh, replace
sed -i '' "s|^\(export K_DIFFUSION_REPO\s*=\s*\).*|\1\"https://github.com/erancihan/k-diffusion.git\"|g" "$STABLE_DIFFUSION_WEBUI_PATH/webui-user.sh"
else
# K_DIFFUSION_REPO is not in webui-user.sh, append
echo "export K_DIFFUSION_REPO=\"https://github.com/erancihan/k-diffusion.git\"" >> "$STABLE_DIFFUSION_WEBUI_PATH/webui-user.sh"
fi
if grep -q "K_DIFFUSION_COMMIT_HASH" "$STABLE_DIFFUSION_WEBUI_PATH/webui-user.sh";
then
# K_DIFFUSION_COMMIT_HASH is already in webui-user.sh, replace
sed -i '' "s|^\(export K_DIFFUSION_COMMIT_HASH\s*=\s*\).*|\1\"$latest_sha\"|g" "$STABLE_DIFFUSION_WEBUI_PATH/webui-user.sh"
else
# K_DIFFUSION_COMMIT_HASH is not in webui-user.sh, append
echo "export K_DIFFUSION_COMMIT_HASH=\"$latest_sha\"" >> "$STABLE_DIFFUSION_WEBUI_PATH/webui-user.sh"
fi
if grep -q "K_DIFFUSION_REPO" "$STABLE_DIFFUSION_WEBUI_PATH/webui-macos-env.sh";
then
# K_DIFFUSION_REPO is already in webui-macos-env.sh, replace
sed -i '' "s|^\(export K_DIFFUSION_REPO\s*=\s*\).*|\1\"https://github.com/erancihan/k-diffusion.git\"|g" "$STABLE_DIFFUSION_WEBUI_PATH/webui-macos-env.sh"
else
# K_DIFFUSION_REPO is not in webui-macos-env.sh, append
echo "export K_DIFFUSION_REPO=\"https://github.com/erancihan/k-diffusion.git\"" >> "$STABLE_DIFFUSION_WEBUI_PATH/webui-macos-env.sh"
fi
if grep -q "K_DIFFUSION_COMMIT_HASH" "$STABLE_DIFFUSION_WEBUI_PATH/webui-macos-env.sh";
then
# K_DIFFUSION_COMMIT_HASH is already in webui-macos-env.sh, replace
sed -i '' "s|^\(export K_DIFFUSION_COMMIT_HASH\s*=\s*\).*|\1\"$latest_sha\"|g" "$STABLE_DIFFUSION_WEBUI_PATH/webui-macos-env.sh"
else
# K_DIFFUSION_COMMIT_HASH is not in webui-macos-env.sh, append
echo "export K_DIFFUSION_COMMIT_HASH=\"$latest_sha\"" >> "$STABLE_DIFFUSION_WEBUI_PATH/webui-macos-env.sh"
fi
fi
# send open url first while the server is starting
sleep 6 && open http://127.0.0.1:7860 &
python_cmd="python3.10" ./webui.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment