功能
- stable-diffusion(文本生成图片)
- webui-win搭建(开启api+界面汉化)
- PaddleHub
- HuggingFace: SD2,中文-alibaba/EasyNLP
stable-diffusion-webui
下载与安装
环境相关下载
python(文档推荐:Install Python 3.10.6 (Newer version of Python does not support torch))
https://www.python.org/downloads/
git-win
https://git-scm.com/download
CUDA(根据电脑配置选择)
https://developer.nvidia.com/cuda-toolkit
nvidia驱动(根据电脑配置选择)
https://www.nvidia.com/Download/index.aspx项目部署相关下载
项目(具体部署以此项目文档为准)
https://github.com/AUTOMATIC1111/stable-diffusion-webui
汉化(操作流程参考此项目文档)
https://github.com/VinsonLaro/stable-diffusion-webui-chinese
模型(本示例选择:sd-v1-4.ckpt)
https://huggingface.co/CompVis/stable-diffusion-v-1-4-original备注:
环境安装查看其他文档
配置与启动
模型配置
复制到:stable-diffusion-webui\models\Stable-diffusion目录
webui-user.bat配置(根据情况添加)
set COMMANDLINE_ARGS= --api --no-halfcmd运行:webui-user.bat备注:
--api 开启sdapi
gui地址:http://127.0.0.1:7860/
api-docs地址:http://127.0.0.1:7860/docs
结果
PaddleHub
环境
pip install paddlepaddle-gpu
pip install paddlehub
代码
from PIL import Image
import paddlehub as hub
module = hub.Module(name='stable_diffusion')## 保存在demo目录
result = module.generate_image(text_prompts="clouds surround the mountains and Chinese palaces,sunshine,lake,overlook,overlook,unreal engine,light effect,Dream,Greg Rutkowski,James Gurney,artstation", output_dir='demo')
结果
HuggingFace--SD2
环境
pip install diffusers transformers accelerate scipy safetensors
代码
import torch
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import matplotlib.pyplot as plt
import matplotlib.image as mpimgdef show(image_path):# 使用 Matplotlib 加载图片文件image = mpimg.imread(image_path)# 显示图片plt.imshow(image)plt.axis('off') # 关闭坐标轴plt.show()model_id = "stabilityai/stable-diffusion-2-1"# Use the DPMSolverMultistepScheduler (DPM-Solver++) scheduler here instead
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")prompt = "clouds surround the mountains and Chinese palaces,sunshine,lake,overlook,overlook,unreal engine,light effect,Dream,Greg Rutkowski,James Gurney,artstation"
image = pipe(prompt).images[0]image.save("test.png")
show('test.png')
结果
HuggingFace--中文-alibaba/EasyNLP
代码
from diffusers import StableDiffusionPipeline### 环境使用SD2model_id = "alibaba-pai/pai-diffusion-artist-large-zh"
pipe = StableDiffusionPipeline.from_pretrained(model_id)
pipe = pipe.to("cuda")prompt = "云环绕群山和中国宫殿,阳光,湖泊,俯瞰,俯瞰,虚幻的引擎,灯光效果,梦想,格雷格·鲁特考斯基,詹姆斯·格尼,艺术站"
image = pipe(prompt).images[0]
image.save("result.png")
show('result.png')
结果