Deadmon commited on
Commit
2b96aad
1 Parent(s): cf9fa34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -120
app.py CHANGED
@@ -1,88 +1,36 @@
1
- import torch
2
  import spaces
3
- from diffusers import DDIMScheduler, StableDiffusionXLPipeline
4
- import ipown
 
 
5
  from huggingface_hub import hf_hub_download
6
  from insightface.app import FaceAnalysis
 
7
  import gradio as gr
8
  import cv2
9
 
10
- # List of models for switching
11
- model_options = {
12
- "CyberRealistic": "John6666/cyberrealistic-pony-v61-sdxl",
13
- "StallionDreams": "John6666/stallion-dreams-pony-realistic-v1-sdxl",
14
- "PonyRealism": "John6666/pony-realism-v21main-sdxl"
 
 
15
  }
16
 
17
- # Full style list for applying styles to the prompt
18
- style_list = [
19
- {
20
- "name": "(No style)",
21
- "prompt": "{prompt}",
22
- "negative_prompt": "longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
23
- },
24
- {
25
- "name": "Cinematic",
26
- "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
27
- "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
28
- },
29
- {
30
- "name": "3D Model",
31
- "prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
32
- "negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
33
- },
34
- {
35
- "name": "Anime",
36
- "prompt": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime, highly detailed",
37
- "negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
38
- },
39
- {
40
- "name": "Digital Art",
41
- "prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed",
42
- "negative_prompt": "photo, photorealistic, realism, ugly",
43
- },
44
- {
45
- "name": "Photographic",
46
- "prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
47
- "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
48
- },
49
- {
50
- "name": "Pixel art",
51
- "prompt": "pixel-art {prompt} . low-res, blocky, pixel art style, 8-bit graphics",
52
- "negative_prompt": "sloppy, messy, blurry, noisy, highly detailed, ultra textured, photo, realistic",
53
- },
54
- {
55
- "name": "Fantasy art",
56
- "prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
57
- "negative_prompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white",
58
- },
59
- {
60
- "name": "Neonpunk",
61
- "prompt": "neonpunk style {prompt} . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional",
62
- "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured",
63
- },
64
- {
65
- "name": "Manga",
66
- "prompt": "manga style {prompt} . vibrant, high-energy, detailed, iconic, Japanese comic style",
67
- "negative_prompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, Western comic style",
68
- },
69
- ]
70
-
71
- # Styles dictionary to map style names to prompts and negative prompts
72
- styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
73
- STYLE_NAMES = list(styles.keys())
74
- DEFAULT_STYLE_NAME = "(No style)"
75
-
76
- # Function to apply the selected style
77
- def apply_style(style_name: str, positive: str, negative: str = "") -> tuple[str, str]:
78
- p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
79
- return p.replace("{prompt}", positive), n + negative
80
-
81
- # Download the necessary model component
82
- ip_ckpt = hf_hub_download(repo_id="h94/IP-Adapter-FaceID", filename="ip-adapter-faceid_sdxl.bin", repo_type="model")
83
  device = "cuda"
84
 
85
- # Configure the noise scheduler
86
  noise_scheduler = DDIMScheduler(
87
  num_train_timesteps=1000,
88
  beta_start=0.00085,
@@ -92,55 +40,71 @@ noise_scheduler = DDIMScheduler(
92
  set_alpha_to_one=False,
93
  steps_offset=1,
94
  )
 
95
 
96
- # Function to initialize the pipeline with a selected model
97
- def get_pipeline(model_path):
98
- return StableDiffusionXLPipeline.from_pretrained(
99
- model_path,
100
  torch_dtype=torch.float16,
101
  scheduler=noise_scheduler,
102
- use_safetensors=True,
103
- )
 
 
 
104
 
105
- # Initialize with a default model
106
- current_model = model_options["PonyRealism"]
107
- pipe = get_pipeline(current_model)
108
- ip_model = ipown.IPAdapterFaceIDXL(pipe, ip_ckpt, device)
109
 
110
- @spaces.GPU()
111
- def generate_image(images, model_choice, style_name, prompt, negative_prompt, face_strength, likeness_strength, num_inference_steps, guidance_scale, width, height):
112
- global current_model, pipe, ip_model
113
 
114
- # Update the model if the choice has changed
115
- if model_options[model_choice] != current_model:
116
- current_model = model_options[model_choice]
117
- pipe = get_pipeline(current_model)
118
- ip_model = ipown.IPAdapterFaceIDXL(pipe, ip_ckpt, device)
119
 
120
- torch.cuda.empty_cache()
121
-
122
- app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
123
- app.prepare(ctx_id=0, det_size=(512, 512))
 
 
 
124
 
125
  faceid_all_embeds = []
 
126
  for image in images:
127
  face = cv2.imread(image)
128
  faces = app.get(face)
129
  faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
130
  faceid_all_embeds.append(faceid_embed)
131
-
 
 
 
132
  average_embedding = torch.mean(torch.stack(faceid_all_embeds, dim=0), dim=0)
133
 
134
- # Apply the selected style
135
- styled_prompt, styled_negative_prompt = apply_style(style_name, prompt, negative_prompt)
136
 
137
- image = ip_model.generate(
138
- prompt=styled_prompt, negative_prompt=styled_negative_prompt, faceid_embeds=average_embedding,
139
- scale=likeness_strength, width=width, height=height, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps
140
- )
141
-
 
 
 
 
 
 
 
 
142
  return image
143
 
 
 
 
 
 
 
144
  def swap_to_gallery(images):
145
  return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
146
 
@@ -149,34 +113,48 @@ def remove_back_to_files():
149
 
150
  css = '''
151
  h1{margin-bottom: 0 !important}
 
152
  '''
153
 
154
  with gr.Blocks(css=css) as demo:
155
- gr.Markdown("# IP-Adapter-FaceID SDXL demo")
156
- gr.Markdown("A simple Demo for the [h94/IP-Adapter-FaceID SDXL model](https://huggingface.co/h94/IP-Adapter-FaceID).")
157
  with gr.Row():
158
  with gr.Column():
159
- model_dropdown = gr.Dropdown(label="Select Model", choices=list(model_options.keys()), value="PonyRealism")
160
- style_dropdown = gr.Dropdown(label="Style", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)
161
- files = gr.Files(label="Drag 1 or more photos of your face", file_types=["image"])
162
- uploaded_files = gr.Gallery(label="Your images", visible=False, columns=5, rows=1, height=250)
 
163
  with gr.Column(visible=False) as clear_button:
164
- remove_and_reupload = gr.ClearButton(value="Remove files and upload new ones", components=files, size="sm")
165
- prompt = gr.Textbox(label="Prompt", placeholder="A photo of a man/woman/person ...")
 
 
166
  negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="low quality")
167
- face_strength = gr.Slider(label="Face Strength", value=7.5, step=0.1, minimum=0, maximum=15)
168
- likeness_strength = gr.Slider(label="Likeness Strength", value=1.0, step=0.1, minimum=0, maximum=5)
169
- with gr.Accordion("Advanced Options", open=False):
 
 
 
 
 
170
  num_inference_steps = gr.Slider(label="Number of Inference Steps", value=30, step=1, minimum=10, maximum=100)
171
  guidance_scale = gr.Slider(label="Guidance Scale", value=7.5, step=0.1, minimum=1, maximum=20)
172
  width = gr.Slider(label="Width", value=512, step=64, minimum=256, maximum=1024)
173
  height = gr.Slider(label="Height", value=512, step=64, minimum=256, maximum=1024)
174
- submit = gr.Button("Submit", variant="primary")
175
  with gr.Column():
176
  gallery = gr.Gallery(label="Generated Images")
177
-
 
 
178
  files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
179
  remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
180
- submit.click(fn=generate_image, inputs=[files, model_dropdown, style_dropdown, prompt, negative_prompt, face_strength, likeness_strength, num_inference_steps, guidance_scale, width, height], outputs=gallery)
181
-
 
 
 
 
182
  demo.launch()
 
1
+ rt 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_paths = {
14
+ "RealisticVisionV4": "SG161222/Realistic_Vision_V4.0_noVAE",
15
+ "RealisticVisionV6": "SG161222/Realistic_Vision_V6.0_B1_noVAE",
16
+ "Deliberate": "Yntec/Deliberate",
17
+ "DeliberateV2": "Yntec/Deliberate2",
18
+ "Dreamshaper8": "Lykon/dreamshaper-8",
19
+ "EpicRealism": "emilianJR/epiCRealism"
20
  }
21
 
22
+
23
+ vae_model_path = "stabilityai/sd-vae-ft-mse"
24
+ image_encoder_path = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K"
25
+ ip_ckpt = hf_hub_download(repo_id="h94/IP-Adapter-FaceID", filename="ip-adapter-faceid_sd15.bin", repo_type="model")
26
+ ip_plus_ckpt = hf_hub_download(repo_id="h94/IP-Adapter-FaceID", filename="ip-adapter-faceid-plusv2_sd15.bin", repo_type="model")
27
+
28
+ safety_model_id = "CompVis/stable-diffusion-safety-checker"
29
+ safety_feature_extractor = AutoFeatureExtractor.from_pretrained(safety_model_id)
30
+ safety_checker = StableDiffusionSafetyChecker.from_pretrained(safety_model_id)
31
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  device = "cuda"
33
 
 
34
  noise_scheduler = DDIMScheduler(
35
  num_train_timesteps=1000,
36
  beta_start=0.00085,
 
40
  set_alpha_to_one=False,
41
  steps_offset=1,
42
  )
43
+ vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16)
44
 
45
+ def load_model(base_model_path):
46
+ pipe = StableDiffusionPipeline.from_pretrained(
47
+ base_model_path,
 
48
  torch_dtype=torch.float16,
49
  scheduler=noise_scheduler,
50
+ vae=vae,
51
+ feature_extractor=safety_feature_extractor,
52
+ safety_checker=None # <--- Disable safety checker
53
+ ).to(device)
54
+ return pipe
55
 
56
+ ip_model = None
57
+ ip_model_plus = None
 
 
58
 
59
+ app = FaceAnalysis(name="buffalo_l", providers=['CPUExecutionProvider'])
60
+ app.prepare(ctx_id=0, det_size=(640, 640))
 
61
 
62
+ cv2.setNumThreads(1)
 
 
 
 
63
 
64
+ @spaces.GPU(enable_queue=True)
65
+ def generate_image(images, prompt, negative_prompt, preserve_face_structure, face_strength, likeness_strength, nfaa_negative_prompt, base_model, num_inference_steps, guidance_scale, width, height, progress=gr.Progress(track_tqdm=True)):
66
+ global ip_model, ip_model_plus
67
+ base_model_path = base_model_paths[base_model]
68
+ pipe = load_model(base_model_path)
69
+ ip_model = IPAdapterFaceID(pipe, ip_ckpt, device)
70
+ ip_model_plus = IPAdapterFaceIDPlus(pipe, image_encoder_path, ip_plus_ckpt, device)
71
 
72
  faceid_all_embeds = []
73
+ first_iteration = True
74
  for image in images:
75
  face = cv2.imread(image)
76
  faces = app.get(face)
77
  faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
78
  faceid_all_embeds.append(faceid_embed)
79
+ if(first_iteration and preserve_face_structure):
80
+ face_image = face_align.norm_crop(face, landmark=faces[0].kps, image_size=224) # you can also segment the face
81
+ first_iteration = False
82
+
83
  average_embedding = torch.mean(torch.stack(faceid_all_embeds, dim=0), dim=0)
84
 
85
+ total_negative_prompt = f"{negative_prompt} {nfaa_negative_prompt}"
 
86
 
87
+ if(not preserve_face_structure):
88
+ print("Generating normal")
89
+ image = ip_model.generate(
90
+ prompt=prompt, negative_prompt=total_negative_prompt, faceid_embeds=average_embedding,
91
+ scale=likeness_strength, width=width, height=height, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale
92
+ )
93
+ else:
94
+ print("Generating plus")
95
+ image = ip_model_plus.generate(
96
+ prompt=prompt, negative_prompt=total_negative_prompt, faceid_embeds=average_embedding,
97
+ scale=likeness_strength, face_image=face_image, shortcut=True, s_scale=face_strength, width=width, height=height, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale
98
+ )
99
+ print(image)
100
  return image
101
 
102
+ def change_style(style):
103
+ if style == "Photorealistic":
104
+ return(gr.update(value=True), gr.update(value=1.3), gr.update(value=1.0))
105
+ else:
106
+ return(gr.update(value=True), gr.update(value=0.1), gr.update(value=0.8))
107
+
108
  def swap_to_gallery(images):
109
  return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
110
 
 
113
 
114
  css = '''
115
  h1{margin-bottom: 0 !important}
116
+ footer{display:none !important}
117
  '''
118
 
119
  with gr.Blocks(css=css) as demo:
120
+ gr.Markdown("")
121
+ gr.Markdown("")
122
  with gr.Row():
123
  with gr.Column():
124
+ files = gr.Files(
125
+ label="Drag 1 or more photos of your face",
126
+ file_types=["image"]
127
+ )
128
+ uploaded_files = gr.Gallery(label="Your images", visible=False, columns=5, rows=1, height=125)
129
  with gr.Column(visible=False) as clear_button:
130
+ remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
131
+ prompt = gr.Textbox(label="Prompt",
132
+ info="Try something like 'a photo of a man/woman/person'",
133
+ placeholder="A photo of a [man/woman/person]...")
134
  negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="low quality")
135
+ style = gr.Radio(label="Generation type", info="For stylized try prompts like 'a watercolor painting of a woman'", choices=["Photorealistic", "Stylized"], value="Photorealistic")
136
+ base_model = gr.Dropdown(label="Base Model", choices=list(base_model_paths.keys()), value="Realistic_Vision_V4.0_noVAE")
137
+ submit = gr.Button("Submit")
138
+ with gr.Accordion(open=False, label="Advanced Options"):
139
+ 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)
140
+ 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)
141
+ likeness_strength = gr.Slider(label="Face Embed strength", value=1.0, step=0.1, minimum=0, maximum=5)
142
+ 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")
143
  num_inference_steps = gr.Slider(label="Number of Inference Steps", value=30, step=1, minimum=10, maximum=100)
144
  guidance_scale = gr.Slider(label="Guidance Scale", value=7.5, step=0.1, minimum=1, maximum=20)
145
  width = gr.Slider(label="Width", value=512, step=64, minimum=256, maximum=1024)
146
  height = gr.Slider(label="Height", value=512, step=64, minimum=256, maximum=1024)
 
147
  with gr.Column():
148
  gallery = gr.Gallery(label="Generated Images")
149
+ style.change(fn=change_style,
150
+ inputs=style,
151
+ outputs=[preserve, face_strength, likeness_strength])
152
  files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
153
  remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
154
+ submit.click(fn=generate_image,
155
+ inputs=[files,prompt,negative_prompt,preserve, face_strength, likeness_strength, nfaa_negative_prompts, base_model, num_inference_steps, guidance_scale, width, height],
156
+ outputs=gallery)
157
+
158
+ gr.Markdown("")
159
+
160
  demo.launch()