adaface-neurips commited on
Commit
bb9ba55
1 Parent(s): f0b9ada

Move 'reloading model weight' out of @spaces.GPU to avoid hanging

Browse files
.gitignore CHANGED
@@ -5,3 +5,4 @@ gradio_cached_examples/*
5
  gradio_cached_examples/
6
  samples/*
7
  samples/
 
 
5
  gradio_cached_examples/
6
  samples/*
7
  samples/
8
+ .gradio/certificate.pem
adaface/adaface_wrapper.py CHANGED
@@ -399,8 +399,9 @@ class AdaFaceWrapper(nn.Module):
399
  # ref_img_strength is used only in the img2img pipeline.
400
  def forward(self, noise, prompt, negative_prompt=None,
401
  placeholder_tokens_pos='append',
 
402
  do_neg_id_prompt_weight=0,
403
- guidance_scale=6.0, out_image_count=4,
404
  ref_img_strength=0.8, generator=None, verbose=False):
405
  noise = noise.to(device=self.device, dtype=torch.float16)
406
 
 
399
  # ref_img_strength is used only in the img2img pipeline.
400
  def forward(self, noise, prompt, negative_prompt=None,
401
  placeholder_tokens_pos='append',
402
+ guidance_scale=6.0,
403
  do_neg_id_prompt_weight=0,
404
+ out_image_count=4,
405
  ref_img_strength=0.8, generator=None, verbose=False):
406
  noise = noise.to(device=self.device, dtype=torch.float16)
407
 
app.py CHANGED
@@ -19,7 +19,7 @@ from insightface.utils import face_align
19
  from PIL import Image
20
  import torch
21
  import argparse
22
- # From command line read command adaface_ckpt_path
23
  parser = argparse.ArgumentParser()
24
  parser.add_argument("--adaface_encoder_types", type=str, nargs="+", default=["consistentID", "arc2face"],
25
  choices=["arc2face", "consistentID"], help="Type(s) of the ID2Ada prompt encoders")
@@ -29,7 +29,7 @@ parser.add_argument('--model_style_type', type=str, default='realistic',
29
  choices=["realistic", "anime", "photorealistic"], help="Type of the base model")
30
  parser.add_argument("--guidance_scale", type=float, default=8.0,
31
  help="The guidance scale for the diffusion model. Default: 8.0")
32
- parser.add_argument("--do_neg_id_prompt_weight", type=float, default=0.2,
33
  help="The weight of added ID prompt embeddings into the negative prompt. Default: 0, disabled.")
34
 
35
  parser.add_argument('--gpu', type=int, default=None)
@@ -79,22 +79,12 @@ def get_clicked_image(data: gr.SelectData):
79
  return data.index
80
 
81
  @spaces.GPU
82
- def gen_init_images(uploaded_image_paths, model_style_type, prompt, out_image_count=3):
 
83
  global adaface, id_animator
84
  if uploaded_image_paths is None:
85
  print("No image uploaded")
86
  return None, None, None
87
-
88
- model_style_type = model_style_type.lower()
89
- base_model_path = model_style_type2base_model_path[model_style_type]
90
- # If the base model type is changed, reload the model.
91
- if model_style_type != args.model_style_type:
92
- id_animator = load_model(model_style_type=model_style_type, device=device)
93
- adaface = AdaFaceWrapper(pipeline_name="text2img", base_model_path=base_model_path,
94
- adaface_encoder_types=args.adaface_encoder_types,
95
- adaface_ckpt_paths=[args.adaface_ckpt_path], device=device)
96
- # Update base model type.
97
- args.model_style_type = model_style_type
98
 
99
  # uploaded_image_paths is a list of tuples:
100
  # [('/tmp/gradio/249981e66a7c665aaaf1c7eaeb24949af4366c88/jensen huang.jpg', None)]
@@ -120,8 +110,9 @@ def gen_init_images(uploaded_image_paths, model_style_type, prompt, out_image_co
120
 
121
  # samples: A list of PIL Image instances.
122
  with torch.no_grad():
123
- samples = adaface(noise, prompt,
124
- placeholder_tokens_pos='append',
 
125
  out_image_count=out_image_count, verbose=True)
126
 
127
  face_paths = []
@@ -137,23 +128,12 @@ def gen_init_images(uploaded_image_paths, model_style_type, prompt, out_image_co
137
 
138
  @spaces.GPU(duration=90)
139
  def generate_video(image_container, uploaded_image_paths, init_img_file_paths, init_img_selected_idx,
140
- init_image_strength, init_image_final_weight, model_style_type,
141
  prompt, negative_prompt, num_steps, video_length, guidance_scale, do_neg_id_prompt_weight,
142
  seed, attn_scale, image_embed_cfg_begin_scale, image_embed_cfg_end_scale,
143
  is_adaface_enabled, adaface_ckpt_path, adaface_power_scale,
144
  id_animator_anneal_steps, progress=gr.Progress(track_tqdm=True)):
145
-
146
  global adaface, id_animator
147
- model_style_type = model_style_type.lower()
148
- base_model_path = model_style_type2base_model_path[model_style_type]
149
- # If the base model type is changed, reload the model.
150
- if model_style_type != args.model_style_type:
151
- id_animator = load_model(model_style_type=model_style_type, device=device)
152
- adaface = AdaFaceWrapper(pipeline_name="text2img", base_model_path=base_model_path,
153
- adaface_encoder_types=args.adaface_encoder_types,
154
- adaface_ckpt_paths=[args.adaface_ckpt_path], device=device)
155
- # Update base model type.
156
- args.model_style_type = model_style_type
157
 
158
  if prompt is None:
159
  prompt = ""
@@ -231,11 +211,24 @@ def generate_video(image_container, uploaded_image_paths, init_img_file_paths, i
231
  save_videos_grid(sample, save_sample_path)
232
  return save_sample_path
233
 
234
- def validate_prompt(prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
235
  if not prompt:
236
  raise gr.Error("Prompt cannot be blank")
237
 
238
- with gr.Blocks(css=css) as demo:
239
  gr.Markdown(
240
  """
241
  # AdaFace-Animate: Zero-Shot Subject-Driven Video Generation for Humans
@@ -269,7 +262,7 @@ with gr.Blocks(css=css) as demo:
269
  file_count="multiple"
270
  )
271
  image_container = gr.Image(label="image container", sources="upload", type="numpy", height=256, visible=False)
272
- uploaded_files_gallery = gr.Gallery(label="Subject images", visible=False, columns=3, rows=1, height=300)
273
  with gr.Column(visible=False) as clear_button_column:
274
  remove_and_reupload = gr.ClearButton(value="Remove and upload subject images", components=files, size="sm")
275
 
@@ -348,6 +341,7 @@ with gr.Blocks(css=css) as demo:
348
  maximum=0.9,
349
  step=0.1,
350
  value=args.do_neg_id_prompt_weight,
 
351
  )
352
 
353
  seed = gr.Slider(
@@ -447,15 +441,18 @@ with gr.Blocks(css=css) as demo:
447
  files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files_gallery, clear_button_column, files])
448
  remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files_gallery, clear_button_column, files, init_img_selected_idx])
449
 
450
- init_img_files.upload(fn=swap_to_gallery, inputs=init_img_files, outputs=[uploaded_init_img_gallery, init_clear_button_column, init_img_files])
451
- remove_init_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_init_img_gallery, init_clear_button_column,
452
- init_img_files, init_img_selected_idx])
453
- gen_init.click(fn=gen_init_images, inputs=[uploaded_files_gallery, model_style_type, prompt],
 
 
 
454
  outputs=[uploaded_init_img_gallery, init_img_files, init_clear_button_column])
455
  uploaded_init_img_gallery.select(fn=get_clicked_image, inputs=None, outputs=init_img_selected_idx)
456
 
457
- submit.click(fn=validate_prompt,
458
- inputs=[prompt],outputs=None).success(
459
  fn=randomize_seed_fn,
460
  inputs=[seed, randomize_seed],
461
  outputs=seed,
@@ -463,8 +460,8 @@ with gr.Blocks(css=css) as demo:
463
  api_name=False,
464
  ).then(
465
  fn=generate_video,
466
- inputs=[image_container, files, init_img_files, init_img_selected_idx, init_image_strength,
467
- init_image_final_weight, model_style_type,
468
  prompt, negative_prompt, num_steps, video_length, guidance_scale, do_neg_id_prompt_weight,
469
  seed, attn_scale, image_embed_cfg_begin_scale, image_embed_cfg_end_scale,
470
  is_adaface_enabled, adaface_ckpt_path, adaface_power_scale, id_animator_anneal_steps],
 
19
  from PIL import Image
20
  import torch
21
  import argparse
22
+
23
  parser = argparse.ArgumentParser()
24
  parser.add_argument("--adaface_encoder_types", type=str, nargs="+", default=["consistentID", "arc2face"],
25
  choices=["arc2face", "consistentID"], help="Type(s) of the ID2Ada prompt encoders")
 
29
  choices=["realistic", "anime", "photorealistic"], help="Type of the base model")
30
  parser.add_argument("--guidance_scale", type=float, default=8.0,
31
  help="The guidance scale for the diffusion model. Default: 8.0")
32
+ parser.add_argument("--do_neg_id_prompt_weight", type=float, default=0,
33
  help="The weight of added ID prompt embeddings into the negative prompt. Default: 0, disabled.")
34
 
35
  parser.add_argument('--gpu', type=int, default=None)
 
79
  return data.index
80
 
81
  @spaces.GPU
82
+ def gen_init_images(uploaded_image_paths, prompt,
83
+ guidance_scale, do_neg_id_prompt_weight, out_image_count=3):
84
  global adaface, id_animator
85
  if uploaded_image_paths is None:
86
  print("No image uploaded")
87
  return None, None, None
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  # uploaded_image_paths is a list of tuples:
90
  # [('/tmp/gradio/249981e66a7c665aaaf1c7eaeb24949af4366c88/jensen huang.jpg', None)]
 
110
 
111
  # samples: A list of PIL Image instances.
112
  with torch.no_grad():
113
+ samples = adaface(noise, prompt, placeholder_tokens_pos='append',
114
+ guidance_scale=guidance_scale,
115
+ do_neg_id_prompt_weight=do_neg_id_prompt_weight,
116
  out_image_count=out_image_count, verbose=True)
117
 
118
  face_paths = []
 
128
 
129
  @spaces.GPU(duration=90)
130
  def generate_video(image_container, uploaded_image_paths, init_img_file_paths, init_img_selected_idx,
131
+ init_image_strength, init_image_final_weight,
132
  prompt, negative_prompt, num_steps, video_length, guidance_scale, do_neg_id_prompt_weight,
133
  seed, attn_scale, image_embed_cfg_begin_scale, image_embed_cfg_end_scale,
134
  is_adaface_enabled, adaface_ckpt_path, adaface_power_scale,
135
  id_animator_anneal_steps, progress=gr.Progress(track_tqdm=True)):
 
136
  global adaface, id_animator
 
 
 
 
 
 
 
 
 
 
137
 
138
  if prompt is None:
139
  prompt = ""
 
211
  save_videos_grid(sample, save_sample_path)
212
  return save_sample_path
213
 
214
+ def check_prompt_and_model_type(prompt, model_style_type):
215
+ global adaface, id_animator
216
+
217
+ model_style_type = model_style_type.lower()
218
+ base_model_path = model_style_type2base_model_path[model_style_type]
219
+ # If the base model type is changed, reload the model.
220
+ if model_style_type != args.model_style_type:
221
+ id_animator = load_model(model_style_type=model_style_type, device=device)
222
+ adaface = AdaFaceWrapper(pipeline_name="text2img", base_model_path=base_model_path,
223
+ adaface_encoder_types=args.adaface_encoder_types,
224
+ adaface_ckpt_paths=[args.adaface_ckpt_path], device=device)
225
+ # Update base model type.
226
+ args.model_style_type = model_style_type
227
+
228
  if not prompt:
229
  raise gr.Error("Prompt cannot be blank")
230
 
231
+ with gr.Blocks(css=css, theme=gr.themes.Origin()) as demo:
232
  gr.Markdown(
233
  """
234
  # AdaFace-Animate: Zero-Shot Subject-Driven Video Generation for Humans
 
262
  file_count="multiple"
263
  )
264
  image_container = gr.Image(label="image container", sources="upload", type="numpy", height=256, visible=False)
265
+ uploaded_files_gallery = gr.Gallery(label="Subject images", visible=False, columns=3, rows=2, height=300)
266
  with gr.Column(visible=False) as clear_button_column:
267
  remove_and_reupload = gr.ClearButton(value="Remove and upload subject images", components=files, size="sm")
268
 
 
341
  maximum=0.9,
342
  step=0.1,
343
  value=args.do_neg_id_prompt_weight,
344
+ visible=False
345
  )
346
 
347
  seed = gr.Slider(
 
441
  files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files_gallery, clear_button_column, files])
442
  remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files_gallery, clear_button_column, files, init_img_selected_idx])
443
 
444
+ init_img_files.upload(fn=swap_to_gallery, inputs=init_img_files,
445
+ outputs=[uploaded_init_img_gallery, init_clear_button_column, init_img_files])
446
+ remove_init_and_reupload.click(fn=remove_back_to_files,
447
+ outputs=[uploaded_init_img_gallery, init_clear_button_column,
448
+ init_img_files, init_img_selected_idx])
449
+ gen_init.click(fn=gen_init_images, inputs=[uploaded_files_gallery, prompt,
450
+ guidance_scale, do_neg_id_prompt_weight],
451
  outputs=[uploaded_init_img_gallery, init_img_files, init_clear_button_column])
452
  uploaded_init_img_gallery.select(fn=get_clicked_image, inputs=None, outputs=init_img_selected_idx)
453
 
454
+ submit.click(fn=check_prompt_and_model_type,
455
+ inputs=[prompt, model_style_type],outputs=None).success(
456
  fn=randomize_seed_fn,
457
  inputs=[seed, randomize_seed],
458
  outputs=seed,
 
460
  api_name=False,
461
  ).then(
462
  fn=generate_video,
463
+ inputs=[image_container, files,
464
+ init_img_files, init_img_selected_idx, init_image_strength, init_image_final_weight,
465
  prompt, negative_prompt, num_steps, video_length, guidance_scale, do_neg_id_prompt_weight,
466
  seed, attn_scale, image_embed_cfg_begin_scale, image_embed_cfg_end_scale,
467
  is_adaface_enabled, adaface_ckpt_path, adaface_power_scale, id_animator_anneal_steps],
models/adaface/{VGGface2_HQ_masks2024-10-08T14-42-05_zero3-ada-30000.pt → VGGface2_HQ_masks2024-10-13T11-21-07_zero3-ada-9000.pt} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:34bbdaa97fb2da9e2aae4204bfd2f5c1565a84c664520a5f537129419ecb53fa
3
- size 1814921594
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c8d4dae6291aa783c03de6e54997a79b7d61f7bdc2457afb6303bcfc290d0956
3
+ size 1814921242