multimodalart HF staff commited on
Commit
0272460
1 Parent(s): 874e33b
Files changed (1) hide show
  1. app.py +51 -23
app.py CHANGED
@@ -37,7 +37,7 @@ ip_model = IPAdapterFaceID(pipe, ip_ckpt, device)
37
  ip_model_plus = IPAdapterFaceIDPlus(pipe, image_encoder_path, ip_plus_ckpt, device)
38
 
39
  @spaces.GPU(enable_queue=True)
40
- def generate_image(images, prompt, negative_prompt, preserve_face_structure, progress=gr.Progress(track_tqdm=True)):
41
  pipe.to(device)
42
  app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
43
  app.prepare(ctx_id=0, det_size=(640, 640))
@@ -55,40 +55,68 @@ def generate_image(images, prompt, negative_prompt, preserve_face_structure, pro
55
 
56
  average_embedding = torch.mean(torch.stack(faceid_all_embeds, dim=0), dim=0)
57
 
 
 
58
  if(not preserve_face_structure):
59
  print("Generating normal")
60
  image = ip_model.generate(
61
- prompt=prompt, negative_prompt=negative_prompt, faceid_embeds=average_embedding,
62
- width=512, height=512, num_inference_steps=30
63
  )
64
  else:
65
  print("Generating plus")
66
  image = ip_model_plus.generate(
67
- prompt=prompt, negative_prompt=negative_prompt, faceid_embeds=average_embedding,
68
- face_image=face_image, shortcut=True, s_scale=1.5, width=512, height=512, num_inference_steps=30
69
  )
70
  print(image)
71
  return image
 
 
 
 
 
 
 
 
 
 
 
 
72
  css = '''
73
  h1{margin-bottom: 0 !important}
74
  '''
75
- demo = gr.Interface(
76
- css=css,
77
- fn=generate_image,
78
- inputs=[
79
- gr.Files(
80
- label="Drag 1 or more photos of your face",
81
- file_types=["image"]
82
- ),
83
- gr.Textbox(label="Prompt",
 
 
 
 
84
  info="Try something like 'a photo of a man/woman/person'",
85
- placeholder="A photo of a [man/woman/person]..."),
86
- gr.Textbox(label="Negative Prompt", placeholder="low quality"),
87
- gr.Checkbox(label="Preserve Face Structure", info="Higher quality, less versatility (the face structure of your first photo will be preserved)", value=True),
88
- ],
89
- outputs=[gr.Gallery(label="Generated Image")],
90
- title="IP-Adapter-FaceID demo",
91
- description="Demo for the [h94/IP-Adapter-FaceID model](https://huggingface.co/h94/IP-Adapter-FaceID) - 'preserve face structure' uses the plus v2 model. Non-commercial license",
92
- allow_flagging=False,
93
- )
 
 
 
 
 
 
 
 
 
 
94
  demo.launch()
 
37
  ip_model_plus = IPAdapterFaceIDPlus(pipe, image_encoder_path, ip_plus_ckpt, device)
38
 
39
  @spaces.GPU(enable_queue=True)
40
+ def generate_image(images, prompt, negative_prompt, preserve_face_structure, face_strength, likeness_strength, nfaa_negative_prompt, progress=gr.Progress(track_tqdm=True)):
41
  pipe.to(device)
42
  app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
43
  app.prepare(ctx_id=0, det_size=(640, 640))
 
55
 
56
  average_embedding = torch.mean(torch.stack(faceid_all_embeds, dim=0), dim=0)
57
 
58
+ total_negative_prompt = f"{negative_prompt} {nfaa_negative_prompt}"
59
+
60
  if(not preserve_face_structure):
61
  print("Generating normal")
62
  image = ip_model.generate(
63
+ prompt=prompt, negative_prompt=total_negative_prompt, faceid_embeds=average_embedding,
64
+ scale=likeness_strength, width=512, height=512, num_inference_steps=30
65
  )
66
  else:
67
  print("Generating plus")
68
  image = ip_model_plus.generate(
69
+ prompt=prompt, negative_prompt=total_negative_prompt, faceid_embeds=average_embedding,
70
+ scale=likeness_strength, face_image=face_image, shortcut=True, s_scale=face_strength, width=512, height=512, num_inference_steps=30
71
  )
72
  print(image)
73
  return image
74
+
75
+ def change_style(style):
76
+ if style == "Photorealistic":
77
+ return(gr.update(value=True), gr.update(value=1.3), gr.update(value=1.0))
78
+ else:
79
+ return(gr.update(value=True), gr.update(value=0.1), gr.update(value=0.8))
80
+
81
+ def swap_to_gallery(images):
82
+ return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
83
+
84
+ def remove_back_to_files():
85
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
86
  css = '''
87
  h1{margin-bottom: 0 !important}
88
  '''
89
+ with gr.Blocks(css=css) as demo:
90
+ gr.Markdown("# IP-Adapter-FaceID demo")
91
+ gr.Markdown("Demo for the [h94/IP-Adapter-FaceID model](https://huggingface.co/h94/IP-Adapter-FaceID) - 'preserve face structure' uses the plus v2 model. Non-commercial license")
92
+ with gr.Row():
93
+ with gr.Column():
94
+ files = gr.Files(
95
+ label="Drag 1 or more photos of your face",
96
+ file_types=["image"]
97
+ )
98
+ uploaded_files = gr.Gallery(label="Your images", visible=False, columns=5, rows=1, height=125)
99
+ with gr.Column(visible=False) as clear_button:
100
+ remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
101
+ prompt = gr.Textbox(label="Prompt",
102
  info="Try something like 'a photo of a man/woman/person'",
103
+ placeholder="A photo of a [man/woman/person]...")
104
+ negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="low quality")
105
+ style = gr.Radio(label="Generation type", info="For stylized try prompts like 'a watercolor painting of a woman'", choices=["Photorealistic", "Stylized"], value="Photorealistic")
106
+ submit = gr.Button("Submit")
107
+ with gr.Accordion(open=False, label="Advanced Options"):
108
+ preserve = gr.Checkbox(label="Preserve Face Structure", info="Higher quality, less versatility (the face structure of your first photo will be preserved)", value=True)
109
+ face_strength = gr.Slider(label="Face Structure strength", info="Only applied if preserve face structure is checked", value=1.3, step=0.1, minimum=0, maximum=3)
110
+ likeness_strength = gr.Slider(label="Face Embed strength", value=1.0, step=0.1, minimum=0, maximum=5)
111
+ nfaa_negative_prompts = gr.Textbox(label="Appended Negative Prompts", info="Negative prompts to steer generations towards safe for all audiences outputs", value="naked, swimsuit")
112
+ with gr.Column():
113
+ gallery = gr.Gallery(label="Generated Images")
114
+ style.change(fn=change_style,
115
+ inputs=style,
116
+ outputs=[preserve, face_strength, likeness_strength])
117
+ files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
118
+ remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
119
+ submit.click(fn=generate_image,
120
+ inputs=[files,prompt,negative_prompt,preserve, face_strength, likeness_strength, nfaa_negative_prompts],
121
+ outputs=gallery)
122
  demo.launch()