how to use negative prompt?

#90
by manadopeee - opened

import torch
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler

model_id = "stabilityai/stable-diffusion-2-1"

pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]

image.save("astronaut_rides_horse.png")

This example lacks instructions on the use of negative prompts

delete # Use the DPMSolverMultistepScheduler (DPM-Solver++) scheduler here instead

You're simply passing the negative prompt through a diffusers library pipeline class import so you can use the same parameter syntax given here: https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline.__call

Great part about that is you can modify other parameters as well.

Updated example code below w/ usage of negative prompt:

from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler

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 = "a photo of an astronaut riding a horse on mars"
neg_prompt = "((white astronaut suit)), (((distorted limbs))), (((bad proportions))), ((extra limbs))"

images = pipe(prompt, negative_prompt=neg_prompt).images[0]

images.save("astronaut_rides_horse_with_neg_prompt.png")

Sign up or log in to comment