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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import (
|
2 |
+
StableDiffusionXLControlNetImg2ImgPipeline,
|
3 |
+
ControlNetModel,
|
4 |
+
AutoencoderKL,
|
5 |
+
AutoencoderTiny,
|
6 |
+
LCMScheduler,
|
7 |
+
)
|
8 |
+
from compel import Compel, ReturnedEmbeddingsType
|
9 |
+
import torch
|
10 |
+
from pipelines.utils.canny_gpu import SobelOperator
|
11 |
+
|
12 |
+
try:
|
13 |
+
import intel_extension_for_pytorch as ipex # type: ignore
|
14 |
+
except:
|
15 |
+
pass
|
16 |
+
|
17 |
+
import psutil
|
18 |
+
from config import Args
|
19 |
+
from pydantic import BaseModel, Field
|
20 |
+
from PIL import Image
|
21 |
+
import math
|
22 |
+
|
23 |
+
controlnet_model = "diffusers/controlnet-canny-sdxl-1.0"
|
24 |
+
base_model = "segmind/Segmind-Vega"
|
25 |
+
lora_model = "segmind/Segmind-VegaRT"
|
26 |
+
taesd_model = "madebyollin/taesdxl"
|
27 |
+
|
28 |
+
default_prompt = "Portrait of The Terminator with , glare pose, detailed, intricate, full of colour, cinematic lighting, trending on artstation, 8k, hyperrealistic, focused, extreme details, unreal engine 5 cinematic, masterpiece"
|
29 |
+
default_negative_prompt = "blurry, low quality, render, 3D, oversaturated"
|
30 |
+
page_content = """
|
31 |
+
<h1 class="text-3xl font-bold">Real-Time SegmindVegaRT</h1>
|
32 |
+
<h3 class="text-xl font-bold">Image-to-Image ControlNet</h3>
|
33 |
+
<p class="text-sm">
|
34 |
+
This demo showcases
|
35 |
+
<a
|
36 |
+
href="https://huggingface.co/segmind/Segmind-VegaRT"
|
37 |
+
target="_blank"
|
38 |
+
class="text-blue-500 underline hover:no-underline">Segmind-VegaRT</a>
|
39 |
+
Image to Image pipeline using
|
40 |
+
<a
|
41 |
+
href="https://huggingface.co/docs/diffusers/main/en/using-diffusers/sdxl_turbo"
|
42 |
+
target="_blank"
|
43 |
+
class="text-blue-500 underline hover:no-underline">Diffusers</a
|
44 |
+
> with a MJPEG stream server.
|
45 |
+
</p>
|
46 |
+
<p class="text-sm text-gray-500">
|
47 |
+
Change the prompt to generate different images, accepts <a
|
48 |
+
href="https://github.com/damian0815/compel/blob/main/doc/syntax.md"
|
49 |
+
target="_blank"
|
50 |
+
class="text-blue-500 underline hover:no-underline">Compel</a
|
51 |
+
> syntax.
|
52 |
+
</p>
|
53 |
+
"""
|
54 |
+
|
55 |
+
|
56 |
+
class Pipeline:
|
57 |
+
class Info(BaseModel):
|
58 |
+
name: str = "controlnet+SegmindVegaRT"
|
59 |
+
title: str = "SegmindVegaRT + Controlnet"
|
60 |
+
description: str = "Generates an image from a text prompt"
|
61 |
+
input_mode: str = "image"
|
62 |
+
page_content: str = page_content
|
63 |
+
|
64 |
+
class InputParams(BaseModel):
|
65 |
+
prompt: str = Field(
|
66 |
+
default_prompt,
|
67 |
+
title="Prompt",
|
68 |
+
field="textarea",
|
69 |
+
id="prompt",
|
70 |
+
)
|
71 |
+
negative_prompt: str = Field(
|
72 |
+
default_negative_prompt,
|
73 |
+
title="Negative Prompt",
|
74 |
+
field="textarea",
|
75 |
+
id="negative_prompt",
|
76 |
+
hide=True,
|
77 |
+
)
|
78 |
+
seed: int = Field(
|
79 |
+
2159232, min=0, title="Seed", field="seed", hide=True, id="seed"
|
80 |
+
)
|
81 |
+
steps: int = Field(
|
82 |
+
2, min=1, max=15, title="Steps", field="range", hide=True, id="steps"
|
83 |
+
)
|
84 |
+
width: int = Field(
|
85 |
+
1024, min=2, max=15, title="Width", disabled=True, hide=True, id="width"
|
86 |
+
)
|
87 |
+
height: int = Field(
|
88 |
+
1024, min=2, max=15, title="Height", disabled=True, hide=True, id="height"
|
89 |
+
)
|
90 |
+
guidance_scale: float = Field(
|
91 |
+
0.0,
|
92 |
+
min=0,
|
93 |
+
max=1,
|
94 |
+
step=0.001,
|
95 |
+
title="Guidance Scale",
|
96 |
+
field="range",
|
97 |
+
hide=True,
|
98 |
+
id="guidance_scale",
|
99 |
+
)
|
100 |
+
strength: float = Field(
|
101 |
+
0.5,
|
102 |
+
min=0.25,
|
103 |
+
max=1.0,
|
104 |
+
step=0.001,
|
105 |
+
title="Strength",
|
106 |
+
field="range",
|
107 |
+
hide=True,
|
108 |
+
id="strength",
|
109 |
+
)
|
110 |
+
controlnet_scale: float = Field(
|
111 |
+
0.5,
|
112 |
+
min=0,
|
113 |
+
max=1.0,
|
114 |
+
step=0.001,
|
115 |
+
title="Controlnet Scale",
|
116 |
+
field="range",
|
117 |
+
hide=True,
|
118 |
+
id="controlnet_scale",
|
119 |
+
)
|
120 |
+
controlnet_start: float = Field(
|
121 |
+
0.0,
|
122 |
+
min=0,
|
123 |
+
max=1.0,
|
124 |
+
step=0.001,
|
125 |
+
title="Controlnet Start",
|
126 |
+
field="range",
|
127 |
+
hide=True,
|
128 |
+
id="controlnet_start",
|
129 |
+
)
|
130 |
+
controlnet_end: float = Field(
|
131 |
+
1.0,
|
132 |
+
min=0,
|
133 |
+
max=1.0,
|
134 |
+
step=0.001,
|
135 |
+
title="Controlnet End",
|
136 |
+
field="range",
|
137 |
+
hide=True,
|
138 |
+
id="controlnet_end",
|
139 |
+
)
|
140 |
+
canny_low_threshold: float = Field(
|
141 |
+
0.31,
|
142 |
+
min=0,
|
143 |
+
max=1.0,
|
144 |
+
step=0.001,
|
145 |
+
title="Canny Low Threshold",
|
146 |
+
field="range",
|
147 |
+
hide=True,
|
148 |
+
id="canny_low_threshold",
|
149 |
+
)
|
150 |
+
canny_high_threshold: float = Field(
|
151 |
+
0.125,
|
152 |
+
min=0,
|
153 |
+
max=1.0,
|
154 |
+
step=0.001,
|
155 |
+
title="Canny High Threshold",
|
156 |
+
field="range",
|
157 |
+
hide=True,
|
158 |
+
id="canny_high_threshold",
|
159 |
+
)
|
160 |
+
debug_canny: bool = Field(
|
161 |
+
False,
|
162 |
+
title="Debug Canny",
|
163 |
+
field="checkbox",
|
164 |
+
hide=True,
|
165 |
+
id="debug_canny",
|
166 |
+
)
|
167 |
+
|
168 |
+
def __init__(self, args: Args, device: torch.device, torch_dtype: torch.dtype):
|
169 |
+
controlnet_canny = ControlNetModel.from_pretrained(
|
170 |
+
controlnet_model,
|
171 |
+
torch_dtype=torch_dtype,
|
172 |
+
).to(device)
|
173 |
+
vae = AutoencoderKL.from_pretrained(
|
174 |
+
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch_dtype
|
175 |
+
)
|
176 |
+
if args.safety_checker:
|
177 |
+
self.pipe = StableDiffusionXLControlNetImg2ImgPipeline.from_pretrained(
|
178 |
+
base_model,
|
179 |
+
controlnet=controlnet_canny,
|
180 |
+
vae=vae,
|
181 |
+
)
|
182 |
+
else:
|
183 |
+
self.pipe = StableDiffusionXLControlNetImg2ImgPipeline.from_pretrained(
|
184 |
+
base_model,
|
185 |
+
safety_checker=None,
|
186 |
+
controlnet=controlnet_canny,
|
187 |
+
vae=vae,
|
188 |
+
)
|
189 |
+
self.canny_torch = SobelOperator(device=device)
|
190 |
+
|
191 |
+
self.pipe.load_lora_weights(lora_model)
|
192 |
+
self.pipe.fuse_lora()
|
193 |
+
self.pipe.scheduler = LCMScheduler.from_pretrained(
|
194 |
+
base_model, subfolder="scheduler"
|
195 |
+
)
|
196 |
+
self.pipe.set_progress_bar_config(disable=True)
|
197 |
+
self.pipe.to(device=device, dtype=torch_dtype).to(device)
|
198 |
+
if device.type != "mps":
|
199 |
+
self.pipe.unet.to(memory_format=torch.channels_last)
|
200 |
+
|
201 |
+
if psutil.virtual_memory().total < 64 * 1024**3:
|
202 |
+
self.pipe.enable_attention_slicing()
|
203 |
+
|
204 |
+
if args.compel:
|
205 |
+
self.pipe.compel_proc = Compel(
|
206 |
+
tokenizer=[self.pipe.tokenizer, self.pipe.tokenizer_2],
|
207 |
+
text_encoder=[self.pipe.text_encoder, self.pipe.text_encoder_2],
|
208 |
+
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
|
209 |
+
requires_pooled=[False, True],
|
210 |
+
)
|
211 |
+
if args.use_taesd:
|
212 |
+
self.pipe.vae = AutoencoderTiny.from_pretrained(
|
213 |
+
taesd_model, torch_dtype=torch_dtype, use_safetensors=True
|
214 |
+
).to(device)
|
215 |
+
|
216 |
+
if args.torch_compile:
|
217 |
+
self.pipe.unet = torch.compile(
|
218 |
+
self.pipe.unet, mode="reduce-overhead", fullgraph=True
|
219 |
+
)
|
220 |
+
self.pipe.vae = torch.compile(
|
221 |
+
self.pipe.vae, mode="reduce-overhead", fullgraph=True
|
222 |
+
)
|
223 |
+
self.pipe(
|
224 |
+
prompt="warmup",
|
225 |
+
image=[Image.new("RGB", (768, 768))],
|
226 |
+
control_image=[Image.new("RGB", (768, 768))],
|
227 |
+
)
|
228 |
+
|
229 |
+
def predict(self, params: "Pipeline.InputParams") -> Image.Image:
|
230 |
+
generator = torch.manual_seed(params.seed)
|
231 |
+
|
232 |
+
prompt = params.prompt
|
233 |
+
negative_prompt = params.negative_prompt
|
234 |
+
prompt_embeds = None
|
235 |
+
pooled_prompt_embeds = None
|
236 |
+
negative_prompt_embeds = None
|
237 |
+
negative_pooled_prompt_embeds = None
|
238 |
+
if hasattr(self.pipe, "compel_proc"):
|
239 |
+
_prompt_embeds, pooled_prompt_embeds = self.pipe.compel_proc(
|
240 |
+
[params.prompt, params.negative_prompt]
|
241 |
+
)
|
242 |
+
prompt = None
|
243 |
+
negative_prompt = None
|
244 |
+
prompt_embeds = _prompt_embeds[0:1]
|
245 |
+
pooled_prompt_embeds = pooled_prompt_embeds[0:1]
|
246 |
+
negative_prompt_embeds = _prompt_embeds[1:2]
|
247 |
+
negative_pooled_prompt_embeds = pooled_prompt_embeds[1:2]
|
248 |
+
|
249 |
+
control_image = self.canny_torch(
|
250 |
+
params.image, params.canny_low_threshold, params.canny_high_threshold
|
251 |
+
)
|
252 |
+
steps = params.steps
|
253 |
+
strength = params.strength
|
254 |
+
if int(steps * strength) < 1:
|
255 |
+
steps = math.ceil(1 / max(0.10, strength))
|
256 |
+
|
257 |
+
results = self.pipe(
|
258 |
+
image=params.image,
|
259 |
+
control_image=control_image,
|
260 |
+
prompt=prompt,
|
261 |
+
negative_prompt=negative_prompt,
|
262 |
+
prompt_embeds=prompt_embeds,
|
263 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
264 |
+
negative_prompt_embeds=negative_prompt_embeds,
|
265 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
|
266 |
+
generator=generator,
|
267 |
+
strength=strength,
|
268 |
+
num_inference_steps=steps,
|
269 |
+
guidance_scale=params.guidance_scale,
|
270 |
+
width=params.width,
|
271 |
+
height=params.height,
|
272 |
+
output_type="pil",
|
273 |
+
controlnet_conditioning_scale=params.controlnet_scale,
|
274 |
+
control_guidance_start=params.controlnet_start,
|
275 |
+
control_guidance_end=params.controlnet_end,
|
276 |
+
)
|
277 |
+
|
278 |
+
nsfw_content_detected = (
|
279 |
+
results.nsfw_content_detected[0]
|
280 |
+
if "nsfw_content_detected" in results
|
281 |
+
else False
|
282 |
+
)
|
283 |
+
if nsfw_content_detected:
|
284 |
+
return None
|
285 |
+
result_image = results.images[0]
|
286 |
+
if params.debug_canny:
|
287 |
+
# paste control_image on top of result_image
|
288 |
+
w0, h0 = (200, 200)
|
289 |
+
control_image = control_image.resize((w0, h0))
|
290 |
+
w1, h1 = result_image.size
|
291 |
+
result_image.paste(control_image, (w1 - w0, h1 - h0))
|
292 |
+
|
293 |
+
return result_image
|
pipelines/img2imgSegmindVegaRT.py
CHANGED
@@ -24,14 +24,14 @@ taesd_model = "madebyollin/taesdxl"
|
|
24 |
default_prompt = "close-up photography of old man standing in the rain at night, in a street lit by lamps, leica 35mm summilux"
|
25 |
default_negative_prompt = "blurry, low quality, render, 3D, oversaturated"
|
26 |
page_content = """
|
27 |
-
<h1 class="text-3xl font-bold">Real-Time
|
28 |
<h3 class="text-xl font-bold">Image-to-Image</h3>
|
29 |
<p class="text-sm">
|
30 |
This demo showcases
|
31 |
<a
|
32 |
-
href="https://huggingface.co/
|
33 |
target="_blank"
|
34 |
-
class="text-blue-500 underline hover:no-underline">
|
35 |
Image to Image pipeline using
|
36 |
<a
|
37 |
href="https://huggingface.co/docs/diffusers/main/en/using-diffusers/sdxl_turbo"
|
@@ -84,9 +84,9 @@ class Pipeline:
|
|
84 |
1024, min=2, max=15, title="Height", disabled=True, hide=True, id="height"
|
85 |
)
|
86 |
guidance_scale: float = Field(
|
87 |
-
0.
|
88 |
min=0,
|
89 |
-
max=
|
90 |
step=0.001,
|
91 |
title="Guidance Scale",
|
92 |
field="range",
|
@@ -138,10 +138,10 @@ class Pipeline:
|
|
138 |
if args.torch_compile:
|
139 |
print("Running torch compile")
|
140 |
self.pipe.unet = torch.compile(
|
141 |
-
self.pipe.unet,
|
142 |
)
|
143 |
self.pipe.vae = torch.compile(
|
144 |
-
self.pipe.vae,
|
145 |
)
|
146 |
|
147 |
self.pipe(
|
@@ -165,14 +165,14 @@ class Pipeline:
|
|
165 |
negative_prompt_embeds = None
|
166 |
negative_pooled_prompt_embeds = None
|
167 |
if hasattr(self.pipe, "compel_proc"):
|
168 |
-
|
169 |
[params.prompt, params.negative_prompt]
|
170 |
)
|
171 |
prompt = None
|
172 |
negative_prompt = None
|
173 |
-
prompt_embeds =
|
174 |
pooled_prompt_embeds = pooled_prompt_embeds[0:1]
|
175 |
-
negative_prompt_embeds =
|
176 |
negative_pooled_prompt_embeds = pooled_prompt_embeds[1:2]
|
177 |
|
178 |
steps = params.steps
|
|
|
24 |
default_prompt = "close-up photography of old man standing in the rain at night, in a street lit by lamps, leica 35mm summilux"
|
25 |
default_negative_prompt = "blurry, low quality, render, 3D, oversaturated"
|
26 |
page_content = """
|
27 |
+
<h1 class="text-3xl font-bold">Real-Time SegmindVegaRT</h1>
|
28 |
<h3 class="text-xl font-bold">Image-to-Image</h3>
|
29 |
<p class="text-sm">
|
30 |
This demo showcases
|
31 |
<a
|
32 |
+
href="https://huggingface.co/segmind/Segmind-VegaRT"
|
33 |
target="_blank"
|
34 |
+
class="text-blue-500 underline hover:no-underline">SegmindVegaRT</a>
|
35 |
Image to Image pipeline using
|
36 |
<a
|
37 |
href="https://huggingface.co/docs/diffusers/main/en/using-diffusers/sdxl_turbo"
|
|
|
84 |
1024, min=2, max=15, title="Height", disabled=True, hide=True, id="height"
|
85 |
)
|
86 |
guidance_scale: float = Field(
|
87 |
+
0.0,
|
88 |
min=0,
|
89 |
+
max=1,
|
90 |
step=0.001,
|
91 |
title="Guidance Scale",
|
92 |
field="range",
|
|
|
138 |
if args.torch_compile:
|
139 |
print("Running torch compile")
|
140 |
self.pipe.unet = torch.compile(
|
141 |
+
self.pipe.unet, mode="reduce-overhead", fullgraph=False
|
142 |
)
|
143 |
self.pipe.vae = torch.compile(
|
144 |
+
self.pipe.vae, mode="reduce-overhead", fullgraph=False
|
145 |
)
|
146 |
|
147 |
self.pipe(
|
|
|
165 |
negative_prompt_embeds = None
|
166 |
negative_pooled_prompt_embeds = None
|
167 |
if hasattr(self.pipe, "compel_proc"):
|
168 |
+
_prompt_embeds, pooled_prompt_embeds = self.pipe.compel_proc(
|
169 |
[params.prompt, params.negative_prompt]
|
170 |
)
|
171 |
prompt = None
|
172 |
negative_prompt = None
|
173 |
+
prompt_embeds = _prompt_embeds[0:1]
|
174 |
pooled_prompt_embeds = pooled_prompt_embeds[0:1]
|
175 |
+
negative_prompt_embeds = _prompt_embeds[1:2]
|
176 |
negative_pooled_prompt_embeds = pooled_prompt_embeds[1:2]
|
177 |
|
178 |
steps = params.steps
|