Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -104,64 +104,41 @@ def infer(
|
|
104 |
|
105 |
# Генерация с Ip_Adapter
|
106 |
if use_ip_adapter and ip_source_image is not None and ip_adapter_image is not None:
|
107 |
-
pipe_ip_adapter = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
108 |
model_default,
|
109 |
controlnet=controlnet,
|
110 |
torch_dtype=torch_dtype
|
111 |
).to(device)
|
112 |
|
113 |
-
#
|
114 |
pipe_ip_adapter.load_ip_adapter(IP_ADAPTER, subfolder="models", weight_name=IP_ADAPTER_WEIGHT_NAME)
|
115 |
-
pipe_ip_adapter.set_ip_adapter_scale(ip_adapter_strength)
|
116 |
-
|
117 |
-
#
|
118 |
ip_source_image = preprocess_image(ip_source_image, width, height)
|
119 |
ip_adapter_image = preprocess_image(ip_adapter_image, width, height)
|
120 |
-
|
121 |
-
#
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
pipe_ip_adapter.unet,
|
126 |
-
'./lora_man_animestyle/unet',
|
127 |
-
adapter_name="default"
|
128 |
-
)
|
129 |
-
pipe_ip_adapter.unet.set_adapter("default")
|
130 |
-
|
131 |
-
# Загружаем LoRA для Text Encoder, если она существует
|
132 |
-
text_encoder_lora_path = './lora_man_animestyle/text_encoder'
|
133 |
-
if os.path.exists(text_encoder_lora_path):
|
134 |
-
pipe_ip_adapter.text_encoder = PeftModel.from_pretrained(
|
135 |
-
pipe_ip_adapter.text_encoder,
|
136 |
-
text_encoder_lora_path,
|
137 |
-
adapter_name="default"
|
138 |
-
)
|
139 |
-
pipe_ip_adapter.text_encoder.set_adapter("default")
|
140 |
-
|
141 |
-
# Объединяем LoRA с основной моделью
|
142 |
-
pipe_ip_adapter.fuse_lora(lora_scale=lora_scale)
|
143 |
-
pipe_ip_adapter.lora_loaded = True # Помечаем, что LoRA загружена
|
144 |
-
|
145 |
-
# Убедимся, что ip_adapter_strength имеет тип float
|
146 |
-
ip_adapter_strength = float(ip_adapter_strength)
|
147 |
-
#strength_ip = float(strength_ip)
|
148 |
-
|
149 |
-
# Используем IP_adapter с LoRA
|
150 |
prompt_embeds = long_prompt_encoder(prompt, pipe_ip_adapter.tokenizer, pipe_ip_adapter.text_encoder)
|
151 |
negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe_ip_adapter.tokenizer, pipe_ip_adapter.text_encoder)
|
152 |
prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
|
|
|
153 |
image = pipe_ip_adapter(
|
154 |
prompt_embeds=prompt_embeds,
|
155 |
negative_prompt_embeds=negative_prompt_embeds,
|
156 |
-
image=ip_source_image,
|
157 |
-
control_image=ip_adapter_image,
|
158 |
-
strength=strength_ip,
|
159 |
width=width,
|
160 |
height=height,
|
161 |
num_inference_steps=num_inference_steps,
|
162 |
guidance_scale=guidance_scale,
|
163 |
-
controlnet_conditioning_scale=1.0,
|
164 |
-
generator=generator
|
|
|
165 |
).images[0]
|
166 |
else:
|
167 |
# Генерация с ControlNet
|
|
|
104 |
|
105 |
# Генерация с Ip_Adapter
|
106 |
if use_ip_adapter and ip_source_image is not None and ip_adapter_image is not None:
|
107 |
+
pipe_ip_adapter = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
108 |
model_default,
|
109 |
controlnet=controlnet,
|
110 |
torch_dtype=torch_dtype
|
111 |
).to(device)
|
112 |
|
113 |
+
# Загрузка IP-Adapter
|
114 |
pipe_ip_adapter.load_ip_adapter(IP_ADAPTER, subfolder="models", weight_name=IP_ADAPTER_WEIGHT_NAME)
|
115 |
+
pipe_ip_adapter.set_ip_adapter_scale(ip_adapter_strength)
|
116 |
+
|
117 |
+
# Преобразование изображений
|
118 |
ip_source_image = preprocess_image(ip_source_image, width, height)
|
119 |
ip_adapter_image = preprocess_image(ip_adapter_image, width, height)
|
120 |
+
|
121 |
+
# Получение эмбеддингов изображения для IP-Adapter
|
122 |
+
image_embeds = pipe_ip_adapter.get_image_embeds(ip_adapter_image)
|
123 |
+
|
124 |
+
# Генерация изображения с использованием IP-Adapter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
prompt_embeds = long_prompt_encoder(prompt, pipe_ip_adapter.tokenizer, pipe_ip_adapter.text_encoder)
|
126 |
negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe_ip_adapter.tokenizer, pipe_ip_adapter.text_encoder)
|
127 |
prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
|
128 |
+
|
129 |
image = pipe_ip_adapter(
|
130 |
prompt_embeds=prompt_embeds,
|
131 |
negative_prompt_embeds=negative_prompt_embeds,
|
132 |
+
image=ip_source_image,
|
133 |
+
control_image=ip_adapter_image,
|
134 |
+
strength=strength_ip,
|
135 |
width=width,
|
136 |
height=height,
|
137 |
num_inference_steps=num_inference_steps,
|
138 |
guidance_scale=guidance_scale,
|
139 |
+
controlnet_conditioning_scale=1.0,
|
140 |
+
generator=generator,
|
141 |
+
image_embeds=image_embeds, # Передача эмбеддингов изображения
|
142 |
).images[0]
|
143 |
else:
|
144 |
# Генерация с ControlNet
|