Stable Diffusion webUI 最全且简单配置指南
# 安装必要的库
!pip install gradio
# 导入stable diffusion模型和必要的库
import os
from gradio import utils
from gradio import blocks as gr
from stable_diffusion import StableDiffusionPipeline
# 初始化stable diffusion模型
def init_stable_diffusion():
global pipeline
model_path = "path/to/stable-diffusion-model"
device = "cuda" if torch.cuda.is_available() else "cpu"
pipeline = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16 if device == "cuda" else torch.float32, device=device).to(device)
# 加载图片并进行预测
def predict(image_path):
global pipeline
init_stable_diffusion()
image = utils.image.open(image_path, mode="RGB")
inpaint_image = pipeline.inpaint(image)
return inpaint_image
# 创建Gradio界面
gr.Blocks(
[
gr.Image(type="pil", label="Image to inpaint"),
gr.Image(type="pil", label="Inpainted Image"),
],
[predict],
"image_inpaint_demo",
title="Image Inpaint Demo",
).launch()
这段代码首先导入所需的库,然后初始化Stable Diffusion模型。用户可以通过Gradio界面上传需要去噪的图片,然后代码会调用模型进行去噪处理,并将结果展示出来。这个过程是交互式的,用户可以在上传图片后即时看到处理结果。
评论已关闭