Stable Diffusion webui api介绍 网站调用stable diffuion接口教程

古风汉服美女图集

大家好,我是Stable Diffusion攻略的小编。在今天的文章中,我将为您介绍Stable Diffusion WebUI和API的基本内容,以及如何搭建和使用这一强大的工具,以满足您的创作需求。Stable Diffusion webui api介绍 网站调用stable diffuion接口教程

安装环境依赖

在使用Stable Diffusion WebUI和API之前,首先需要安装必要的环境依赖。以下是一些必要的步骤:

安装显卡驱动

要使用CUDA,您需要安装Nvidia驱动程序。请执行以下命令:

sudo apt update
sudo apt purge *nvidia*
# 列出适用于您的GPU的可用驱动程序
ubuntu-drivers list
sudo apt install nvidia-driver-525
reboot

安装CUDA

按照NVIDIA Developers的说明,安装CUDA并重启机器:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda-repo-ubuntu2204-12-1-local_12.1.0-530.30.02-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2204-12-1-local_12.1.0-530.30.02-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2204-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda

验证安装

使用以下命令验证驱动和CUDA的安装情况:

nvidia-smi
nvcc --version

安装Python、Wget和Git

安装Python、Wget和Git,以便后续的配置和操作:

sudo apt install python3 python3-pip python3-virtualenv wget git

安装Conda

为了配置不同版本的Python虚拟环境,您可以安装Conda:

wgt https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
# 确认所有设置并安装

创建Python虚拟环境

使用Conda创建一个Python虚拟环境,例如:

conda create --name sdwebui python=3.10.6

搭建Stable Diffusion WebUI环境

现在,让我们来搭建Stable Diffusion WebUI环境,以便进行图像生成和创作。

克隆Stable Diffusion WebUI代码

首先,将Stable Diffusion WebUI的代码克隆到您的机器上:

cd ~
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git

进入虚拟环境

激活刚刚创建的Python虚拟环境:

conda activate sdwebui

设置启动参数

根据您的电脑配置,编辑webui_user.sh文件并设置相应的参数。您可以根据GPU显存的大小来调整参数以获得最佳性能。

安装Stable Diffusion WebUI依赖

运行以下命令以自动安装所需的依赖项:

bash webui.sh

如果一切顺利,依赖项将被安装并且WebUI将成功启动。您可以在浏览器中打开相应的链接地址,开始使用WebUI进行图像生成和创作。

如果克隆代码时出现问题,您还可以手动下载相应的仓库并将其放置在正确的位置,具体的依赖仓库地址可以参考lanch.py文件。

安装插件

您还可以为Stable Diffusion WebUI安装各种插件,以扩展其功能。找到各种插件对应的Git仓库,将代码克隆下来并放置在stable-diffusion-webui/extensions文件夹下。

配置模型

下载所需类型的模型文件,并将其放置在stable-diffusion-webui/models文件夹的相应目录下。

使用API模式

如果您更倾向于使用API模式,可以按照以下步骤进行操作:

启动Stable Diffusion WebUI

运行以下命令以启动Stable Diffusion WebUI,但不启动WebUI界面:

bash webui.sh --nowebui

调用API

通过向API的接口发送POST请求,您可以使用API进行图像生成。以下是一个示例代码:

import json
import base64
import requests

def submit_post(url: str, data: dict):
return requests.post(url, data=json.dumps(data)

def save_encoded_image(b64_image: str, output_path: str):
with open(output_path, 'wb') as image_file:
image_file.write(base64.b64decode(b64_image)

if __name__ == '__main__':
txt2img_url = r'http://127.0.0.1:7861/sdapi/v1/txt2img'

data = {'prompt': 'a dog wearing a hat',
'negative_prompt': '',
'sampler_index': 'DPM++ SDE',
'seed': 1234,
'steps': 20,
'width': 512,
'height': 512,
'cfg_scale': 8}

response = submit_post(txt2img_url, data)
save_image_path = r'tmp.png'
save_encoded_image(response.json()['images'][0], save_image_path)

© 版权声明

相关文章