Spaces:
Runtime error
Runtime error
Update generator.py
Browse files- generator.py +44 -9
generator.py
CHANGED
|
@@ -458,10 +458,18 @@ class RetroArtConverter:
|
|
| 458 |
# Configure ControlNets + IP-Adapter (SIMPLIFIED!)
|
| 459 |
# First, check if we need to disable IP-Adapter
|
| 460 |
if not (has_detected_faces and face_embeddings is not None):
|
| 461 |
-
# No faces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
try:
|
| 463 |
self.pipe.set_ip_adapter_scale(0.0)
|
| 464 |
-
print("[IP-ADAPTER] Disabled (scale set to 0)
|
| 465 |
except Exception as e:
|
| 466 |
print(f"[IP-ADAPTER] Could not disable: {e}")
|
| 467 |
|
|
@@ -485,8 +493,6 @@ class RetroArtConverter:
|
|
| 485 |
]
|
| 486 |
|
| 487 |
# Control guidance timing (when each ControlNet is active)
|
| 488 |
-
# [start, start] - both active from beginning
|
| 489 |
-
# [end, end] - both active until end
|
| 490 |
pipe_kwargs["control_guidance_start"] = [0.0, 0.0]
|
| 491 |
pipe_kwargs["control_guidance_end"] = [1.0, 1.0]
|
| 492 |
|
|
@@ -503,6 +509,10 @@ class RetroArtConverter:
|
|
| 503 |
# Have keypoints but no embeddings (shouldn't happen but handle it)
|
| 504 |
print("Using keypoints only + Depth ControlNets (no embeddings)")
|
| 505 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
# Control images: [face keypoints, depth map]
|
| 507 |
pipe_kwargs["control_image"] = [face_kps_image, depth_image]
|
| 508 |
|
|
@@ -516,8 +526,10 @@ class RetroArtConverter:
|
|
| 516 |
pipe_kwargs["control_guidance_start"] = [0.0, 0.0]
|
| 517 |
pipe_kwargs["control_guidance_end"] = [1.0, 1.0]
|
| 518 |
|
| 519 |
-
#
|
| 520 |
-
|
|
|
|
|
|
|
| 521 |
|
| 522 |
else:
|
| 523 |
print("No faces detected - using Depth ControlNet only")
|
|
@@ -530,15 +542,38 @@ class RetroArtConverter:
|
|
| 530 |
pipe_kwargs["control_guidance_start"] = [0.0, 0.0]
|
| 531 |
pipe_kwargs["control_guidance_end"] = [1.0, 1.0]
|
| 532 |
|
| 533 |
-
# IMPORTANT:
|
| 534 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
|
| 536 |
# Generate
|
| 537 |
print(f"Generating: Steps={num_inference_steps}, CFG={guidance_scale}, Strength={strength}")
|
| 538 |
|
| 539 |
# Debug: Print what IP-Adapter params we're passing
|
| 540 |
if "image_embeds" in pipe_kwargs:
|
| 541 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
else:
|
| 543 |
print("[DEBUG] No image_embeds in pipeline kwargs")
|
| 544 |
|
|
|
|
| 458 |
# Configure ControlNets + IP-Adapter (SIMPLIFIED!)
|
| 459 |
# First, check if we need to disable IP-Adapter
|
| 460 |
if not (has_detected_faces and face_embeddings is not None):
|
| 461 |
+
# No faces - create dummy embeddings to prevent pipeline error
|
| 462 |
+
# The pipeline expects embeddings even when IP-Adapter scale is 0
|
| 463 |
+
print("[IP-ADAPTER] No faces - using dummy embeddings with scale=0")
|
| 464 |
+
|
| 465 |
+
# Create zero embeddings of the expected shape (512D for InsightFace)
|
| 466 |
+
import numpy as np
|
| 467 |
+
face_embeddings = np.zeros(512, dtype=np.float32)
|
| 468 |
+
|
| 469 |
+
# Disable IP-Adapter
|
| 470 |
try:
|
| 471 |
self.pipe.set_ip_adapter_scale(0.0)
|
| 472 |
+
print("[IP-ADAPTER] Disabled (scale set to 0)")
|
| 473 |
except Exception as e:
|
| 474 |
print(f"[IP-ADAPTER] Could not disable: {e}")
|
| 475 |
|
|
|
|
| 493 |
]
|
| 494 |
|
| 495 |
# Control guidance timing (when each ControlNet is active)
|
|
|
|
|
|
|
| 496 |
pipe_kwargs["control_guidance_start"] = [0.0, 0.0]
|
| 497 |
pipe_kwargs["control_guidance_end"] = [1.0, 1.0]
|
| 498 |
|
|
|
|
| 509 |
# Have keypoints but no embeddings (shouldn't happen but handle it)
|
| 510 |
print("Using keypoints only + Depth ControlNets (no embeddings)")
|
| 511 |
|
| 512 |
+
# Create dummy embeddings
|
| 513 |
+
import numpy as np
|
| 514 |
+
face_embeddings = np.zeros(512, dtype=np.float32)
|
| 515 |
+
|
| 516 |
# Control images: [face keypoints, depth map]
|
| 517 |
pipe_kwargs["control_image"] = [face_kps_image, depth_image]
|
| 518 |
|
|
|
|
| 526 |
pipe_kwargs["control_guidance_start"] = [0.0, 0.0]
|
| 527 |
pipe_kwargs["control_guidance_end"] = [1.0, 1.0]
|
| 528 |
|
| 529 |
+
# Pass dummy embeddings with scale=0
|
| 530 |
+
pipe_kwargs["image_embeds"] = face_embeddings
|
| 531 |
+
|
| 532 |
+
print(" [WARNING] Using dummy embeddings - keypoints only")
|
| 533 |
|
| 534 |
else:
|
| 535 |
print("No faces detected - using Depth ControlNet only")
|
|
|
|
| 542 |
pipe_kwargs["control_guidance_start"] = [0.0, 0.0]
|
| 543 |
pipe_kwargs["control_guidance_end"] = [1.0, 1.0]
|
| 544 |
|
| 545 |
+
# IMPORTANT: The InstantID pipeline REQUIRES image_embeds
|
| 546 |
+
# Pass dummy embeddings when no faces are detected
|
| 547 |
+
# Create zero embeddings of the expected shape
|
| 548 |
+
import numpy as np
|
| 549 |
+
dummy_embeddings = np.zeros(512, dtype=np.float32)
|
| 550 |
+
|
| 551 |
+
# Ensure it's a numpy array with the right shape
|
| 552 |
+
if len(dummy_embeddings.shape) == 1 and dummy_embeddings.shape[0] == 512:
|
| 553 |
+
pipe_kwargs["image_embeds"] = dummy_embeddings
|
| 554 |
+
print(f" [INFO] Using dummy embeddings (shape: {dummy_embeddings.shape})")
|
| 555 |
+
else:
|
| 556 |
+
# Fallback to correct shape if something went wrong
|
| 557 |
+
pipe_kwargs["image_embeds"] = np.zeros(512, dtype=np.float32)
|
| 558 |
+
print(" [INFO] Using dummy embeddings (IP-Adapter disabled)")
|
| 559 |
+
|
| 560 |
+
# Make sure IP-Adapter is disabled
|
| 561 |
+
try:
|
| 562 |
+
self.pipe.set_ip_adapter_scale(0.0)
|
| 563 |
+
except:
|
| 564 |
+
pass
|
| 565 |
|
| 566 |
# Generate
|
| 567 |
print(f"Generating: Steps={num_inference_steps}, CFG={guidance_scale}, Strength={strength}")
|
| 568 |
|
| 569 |
# Debug: Print what IP-Adapter params we're passing
|
| 570 |
if "image_embeds" in pipe_kwargs:
|
| 571 |
+
embeds = pipe_kwargs['image_embeds']
|
| 572 |
+
if isinstance(embeds, np.ndarray):
|
| 573 |
+
is_dummy = np.allclose(embeds, 0)
|
| 574 |
+
print(f"[DEBUG] Passing image_embeds: shape={embeds.shape}, dummy={is_dummy}")
|
| 575 |
+
else:
|
| 576 |
+
print(f"[DEBUG] Passing image_embeds: type={type(embeds)}")
|
| 577 |
else:
|
| 578 |
print("[DEBUG] No image_embeds in pipeline kwargs")
|
| 579 |
|