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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
weight_name=IP_STYLE_WEIGHT,
|
| 109 |
adapter_name="style",
|
| 110 |
)
|
| 111 |
+
load_logs.append("✅ IP-Adapter Style (SDXL) chargé (adapter_name='style').")
|
| 112 |
+
HAS_STYLE_ADAPTER = True
|
| 113 |
+
except Exception as e:
|
| 114 |
+
load_logs.append(f"ℹ️ IP-Adapter Style non chargé: {e}")
|
| 115 |
+
|
| 116 |
+
if DEVICE == "cuda":
|
| 117 |
+
if hasattr(pipe, "image_proj_model"): pipe.image_proj_model.to("cuda")
|
| 118 |
+
if hasattr(pipe, "unet"): pipe.unet.to("cuda")
|
| 119 |
+
|
| 120 |
+
load_logs.append("✅ InstantID prêt.")
|
| 121 |
+
except Exception:
|
| 122 |
+
load_logs += ["❌ ERREUR au chargement:", traceback.format_exc()]
|
| 123 |
+
pipe = None
|
| 124 |
+
|
| 125 |
+
if pipe is None:
|
| 126 |
+
raise RuntimeError("Échec de chargement du pipeline.\n" + "\n".join(load_logs))
|
| 127 |
+
|
| 128 |
+
def load_face_analyser():
|
| 129 |
+
errors = []
|
| 130 |
+
for name in ("antelopev2", "buffalo_l"):
|
| 131 |
+
try:
|
| 132 |
+
fa = FaceAnalysis(name=name, root="./models", providers=["CPUExecutionProvider"])
|
| 133 |
+
fa.prepare(ctx_id=0, det_size=(640, 640))
|
| 134 |
+
print(f"✅ InsightFace chargé: {name}")
|
| 135 |
+
return fa
|
| 136 |
+
except Exception as e:
|
| 137 |
+
errors.append(f"{name}: {e}")
|
| 138 |
+
print(f"⚠️ InsightFace échec {name} → {e}")
|
| 139 |
+
raise RuntimeError("Echec chargement InsightFace. Détails: " + " | ".join(errors))
|
| 140 |
+
|
| 141 |
+
fa = load_face_analyser()
|
| 142 |
+
|
| 143 |
+
def extract_face_embed_and_kps(pil_img):
|
| 144 |
+
import numpy as np, cv2
|
| 145 |
+
img_cv2 = cv2.cvtColor(np.array(pil_img.convert("RGB")), cv2.COLOR_RGB2BGR)
|
| 146 |
+
faces = fa.get(img_cv2)
|
| 147 |
+
if not faces:
|
| 148 |
+
raise ValueError("Aucun visage détecté dans la photo.")
|
| 149 |
+
face = faces[-1]
|
| 150 |
+
emb_np = face["embedding"]
|
| 151 |
+
if not isinstance(emb_np, np.ndarray):
|
| 152 |
+
emb_np = np.asarray(emb_np, dtype="float32")
|
| 153 |
+
if emb_np.ndim == 1:
|
| 154 |
+
emb_np = emb_np[None, ...] # (1, D)
|
| 155 |
+
face_emb = torch.from_numpy(emb_np).to(device=DEVICE, dtype=DTYPE) # ← Tensor [1,D] sur bon device/dtype
|
| 156 |
+
kps_img = draw_kps_local(pil_img, face["kps"])
|
| 157 |
+
return face_emb, kps_img
|
| 158 |
+
|
| 159 |
+
def generate(face_image, style_image, prompt, negative_prompt,
|
| 160 |
+
identity_strength, adapter_strength, style_strength,
|
| 161 |
+
steps, cfg, width, height, seed):
|
| 162 |
+
try:
|
| 163 |
+
if face_image is None:
|
| 164 |
+
return None, "Merci d'ajouter une photo visage.", "\n".join(load_logs)
|
| 165 |
+
|
| 166 |
+
gen = None if seed is None or int(seed) < 0 else torch.Generator(device=DEVICE).manual_seed(int(seed))
|
| 167 |
+
|
| 168 |
+
face = ImageOps.exif_transpose(face_image).convert("RGB")
|
| 169 |
+
ms = min(face.size); x = (face.width - ms) // 2; y = (face.height - ms) // 2
|
| 170 |
+
face_sq = face.crop((x, y, x + ms, y + ms)).resize((512, 512), Image.Resampling.LANCZOS)
|
| 171 |
+
|
| 172 |
+
face_emb, kps_img = extract_face_embed_and_kps(face_sq)
|
| 173 |
+
|
| 174 |
+
try:
|
| 175 |
+
if HAS_STYLE_ADAPTER and style_image is not None:
|
| 176 |
+
pipe.set_ip_adapter_scale({"instantid": float(adapter_strength), "style": float(style_strength)})
|
| 177 |
+
else:
|
| 178 |
+
pipe.set_ip_adapter_scale(float(adapter_strength))
|
| 179 |
+
except Exception as e:
|
| 180 |
+
print(f"ℹ️ set_ip_adapter_scale ignoré: {e}")
|
| 181 |
+
|
| 182 |
+
cn = getattr(pipe, "controlnet", None)
|
| 183 |
+
if isinstance(cn, (list, tuple)):
|
| 184 |
+
n_cn = len(cn)
|
| 185 |
+
else:
|
| 186 |
+
try: n_cn = len(cn)
|
| 187 |
+
except Exception: n_cn = 1
|
| 188 |
+
|
| 189 |
+
image_arg = [kps_img] * n_cn if n_cn > 1 else ([kps_img] if isinstance(cn, (list, tuple)) else kps_img)
|
| 190 |
+
scale_val = float(identity_strength)
|
| 191 |
+
scale_arg = [scale_val] * n_cn if n_cn > 1 else ([scale_val] if isinstance(cn, (list, tuple)) else scale_val)
|
| 192 |
+
|
| 193 |
+
# ✅ Construction des kwargs d’inférence — compat 0.29/0.30
|
| 194 |
+
gen_kwargs = dict(
|
| 195 |
+
prompt=(prompt or "").strip(),
|
| 196 |
+
negative_prompt=(negative_prompt or "").strip(),
|
| 197 |
+
image=image_arg, # IdentityNet (landmarks)
|
| 198 |
+
image_embeds=face_emb, # compat pipeline
|
| 199 |
+
# --- CLÉ : passer aux deux noms pour couvrir 0.29 (added_cond_kwargs) et 0.30 (added_conditions)
|
| 200 |
+
added_conditions={"image_embeds": face_emb}, # ← diffusers >= 0.30.0
|
| 201 |
+
added_cond_kwargs={"image_embeds": face_emb}, # ← diffusers 0.29.x (safe no-op si ignoré)
|
| 202 |
+
controlnet_conditioning_scale=scale_arg,
|
| 203 |
+
num_inference_steps=int(steps),
|
| 204 |
+
guidance_scale=float(cfg),
|
| 205 |
+
width=int(width),
|
| 206 |
+
height=int(height),
|
| 207 |
+
generator=gen,
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
if HAS_STYLE_ADAPTER and style_image is not None:
|
| 212 |
+
try:
|
| 213 |
+
gen_kwargs["ip_adapter_image"] = ImageOps.exif_transpose(style_image).convert("RGB")
|
| 214 |
+
except Exception as e:
|
| 215 |
+
print(f"ℹ️ ip_adapter_image ignoré: {e}")
|
| 216 |
+
|
| 217 |
+
images = pipe(**gen_kwargs).images
|
| 218 |
+
return images[0], "", "\n".join(load_logs)
|
| 219 |
+
|
| 220 |
+
except torch.cuda.OutOfMemoryError:
|
| 221 |
+
return None, "CUDA OOM: baisse la résolution ou les steps.", "\n".join(load_logs)
|
| 222 |
+
except Exception:
|
| 223 |
+
return None, "Erreur:\n" + traceback.format_exc(), "\n".join(load_logs)
|
| 224 |
+
|
| 225 |
+
EX_PROMPT = (
|
| 226 |
+
"one piece style, Eiichiro Oda style, anime portrait, upper body, pirate outfit, "
|
| 227 |
+
"clean lineart, cel shading, vibrant colors, expressive eyes, dynamic composition, simple background"
|
| 228 |
+
)
|
| 229 |
+
EX_NEG = (
|
| 230 |
+
"realistic, photo, photorealistic, skin pores, complex lighting, "
|
| 231 |
+
"low quality, worst quality, lowres, blurry, noisy, watermark, text, logo, jpeg artifacts, "
|
| 232 |
+
"bad anatomy, deformed, multiple faces, nsfw"
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
with gr.Blocks(css="footer{display:none !important}") as demo:
|
| 236 |
+
gr.Markdown("# 🏴☠️ InstantID SDXL + IP-Adapter Style (2D) — visage → perso One Piece")
|
| 237 |
+
with gr.Row():
|
| 238 |
+
with gr.Column():
|
| 239 |
+
face_image = gr.Image(type="pil", label="Photo visage (obligatoire)", height=260)
|
| 240 |
+
style_image = gr.Image(type="pil", label="Image de style (optionnel)", height=260)
|
| 241 |
+
gr.Markdown("Astuce : poster/planche One Piece → rendu 2D renforcé via IP-Adapter Style.")
|
| 242 |
+
prompt = gr.Textbox(label="Prompt", value=EX_PROMPT, lines=3)
|
| 243 |
+
negative = gr.Textbox(label="Negative Prompt", value=EX_NEG, lines=3)
|
| 244 |
+
with gr.Row():
|
| 245 |
+
identity_strength = gr.Slider(0.2, 1.5, 0.95, 0.05, label="Fidélité visage (IdentityNet)")
|
| 246 |
+
adapter_strength = gr.Slider(0.1, 1.5, 0.85, 0.05, label="Détails anime (InstantID)")
|
| 247 |
+
style_strength = gr.Slider(0.1, 1.5, 0.95, 0.05, label="Force style (IP-Adapter Style)")
|
| 248 |
+
steps = gr.Slider(10, 60, 30, 1, label="Steps")
|
| 249 |
+
cfg = gr.Slider(0.1, 12.0, 6.5, 0.1, label="CFG")
|
| 250 |
+
width = gr.Dropdown(choices=[576, 640, 704, 768, 896], value=704, label="Largeur")
|
| 251 |
+
height = gr.Dropdown(choices=[704, 768, 896, 1024], value=896, label="Hauteur")
|
| 252 |
+
seed = gr.Number(value=-1, label="Seed (-1 aléatoire)")
|
| 253 |
+
btn = gr.Button("🎨 Générer", variant="primary")
|
| 254 |
+
with gr.Column():
|
| 255 |
+
out_image = gr.Image(label="Résultat", interactive=False)
|
| 256 |
+
err_box = gr.Textbox(label="Erreurs", visible=False)
|
| 257 |
+
log_box = gr.Textbox(label="Logs", value="\n".join(load_logs), lines=12)
|
| 258 |
+
|
| 259 |
+
def wrap(*args):
|
| 260 |
+
img, err, logs = generate(*args)
|
| 261 |
+
return img, gr.update(visible=bool(err), value=err), gr.update(value=logs)
|
| 262 |
+
|
| 263 |
+
btn.click(
|
| 264 |
+
wrap,
|
| 265 |
+
inputs=[face_image, style_image, prompt, negative,
|
| 266 |
+
identity_strength, adapter_strength, style_strength,
|
| 267 |
+
steps, cfg, width, height, seed],
|
| 268 |
+
outputs=[out_image, err_box, log_box],
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
demo.queue(api_open=False)
|
| 272 |
+
if __name__ == "__main__":
|
| 273 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
|