Lifeinhockey commited on
Commit
1e2dd98
·
verified ·
1 Parent(s): abdaa5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -73
app.py CHANGED
@@ -1,10 +1,9 @@
1
- # app.py 07.02.25
2
-
3
  import gradio as gr
4
  import numpy as np
5
  import torch
6
- from diffusers import StableDiffusionPipeline
7
  from peft import PeftModel, LoraConfig
 
8
  import os
9
 
10
  MAX_SEED = np.iinfo(np.int32).max
@@ -14,13 +13,16 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
  model_default = "stable-diffusion-v1-5/stable-diffusion-v1-5"
15
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
16
 
 
 
 
 
17
  def get_lora_sd_pipeline(
18
  lora_dir='./lora_man_animestyle',
19
  base_model_name_or_path=None,
20
  dtype=torch.float16,
21
  adapter_name="default"
22
- ):
23
-
24
  unet_sub_dir = os.path.join(lora_dir, "unet")
25
  text_encoder_sub_dir = os.path.join(lora_dir, "text_encoder")
26
 
@@ -59,6 +61,11 @@ def align_embeddings(prompt_embeds, negative_prompt_embeds):
59
  torch.nn.functional.pad(negative_prompt_embeds, (0, 0, 0, max_length - negative_prompt_embeds.shape[1]))
60
 
61
  pipe_default = get_lora_sd_pipeline(lora_dir='./lora_man_animestyle', base_model_name_or_path=model_default, dtype=torch_dtype).to(device)
 
 
 
 
 
62
 
63
  def infer(
64
  prompt,
@@ -70,34 +77,54 @@ def infer(
70
  seed=4,
71
  guidance_scale=7.5,
72
  lora_scale=0.5,
 
 
 
73
  progress=gr.Progress(track_tqdm=True)
74
- ):
75
-
76
  generator = torch.Generator(device).manual_seed(seed)
77
 
78
- if model != model_default:
79
- pipe = StableDiffusionPipeline.from_pretrained(model, torch_dtype=torch_dtype).to(device)
80
- prompt_embeds = long_prompt_encoder(prompt, pipe.tokenizer, pipe.text_encoder)
81
- negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe.tokenizer, pipe.text_encoder)
82
- prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
 
 
 
 
 
 
 
 
 
83
  else:
84
- pipe = pipe_default
85
- prompt_embeds = long_prompt_encoder(prompt, pipe.tokenizer, pipe.text_encoder)
86
- negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe.tokenizer, pipe.text_encoder)
87
- prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
88
- pipe.fuse_lora(lora_scale=lora_scale) # Коэфф. добавления lora
89
-
90
- params = {
91
- 'prompt_embeds': prompt_embeds,
92
- 'negative_prompt_embeds': negative_prompt_embeds,
93
- 'guidance_scale': guidance_scale,
94
- 'num_inference_steps': num_inference_steps,
95
- 'width': width,
96
- 'height': height,
97
- 'generator': generator,
98
- }
 
 
 
 
 
 
 
 
 
99
 
100
- return pipe(**params).images[0]
101
 
102
  examples = [
103
  "A young man in anime style. The image is characterized by high definition and resolution. Handsome, thoughtful man, attentive eyes. The man is depicted in the foreground, close-up or in the middle. High-quality images of the face, eyes, nose, lips, hands and clothes. The background and background are blurred and indistinct. The play of light and shadow is visible on the face and clothes.",
@@ -198,27 +225,14 @@ with gr.Blocks(css=css) as demo:
198
  value=512,
199
  )
200
 
201
- # Функция для работы с ControlNet ---------------------------------------------------------------------
202
- def process_input_ControlNet(image, use_control_net, control_strength, control_mode):
203
- if use_control_net:
204
- # Логика для обработки с использованием ControlNet
205
- result = f"ControlNet активен! Режим: {control_mode}, Интенсивность: {control_strength}"
206
- else:
207
- # Логика для обработки без ControlNet
208
- result = "ControlNet отключен."
209
- return result
210
-
211
  with gr.Blocks():
212
  with gr.Row():
213
- # Чекбокс для включения/отключения ControlNet
214
  use_control_net = gr.Checkbox(
215
  label="Use ControlNet",
216
  value=False,
217
  )
218
 
219
- # Дополнительные опции для ControlNet
220
  with gr.Column(visible=False) as control_net_options:
221
- # Слайдер для настройки интенсивности
222
  control_strength = gr.Slider(
223
  label="Control Strength",
224
  minimum=0.0,
@@ -227,59 +241,28 @@ with gr.Blocks(css=css) as demo:
227
  step=0.05,
228
  )
229
 
230
- # Выпадающий список для выбора режима
231
  control_mode = gr.Dropdown(
232
  label="Control Mode",
233
  choices=[
234
- "edge_detection",
235
- "canny_edge_detection",
236
  "pose_estimation",
237
- "depth_map",
238
- "segmentation_map",
239
- "scribble_sketch",
240
- "normal_map",
241
- "hed_edge_detection",
242
- "openpose",
243
- "mlsd_line_detection",
244
- "scribble_diffusion",
245
- "semantic_segmentation",
246
- "style_transfer",
247
- "colorization",
248
- "custom_map"
249
  ],
250
  value="pose_estimation",
251
  )
252
 
253
- # Окно для загрузки изображений
254
  control_image = gr.Image(label="Upload Control Image")
255
 
256
- # Кнопка для запуска работы ControlNet
257
- run_button = gr.Button("Run")
258
-
259
- # Текстовое поле для вывода результата
260
- output = gr.Textbox(label="Output")
261
-
262
- # Логика для отображения/скрытия дополнительных опций
263
  use_control_net.change(
264
  fn=lambda x: gr.Row.update(visible=x),
265
  inputs=use_control_net,
266
  outputs=control_net_options,
267
  )
268
 
269
- # Привязка кнопки Run к функции работы с ControlNet
270
- run_button.click(
271
- fn=process_input_ControlNet,
272
- inputs=[control_image, use_control_net, control_strength, control_mode],
273
- outputs=output,
274
- )
275
-
276
  gr.Examples(examples=examples, inputs=[prompt])
277
  gr.Examples(examples=examples_negative, inputs=[negative_prompt])
278
 
279
  run_button = gr.Button("Run", scale=1, variant="primary")
280
  result = gr.Image(label="Result", show_label=False)
281
 
282
-
283
  gr.on(
284
  triggers=[run_button.click, prompt.submit],
285
  fn=infer,
@@ -293,10 +276,12 @@ with gr.Blocks(css=css) as demo:
293
  seed,
294
  guidance_scale,
295
  lora_scale,
 
 
 
296
  ],
297
  outputs=[result],
298
  )
299
 
300
  if __name__ == "__main__":
301
  demo.launch()
302
-
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import torch
4
+ from diffusers import StableDiffusionPipeline, ControlNetModel, StableDiffusionControlNetPipeline
5
  from peft import PeftModel, LoraConfig
6
+ from controlnet_aux import OpenposeDetector
7
  import os
8
 
9
  MAX_SEED = np.iinfo(np.int32).max
 
13
  model_default = "stable-diffusion-v1-5/stable-diffusion-v1-5"
14
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
15
 
16
+ # Инициализация ControlNet и OpenposeDetector
17
+ controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-openpose", torch_dtype=torch_dtype)
18
+ openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
19
+
20
  def get_lora_sd_pipeline(
21
  lora_dir='./lora_man_animestyle',
22
  base_model_name_or_path=None,
23
  dtype=torch.float16,
24
  adapter_name="default"
25
+ ):
 
26
  unet_sub_dir = os.path.join(lora_dir, "unet")
27
  text_encoder_sub_dir = os.path.join(lora_dir, "text_encoder")
28
 
 
61
  torch.nn.functional.pad(negative_prompt_embeds, (0, 0, 0, max_length - negative_prompt_embeds.shape[1]))
62
 
63
  pipe_default = get_lora_sd_pipeline(lora_dir='./lora_man_animestyle', base_model_name_or_path=model_default, dtype=torch_dtype).to(device)
64
+ pipe_controlnet = StableDiffusionControlNetPipeline.from_pretrained(
65
+ model_default,
66
+ controlnet=controlnet,
67
+ torch_dtype=torch_dtype
68
+ ).to(device)
69
 
70
  def infer(
71
  prompt,
 
77
  seed=4,
78
  guidance_scale=7.5,
79
  lora_scale=0.5,
80
+ use_control_net=False, # Параметр для включения ControlNet
81
+ control_strength=0.5, # Сила влияния ControlNet
82
+ control_image=None, # Контрольное изображение
83
  progress=gr.Progress(track_tqdm=True)
84
+ ):
 
85
  generator = torch.Generator(device).manual_seed(seed)
86
 
87
+ if use_control_net and control_image is not None:
88
+ # Используем ControlNet
89
+ pose_image = openpose(control_image)
90
+ image = pipe_controlnet(
91
+ prompt=prompt,
92
+ negative_prompt=negative_prompt,
93
+ image=pose_image,
94
+ width=width,
95
+ height=height,
96
+ num_inference_steps=num_inference_steps,
97
+ guidance_scale=guidance_scale,
98
+ controlnet_conditioning_scale=control_strength,
99
+ generator=generator
100
+ ).images[0]
101
  else:
102
+ # Стандартная генерация без ControlNet
103
+ if model != model_default:
104
+ pipe = StableDiffusionPipeline.from_pretrained(model, torch_dtype=torch_dtype).to(device)
105
+ prompt_embeds = long_prompt_encoder(prompt, pipe.tokenizer, pipe.text_encoder)
106
+ negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe.tokenizer, pipe.text_encoder)
107
+ prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
108
+ else:
109
+ pipe = pipe_default
110
+ prompt_embeds = long_prompt_encoder(prompt, pipe.tokenizer, pipe.text_encoder)
111
+ negative_prompt_embeds = long_prompt_encoder(negative_prompt, pipe.tokenizer, pipe.text_encoder)
112
+ prompt_embeds, negative_prompt_embeds = align_embeddings(prompt_embeds, negative_prompt_embeds)
113
+ pipe.fuse_lora(lora_scale=lora_scale)
114
+
115
+ params = {
116
+ 'prompt_embeds': prompt_embeds,
117
+ 'negative_prompt_embeds': negative_prompt_embeds,
118
+ 'guidance_scale': guidance_scale,
119
+ 'num_inference_steps': num_inference_steps,
120
+ 'width': width,
121
+ 'height': height,
122
+ 'generator': generator,
123
+ }
124
+
125
+ image = pipe(**params).images[0]
126
 
127
+ return image
128
 
129
  examples = [
130
  "A young man in anime style. The image is characterized by high definition and resolution. Handsome, thoughtful man, attentive eyes. The man is depicted in the foreground, close-up or in the middle. High-quality images of the face, eyes, nose, lips, hands and clothes. The background and background are blurred and indistinct. The play of light and shadow is visible on the face and clothes.",
 
225
  value=512,
226
  )
227
 
 
 
 
 
 
 
 
 
 
 
228
  with gr.Blocks():
229
  with gr.Row():
 
230
  use_control_net = gr.Checkbox(
231
  label="Use ControlNet",
232
  value=False,
233
  )
234
 
 
235
  with gr.Column(visible=False) as control_net_options:
 
236
  control_strength = gr.Slider(
237
  label="Control Strength",
238
  minimum=0.0,
 
241
  step=0.05,
242
  )
243
 
 
244
  control_mode = gr.Dropdown(
245
  label="Control Mode",
246
  choices=[
 
 
247
  "pose_estimation",
 
 
 
 
 
 
 
 
 
 
 
 
248
  ],
249
  value="pose_estimation",
250
  )
251
 
 
252
  control_image = gr.Image(label="Upload Control Image")
253
 
 
 
 
 
 
 
 
254
  use_control_net.change(
255
  fn=lambda x: gr.Row.update(visible=x),
256
  inputs=use_control_net,
257
  outputs=control_net_options,
258
  )
259
 
 
 
 
 
 
 
 
260
  gr.Examples(examples=examples, inputs=[prompt])
261
  gr.Examples(examples=examples_negative, inputs=[negative_prompt])
262
 
263
  run_button = gr.Button("Run", scale=1, variant="primary")
264
  result = gr.Image(label="Result", show_label=False)
265
 
 
266
  gr.on(
267
  triggers=[run_button.click, prompt.submit],
268
  fn=infer,
 
276
  seed,
277
  guidance_scale,
278
  lora_scale,
279
+ use_control_net, # Добавляем чекбокс для ControlNet
280
+ control_strength, # Добавляем контроль силы
281
+ control_image, # Добавляем контрольное изображение
282
  ],
283
  outputs=[result],
284
  )
285
 
286
  if __name__ == "__main__":
287
  demo.launch()