Python AI 绘画
本文我们将为大家介绍如何基于一些开源的库来搭建一套自己的 AI 作图工具。
需要使用的开源库为 Stable Diffusion web UI,它是基于 Gradio 库的 Stable Diffusion 浏览器界面
Stable Diffusion web UI GitHub 地址:GitHub - AUTOMATIC1111/stable-diffusion-webui: Stable Diffusion web UI
运行 Stable Diffusion 需要硬件要求比较高,运行时会消耗较大的资源,特别是显卡。
Windows 环境安装
本地环境要求安装 Python 3.10.6 或以上版本,并把它加入到本机的环境变量中。
下载 Stable Diffusion web UI GitHub 源码 GitHub - AUTOMATIC1111/stable-diffusion-webui: Stable Diffusion web UI。
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
如果没有安装 Git ,可以在右上角下载 zip 压缩包。

解压 stable-diffusion-webui,并进入 stable-diffusion-webui 目录。
接下来我们需要下载模型,下载地址:https://huggingface.co/CompVis/stable-diffusion-v-1-4-original

将下载的模型移动到 stable-diffusion-webui/models/Stable-diffusion 目录下。
进入 stable-diffusion-webui 目录:
Windows 使用非管理员运行:
webui-user.bat
Linux 和 Mac OS 环境执行以下命令:
./webui.sh
接下来程序就会自动安装并启动,启动成功会看到一个可访问的 URL 地址 http://127.0.0.1:7860:

访问 http://127.0.0.1:7860,界面如下:

注意: 如果安装出现卡住不动,很可能是下载 Github 源码出现问题了,可以使用 Github 的一些镜像来解决,目前还没非常稳定的镜像,建议 Google 搜索下。 我这边在 2023 年 4 月 6 号使用以下镜像地址 https://hub.fgit.ml,打开 stable-diffusion-webui 目录的 launch.py 文件,替换以下部分代码的 Github 地址(代码大概在 230~240 行之间):
Civitai 介绍
Civitai 有许多定制好的模型,而且可以免费下载,我们使用国风3 模型来测试,下载地址:https://civitai.com/models/10415/3-guofeng3?modelVersionId=36644

下载完后,我们将模型移动到 stable-diffusion-webui/models/Stable-diffusion 目录下,重新启动 stable-diffusion-webui :
./webui.sh
这样我们就可以在模型列表中选择国风3模型了:

选择后,我们可以到模型介绍页面去拷贝一些提示词和测试参数:



为了快速生成,我高度跟宽度都减半了,之后点生成按钮即可:
