forked from replicate/cog-stable-diffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
188 lines (169 loc) · 6.86 KB
/
predict.py
File metadata and controls
188 lines (169 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import os
from typing import List
import torch
from diffusers import (
PNDMScheduler,
LMSDiscreteScheduler,
DDIMScheduler,
StableDiffusionPipeline,
StableDiffusionImg2ImgPipeline,
StableDiffusionInpaintPipelineLegacy,
)
from PIL import Image
import PIL.ImageOps
import base64
from io import BytesIO
from cog import BasePredictor, Input, Path
MODEL_CACHE = "diffusers-cache"
class Predictor(BasePredictor):
def setup(self):
"""Load the model into memory to make running multiple predictions efficient"""
print("Loading pipeline...")
self.txt2img_pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
cache_dir=MODEL_CACHE,
local_files_only=True,
).to("cuda")
self.img2img_pipe = StableDiffusionImg2ImgPipeline(
vae=self.txt2img_pipe.vae,
text_encoder=self.txt2img_pipe.text_encoder,
tokenizer=self.txt2img_pipe.tokenizer,
unet=self.txt2img_pipe.unet,
scheduler=self.txt2img_pipe.scheduler,
safety_checker=self.txt2img_pipe.safety_checker,
feature_extractor=self.txt2img_pipe.feature_extractor,
).to("cuda")
self.inpaint_pipe = StableDiffusionInpaintPipelineLegacy(
vae=self.txt2img_pipe.vae,
text_encoder=self.txt2img_pipe.text_encoder,
tokenizer=self.txt2img_pipe.tokenizer,
unet=self.txt2img_pipe.unet,
scheduler=self.txt2img_pipe.scheduler,
safety_checker=self.txt2img_pipe.safety_checker,
feature_extractor=self.txt2img_pipe.feature_extractor,
).to("cuda")
@torch.inference_mode()
@torch.cuda.amp.autocast()
def predict(
self,
prompt: str = Input(description="Input prompt", default=""),
width: int = Input(
description="Width of output image. Maximum size is 1024x768 or 768x1024 because of memory limits",
choices=[128, 256, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024],
default=512,
),
height: int = Input(
description="Height of output image. Maximum size is 1024x768 or 768x1024 because of memory limits",
choices=[128, 256, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024],
default=512,
),
init_image: str = Input(
description="Initial image to generate variations of. Will be resized to the specified width and height",
default=None,
),
mask: str = Input(
description="Black and white image to use as mask for inpainting over init_image. Black pixels are inpainted and white pixels are preserved. Tends to work better with prompt strength of 0.5-0.7. Consider using https://replicate.com/andreasjansson/stable-diffusion-inpainting instead.",
default=None,
),
prompt_strength: float = Input(
description="Prompt strength when using init image. 1.0 corresponds to full destruction of information in init image",
default=0.8,
),
num_outputs: int = Input(
description="Number of images to output. If the NSFW filter is triggered, you may get fewer outputs than this.",
ge=1,
le=10,
default=1
),
num_inference_steps: int = Input(
description="Number of denoising steps", ge=1, le=500, default=50
),
guidance_scale: float = Input(
description="Scale for classifier-free guidance", ge=1, le=20, default=7.5
),
scheduler: str = Input(
default="K-LMS",
choices=["DDIM", "K-LMS", "PNDM"],
description="Choose a scheduler. If you use an init image, PNDM will be used",
),
seed: int = Input(
description="Random seed. Leave blank to randomize the seed", default=None
),
) -> List[Path]:
"""Run a single prediction on the model"""
if seed is None:
seed = int.from_bytes(os.urandom(2), "big")
print(f"Using seed: {seed}")
if width * height > 786432:
raise ValueError(
"Maximum size is 1024x768 or 768x1024 pixels, because of memory limits. Please select a lower width or height."
)
extra_kwargs = {}
if mask:
if not init_image:
raise ValueError("mask was provided without init_image")
pipe = self.inpaint_pipe
init_image = Image.open(BytesIO(base64.b64decode(init_image))).convert("RGB")
mask = Image.open(BytesIO(base64.b64decode(mask)))
red, green, blue, mask = mask.split()
mask = PIL.ImageOps.invert(mask)
extra_kwargs = {
"mask_image": mask,
"init_image": init_image,
"strength": prompt_strength,
}
elif init_image:
pipe = self.img2img_pipe
init_image = Image.open(BytesIO(base64.b64decode(init_image))).convert("RGB")
extra_kwargs = {
"init_image": init_image,
"strength": prompt_strength,
}
else:
pipe = self.txt2img_pipe
pipe.scheduler = make_scheduler(scheduler)
generator = torch.Generator("cuda").manual_seed(seed)
output = pipe(
prompt=[prompt] * num_outputs if prompt is not None else None,
width=width,
height=height,
guidance_scale=guidance_scale,
generator=generator,
num_inference_steps=num_inference_steps,
**extra_kwargs,
)
samples = [
output.images[i]
for i, nsfw_flag in enumerate(output.nsfw_content_detected)
if not nsfw_flag
]
if len(samples) == 0:
raise Exception(
f"NSFW content detected. Try running it again, or try a different prompt."
)
if num_outputs > len(samples):
print(
f"NSFW content detected in {num_outputs - len(samples)} outputs, showing the rest {len(samples)} images..."
)
output_paths = []
for i, sample in enumerate(samples):
output_path = f"/tmp/out-{i}.png"
sample.save(output_path)
output_paths.append(Path(output_path))
return output_paths
def make_scheduler(name):
return {
"PNDM": PNDMScheduler(
beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear"
),
"K-LMS": LMSDiscreteScheduler(
beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear"
),
"DDIM": DDIMScheduler(
beta_start=0.00085,
beta_end=0.012,
beta_schedule="scaled_linear",
clip_sample=False,
set_alpha_to_one=False,
),
}[name]