Richard Neuschulz commited on
Commit
ed5c8ca
1 Parent(s): 606515a

major overhaul (unsafe)

Browse files
Files changed (1) hide show
  1. app.py +111 -29
app.py CHANGED
@@ -1,24 +1,25 @@
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import cv2
3
- from insightface.app import FaceAnalysis
4
- import torch
5
-
6
- app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
7
- app.prepare(ctx_id=0, det_size=(640, 640))
8
-
9
- image = cv2.imread("person.jpg")
10
- faces = app.get(image)
11
 
12
- faceid_embeds = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
13
-
14
- import torch
15
- from diffusers import StableDiffusionXLPipeline, DDIMScheduler
16
- from PIL import Image
17
 
18
- from ip_adapter.ip_adapter_faceid import IPAdapterFaceIDXL
 
 
19
 
20
- base_model_path = "SG161222/RealVisXL_V3.0"
21
- ip_ckpt = "ip-adapter-faceid_sdxl.bin"
22
  device = "cuda"
23
 
24
  noise_scheduler = DDIMScheduler(
@@ -30,25 +31,106 @@ noise_scheduler = DDIMScheduler(
30
  set_alpha_to_one=False,
31
  steps_offset=1,
32
  )
33
- pipe = StableDiffusionXLPipeline.from_pretrained(
 
34
  base_model_path,
35
  torch_dtype=torch.float16,
36
  scheduler=noise_scheduler,
37
- add_watermarker=False,
 
 
38
  )
39
 
40
- # load ip-adapter
41
- ip_model = IPAdapterFaceIDXL(pipe, ip_ckpt, device)
42
 
43
- # generate image
44
- prompt = "A closeup shot of a beautiful Asian teenage girl in a white dress wearing small silver earrings in the garden, under the soft morning light"
45
- negative_prompt = "monochrome, lowres, bad anatomy, worst quality, low quality, blurry"
46
 
47
- images = ip_model.generate(
48
- prompt=prompt, negative_prompt=negative_prompt, faceid_embeds=faceid_embeds, num_samples=2,
49
- width=1024, height=1024,
50
- num_inference_steps=30, guidance_scale=7.5, seed=2023
51
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- gr.load("models/h94/IP-Adapter-FaceID").launch()
 
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import spaces
3
+ from diffusers import StableDiffusionPipeline, DDIMScheduler, AutoencoderKL
4
+ from transformers import AutoFeatureExtractor
5
+ from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
6
+ from ip_adapter.ip_adapter_faceid import IPAdapterFaceID, IPAdapterFaceIDPlus
7
+ from huggingface_hub import hf_hub_download
8
+ from insightface.app import FaceAnalysis
9
+ from insightface.utils import face_align
10
  import gradio as gr
11
  import cv2
 
 
 
 
 
 
 
 
12
 
13
+ base_model_path = "SG161222/RealVisXL_V3.0"
14
+ # vae_model_path = "stabilityai/sd-vae-ft-mse"
15
+ image_encoder_path = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K"
16
+ ip_ckpt = hf_hub_download(repo_id="h94/ip-adapter-faceid_sdxl.bin", filename="ip-adapter-faceid_sd15.bin", repo_type="model")
17
+ ip_plus_ckpt = hf_hub_download(repo_id="h94/ip-adapter-faceid_sdxl.bin", filename="ip-adapter-faceid-plusv2_sd15.bin", repo_type="model")
18
 
19
+ #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
  device = "cuda"
24
 
25
  noise_scheduler = DDIMScheduler(
 
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,
41
+ #safety_checker=safety_checker
42
  )
43
 
44
+ #pipe.load_lora_weights("h94/IP-Adapter-FaceID", weight_name="ip-adapter-faceid-plusv2_sd15_lora.safetensors")
45
+ #pipe.fuse_lora()
46
 
47
+ ip_model = IPAdapterFaceID(pipe, ip_ckpt, device)
48
+ ip_model_plus = IPAdapterFaceIDPlus(pipe, image_encoder_path, ip_plus_ckpt, device)
 
49
 
50
+ @spaces.GPU(enable_queue=True)
51
+ def generate_image(images, prompt, negative_prompt, preserve_face_structure, face_strength, likeness_strength, nfaa_negative_prompt, progress=gr.Progress(track_tqdm=True)):
52
+ pipe.to(device)
53
+ app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
54
+ app.prepare(ctx_id=0, det_size=(640, 640))
55
+
56
+ faceid_all_embeds = []
57
+ first_iteration = True
58
+ for image in images:
59
+ face = cv2.imread(image)
60
+ faces = app.get(face)
61
+ faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
62
+ faceid_all_embeds.append(faceid_embed)
63
+ if(first_iteration and preserve_face_structure):
64
+ face_image = face_align.norm_crop(face, landmark=faces[0].kps, image_size=224) # you can also segment the face
65
+ first_iteration = False
66
+
67
+ average_embedding = torch.mean(torch.stack(faceid_all_embeds, dim=0), dim=0)
68
+
69
+ total_negative_prompt = f"{negative_prompt} {nfaa_negative_prompt}"
70
+
71
+ if(not preserve_face_structure):
72
+ print("Generating normal")
73
+ image = ip_model.generate(
74
+ prompt=prompt, negative_prompt=total_negative_prompt, faceid_embeds=average_embedding,
75
+ scale=likeness_strength, width=512, height=512, num_inference_steps=30
76
+ )
77
+ else:
78
+ print("Generating plus")
79
+ image = ip_model_plus.generate(
80
+ prompt=prompt, negative_prompt=total_negative_prompt, faceid_embeds=average_embedding,
81
+ scale=likeness_strength, face_image=face_image, shortcut=True, s_scale=face_strength, width=512, height=512, num_inference_steps=30
82
+ )
83
+ print(image)
84
+ return image
85
+
86
+ def change_style(style):
87
+ if style == "Photorealistic":
88
+ return(gr.update(value=True), gr.update(value=1.3), gr.update(value=1.0))
89
+ else:
90
+ return(gr.update(value=True), gr.update(value=0.1), gr.update(value=0.8))
91
 
92
+ def swap_to_gallery(images):
93
+ return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
94
 
95
+ def remove_back_to_files():
96
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
97
+ css = '''
98
+ h1{margin-bottom: 0 !important}
99
+ '''
100
+ with gr.Blocks(css=css) as demo:
101
+ gr.Markdown("# IP-Adapter-FaceID Plus demo")
102
+ gr.Markdown("Demo for the [h94/IP-Adapter-FaceID model](https://huggingface.co/h94/IP-Adapter-FaceID) - Non-commercial license")
103
+ with gr.Row():
104
+ with gr.Column():
105
+ files = gr.Files(
106
+ label="Drag 1 or more photos of your face",
107
+ file_types=["image"]
108
+ )
109
+ uploaded_files = gr.Gallery(label="Your images", visible=False, columns=5, rows=1, height=125)
110
+ with gr.Column(visible=False) as clear_button:
111
+ remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
112
+ prompt = gr.Textbox(label="Prompt",
113
+ info="Try something like 'a photo of a man/woman/person'",
114
+ placeholder="A photo of a [man/woman/person]...")
115
+ negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="low quality")
116
+ style = gr.Radio(label="Generation type", info="For stylized try prompts like 'a watercolor painting of a woman'", choices=["Photorealistic", "Stylized"], value="Photorealistic")
117
+ submit = gr.Button("Submit")
118
+ with gr.Accordion(open=False, label="Advanced Options"):
119
+ preserve = gr.Checkbox(label="Preserve Face Structure", info="Higher quality, less versatility (the face structure of your first photo will be preserved). Unchecking this will use the v1 model.", value=True)
120
+ 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)
121
+ likeness_strength = gr.Slider(label="Face Embed strength", value=1.0, step=0.1, minimum=0, maximum=5)
122
+ nfaa_negative_prompts = gr.Textbox(label="Appended Negative Prompts", info="Negative prompts to steer generations towards safe for all audiences outputs", value="naked, bikini, skimpy, scanty, bare skin, lingerie, swimsuit, exposed, see-through")
123
+ with gr.Column():
124
+ gallery = gr.Gallery(label="Generated Images")
125
+ style.change(fn=change_style,
126
+ inputs=style,
127
+ outputs=[preserve, face_strength, likeness_strength])
128
+ files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
129
+ remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
130
+ submit.click(fn=generate_image,
131
+ inputs=[files,prompt,negative_prompt,preserve, face_strength, likeness_strength, nfaa_negative_prompts],
132
+ outputs=gallery)
133
+
134
+ # 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.")
135
+
136
+ demo.launch()