Nad54 commited on
Commit
fc9e0f9
·
verified ·
1 Parent(s): 45787a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -207
app.py CHANGED
@@ -1,15 +1,10 @@
1
  # app.py — InstantID SDXL (officiel) + IP-Adapter Style (optionnel, rendu 2D)
2
- # Hugging Face Space ready
3
 
4
- # 0) Environnement AVANT imports
5
  import os, sys
6
  os.environ["OMP_NUM_THREADS"] = "4"
7
  os.environ.setdefault("HF_HUB_ENABLE_HF_TRANSFER", "1")
8
-
9
- # rendre importable ./instantid (pipeline officielle à placer ici)
10
  sys.path.insert(0, os.path.abspath("./instantid"))
11
 
12
- # 1) Imports
13
  import traceback, importlib.util
14
  import torch, gradio as gr
15
  from PIL import Image, ImageOps, ImageDraw
@@ -20,7 +15,6 @@ from insightface.app import FaceAnalysis
20
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
21
  DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
22
 
23
- # 2) Chemins & Hub (poids InstantID officiels + IP-Adapter Style SDXL)
24
  ASSETS_REPO = "InstantX/InstantID"
25
  CHECKPOINTS_DIR = "./checkpoints"
26
  CN_LOCAL_DIR = os.path.join(CHECKPOINTS_DIR, "ControlNetModel")
@@ -30,15 +24,12 @@ IP_STYLE_REPO = "h94/IP-Adapter"
30
  IP_STYLE_SUBFOLDER = "sdxl_models"
31
  IP_STYLE_WEIGHT = "ip-adapter_sdxl.bin"
32
 
33
- # Modèle de base (remplaçable par un checkpoint anime)
34
  BASE_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
35
 
36
- # 3) Téléchargements sûrs
37
  def safe_download(repo, filename, local_dir, min_bytes, label, subfolder=None):
38
  os.makedirs(local_dir, exist_ok=True)
39
  local_path = os.path.join(local_dir, os.path.basename(filename))
40
  if os.path.exists(local_path) and os.path.getsize(local_path) < min_bytes:
41
- print(f"⚠️ {label} corrompu ({os.path.getsize(local_path)} bytes) → suppression")
42
  try: os.remove(local_path)
43
  except Exception: pass
44
  path = hf_hub_download(
@@ -51,22 +42,19 @@ def safe_download(repo, filename, local_dir, min_bytes, label, subfolder=None):
51
  subfolder=subfolder,
52
  )
53
  size = os.path.getsize(path)
54
- print(f"✅ {label} téléchargé ({size/1e6:.1f} MB)")
55
  if size < min_bytes:
56
  raise RuntimeError(f"Téléchargement incomplet de {label} (taille: {size} bytes).")
 
57
  return path
58
 
59
  def ensure_assets_or_download():
60
  os.makedirs(CHECKPOINTS_DIR, exist_ok=True)
61
  os.makedirs(CN_LOCAL_DIR, exist_ok=True)
62
- # IdentityNet (ControlNet) + ip-adapter (InstantID)
63
  safe_download(ASSETS_REPO, "ControlNetModel/config.json", CHECKPOINTS_DIR, 1_000, "IdentityNet config")
64
  safe_download(ASSETS_REPO, "ControlNetModel/diffusion_pytorch_model.safetensors", CHECKPOINTS_DIR, 100_000_000, "IdentityNet weights")
65
  safe_download(ASSETS_REPO, "ip-adapter.bin", CHECKPOINTS_DIR, 100_000_000, "IP-Adapter (InstantID)")
66
- # IP-Adapter Style (SDXL) — optionnel
67
  safe_download(IP_STYLE_REPO, IP_STYLE_WEIGHT, CHECKPOINTS_DIR, 20_000_000, "IP-Adapter Style (SDXL)", subfolder=IP_STYLE_SUBFOLDER)
68
 
69
- # 4) Import dynamique de la pipeline SDXL officielle
70
  def import_pipeline_or_fail():
71
  candidates = [
72
  "./instantid/pipeline_stable_diffusion_xl_instantid_full.py",
@@ -77,7 +65,6 @@ def import_pipeline_or_fail():
77
  raise RuntimeError("❌ Pipeline manquante. Place `pipeline_stable_diffusion_xl_instantid_full.py` dans ./instantid/")
78
  if os.path.getsize(pipeline_file) < 1024:
79
  raise RuntimeError("❌ Pipeline trop petite (vide ?). Utilise la version SDXL officielle.")
80
-
81
  spec = importlib.util.spec_from_file_location("instantid_pipeline", pipeline_file)
82
  mod = importlib.util.module_from_spec(spec)
83
  spec.loader.exec_module(mod)
@@ -88,7 +75,6 @@ def import_pipeline_or_fail():
88
  avail = [n for n, o in vars(mod).items() if isinstance(o, type)]
89
  raise RuntimeError("❌ Aucune classe pipeline InstantID trouvée. Classes dispo: " + ", ".join(avail))
90
 
91
- # 5) util — dessin landmarks (kps)
92
  def draw_kps_local(img_pil, kps):
93
  w, h = img_pil.size
94
  out = Image.new("RGB", (w, h), "white")
@@ -98,30 +84,23 @@ def draw_kps_local(img_pil, kps):
98
  d.ellipse((x - r, y - r, x + r, y + r), fill="black")
99
  return out
100
 
101
- # 6) Chargement pipeline
102
  load_logs = []
103
  HAS_STYLE_ADAPTER = False
104
-
105
  try:
106
- # a) pipeline
107
  SDXLInstantID = import_pipeline_or_fail()
108
  ensure_assets_or_download()
109
 
110
- # b) IdentityNet (ControlNet)
111
  controlnet_identitynet = ControlNetModel.from_pretrained(CN_LOCAL_DIR, torch_dtype=DTYPE)
112
-
113
  pipe = SDXLInstantID.from_pretrained(
114
  BASE_MODEL,
115
- controlnet=controlnet_identitynet, # objet unique
116
  torch_dtype=DTYPE,
117
  safety_checker=None,
118
  feature_extractor=None,
119
  ).to(DEVICE)
120
 
121
- # c) IP-Adapter InstantID (identité)
122
  pipe.load_ip_adapter_instantid(IP_ADAPTER_LOCAL)
123
 
124
- # d) IP-Adapter Style SDXL (optionnel), nommé "style"
125
  try:
126
  pipe.load_ip_adapter(
127
  IP_STYLE_REPO,
@@ -129,187 +108,4 @@ try:
129
  weight_name=IP_STYLE_WEIGHT,
130
  adapter_name="style",
131
  )
132
- load_logs.append("✅ IP-Adapter Style (SDXL) chargé (adapter_name='style').")
133
- HAS_STYLE_ADAPTER = True
134
- except Exception as e:
135
- load_logs.append(f"ℹ️ IP-Adapter Style non chargé: {e}")
136
-
137
- # e) devices
138
- if DEVICE == "cuda":
139
- if hasattr(pipe, "image_proj_model"): pipe.image_proj_model.to("cuda")
140
- if hasattr(pipe, "unet"): pipe.unet.to("cuda")
141
-
142
- load_logs.append("✅ InstantID prêt.")
143
- except Exception:
144
- load_logs += ["❌ ERREUR au chargement:", traceback.format_exc()]
145
- pipe = None
146
-
147
- if pipe is None:
148
- raise RuntimeError("Échec de chargement du pipeline.\n" + "\n".join(load_logs))
149
-
150
- # 7) InsightFace (antelopev2 → buffalo_l)
151
- def load_face_analyser():
152
- errors = []
153
- for name in ("antelopev2", "buffalo_l"):
154
- try:
155
- fa = FaceAnalysis(name=name, root="./models", providers=["CPUExecutionProvider"])
156
- fa.prepare(ctx_id=0, det_size=(640, 640))
157
- print(f"✅ InsightFace chargé: {name}")
158
- return fa
159
- except Exception as e:
160
- errors.append(f"{name}: {e}")
161
- print(f"⚠️ InsightFace échec {name} → {e}")
162
- raise RuntimeError("Echec chargement InsightFace. Détails: " + " | ".join(errors))
163
-
164
- fa = load_face_analyser()
165
-
166
- def extract_face_embed_and_kps(pil_img):
167
- """
168
- Retourne:
169
- - face_emb (torch.Tensor de forme [1, D] sur DEVICE/DTYPE) — requis pour UNet (encoder_hid_dim_type='ip_image_proj')
170
- - kps_img (PIL.Image) — landmarks pour IdentityNet
171
- """
172
- import numpy as np, cv2
173
- img_cv2 = cv2.cvtColor(np.array(pil_img.convert("RGB")), cv2.COLOR_RGB2BGR)
174
- faces = fa.get(img_cv2)
175
- if not faces:
176
- raise ValueError("Aucun visage détecté dans la photo.")
177
- face = faces[-1]
178
-
179
- # (1) Embedding InsightFace -> torch [1, D] sur bon device/dtype
180
- emb_np = face["embedding"]
181
- if not isinstance(emb_np, np.ndarray):
182
- emb_np = np.asarray(emb_np, dtype="float32")
183
- if emb_np.ndim == 1:
184
- emb_np = emb_np[None, ...] # (1, D)
185
- face_emb = torch.from_numpy(emb_np).to(device=DEVICE, dtype=DTYPE)
186
-
187
- # (2) Landmarks -> image kps
188
- kps_img = draw_kps_local(pil_img, face["kps"])
189
- return face_emb, kps_img
190
-
191
- # 8) Génération
192
- def generate(face_image, style_image, prompt, negative_prompt,
193
- identity_strength, adapter_strength, style_strength,
194
- steps, cfg, width, height, seed):
195
- try:
196
- if face_image is None:
197
- return None, "Merci d'ajouter une photo visage.", "\n".join(load_logs)
198
-
199
- gen = None if seed is None or int(seed) < 0 else torch.Generator(device=DEVICE).manual_seed(int(seed))
200
-
201
- # visage → carré 512 pour détection stable
202
- face = ImageOps.exif_transpose(face_image).convert("RGB")
203
- ms = min(face.size); x = (face.width - ms) // 2; y = (face.height - ms) // 2
204
- face_sq = face.crop((x, y, x + ms, y + ms)).resize((512, 512), Image.Resampling.LANCZOS)
205
-
206
- # InsightFace : embedding (torch [1,D]) + landmarks
207
- face_emb, kps_img = extract_face_embed_and_kps(face_sq)
208
-
209
- # IP-Adapter scales
210
- try:
211
- if HAS_STYLE_ADAPTER and style_image is not None:
212
- pipe.set_ip_adapter_scale({"instantid": float(adapter_strength), "style": float(style_strength)})
213
- else:
214
- pipe.set_ip_adapter_scale(float(adapter_strength))
215
- except Exception as e:
216
- print(f"ℹ️ set_ip_adapter_scale ignoré: {e}")
217
-
218
- # compat multi-ControlNet (même si on en a qu’un)
219
- cn = getattr(pipe, "controlnet", None)
220
- if isinstance(cn, (list, tuple)):
221
- n_cn = len(cn)
222
- else:
223
- try: n_cn = len(cn)
224
- except Exception: n_cn = 1
225
-
226
- image_arg = [kps_img] * n_cn if n_cn > 1 else ([kps_img] if isinstance(cn, (list, tuple)) else kps_img)
227
- scale_val = float(identity_strength)
228
- scale_arg = [scale_val] * n_cn if n_cn > 1 else ([scale_val] if isinstance(cn, (list, tuple)) else scale_val)
229
-
230
- # kwargs d’inférence — IMPORTANT: image_embeds est un torch Tensor [1,D]
231
- gen_kwargs = dict(
232
- prompt=(prompt or "").strip(),
233
- negative_prompt=(negative_prompt or "").strip(),
234
- image=image_arg, # IdentityNet (landmarks)
235
- image_embeds=face_emb, # ← torch.Tensor [1, D] sur DEVICE/DTYPE
236
- controlnet_conditioning_scale=scale_arg,
237
- num_inference_steps=int(steps),
238
- guidance_scale=float(cfg),
239
- width=int(width),
240
- height=int(height),
241
- generator=gen,
242
- )
243
-
244
- # passer l’image de style à l’IP-Adapter Style (si dispo + fournie)
245
- if HAS_STYLE_ADAPTER and style_image is not None:
246
- try:
247
- gen_kwargs["ip_adapter_image"] = ImageOps.exif_transpose(style_image).convert("RGB")
248
- except Exception as e:
249
- print(f"ℹ️ ip_adapter_image ignoré: {e}")
250
-
251
- images = pipe(**gen_kwargs).images
252
- return images[0], "", "\n".join(load_logs)
253
-
254
- except torch.cuda.OutOfMemoryError:
255
- return None, "CUDA OOM: baisse la résolution ou les steps.", "\n".join(load_logs)
256
- except Exception:
257
- return None, "Erreur:\n" + traceback.format_exc(), "\n".join(load_logs)
258
-
259
- # 9) UI
260
- EX_PROMPT = (
261
- "one piece style, Eiichiro Oda style, anime portrait, upper body, pirate outfit, "
262
- "clean lineart, cel shading, vibrant colors, expressive eyes, dynamic composition, simple background"
263
- )
264
- EX_NEG = (
265
- "realistic, photo, photorealistic, skin pores, complex lighting, "
266
- "low quality, worst quality, lowres, blurry, noisy, watermark, text, logo, jpeg artifacts, "
267
- "bad anatomy, deformed, multiple faces, nsfw"
268
- )
269
-
270
- with gr.Blocks(css="footer{display:none !important}") as demo:
271
- gr.Markdown("# 🏴‍☠️ InstantID SDXL + IP-Adapter Style (2D) — visage → perso One Piece")
272
-
273
- with gr.Row():
274
- with gr.Column():
275
- face_image = gr.Image(type="pil", label="Photo visage (obligatoire)", height=260)
276
- style_image = gr.Image(type="pil", label="Image de style (optionnel)", height=260)
277
-
278
- gr.Markdown("Astuce : charge un poster/planche One Piece (ou visuel manga) pour forcer le rendu 2D via IP-Adapter Style.")
279
-
280
- prompt = gr.Textbox(label="Prompt", value=EX_PROMPT, lines=3)
281
- negative = gr.Textbox(label="Negative Prompt", value=EX_NEG, lines=3)
282
-
283
- with gr.Row():
284
- identity_strength = gr.Slider(0.2, 1.5, 0.95, 0.05, label="Fidélité visage (IdentityNet)")
285
- adapter_strength = gr.Slider(0.1, 1.5, 0.85, 0.05, label="Détails anime (InstantID)")
286
-
287
- style_strength = gr.Slider(0.1, 1.5, 0.95, 0.05, label="Force style (IP-Adapter Style)")
288
-
289
- steps = gr.Slider(10, 60, 30, 1, label="Steps")
290
- cfg = gr.Slider(0.1, 12.0, 6.5, 0.1, label="CFG")
291
- width = gr.Dropdown(choices=[576, 640, 704, 768, 896], value=704, label="Largeur")
292
- height = gr.Dropdown(choices=[704, 768, 896, 1024], value=896, label="Hauteur")
293
- seed = gr.Number(value=-1, label="Seed (-1 aléatoire)")
294
- btn = gr.Button("🎨 Générer", variant="primary")
295
-
296
- with gr.Column():
297
- out_image = gr.Image(label="Résultat", interactive=False)
298
- err_box = gr.Textbox(label="Erreurs", visible=False)
299
- log_box = gr.Textbox(label="Logs", value="\n".join(load_logs), lines=12)
300
-
301
- def wrap(*args):
302
- img, err, logs = generate(*args)
303
- return img, gr.update(visible=bool(err), value=err), gr.update(value=logs)
304
-
305
- btn.click(
306
- wrap,
307
- inputs=[face_image, style_image, prompt, negative,
308
- identity_strength, adapter_strength, style_strength,
309
- steps, cfg, width, height, seed],
310
- outputs=[out_image, err_box, log_box],
311
- )
312
-
313
- demo.queue(api_open=False)
314
- if __name__ == "__main__":
315
- demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
 
1
  # app.py — InstantID SDXL (officiel) + IP-Adapter Style (optionnel, rendu 2D)
 
2
 
 
3
  import os, sys
4
  os.environ["OMP_NUM_THREADS"] = "4"
5
  os.environ.setdefault("HF_HUB_ENABLE_HF_TRANSFER", "1")
 
 
6
  sys.path.insert(0, os.path.abspath("./instantid"))
7
 
 
8
  import traceback, importlib.util
9
  import torch, gradio as gr
10
  from PIL import Image, ImageOps, ImageDraw
 
15
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
16
  DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
17
 
 
18
  ASSETS_REPO = "InstantX/InstantID"
19
  CHECKPOINTS_DIR = "./checkpoints"
20
  CN_LOCAL_DIR = os.path.join(CHECKPOINTS_DIR, "ControlNetModel")
 
24
  IP_STYLE_SUBFOLDER = "sdxl_models"
25
  IP_STYLE_WEIGHT = "ip-adapter_sdxl.bin"
26
 
 
27
  BASE_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
28
 
 
29
  def safe_download(repo, filename, local_dir, min_bytes, label, subfolder=None):
30
  os.makedirs(local_dir, exist_ok=True)
31
  local_path = os.path.join(local_dir, os.path.basename(filename))
32
  if os.path.exists(local_path) and os.path.getsize(local_path) < min_bytes:
 
33
  try: os.remove(local_path)
34
  except Exception: pass
35
  path = hf_hub_download(
 
42
  subfolder=subfolder,
43
  )
44
  size = os.path.getsize(path)
 
45
  if size < min_bytes:
46
  raise RuntimeError(f"Téléchargement incomplet de {label} (taille: {size} bytes).")
47
+ print(f"✅ {label} téléchargé ({size/1e6:.1f} MB)")
48
  return path
49
 
50
  def ensure_assets_or_download():
51
  os.makedirs(CHECKPOINTS_DIR, exist_ok=True)
52
  os.makedirs(CN_LOCAL_DIR, exist_ok=True)
 
53
  safe_download(ASSETS_REPO, "ControlNetModel/config.json", CHECKPOINTS_DIR, 1_000, "IdentityNet config")
54
  safe_download(ASSETS_REPO, "ControlNetModel/diffusion_pytorch_model.safetensors", CHECKPOINTS_DIR, 100_000_000, "IdentityNet weights")
55
  safe_download(ASSETS_REPO, "ip-adapter.bin", CHECKPOINTS_DIR, 100_000_000, "IP-Adapter (InstantID)")
 
56
  safe_download(IP_STYLE_REPO, IP_STYLE_WEIGHT, CHECKPOINTS_DIR, 20_000_000, "IP-Adapter Style (SDXL)", subfolder=IP_STYLE_SUBFOLDER)
57
 
 
58
  def import_pipeline_or_fail():
59
  candidates = [
60
  "./instantid/pipeline_stable_diffusion_xl_instantid_full.py",
 
65
  raise RuntimeError("❌ Pipeline manquante. Place `pipeline_stable_diffusion_xl_instantid_full.py` dans ./instantid/")
66
  if os.path.getsize(pipeline_file) < 1024:
67
  raise RuntimeError("❌ Pipeline trop petite (vide ?). Utilise la version SDXL officielle.")
 
68
  spec = importlib.util.spec_from_file_location("instantid_pipeline", pipeline_file)
69
  mod = importlib.util.module_from_spec(spec)
70
  spec.loader.exec_module(mod)
 
75
  avail = [n for n, o in vars(mod).items() if isinstance(o, type)]
76
  raise RuntimeError("❌ Aucune classe pipeline InstantID trouvée. Classes dispo: " + ", ".join(avail))
77
 
 
78
  def draw_kps_local(img_pil, kps):
79
  w, h = img_pil.size
80
  out = Image.new("RGB", (w, h), "white")
 
84
  d.ellipse((x - r, y - r, x + r, y + r), fill="black")
85
  return out
86
 
 
87
  load_logs = []
88
  HAS_STYLE_ADAPTER = False
 
89
  try:
 
90
  SDXLInstantID = import_pipeline_or_fail()
91
  ensure_assets_or_download()
92
 
 
93
  controlnet_identitynet = ControlNetModel.from_pretrained(CN_LOCAL_DIR, torch_dtype=DTYPE)
 
94
  pipe = SDXLInstantID.from_pretrained(
95
  BASE_MODEL,
96
+ controlnet=controlnet_identitynet,
97
  torch_dtype=DTYPE,
98
  safety_checker=None,
99
  feature_extractor=None,
100
  ).to(DEVICE)
101
 
 
102
  pipe.load_ip_adapter_instantid(IP_ADAPTER_LOCAL)
103
 
 
104
  try:
105
  pipe.load_ip_adapter(
106
  IP_STYLE_REPO,
 
108
  weight_name=IP_STYLE_WEIGHT,
109
  adapter_name="style",
110
  )
111
+ load_logs.append("✅ IP-Adapter Style (SDXL) chargé (adapter_na