Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -70,11 +70,11 @@ def preprocess_image(image, target_width, target_height): # Преобразуе
|
|
70 |
|
71 |
pipe_default = get_lora_sd_pipeline(lora_dir='./lora_man_animestyle', base_model_name_or_path=model_default, dtype=torch_dtype).to(device)
|
72 |
#pipe_controlnet = StableDiffusionControlNetPipeline.from_pretrained(
|
73 |
-
pipe_controlnet = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
).to(device)
|
78 |
|
79 |
def infer(
|
80 |
prompt,
|
@@ -102,50 +102,50 @@ def infer(
|
|
102 |
|
103 |
# Генерация с IP_adapter
|
104 |
if use_ip_adapter and ip_source_image is not None and ip_adapter_image is not None:
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
|
111 |
# Преобразуем изображения
|
112 |
ip_source_image = preprocess_image(ip_source_image, width, height)
|
113 |
ip_adapter_image = preprocess_image(ip_adapter_image, width, height)
|
114 |
|
115 |
# Создаём пайплайн IP_adapter с LoRA, если он ещё не создан ???????????????????????????????????????????????????????????????
|
116 |
-
if not hasattr(
|
117 |
# Загружаем LoRA для UNet
|
118 |
-
|
119 |
-
|
120 |
'./lora_man_animestyle/unet',
|
121 |
adapter_name="default"
|
122 |
)
|
123 |
-
|
124 |
|
125 |
# Загружаем LoRA для Text Encoder, если она существует
|
126 |
text_encoder_lora_path = './lora_man_animestyle/text_encoder'
|
127 |
if os.path.exists(text_encoder_lora_path):
|
128 |
-
|
129 |
-
|
130 |
text_encoder_lora_path,
|
131 |
adapter_name="default"
|
132 |
)
|
133 |
-
|
134 |
|
135 |
# Объединяем LoRA с основной моделью
|
136 |
-
|
137 |
-
|
138 |
|
139 |
# Убедимся, что ip_adapter_strength имеет тип float
|
140 |
ip_adapter_strength = float(ip_adapter_strength)
|
141 |
#strength_ip = float(strength_ip)
|
142 |
|
143 |
# Используем IP_adapter с LoRA ????????????????????????????????????????????????????????????????????????
|
144 |
-
pipe =
|
145 |
-
prompt_embeds = long_prompt_encoder(prompt,
|
146 |
-
negative_prompt_embeds = long_prompt_encoder(negative_prompt,
|
147 |
prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
|
148 |
-
image =
|
149 |
prompt_embeds=prompt_embeds,
|
150 |
negative_prompt_embeds=negative_prompt_embeds,
|
151 |
image=ip_source_image,
|
@@ -161,11 +161,11 @@ def infer(
|
|
161 |
else:
|
162 |
# Генерация с ControlNet
|
163 |
if use_control_net and control_image is not None and cn_source_image is not None:
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
|
170 |
# Преобразуем изображения
|
171 |
cn_source_image = preprocess_image(cn_source_image, width, height)
|
@@ -200,9 +200,9 @@ def infer(
|
|
200 |
#strength_sn = float(strength_sn)
|
201 |
|
202 |
# Используем ControlNet с LoRA
|
203 |
-
pipe = pipe_controlnet
|
204 |
-
prompt_embeds = long_prompt_encoder(prompt,
|
205 |
-
negative_prompt_embeds = long_prompt_encoder(negative_prompt,
|
206 |
prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
|
207 |
image = pipe_controlnet(
|
208 |
prompt_embeds=prompt_embeds,
|
@@ -423,7 +423,6 @@ with gr.Blocks(css=css) as demo:
|
|
423 |
outputs=ip_adapter_options
|
424 |
)
|
425 |
# --------------------------------------------------------------------------------------
|
426 |
-
|
427 |
|
428 |
gr.Examples(examples=examples, inputs=[prompt])
|
429 |
gr.Examples(examples=examples_negative, inputs=[negative_prompt])
|
@@ -444,11 +443,11 @@ with gr.Blocks(css=css) as demo:
|
|
444 |
seed,
|
445 |
guidance_scale,
|
446 |
lora_scale,
|
447 |
-
strength_cn,
|
448 |
-
use_control_net,
|
449 |
-
control_strength,
|
450 |
-
cn_source_image,
|
451 |
-
control_image,
|
452 |
strength_ip, # Коэфф. зашумления IP_adapter
|
453 |
use_ip_adapter, # Параметр для включения IP_adapter
|
454 |
ip_adapter_strength,# Сила влияния IP_adapter
|
|
|
70 |
|
71 |
pipe_default = get_lora_sd_pipeline(lora_dir='./lora_man_animestyle', base_model_name_or_path=model_default, dtype=torch_dtype).to(device)
|
72 |
#pipe_controlnet = StableDiffusionControlNetPipeline.from_pretrained(
|
73 |
+
# pipe_controlnet = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
74 |
+
# model_default,
|
75 |
+
# controlnet=controlnet,
|
76 |
+
# torch_dtype=torch_dtype
|
77 |
+
# ).to(device)
|
78 |
|
79 |
def infer(
|
80 |
prompt,
|
|
|
102 |
|
103 |
# Генерация с IP_adapter
|
104 |
if use_ip_adapter and ip_source_image is not None and ip_adapter_image is not None:
|
105 |
+
pipe_ip_adapter = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
106 |
+
model_default,
|
107 |
+
controlnet=controlnet,
|
108 |
+
torch_dtype=torch_dtype
|
109 |
+
).to(device)
|
110 |
|
111 |
# Преобразуем изображения
|
112 |
ip_source_image = preprocess_image(ip_source_image, width, height)
|
113 |
ip_adapter_image = preprocess_image(ip_adapter_image, width, height)
|
114 |
|
115 |
# Создаём пайплайн IP_adapter с LoRA, если он ещё не создан ???????????????????????????????????????????????????????????????
|
116 |
+
if not hasattr(pipe_ip_adapter, 'lora_loaded') or not pipe_ip_adapter.lora_loaded:
|
117 |
# Загружаем LoRA для UNet
|
118 |
+
pipe_ip_adapter.unet = PeftModel.from_pretrained(
|
119 |
+
pipe_ip_adapter.unet,
|
120 |
'./lora_man_animestyle/unet',
|
121 |
adapter_name="default"
|
122 |
)
|
123 |
+
pipe_ip_adapter.unet.set_adapter("default")
|
124 |
|
125 |
# Загружаем LoRA для Text Encoder, если она существует
|
126 |
text_encoder_lora_path = './lora_man_animestyle/text_encoder'
|
127 |
if os.path.exists(text_encoder_lora_path):
|
128 |
+
pipe_ip_adapter.text_encoder = PeftModel.from_pretrained(
|
129 |
+
pipe_ip_adapter.text_encoder,
|
130 |
text_encoder_lora_path,
|
131 |
adapter_name="default"
|
132 |
)
|
133 |
+
pipe_ip_adapter.text_encoder.set_adapter("default")
|
134 |
|
135 |
# Объединяем LoRA с основной моделью
|
136 |
+
pipe_ip_adapter.fuse_lora(lora_scale=lora_scale)
|
137 |
+
pipe_ip_adapter.lora_loaded = True # Помечаем, что LoRA загружена
|
138 |
|
139 |
# Убедимся, что ip_adapter_strength имеет тип float
|
140 |
ip_adapter_strength = float(ip_adapter_strength)
|
141 |
#strength_ip = float(strength_ip)
|
142 |
|
143 |
# Используем IP_adapter с LoRA ????????????????????????????????????????????????????????????????????????
|
144 |
+
#pipe = pipe_ip_adapter
|
145 |
+
prompt_embeds = long_prompt_encoder(prompt, pipe_ip_adapter.tokenizer, pipe_ip_adapter.text_encoder)
|
146 |
+
negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe_ip_adapter.tokenizer, pipe_ip_adapter.text_encoder)
|
147 |
prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
|
148 |
+
image = pipe_ip_adapter(
|
149 |
prompt_embeds=prompt_embeds,
|
150 |
negative_prompt_embeds=negative_prompt_embeds,
|
151 |
image=ip_source_image,
|
|
|
161 |
else:
|
162 |
# Генерация с ControlNet
|
163 |
if use_control_net and control_image is not None and cn_source_image is not None:
|
164 |
+
pipe_controlnet = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
165 |
+
model_default,
|
166 |
+
controlnet=controlnet,
|
167 |
+
torch_dtype=torch_dtype
|
168 |
+
).to(device)
|
169 |
|
170 |
# Преобразуем изображения
|
171 |
cn_source_image = preprocess_image(cn_source_image, width, height)
|
|
|
200 |
#strength_sn = float(strength_sn)
|
201 |
|
202 |
# Используем ControlNet с LoRA
|
203 |
+
#pipe = pipe_controlnet
|
204 |
+
prompt_embeds = long_prompt_encoder(prompt, pipe_controlnet.tokenizer, pipe_controlnet.text_encoder)
|
205 |
+
negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe_controlnet.tokenizer, pipe_controlnet.text_encoder)
|
206 |
prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
|
207 |
image = pipe_controlnet(
|
208 |
prompt_embeds=prompt_embeds,
|
|
|
423 |
outputs=ip_adapter_options
|
424 |
)
|
425 |
# --------------------------------------------------------------------------------------
|
|
|
426 |
|
427 |
gr.Examples(examples=examples, inputs=[prompt])
|
428 |
gr.Examples(examples=examples_negative, inputs=[negative_prompt])
|
|
|
443 |
seed,
|
444 |
guidance_scale,
|
445 |
lora_scale,
|
446 |
+
strength_cn, # Коэфф. зашумления ControlNet
|
447 |
+
use_control_net, # Чекбокс для ControlNet
|
448 |
+
control_strength, # Контроль силы ControlNet
|
449 |
+
cn_source_image, # Исходное изображение ControlNet
|
450 |
+
control_image, # Контрольное изображение ControlNet
|
451 |
strength_ip, # Коэфф. зашумления IP_adapter
|
452 |
use_ip_adapter, # Параметр для включения IP_adapter
|
453 |
ip_adapter_strength,# Сила влияния IP_adapter
|