Lifeinhockey commited on
Commit
a88c73f
·
verified ·
1 Parent(s): 74ccd9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -20
app.py CHANGED
@@ -59,14 +59,6 @@ def align_embeddings(prompt_embeds, negative_prompt_embeds):
59
  return torch.nn.functional.pad(prompt_embeds, (0, 0, 0, max_length - prompt_embeds.shape[1])), \
60
  torch.nn.functional.pad(negative_prompt_embeds, (0, 0, 0, max_length - negative_prompt_embeds.shape[1]))
61
 
62
- pipe_default = get_lora_sd_pipeline(lora_dir='./lora_man_animestyle', base_model_name_or_path=model_default, dtype=torch_dtype).to(device)
63
- #pipe_controlnet = StableDiffusionControlNetPipeline.from_pretrained(
64
- pipe_controlnet = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
65
- model_default,
66
- controlnet=controlnet,
67
- torch_dtype=torch_dtype
68
- ).to(device)
69
-
70
  def preprocess_image(image, target_width, target_height): # Преобразует изображение в формат, подходящий для модели.
71
  if isinstance(image, np.ndarray):
72
  image = Image.fromarray(image)
@@ -76,6 +68,14 @@ def preprocess_image(image, target_width, target_height): # Преобразуе
76
  image = torch.from_numpy(image).to(device)
77
  return image
78
 
 
 
 
 
 
 
 
 
79
  def infer(
80
  prompt,
81
  negative_prompt,
@@ -91,12 +91,17 @@ def infer(
91
  control_strength=0.5, # Сила влияния ControlNet
92
  cn_source_image=None, # Исходное изображение ControlNet
93
  control_image=None, # Контрольное изображение ControlNet
 
 
 
 
 
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,
102
  # controlnet=controlnet,
@@ -104,10 +109,10 @@ def infer(
104
  # ).to(device)
105
 
106
  # Преобразуем изображения
107
- cn_source_image = preprocess_image(cn_source_image, width, height)
108
- control_image = preprocess_image(control_image, width, height)
109
 
110
- # Создаём пайплайн ControlNet с LoRA, если он ещё не создан
111
  if not hasattr(pipe_controlnet, 'lora_loaded') or not pipe_controlnet.lora_loaded:
112
  # Загружаем LoRA для UNet
113
  pipe_controlnet.unet = PeftModel.from_pretrained(
@@ -131,11 +136,11 @@ def infer(
131
  pipe_controlnet.fuse_lora(lora_scale=lora_scale)
132
  pipe_controlnet.lora_loaded = True # Помечаем, что LoRA загружена
133
 
134
- # Убедимся, что control_strength имеет тип float
135
- control_strength = float(control_strength)
136
- #strength_sn = float(strength_sn)
137
 
138
- # Используем ControlNet с LoRA
139
  pipe = pipe_controlnet
140
  prompt_embeds = long_prompt_encoder(prompt, pipe.tokenizer, pipe.text_encoder)
141
  negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe.tokenizer, pipe.text_encoder)
@@ -143,14 +148,14 @@ def infer(
143
  image = pipe_controlnet(
144
  prompt_embeds=prompt_embeds,
145
  negative_prompt_embeds=negative_prompt_embeds,
146
- image=cn_source_image,
147
- control_image=control_image,
148
- strength=strength_cn, # Коэфф. зашумления, чем больше, тем больше меняется результирующее изображение относитенльно исходного
149
  width=width,
150
  height=height,
151
  num_inference_steps=num_inference_steps,
152
  guidance_scale=guidance_scale,
153
- controlnet_conditioning_scale=control_strength,
154
  generator=generator
155
  ).images[0]
156
  else:
@@ -444,6 +449,11 @@ with gr.Blocks(css=css) as demo:
444
  control_strength, # Контроль силы ControlNet
445
  cn_source_image, # Исходное изображение ControlNet
446
  control_image, # Контрольное изображение ControlNet
 
 
 
 
 
447
  ],
448
  outputs=[result],
449
  )
 
59
  return torch.nn.functional.pad(prompt_embeds, (0, 0, 0, max_length - prompt_embeds.shape[1])), \
60
  torch.nn.functional.pad(negative_prompt_embeds, (0, 0, 0, max_length - negative_prompt_embeds.shape[1]))
61
 
 
 
 
 
 
 
 
 
62
  def preprocess_image(image, target_width, target_height): # Преобразует изображение в формат, подходящий для модели.
63
  if isinstance(image, np.ndarray):
64
  image = Image.fromarray(image)
 
68
  image = torch.from_numpy(image).to(device)
69
  return image
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,
81
  negative_prompt,
 
91
  control_strength=0.5, # Сила влияния ControlNet
92
  cn_source_image=None, # Исходное изображение ControlNet
93
  control_image=None, # Контрольное изображение ControlNet
94
+ strength_ip=0.5, # Коэфф. зашумления IP_adapter
95
+ use_ip_adapter=False, # Параметр для включения IP_adapter
96
+ ip_adapter_strength=0.5,# Сила влияния IP_adapter
97
+ ip_source_image=None, # Исходное изображение IP_adapter
98
+ ip_adapter_image=None, # Контрольное изображение IP_adapter
99
  progress=gr.Progress(track_tqdm=True)
100
  ):
101
  generator = torch.Generator(device).manual_seed(seed)
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_controlnet = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
106
  # model_default,
107
  # controlnet=controlnet,
 
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_controlnet, 'lora_loaded') or not pipe_controlnet.lora_loaded:
117
  # Загружаем LoRA для UNet
118
  pipe_controlnet.unet = PeftModel.from_pretrained(
 
136
  pipe_controlnet.fuse_lora(lora_scale=lora_scale)
137
  pipe_controlnet.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_controlnet
145
  prompt_embeds = long_prompt_encoder(prompt, pipe.tokenizer, pipe.text_encoder)
146
  negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe.tokenizer, pipe.text_encoder)
 
148
  image = pipe_controlnet(
149
  prompt_embeds=prompt_embeds,
150
  negative_prompt_embeds=negative_prompt_embeds,
151
+ image=ip_source_image,
152
+ control_image=ip_adapter_image,
153
+ strength=strength_ip, # Коэфф. зашумления, чем больше, тем больше меняется результирующее изображение относитенльно исходного
154
  width=width,
155
  height=height,
156
  num_inference_steps=num_inference_steps,
157
  guidance_scale=guidance_scale,
158
+ controlnet_conditioning_scale=ip_adapter_strength, # ???????????????????????????????????????????????????????????????
159
  generator=generator
160
  ).images[0]
161
  else:
 
449
  control_strength, # Контроль силы ControlNet
450
  cn_source_image, # Исходное изображение ControlNet
451
  control_image, # Контрольное изображение ControlNet
452
+ strength_ip, # Коэфф. зашумления IP_adapter
453
+ use_ip_adapter, # Параметр для включения IP_adapter
454
+ ip_adapter_strength,# Сила влияния IP_adapter
455
+ ip_source_image, # Исходное изображение IP_adapter
456
+ ip_adapter_image, # Контрольное изображение IP_adapter
457
  ],
458
  outputs=[result],
459
  )