ziffir commited on
Commit
3fbd768
1 Parent(s): 1c650f1

Upload 4 files

Browse files
Files changed (4) hide show
  1. README (1).md +13 -0
  2. app.py +136 -0
  3. gitattributes (2).txt +35 -0
  4. requirements.txt +10 -0
README (1).md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: IP-Adapter-FaceID
3
+ emoji: 🧑🏿🧑🏽‍🦱
4
+ colorFrom: gray
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 4.11.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/Realistic_Vision_V4.0_noVAE"
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", filename="ip-adapter-faceid_sd15.bin", repo_type="model")
17
+ ip_plus_ckpt = hf_hub_download(repo_id="h94/IP-Adapter-FaceID", 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(
26
+ num_train_timesteps=1000,
27
+ beta_start=0.00085,
28
+ beta_end=0.012,
29
+ beta_schedule="scaled_linear",
30
+ clip_sample=False,
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()
gitattributes (2).txt ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ insightface==0.7.3
2
+ diffusers
3
+ transformers
4
+ accelerate
5
+ safetensors
6
+ einops
7
+ onnxruntime-gpu
8
+ spaces==0.19.4
9
+ opencv-python
10
+ git+https://github.com/tencent-ailab/IP-Adapter.git