Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -94,7 +94,8 @@ def infer(
|
|
| 94 |
progress=gr.Progress(track_tqdm=True)
|
| 95 |
):
|
| 96 |
generator = torch.Generator(device).manual_seed(seed)
|
| 97 |
-
|
|
|
|
| 98 |
if use_control_net and control_image is not None and cn_source_image is not None:
|
| 99 |
# pipe_controlnet = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
| 100 |
# model_default,
|
|
@@ -153,30 +154,89 @@ def infer(
|
|
| 153 |
generator=generator
|
| 154 |
).images[0]
|
| 155 |
else:
|
| 156 |
-
#
|
| 157 |
-
if
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
prompt_embeds = long_prompt_encoder(prompt, pipe.tokenizer, pipe.text_encoder)
|
| 160 |
negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe.tokenizer, pipe.text_encoder)
|
| 161 |
prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
else:
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
|
| 179 |
-
|
| 180 |
|
| 181 |
return image
|
| 182 |
|
|
|
|
| 94 |
progress=gr.Progress(track_tqdm=True)
|
| 95 |
):
|
| 96 |
generator = torch.Generator(device).manual_seed(seed)
|
| 97 |
+
|
| 98 |
+
# Генерация с IP_adapter
|
| 99 |
if use_control_net and control_image is not None and cn_source_image is not None:
|
| 100 |
# pipe_controlnet = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
| 101 |
# model_default,
|
|
|
|
| 154 |
generator=generator
|
| 155 |
).images[0]
|
| 156 |
else:
|
| 157 |
+
# Генерация с ControlNet
|
| 158 |
+
if use_control_net and control_image is not None and cn_source_image is not None:
|
| 159 |
+
# pipe_controlnet = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
| 160 |
+
# model_default,
|
| 161 |
+
# controlnet=controlnet,
|
| 162 |
+
# torch_dtype=torch_dtype
|
| 163 |
+
# ).to(device)
|
| 164 |
+
|
| 165 |
+
# Преобразуем изображения
|
| 166 |
+
cn_source_image = preprocess_image(cn_source_image, width, height)
|
| 167 |
+
control_image = preprocess_image(control_image, width, height)
|
| 168 |
+
|
| 169 |
+
# Создаём пайплайн ControlNet с LoRA, если он ещё не создан
|
| 170 |
+
if not hasattr(pipe_controlnet, 'lora_loaded') or not pipe_controlnet.lora_loaded:
|
| 171 |
+
# Загружаем LoRA для UNet
|
| 172 |
+
pipe_controlnet.unet = PeftModel.from_pretrained(
|
| 173 |
+
pipe_controlnet.unet,
|
| 174 |
+
'./lora_man_animestyle/unet',
|
| 175 |
+
adapter_name="default"
|
| 176 |
+
)
|
| 177 |
+
pipe_controlnet.unet.set_adapter("default")
|
| 178 |
+
|
| 179 |
+
# Загружаем LoRA для Text Encoder, если она существует
|
| 180 |
+
text_encoder_lora_path = './lora_man_animestyle/text_encoder'
|
| 181 |
+
if os.path.exists(text_encoder_lora_path):
|
| 182 |
+
pipe_controlnet.text_encoder = PeftModel.from_pretrained(
|
| 183 |
+
pipe_controlnet.text_encoder,
|
| 184 |
+
text_encoder_lora_path,
|
| 185 |
+
adapter_name="default"
|
| 186 |
+
)
|
| 187 |
+
pipe_controlnet.text_encoder.set_adapter("default")
|
| 188 |
+
|
| 189 |
+
# Объединяем LoRA с основной моделью
|
| 190 |
+
pipe_controlnet.fuse_lora(lora_scale=lora_scale)
|
| 191 |
+
pipe_controlnet.lora_loaded = True # Помечаем, что LoRA загружена
|
| 192 |
+
|
| 193 |
+
# Убедимся, что control_strength имеет тип float
|
| 194 |
+
control_strength = float(control_strength)
|
| 195 |
+
#strength_sn = float(strength_sn)
|
| 196 |
+
|
| 197 |
+
# Используем ControlNet с LoRA
|
| 198 |
+
pipe = pipe_controlnet
|
| 199 |
prompt_embeds = long_prompt_encoder(prompt, pipe.tokenizer, pipe.text_encoder)
|
| 200 |
negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe.tokenizer, pipe.text_encoder)
|
| 201 |
prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
|
| 202 |
+
image = pipe_controlnet(
|
| 203 |
+
prompt_embeds=prompt_embeds,
|
| 204 |
+
negative_prompt_embeds=negative_prompt_embeds,
|
| 205 |
+
image=cn_source_image,
|
| 206 |
+
control_image=control_image,
|
| 207 |
+
strength=strength_cn, # Коэфф. зашумления, чем больше, тем больше меняется результирующее изображение относитенльно исходного
|
| 208 |
+
width=width,
|
| 209 |
+
height=height,
|
| 210 |
+
num_inference_steps=num_inference_steps,
|
| 211 |
+
guidance_scale=guidance_scale,
|
| 212 |
+
controlnet_conditioning_scale=control_strength,
|
| 213 |
+
generator=generator
|
| 214 |
+
).images[0]
|
| 215 |
else:
|
| 216 |
+
# Генерация без ControlNet и IP_adapter
|
| 217 |
+
if model != model_default:
|
| 218 |
+
pipe = StableDiffusionPipeline.from_pretrained(model, torch_dtype=torch_dtype).to(device)
|
| 219 |
+
prompt_embeds = long_prompt_encoder(prompt, pipe.tokenizer, pipe.text_encoder)
|
| 220 |
+
negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe.tokenizer, pipe.text_encoder)
|
| 221 |
+
prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
|
| 222 |
+
else:
|
| 223 |
+
pipe = pipe_default
|
| 224 |
+
prompt_embeds = long_prompt_encoder(prompt, pipe.tokenizer, pipe.text_encoder)
|
| 225 |
+
negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe.tokenizer, pipe.text_encoder)
|
| 226 |
+
prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
|
| 227 |
+
pipe.fuse_lora(lora_scale=lora_scale)
|
| 228 |
|
| 229 |
+
params = {
|
| 230 |
+
'prompt_embeds': prompt_embeds,
|
| 231 |
+
'negative_prompt_embeds': negative_prompt_embeds,
|
| 232 |
+
'guidance_scale': guidance_scale,
|
| 233 |
+
'num_inference_steps': num_inference_steps,
|
| 234 |
+
'width': width,
|
| 235 |
+
'height': height,
|
| 236 |
+
'generator': generator,
|
| 237 |
+
}
|
| 238 |
|
| 239 |
+
image = pipe(**params).images[0]
|
| 240 |
|
| 241 |
return image
|
| 242 |
|