Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,43 @@ import os
|
|
12 |
import uuid
|
13 |
from datetime import datetime
|
14 |
|
15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
STYLE_PRESETS = [
|
18 |
{
|
@@ -127,7 +163,46 @@ css = '''
|
|
127 |
footer {display: none !important}
|
128 |
'''
|
129 |
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
with gr.Blocks(css=css) as demo:
|
133 |
with gr.Column(elem_classes="container"):
|
@@ -149,6 +224,12 @@ with gr.Blocks(css=css) as demo:
|
|
149 |
type="value"
|
150 |
)
|
151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
with gr.Column(elem_classes="preset-container"):
|
153 |
gr.Markdown("### 🎭 Magic Transformations")
|
154 |
preset_grid = []
|
@@ -158,16 +239,11 @@ with gr.Blocks(css=css) as demo:
|
|
158 |
elem_classes="preset-card"
|
159 |
)
|
160 |
preset_button.click(
|
161 |
-
|
162 |
-
|
|
|
163 |
)
|
164 |
preset_grid.append(preset_button)
|
165 |
-
|
166 |
-
prompt_input = gr.Textbox(
|
167 |
-
label="🎨 Custom Prompt",
|
168 |
-
placeholder="Describe your desired transformation in detail...",
|
169 |
-
lines=3
|
170 |
-
)
|
171 |
|
172 |
generate_button = gr.Button("🚀 Generate Magic", variant="primary")
|
173 |
|
|
|
12 |
import uuid
|
13 |
from datetime import datetime
|
14 |
|
15 |
+
# Model paths
|
16 |
+
base_model_path = "SG161222/Realistic_Vision_V4.0_noVAE"
|
17 |
+
vae_model_path = "stabilityai/sd-vae-ft-mse"
|
18 |
+
image_encoder_path = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K"
|
19 |
+
ip_ckpt = hf_hub_download(repo_id="h94/IP-Adapter-FaceID", filename="ip-adapter-faceid_sd15.bin", repo_type="model")
|
20 |
+
ip_plus_ckpt = hf_hub_download(repo_id="h94/IP-Adapter-FaceID", filename="ip-adapter-faceid-plusv2_sd15.bin", repo_type="model")
|
21 |
+
|
22 |
+
device = "cuda"
|
23 |
+
|
24 |
+
# Initialize the noise scheduler
|
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 |
+
|
35 |
+
# Load models
|
36 |
+
vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16)
|
37 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
38 |
+
base_model_path,
|
39 |
+
torch_dtype=torch.float16,
|
40 |
+
scheduler=noise_scheduler,
|
41 |
+
vae=vae
|
42 |
+
).to(device)
|
43 |
+
|
44 |
+
ip_model = IPAdapterFaceID(pipe, ip_ckpt, device)
|
45 |
+
ip_model_plus = IPAdapterFaceIDPlus(pipe, image_encoder_path, ip_plus_ckpt, device)
|
46 |
+
|
47 |
+
# Initialize FaceAnalysis
|
48 |
+
app = FaceAnalysis(name="buffalo_l", providers=['CPUExecutionProvider'])
|
49 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
50 |
+
|
51 |
+
cv2.setNumThreads(1)
|
52 |
|
53 |
STYLE_PRESETS = [
|
54 |
{
|
|
|
163 |
footer {display: none !important}
|
164 |
'''
|
165 |
|
166 |
+
@spaces.GPU(enable_queue=True)
|
167 |
+
def generate_image(images, gender, prompt, progress=gr.Progress(track_tqdm=True)):
|
168 |
+
if not prompt:
|
169 |
+
prompt = f"Professional portrait of a {gender.lower()}"
|
170 |
+
|
171 |
+
faceid_all_embeds = []
|
172 |
+
first_iteration = True
|
173 |
+
preserve_face_structure = True
|
174 |
+
face_strength = 2.1
|
175 |
+
likeness_strength = 0.7
|
176 |
+
|
177 |
+
for image in images:
|
178 |
+
face = cv2.imread(image)
|
179 |
+
faces = app.get(face)
|
180 |
+
faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
|
181 |
+
faceid_all_embeds.append(faceid_embed)
|
182 |
+
|
183 |
+
if first_iteration and preserve_face_structure:
|
184 |
+
face_image = face_align.norm_crop(face, landmark=faces[0].kps, image_size=224)
|
185 |
+
first_iteration = False
|
186 |
+
|
187 |
+
average_embedding = torch.mean(torch.stack(faceid_all_embeds, dim=0), dim=0)
|
188 |
+
|
189 |
+
image = ip_model_plus.generate(
|
190 |
+
prompt=prompt,
|
191 |
+
faceid_embeds=average_embedding,
|
192 |
+
scale=likeness_strength,
|
193 |
+
face_image=face_image,
|
194 |
+
shortcut=True,
|
195 |
+
s_scale=face_strength,
|
196 |
+
width=512,
|
197 |
+
height=912,
|
198 |
+
num_inference_steps=100
|
199 |
+
)
|
200 |
+
return image
|
201 |
+
|
202 |
+
def create_preset_click_handler(idx, prompt_input):
|
203 |
+
def handler():
|
204 |
+
return {"value": STYLE_PRESETS[idx]["prompt"]}
|
205 |
+
return handler
|
206 |
|
207 |
with gr.Blocks(css=css) as demo:
|
208 |
with gr.Column(elem_classes="container"):
|
|
|
224 |
type="value"
|
225 |
)
|
226 |
|
227 |
+
prompt_input = gr.Textbox(
|
228 |
+
label="🎨 Custom Prompt",
|
229 |
+
placeholder="Describe your desired transformation in detail...",
|
230 |
+
lines=3
|
231 |
+
)
|
232 |
+
|
233 |
with gr.Column(elem_classes="preset-container"):
|
234 |
gr.Markdown("### 🎭 Magic Transformations")
|
235 |
preset_grid = []
|
|
|
239 |
elem_classes="preset-card"
|
240 |
)
|
241 |
preset_button.click(
|
242 |
+
fn=create_preset_click_handler(idx, prompt_input),
|
243 |
+
inputs=[],
|
244 |
+
outputs=[prompt_input]
|
245 |
)
|
246 |
preset_grid.append(preset_button)
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
|
248 |
generate_button = gr.Button("🚀 Generate Magic", variant="primary")
|
249 |
|