takuma104's picture
update example
79ccfb0
raw
history blame
2.23 kB
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
from diffusers import UniPCMultistepScheduler
import gradio as gr
import torch
# Models
controlnet_pose = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-openpose", torch_dtype=torch.float16
)
controlnet_canny = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=[controlnet_pose, controlnet_canny],
safety_checker=None, torch_dtype=torch.float16
).to('cuda')
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
# This command loads the individual model components on GPU on-demand. So, we don't
# need to explicitly call pipe.to("cuda").
#pipe.enable_model_cpu_offload()
# xformers
pipe.enable_xformers_memory_efficient_attention()
# Generator seed,
generator = torch.manual_seed(3)
def generate_images(pose_image, canny_image, prompt):
output = pipe(
prompt,
[pose_image, canny_image],
generator=generator,
num_images_per_prompt=3,
num_inference_steps=20,
)
all_outputs = []
all_outputs.append(pose_image)
all_outputs.append(canny_image)
for image in output.images:
all_outputs.append(image)
return all_outputs
gr.Interface(
generate_images,
inputs=[
gr.Image(type="pil"),
gr.Image(type="pil"),
gr.Textbox(
label="Enter your prompt",
max_lines=1,
placeholder="masterpiece, a professional portrait of woman wearing white shirts",
),
],
outputs=gr.Gallery().style(grid=[2], height="auto"),
title="Generate controlled outputs with Mult-ControlNet and Stable Diffusion using πŸ€—Diffusers",
description="This Space uses pose lines and canny edged image as the additional conditioning. Please refer to the \"Examples\" for what kind of images are appropriate.",
examples=[
["p2_clip.png",
"c2_clip.png",
"masterpiece, a professional portrait of woman wearing white shirts"
],
],
allow_flagging=False,
).launch(enable_queue=True)