-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpredict.py
168 lines (145 loc) · 6 KB
/
predict.py
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
# Prediction interface for Cog ⚙️
# https://github.com/replicate/cog/blob/main/docs/python.md
from cog import BasePredictor, Input, Path
import os
import time
import torch
import subprocess
import numpy as np
from typing import List
from diffusers import FluxPipeline
from transformers import CLIPImageProcessor
from diffusers.pipelines.stable_diffusion.safety_checker import (
StableDiffusionSafetyChecker
)
MODEL_CACHE = "FLUX.1-schnell"
MODEL_URL = "https://weights.replicate.delivery/default/black-forest-labs/FLUX.1-schnell/files.tar"
SAFETY_CACHE = "safety-cache"
FEATURE_EXTRACTOR = "/src/feature-extractor"
SAFETY_URL = "https://weights.replicate.delivery/default/sdxl/safety-1.0.tar"
ASPECT_RATIOS = {
"1:1": (1024, 1024),
"16:9": (1344, 768),
"21:9": (1536, 640),
"3:2": (1216, 832),
"2:3": (832, 1216),
"4:5": (896, 1088),
"5:4": (1088, 896),
"3:4": (896, 1152),
"4:3": (1152, 896),
"9:16": (768, 1344),
"9:21": (640, 1536),
}
def download_weights(url, dest):
start = time.time()
print("downloading url: ", url)
print("downloading to: ", dest)
subprocess.check_call(["pget", "-xf", url, dest], close_fds=False)
print("downloading took: ", time.time() - start)
class Predictor(BasePredictor):
def setup(self) -> None:
"""Load the model into memory to make running multiple predictions efficient"""
start = time.time()
# os.environ["TRANSFORMERS_OFFLINE"] = "1"
print("Loading safety checker...")
if not os.path.exists(SAFETY_CACHE):
download_weights(SAFETY_URL, SAFETY_CACHE)
self.safety_checker = StableDiffusionSafetyChecker.from_pretrained(
SAFETY_CACHE, torch_dtype=torch.float16
).to("cuda")
self.feature_extractor = CLIPImageProcessor.from_pretrained(FEATURE_EXTRACTOR)
print("Loading Flux txt2img Pipeline")
if not os.path.exists(MODEL_CACHE):
download_weights(MODEL_URL, ".")
self.txt2img_pipe = FluxPipeline.from_pretrained(
MODEL_CACHE,
torch_dtype=torch.bfloat16
).to("cuda")
# Save some VRAM by offloading the model to CPU
vram = int(torch.cuda.get_device_properties(0).total_memory/(1024*1024*1024))
if vram < 40:
print("GPU VRAM < 40Gb - Offloading model to CPU")
self.txt2img_pipe.enable_model_cpu_offload()
print("setup took: ", time.time() - start)
@torch.amp.autocast('cuda')
def run_safety_checker(self, image):
safety_checker_input = self.feature_extractor(image, return_tensors="pt").to("cuda")
np_image = [np.array(val) for val in image]
image, has_nsfw_concept = self.safety_checker(
images=np_image,
clip_input=safety_checker_input.pixel_values.to(torch.float16),
)
return image, has_nsfw_concept
def aspect_ratio_to_width_height(self, aspect_ratio: str) -> tuple[int, int]:
return ASPECT_RATIOS[aspect_ratio]
@torch.inference_mode()
def predict(
self,
prompt: str = Input(description="Prompt for generated image"),
aspect_ratio: str = Input(
description="Aspect ratio for the generated image",
choices=list(ASPECT_RATIOS.keys()),
default="1:1"),
num_outputs: int = Input(
description="Number of images to output.",
ge=1,
le=4,
default=1,
),
seed: int = Input(description="Random seed. Set for reproducible generation", default=None),
output_format: str = Input(
description="Format of the output images",
choices=["webp", "jpg", "png"],
default="webp",
),
output_quality: int = Input(
description="Quality when saving the output images, from 0 to 100. 100 is best quality, 0 is lowest quality. Not relevant for .png outputs",
default=80,
ge=0,
le=100,
),
disable_safety_checker: bool = Input(
description="Disable safety checker for generated images. This feature is only available through the API. See [https://replicate.com/docs/how-does-replicate-work#safety](https://replicate.com/docs/how-does-replicate-work#safety)",
default=False,
),
) -> 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}")
width, height = self.aspect_ratio_to_width_height(aspect_ratio)
guidance_scale=0.0
num_inference_steps=4
max_sequence_length=256
flux_kwargs = {}
print(f"Prompt: {prompt}")
print("txt2img mode")
flux_kwargs["width"] = width
flux_kwargs["height"] = height
pipe = self.txt2img_pipe
generator = torch.Generator("cuda").manual_seed(seed)
common_args = {
"prompt": [prompt] * num_outputs,
"guidance_scale": guidance_scale,
"generator": generator,
"num_inference_steps": num_inference_steps,
"max_sequence_length": max_sequence_length,
"output_type": "pil"
}
output = pipe(**common_args, **flux_kwargs)
if not disable_safety_checker:
_, has_nsfw_content = self.run_safety_checker(output.images)
output_paths = []
for i, image in enumerate(output.images):
if not disable_safety_checker and has_nsfw_content[i]:
print(f"NSFW content detected in image {i}")
continue
output_path = f"/tmp/out-{i}.{output_format}"
if output_format != 'png':
image.save(output_path, quality=output_quality, optimize=True)
else:
image.save(output_path)
output_paths.append(Path(output_path))
if len(output_paths) == 0:
raise Exception("NSFW content detected. Try running it again, or try a different prompt.")
return output_paths