KseniaAI commited on
Commit
9667679
β€’
1 Parent(s): 0c7e9b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -9
app.py CHANGED
@@ -20,8 +20,7 @@ safety_model_id = "CompVis/stable-diffusion-safety-checker"
20
  safety_feature_extractor = AutoFeatureExtractor.from_pretrained(safety_model_id)
21
  safety_checker = StableDiffusionSafetyChecker.from_pretrained(safety_model_id)
22
 
23
- # Change device to CPU
24
- device = "cpu"
25
 
26
  noise_scheduler = DDIMScheduler(
27
  num_train_timesteps=1000,
@@ -32,10 +31,10 @@ noise_scheduler = DDIMScheduler(
32
  set_alpha_to_one=False,
33
  steps_offset=1,
34
  )
35
- vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float32) # Change dtype to float32 for CPU compatibility
36
  pipe = StableDiffusionPipeline.from_pretrained(
37
  base_model_path,
38
- torch_dtype=torch.float32, # Change dtype to float32 for CPU
39
  scheduler=noise_scheduler,
40
  vae=vae,
41
  feature_extractor=safety_feature_extractor,
@@ -53,7 +52,7 @@ app.prepare(ctx_id=0, det_size=(640, 640))
53
 
54
  cv2.setNumThreads(1)
55
 
56
- @spaces.GPU(enable_queue=True) # Changed to GPU to allow flexibility between CPU/GPU
57
  def generate_image(images, prompt, negative_prompt, preserve_face_structure, face_strength, likeness_strength, nfaa_negative_prompt, progress=gr.Progress(track_tqdm=True)):
58
  faceid_all_embeds = []
59
  first_iteration = True
@@ -63,7 +62,7 @@ def generate_image(images, prompt, negative_prompt, preserve_face_structure, fac
63
  faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
64
  faceid_all_embeds.append(faceid_embed)
65
  if(first_iteration and preserve_face_structure):
66
- face_image = face_align.norm_crop(face, landmark=faces[0].kps, image_size=224) # you can also segment the face
67
  first_iteration = False
68
 
69
  average_embedding = torch.mean(torch.stack(faceid_all_embeds, dim=0), dim=0)
@@ -96,11 +95,9 @@ def swap_to_gallery(images):
96
 
97
  def remove_back_to_files():
98
  return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
99
-
100
  css = '''
101
  h1{margin-bottom: 0 !important}
102
  '''
103
-
104
  with gr.Blocks(css=css) as demo:
105
  gr.Markdown("# IP-Adapter-FaceID Plus demo")
106
  gr.Markdown("Demo for the [h94/IP-Adapter-FaceID model](https://huggingface.co/h94/IP-Adapter-FaceID) - Generate AI images with your own face - Non-commercial license")
@@ -137,4 +134,4 @@ with gr.Blocks(css=css) as demo:
137
 
138
  gr.Markdown("This demo includes extra features to mitigate the implicit bias of the model and prevent explicit usage of it to generate content with faces of people, including third parties, that is not safe for all audiences, including naked or semi-naked people.")
139
 
140
- demo.launch()
 
20
  safety_feature_extractor = AutoFeatureExtractor.from_pretrained(safety_model_id)
21
  safety_checker = StableDiffusionSafetyChecker.from_pretrained(safety_model_id)
22
 
23
+ device = "cuda"
 
24
 
25
  noise_scheduler = DDIMScheduler(
26
  num_train_timesteps=1000,
 
31
  set_alpha_to_one=False,
32
  steps_offset=1,
33
  )
34
+ vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16)
35
  pipe = StableDiffusionPipeline.from_pretrained(
36
  base_model_path,
37
+ torch_dtype=torch.float16,
38
  scheduler=noise_scheduler,
39
  vae=vae,
40
  feature_extractor=safety_feature_extractor,
 
52
 
53
  cv2.setNumThreads(1)
54
 
55
+ @spaces.GPU(enable_queue=True)
56
  def generate_image(images, prompt, negative_prompt, preserve_face_structure, face_strength, likeness_strength, nfaa_negative_prompt, progress=gr.Progress(track_tqdm=True)):
57
  faceid_all_embeds = []
58
  first_iteration = True
 
62
  faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
63
  faceid_all_embeds.append(faceid_embed)
64
  if(first_iteration and preserve_face_structure):
65
+ face_image = face_align.norm_crop(face, landmark=faces[0].kps, image_size=224) # you can also segment the face
66
  first_iteration = False
67
 
68
  average_embedding = torch.mean(torch.stack(faceid_all_embeds, dim=0), dim=0)
 
95
 
96
  def remove_back_to_files():
97
  return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
 
98
  css = '''
99
  h1{margin-bottom: 0 !important}
100
  '''
 
101
  with gr.Blocks(css=css) as demo:
102
  gr.Markdown("# IP-Adapter-FaceID Plus demo")
103
  gr.Markdown("Demo for the [h94/IP-Adapter-FaceID model](https://huggingface.co/h94/IP-Adapter-FaceID) - Generate AI images with your own face - Non-commercial license")
 
134
 
135
  gr.Markdown("This demo includes extra features to mitigate the implicit bias of the model and prevent explicit usage of it to generate content with faces of people, including third parties, that is not safe for all audiences, including naked or semi-naked people.")
136
 
137
+ demo.launch()