跳到主要内容

简单入门

资源官网开源仓库
stable-diffusion-webui简单版https://sd-webui.github.io/stable-diffusion-webui/https://github.com/sd-webui/stable-diffusion-webui
stable-diffusion-webui综合版https://github.com/AUTOMATIC1111/stable-diffusion-webui
Linux安装stableDiffusion脚本https://github.com/JoshuaKimsey/Linux-StableDiffusion-Script/blob/main/linux-sd.sh
模型https://huggingface.co/CompVis/stable-diffusion-v1-4
original模型https://huggingface.co/CompVis/stable-diffusion-v-1-4-original
colabhttps://colab.research.google.com/github/altryne/sd-webui-colab/blob/main/Stable_Diffusion_WebUi_Altryne.ipynb
huggingface Tokenhttps://huggingface.co/settings/tokens
图像翻译,条件GAN,AI绘画https://arxiv.org/abs/1903.10146https://github.com/youyuge34/PI-REC

具体教程

第一步

在你的系统中创建一个目录,将脚本放进这个文件夹下,命名为linux-sd.sh. 这个目录将会成为Stable Diffusion库的下载位置。

linux-sd.sh
#!/bin/bash -i

# Linux Stable Diffusion Script

# Version: 2.0

# MIT License

# Copyright (c) 2022 Joshua Kimsey

##### Please See My Guide For Running This Script Here: https://rentry.org/linux-sd #####

# Confirmed working as of September 22nd, 2022. May be subject to breakage at a later date due to bleeding-edge updates in the Stable Diffusion WebUI repo
# Please see my GitHub for updates on this script: https://github.com/JoshuaKimsey/Linux-StableDiffusion-Script

printf "\n\n\n"
printf "WELCOME TO THE ULTIMATE STABLE DIFFUSION WEB GUI ON LINUX"
printf "\n\n"
printf "The definitive Stable Diffusion experience™ Now 100% Linux Compatible!"
printf "\n"
printf "Please ensure you have Anaconda installed properly on your Linux system before running this."
printf "\n"
printf "Please refer to the original guide for more info and additional links for this project: https://rentry.org/guitard"
printf "\n\n"

DIRECTORY=./stable-diffusion-webui
REPO=https://github.com/sd-webui/stable-diffusion-webui.git
ENV=ldm

ultimate_stable_diffusion_repo () {
# Check to see if Ultimate Stable Diffusion repo is cloned
if [ -d "$DIRECTORY" ]; then
printf "\n\n########## CHECK FOR UPDATES ##########\n\n"
printf "Ultimate Stable Diffusion already exists. Do you want to update Ultimate Stable Diffusion?\n"
printf "(This will reset your launch arguments and they will need to be set again after updating)\n"
select yn in "Yes" "No"; do
case $yn in
Yes ) printf "Pulling updates for the Stable Diffusion WebUI. Please wait...\n"; ultimate_stable_diffusion_repo_update; break;;
No ) printf "Stable Diffusion WebUI will not be updated. Continuing...\n"; break;;
esac
done
else
printf "Cloning Ultimate Stable Diffusion. Please wait..."
git clone $REPO
cp $DIRECTORY/scripts/relauncher.py $DIRECTORY/scripts/relauncher-backup.py
fi
}

ultimate_stable_diffusion_repo_update () {
cd $DIRECTORY
git fetch --all
git reset --hard origin/master
cp ./scripts/relauncher.py ./scripts/relauncher-backup.py
cd ..;
}

linux_setup_script () {
cd $DIRECTORY
printf "Running webui.sh...\n\n"
bash -i ./webui.sh
}

# Checks to see which mode Ultimate Stable Diffusion is running in: STANDARD or OPTIMIZED
# Then asks the user which mode they wish to use
gradio_stable_diffusion_arguments () {
if [ "$1" = "customize" ]; then
printf "Do you want extra upscaling models to be run on the CPU instead of the GPU to save on VRAM at the cost of speed?\n"
select yn in "Yes" "No"; do
case $yn in
Yes ) printf "Setting extra upscaling models to use the CPU...\n"; sed -i 's/extra_models_cpu = False/extra_models_cpu = True/g' $DIRECTORY/scripts/relauncher.py; break;;
No ) printf "Extra upscaling models will run on the GPU. Continuing...\n"; sed -i 's/extra_models_cpu = True/extra_models_cpu = False/g' $DIRECTORY/scripts/relauncher.py; break;;
esac
done
printf "\n\n"
printf "Do you want for Ultimate Stable Diffusion to automatically launch a new browser window or tab on first launch?\n"
select yn in "Yes" "No"; do
case $yn in
Yes ) printf "Setting Ultimate Stable Diffusion to open a new browser window/tab at first launch...\n"; sed -i 's/open_in_browser = False/open_in_browser = True/g' $DIRECTORY/scripts/relauncher.py; break;;
No ) printf "Ultimate Stable Diffusion will not open automatically in a new browser window/tab. Continuing...\n"; sed -i 's/open_in_browser = True/open_in_browser = False/g' $DIRECTORY/scripts/relauncher.py; break;;
esac
done
printf "\n\n"
printf "Do you want to run Ultimate Stable Diffusion in Optimized mode - Requires only 4GB of VRAM, but is significantly slower?\n"
select yn in "Yes" "No"; do
case $yn in
Yes ) printf "Setting Ultimate Stable Diffusion to run in Optimized Mode...\n"; sed -i 's/optimized = False/optimized = True/g' $DIRECTORY/scripts/relauncher.py; break;;
No ) printf "Ultimate Stable Diffusion will launch in Standard Mode. Continuing...\n"; sed -i 's/optimized = True/optimized = False/g' $DIRECTORY/scripts/relauncher.py; break;;
esac
done
printf "\n\n"
printf "Do you want to start Ultimate Stable Diffusion in Optimized Turbo mode - Requires more VRAM than regular optimized, but is faster (incompatible with Optimized Mode)?\n"
select yn in "Yes" "No"; do
case $yn in
Yes ) printf "Setting Ultimate Stable Diffusion to run in Optimized Turbo mode...\n"; sed -i 's/optimized_turbo = False/optimized_turbo = True/g' $DIRECTORY/scripts/relauncher.py; break;;
No ) printf "Ultimate Stable Diffusion will launch in Standard Mode. Continuing...\n"; sed -i 's/optimized_turbo = True/optimized_turbo = False/g' $DIRECTORY/scripts/relauncher.py; break;;
esac
done
printf "\n\n"
printf "Do you want to create a public xxxxx.gradi.app URL to allow others to uses your interface? (Requires properly forwarded ports)\n"
select yn in "Yes" "No"; do
case $yn in
Yes ) printf "Setting Ultimate Stable Diffusion to open a public share URL...\n"; sed -i 's/share = False/share = True/g' $DIRECTORY/scripts/relauncher.py; break;;
No ) printf "Setting Ultimate Stable Diffusion to not open a public share URL. Continuing...\n"; sed -i 's/share = True/share = False/g' $DIRECTORY/scripts/relauncher.py; break;;
esac
done
printf "\n\nCustomization of Ultimate Stable Diffusion is complete. Continuing...\n\n"
else
printf "\n\n########## GRADIO CUSTOMIZATION ##########\nPlease Note: These Arguments Only Affect The Gradio Interface Version Of The Stable Diffusion Webui.\n\n"
printf "Do you wish to customize the launch arguments for the Gradio Webui Interface?\n"
printf "(This will be where you select Optimized mode, auto open in browser, share to public, and more.)\n"
select yn in "Yes" "No"; do
case $yn in
Yes ) printf "Starting customization of Gradio Interface launch arguments...\n"; gradio_stable_diffusion_arguments customize; break;;
No ) printf "Maintaining current Gradio Interface launch arguments...\n"; break;;
esac
done
fi
}

# Function to install and run the Ultimate Stable Diffusion fork
ultimate_stable_diffusion () {
if [ "$1" = "initial" ]; then
ultimate_stable_diffusion_repo
gradio_stable_diffusion_arguments
linux_setup_script
else
if [[ $(conda env list | grep "$ENV") = $ENV* ]]; then
printf "\n\n########## RUN PREVIOUS SETUP ##########\n\n"
printf "Do you wish to run Ultimate Stable Diffusion with the previous parameters?\n"
printf "(Select NO to customize or update your Ultimate Stable Diffusion setup)\n"
select yn in "Yes" "No"; do
case $yn in
Yes ) printf "Starting Ultimate Stable Diffusion using previous parameters. Please wait..."; linux_setup_script; break;;
No ) printf "Beginning customization of Ultimate Stable Diffusion..."; ultimate_stable_diffusion initial; break;;
esac
done
else
printf "ERROR: Conda Env not found. Will attempt to rebuild, please go through the update steps below...\n"
ultimate_stable_diffusion initial
fi
fi
}

# Initialization
if [ ! -d "$DIRECTORY" ]; then
printf "Starting Ultimate Stable Diffusion installation..."
printf "\n"
ultimate_stable_diffusion initial
else
ultimate_stable_diffusion
fi

第二步

1.4 AI model from HuggingFace模型下载到脚本同级目录。

模型地址

第三步

给脚本授权令其可执行(在脚本同级目录下,打开控制台,输入chmod +x linux-sd.sh,或者别的什么你命名的名字)。

第四步

执行linux-sd.sh脚本。这个脚本将会克隆WebUI Github的仓库到你的脚本同级目录。并且这个目录会被命名为stable-diffusion-webui

第五步

这个脚本会先暂时暂停并且询问你是移动还是复制下载的1.4 AI模型到stable-diffusion-webui文件夹下。选择好后请按回车以使程序可以继续运行。

如果你在一个储存很小的电脑上运行,那么你可以仅仅是移动1.4 AI模型到stable-diffusion-webui文件夹下,它不会被删除,仅仅是简单的移动和被重命名。但是我个人建议你选择复制到仓库文件夹,如果你需要删除并且再次重新打包你的Stable Diffusion

第六步

接下来,这个脚本将会询问你是否想要自定义一些Gradio WebUI界面的启动参数。如果是,那么一系列的选项将会被呈现给用户:

  • 将CPU用于额外的高级模型,以节省虚拟内存(VRAM)
  • 在第一次运行的时候打开浏览器窗口
  • 使用优化模式的终极稳定扩散,这只需要4GB的虚拟内存(VRAM)以速度为代价
  • 使用优化涡轮,它比常规优化使用更多的VRAM,但速度更快(与常规优化模式不兼容)
  • 打开一个公共的xxxx.gradient.appURL与他人共享你的界面(请小心,这是一个潜在的安全风险)

用户将可以通过菜单设置这些选项。

备注

这些只会应用在Gradio WebUI界面上。Streamlit接口版本有/将有能力从web内部设置自己的首选项。

第七步

脚本将会继续执行仓库文件夹中的webui.sh文件。这将会管理conda环境的创建和更新,命名为ldm,同时管理StreamlitGradio使用的更高级的模型的下载。这也会下载概念库为了在Streamlit版本中使用。

创建conda环境根据你的网络环境和系统规格,可能会花费将近15分钟。这是正常的,喝一杯咖啡然后等他完成吧。肉果你正在尝试更新,并且脚本正在安装PIP依赖,那么你需要停止对应的脚本,删除你的src文件夹,然后重新运行linux-sd.sh.

第八步

一旦conda环境成功被创建了,并且高级模型被下载了,那么一个WebUi界面版本的选择(Streamlit或者Gradio)将会被呈现给用户。

  • Streamlit

    • 有一个更加现代化的UI
    • 将会是将来主要的UI
    • 有更多的特性
    • 当前在积极的开发中
    • 缺少一些Gradio的特性
  • Gradio

    • 完善的功能特性
    • 使用更老些的界面交互
    • 不会收到重要更新

第九步

如果一切成功运行,那么一个浏览器窗口将会被打开(Streamlit版本),或者你应该会看见在你的终端中项目运行在http://localhost:7860/端口上(Gradio版本)。生成的图片将会被放置在stable-diffusion-webui目录的outputs目录下。开始享受吧。

具体教程(英文版)

step 1

Create a folder/directory on your system and place this script in it, named linux-sd.sh. This directory will be where the files for Stable Diffusion will be downloaded.

step 2

Download the 1.4 AI model from HuggingFace (or another location, the original guide has some links to mirrors of the model) and place it in the same directory as the script.

Modal Location Link

step 3

Make the script executable by opening the directory in your Terminal and typing chmod +x linux-sd.sh, or whatever you named this file as.

step 4

Run the script with ./linux-sd.sh, it will begin by cloning the WebUI Github Repo to the directory the script is located in. This folder will be named stable-diffusion-webui.

step 5

The script will pause and ask that you move/copy the downloaded 1.4 AI models to the stable-diffusion-webui folder. Press Enter once you have done so to continue.

If you are running low on storage space, you can just move the 1.4 AI models file directly to this directory, it will not be deleted, simply moved and renamed. However my personal suggestion is to just copy it to the repo folder, in case you desire to delete and rebuild your Stable Diffusion build again.

step 6

Next, the script will ask if you wish to customize any of the launch arguments for the Gradio WebUI Interface. If yes, then a series of options will be presented to the user:

  • Use the CPU for Extra Upscaler Models to save on VRAM - Automatically open a new browser window or tab on first launch
  • Use Optimized mode for Ultimate Stable Diffusion, which only requires 4GB of VRAM at the cost of speed
  • Use Optimized Turbo which uses more VRAM than regular optimized, but is faster (Incompatible with regular optimized mode)
  • Open a public xxxxx.gradi.app URL to share your interface with others (Please be careful with this, it is a potential security risk)

The user will have the ability to set these to yes or no using the menu choices.

备注

These only apply to the Gradio WebUI interface. The Streamlit Interface version has/will have the ability to set its own preferences from within the WebUI

step 7

The script will then proceed to call the webui.sh file within the repo folder. This will handle the creation and updating of the conda environment, named ldm, as well as handle the downloading of the upsclaer models used by both Streamlit and Gradio. It will also download the Concepts Library for using custom models in the Streamlit version.

Building the Conda environment may take upwards of 15 minutes, depending on your network connection and system specs. This is normal, just leave it be and let it finish. If you are trying to update and the script hangs at Installing PIP Dependencies for more than 10 minutes, you will need to Ctrl-C to stop the script, delete your src folder, and rerun linux-sd.sh again.

step 8

Once the conda environment has been created and the upscaler models have been downloaded, then the user is presented with a choice to choose between the Streamlit or Gradio versions of the WebUI Interface.

  • Streamlit:

    • Has A More Modern UI
    • More Features Planned
    • Will Be The Main UI Going Forward
    • Currently In Active Development
    • Missing Some Gradio Features
  • Gradio:

    • Currently Feature Complete
    • Uses An Older Interface Style
    • Will Not Receive Major Updates

step 9

If everything has gone successfully, either a new browser window will open with the Streamlit version, or you should see Running on local URL: http://localhost:7860/ in your Terminal if you launched the Gradio Interface version. Generated images will be located in the outputs directory inside of stable-diffusion-webui. Enjoy the definitive Stable Diffusion WebUI experience on Linux! :)