-
Notifications
You must be signed in to change notification settings - Fork 5
/
sd-installer.sh
executable file
·334 lines (279 loc) · 10.6 KB
/
sd-installer.sh
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#! /bin/bash
set -u
# Configurable variables #TODO: Make them configurable
installation_path=$HOME
tmp_path="$HOME/.sd-installer"
mamba_root_path="~/micromamba"
# Other variables
code_path="$installation_path/stable-diffusion-webui"
brew_installer_path="$tmp_path/brew_installer.sh"
brew_pkg_path="/usr/local"
if [[ $(uname -p) == 'arm' ]]; then
echo "Apple Silicon"
brew_pkg_path="/opt/homebrew"
fi
brew_path="$brew_pkg_path/bin/brew"
mamba_path="$brew_pkg_path/opt/micromamba/bin/micromamba"
test_mode=0 # 0 for no test, 1 for Network-Connected-Test, 2 for Network-Not-Connected-Test
update_mode=false
network_connected=true
function clean_up {
echo "############ Clean ###################################"
rm -rf $tmp_path
}
function echo_green {
echo -e "\033[32m$1\033[0m"
}
function echo_red {
echo -e "\033[31m$1\033[0m"
}
# Define a function to handle errors
function handle_error {
if [ "$test_mode" != 0 ]; then
echo_red "测试失败, 不重试"
exit 1
fi
echo_red "安装失败, 是否重试? [y/n] "
read -rp "Installation failed, do you want to retry? [y/n] " error_choice
error_choice=${error_choice:-y}
if [[ $error_choice == [yY] ]]; then
# Retry the command
if [ "$network_connected" == false ]; then
/bin/bash -c "$(curl -fsSL $sd_installer_url)" -c
else
/bin/bash -c "$(curl -fsSL $sd_installer_url)"
fi
else
clean_up
# Exit the script
exit 1
fi
}
# Set the error handler
trap 'handle_error' ERR
# Last command succeeded and the command exists
function verify_installation {
if [ $? -eq 0 ] && command -v $1 &>/dev/null; then
echo_green "$1 has been installed successfully"
else
echo_red "Failed to install $1"
exit 1
fi
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-t | -test)
value=${2:1}
if [ "$value" != 1 ] && [ "$value" != 2 ]; then
echo_red "未知选项: $1 $2"
exit 1
fi
test_mode=$value
shift # past argument
shift # past value
;;
-u | -update)
update_mode=true
shift
;;
-c)
network_connected=false
shift
;;
# -h | --help)
# show_help
# exit 0
# ;;
*)
echo_red "未知选项: $1"
# show_help
exit 1
;;
esac
done
if [ "$test_mode" != 0 ]; then
if [ "$test_mode" == 1 ]; then
network_connected=true
echo_green "Network-Connected-Test"
else
network_connected=false
echo_green "Network-Not-Connected-Test"
fi
else
# echo_green "For non-Chinese users, you could just ignore this and press the Enter key"
# read -rp "网络是否顺畅? 默认y [y/n] " network_choice
# network_choice=${network_choice:-y}
# if [[ $network_choice == [nN] ]]; then
# network_connected=false
# echo_green "将设置国内镜像源安装, 包括 homebrew、conda-forge、pip、github 等, 但链接可能会不稳定/失效, 若失败可以重试"
# else
# echo_green "网络通畅, 正常安装"
# fi
if [ "$network_connected" == false ]; then
echo_green "将设置国内镜像源安装, 包括 homebrew、conda-forge、pip、github 等, 但链接可能会不稳定或失效, 若安装失败可以重试"
else
echo_green "恭喜网络通畅, 正常安装"
fi
fi
brew_installer_url="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
sd_installer_url="https://raw.githubusercontent.com/wy-luke/StableDiffusion-Installer-For-Mac/main/sd-installer.sh"
sd_webui_url="https://github.com/AUTOMATIC1111/stable-diffusion-webui.git"
if [ "$network_connected" == false ]; then
# raw.gitmirror.com 备份
# brew_installer_url="https://raw.gitmirror.com/Homebrew/install/HEAD/install.sh"
# sd_installer_url="https://raw.gitmirror.com/wy-luke/StableDiffusion-Installer-For-Mac/main/sd-installer.sh"
brew_installer_url="https://ghproxy.com/https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
sd_installer_url="https://ghproxy.com/https://raw.githubusercontent.com/wy-luke/StableDiffusion-Installer-For-Mac/main/sd-installer.sh"
sd_webui_url="https://ghproxy.com/https://github.com/AUTOMATIC1111/stable-diffusion-webui"
fi
echo "############ 开始安装 Stable Diffusion web UI #########" && echo
if [ ! -d $tmp_path ]; then
mkdir -p $tmp_path
fi
echo "############ Check and install Homebrew ##############"
# Homebrew: The missing package manager for macOS
# More: https://brew.sh/
# Try to activate homebrew first
eval "$($brew_path shellenv)"
if ! command -v brew &>/dev/null; then
if curl -fsSL $brew_installer_url -o $brew_installer_path && [ -f $brew_installer_path ]; then
# Grant the permission to install Homebrew
sudo dseditgroup -o edit -a $(whoami) -t user admin
sudo dseditgroup -o edit -a $(whoami) -t user wheel
# Grant the permission to execute the installation script
chmod +x $brew_installer_path
if [ "$network_connected" == false ]; then
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
fi
yes | /bin/bash -c $brew_installer_path
eval "$($brew_path shellenv)"
verify_installation brew
else
echo_red "Homebrew 安装文件下载失败, 请检查网络连接"
echo_red "Failed to download Homebrew installation script, please check your network connection"
exit 1
fi
else
echo_green "Homebrew has already been installed"
fi
if [ "$network_connected" == false ]; then
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple"
fi
brew update
echo_green "Install the packages required"
brew install cmake protobuf rust git wget
# brew install --cask cmake
echo
echo "############ Check and install micromamba ############"
# About mamba: A super fast Python package and environment manager (compared to conda)
# About micromamba: A tiny version of the mamba. No base environment nor a default version of Python
# More: https://mamba.readthedocs.io/en/latest/index.html
# Try to activate micromamba first
export MAMBA_ROOT_PREFIX=$mamba_root_path
eval "$($mamba_path shell hook -s posix)"
if ! command -v micromamba &>/dev/null; then
# Install micromamba
brew install micromamba
# Activate micromamba in current shell
eval "$($mamba_path shell hook -s posix)"
verify_installation micromamba
else
echo_green "micromamba has already been installed"
fi
# Set default channels for micromamba
if [ "$network_connected" == false ]; then
micromamba config prepend channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
# micromamba config prepend channels http://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
else
micromamba config prepend channels nodefaults
micromamba config prepend channels conda-forge
fi
micromamba config set channel_priority strict
micromamba clean -i
echo_green "micromamba has been configed"
echo
echo "############ Check and install Git ###################"
# Git: A free and open source distributed version control system
# More: https://git-scm.com/
if ! command -v git &>/dev/null; then
brew install git
verify_installation git
else
echo_green "Git has already been installed"
fi
echo
echo "############ Download code ###########################"
# Check if stable-diffusion-webui's folder exits
if [ ! -d $code_path ]; then
cd $installation_path
git clone --depth=1 $sd_webui_url
echo_green "Code has been installed successfully"
else
echo_green "Code has already been downloaded"
fi
# Enter the SD's folder
cd $code_path
if [ "$update_mode" == true ]; then
git reset --hard
git pull
fi
if [ "$network_connected" == false ]; then
sed -i '' "s/https:\/\/github.com/https:\/\/ghproxy.com\/github.com/g" modules/launch_utils.py
else
sed -i '' "s/https:\/\/ghproxy.com\/github.com/https:\/\/github.com/g" modules/launch_utils.py
fi
echo
echo "############ Create virtual env ######################"
# Check if the env exits
if micromamba env list | grep sd >/dev/null; then
echo_green "The sd env already exists"
else
yes | micromamba create -n sd python=3.10
echo_green "The sd env has been created successfully"
fi
# Activate sd env
echo_green "Activate sd env"
eval "$($mamba_path shell hook --shell posix)"
micromamba activate sd
# micromamba install basicsr=1.4.2 xformers=0.0.20
if [ ! -d "venv" ]; then
# Activate pyvenv to update pip to avoid some errors
python -m venv venv
fi
source venv/bin/activate
# Delete pip cache to avoid some errors
pip cache purge
if [ "$network_connected" == false ]; then
# Tsinghua mirror has no tb-nightly package, which is needed by basicsr
# TODO: reset in clean_up
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
pip config set global.extra-index-url https://pypi.tuna.tsinghua.edu.cn/simple
# pip install torch
else
pip config set global.index-url https://pypi.org/simple
fi
if [[ $(uname -p) != 'arm' ]]; then
pip install --upgrade pip setuptools
pip install basicsr==1.4.2
fi
# Install required packages via micromamba
# micromamba install --yes --file requirements_versions.txt
# Fix issue https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/12210
if [ "$network_connected" == false ]; then
echo >>webui-macos-env.sh
echo "export TORCH_COMMAND=\"pip install wheel==0.41.1 torch==2.0.1 torchvision==0.15.2 cython && pip install git+https://ghproxy.com/https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379 --prefer-binary\"" >>webui-macos-env.sh
fi
# Install Stable Diffusion
echo "############ 开始安装 Stable Diffusion ################"
echo "############ Start to install Stable Diffusion ######"
./webui.sh
clean_up
echo "############ Install Stable Diffusion successfully ###"