Spaces:
Runtime error
Runtime error
Update generator.py
Browse files- generator.py +259 -601
generator.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
Generation logic for Pixagram AI Pixel Art Generator
|
|
|
|
| 3 |
"""
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
|
@@ -7,24 +8,29 @@ import cv2
|
|
| 7 |
from PIL import Image
|
| 8 |
import torch.nn.functional as F
|
| 9 |
from torchvision import transforms
|
|
|
|
|
|
|
| 10 |
|
| 11 |
from config import (
|
| 12 |
device, dtype, TRIGGER_WORD, MULTI_SCALE_FACTORS,
|
| 13 |
-
ADAPTIVE_THRESHOLDS, ADAPTIVE_PARAMS, CAPTION_CONFIG, IDENTITY_BOOST_MULTIPLIER
|
|
|
|
| 14 |
)
|
| 15 |
from utils import (
|
| 16 |
sanitize_text, enhanced_color_match, color_match, create_face_mask,
|
| 17 |
draw_kps, get_demographic_description, calculate_optimal_size, enhance_face_crop
|
| 18 |
)
|
| 19 |
from models import (
|
| 20 |
-
load_face_analysis, load_depth_detector, load_controlnets,
|
| 21 |
load_sdxl_pipeline, load_lora, setup_ip_adapter, setup_compel,
|
| 22 |
-
setup_scheduler, optimize_pipeline, load_caption_model, set_clip_skip
|
|
|
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
|
| 26 |
class RetroArtConverter:
|
| 27 |
-
"""Main class for retro art generation"""
|
| 28 |
|
| 29 |
def __init__(self):
|
| 30 |
self.device = device
|
|
@@ -32,54 +38,52 @@ class RetroArtConverter:
|
|
| 32 |
self.models_loaded = {
|
| 33 |
'custom_checkpoint': False,
|
| 34 |
'lora': False,
|
| 35 |
-
'
|
| 36 |
-
'
|
| 37 |
-
'
|
| 38 |
}
|
| 39 |
|
| 40 |
-
# Initialize face analysis
|
| 41 |
self.face_app, self.face_detection_enabled = load_face_analysis()
|
| 42 |
|
| 43 |
-
# Load
|
| 44 |
-
self.
|
| 45 |
-
self.models_loaded['
|
| 46 |
-
|
| 47 |
-
# Load
|
| 48 |
-
|
| 49 |
-
self.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
if
|
| 55 |
-
self.
|
| 56 |
-
|
| 57 |
-
self.
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
if self.instantid_enabled and self.controlnet_instantid is not None:
|
| 61 |
-
controlnets = [self.controlnet_instantid, controlnet_depth]
|
| 62 |
-
print(f"Initializing with multiple ControlNets: InstantID + Depth")
|
| 63 |
else:
|
|
|
|
| 64 |
controlnets = controlnet_depth
|
| 65 |
print(f"Initializing with single ControlNet: Depth only")
|
| 66 |
-
|
|
|
|
| 67 |
# Load SDXL pipeline
|
| 68 |
self.pipe, checkpoint_success = load_sdxl_pipeline(controlnets)
|
| 69 |
self.models_loaded['custom_checkpoint'] = checkpoint_success
|
| 70 |
|
| 71 |
-
# Load LORA
|
| 72 |
lora_success = load_lora(self.pipe)
|
| 73 |
self.models_loaded['lora'] = lora_success
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
self.image_proj_model = None
|
| 83 |
|
| 84 |
# Setup Compel
|
| 85 |
self.compel, self.use_compel = setup_compel(self.pipe)
|
|
@@ -93,490 +97,161 @@ class RetroArtConverter:
|
|
| 93 |
# Load caption model
|
| 94 |
self.caption_processor, self.caption_model, self.caption_enabled, self.caption_model_type = load_caption_model()
|
| 95 |
|
| 96 |
-
# Report caption model status
|
| 97 |
-
if self.caption_enabled and self.caption_model is not None:
|
| 98 |
-
if self.caption_model_type == "git":
|
| 99 |
-
print(" [OK] Using GIT for detailed captions")
|
| 100 |
-
elif self.caption_model_type == "blip":
|
| 101 |
-
print(" [OK] Using BLIP for standard captions")
|
| 102 |
-
else:
|
| 103 |
-
print(" [OK] Caption model loaded")
|
| 104 |
-
|
| 105 |
-
|
| 106 |
# Set CLIP skip
|
| 107 |
set_clip_skip(self.pipe)
|
| 108 |
|
| 109 |
-
# Track controlnet configuration
|
| 110 |
-
self.using_multiple_controlnets = isinstance(controlnets, list)
|
| 111 |
-
print(f"Pipeline initialized with {'multiple' if self.using_multiple_controlnets else 'single'} ControlNet(s)")
|
| 112 |
-
|
| 113 |
# Print model status
|
| 114 |
self._print_status()
|
| 115 |
|
| 116 |
-
print(" [OK] Model initialization complete!")
|
| 117 |
|
| 118 |
def _print_status(self):
|
| 119 |
"""Print model loading status"""
|
| 120 |
-
print("\n=== MODEL STATUS ===")
|
| 121 |
for model, loaded in self.models_loaded.items():
|
|
|
|
|
|
|
| 122 |
status = "[OK] LOADED" if loaded else "[FALLBACK/DISABLED]"
|
| 123 |
print(f"{model}: {status}")
|
| 124 |
print("===================\n")
|
| 125 |
-
|
| 126 |
-
print("=== UPGRADE VERIFICATION ===")
|
| 127 |
-
try:
|
| 128 |
-
from resampler_enhanced import EnhancedResampler
|
| 129 |
-
from ip_attention_processor_enhanced import EnhancedIPAttnProcessor2_0
|
| 130 |
-
|
| 131 |
-
resampler_check = isinstance(self.image_proj_model, EnhancedResampler) if hasattr(self, 'image_proj_model') and self.image_proj_model is not None else False
|
| 132 |
-
custom_attn_check = any(isinstance(p, EnhancedIPAttnProcessor2_0) for p in self.pipe.unet.attn_processors.values()) if hasattr(self, 'pipe') else False
|
| 133 |
-
|
| 134 |
-
print(f"Enhanced Perceiver Resampler: {'[OK] ACTIVE' if resampler_check else '[INFO] Not active'}")
|
| 135 |
-
print(f"Enhanced IP-Adapter Attention: {'[OK] ACTIVE' if custom_attn_check else '[INFO] Not active'}")
|
| 136 |
-
|
| 137 |
-
if resampler_check and custom_attn_check:
|
| 138 |
-
print("[SUCCESS] Face preservation upgrade fully active")
|
| 139 |
-
print(" Expected improvement: +10-15% face similarity")
|
| 140 |
-
elif resampler_check or custom_attn_check:
|
| 141 |
-
print("[PARTIAL] Some upgrades active")
|
| 142 |
-
else:
|
| 143 |
-
print("[INFO] Using standard components")
|
| 144 |
-
except Exception as e:
|
| 145 |
-
print(f"[INFO] Verification skipped: {e}")
|
| 146 |
-
print("============================\n")
|
| 147 |
-
|
| 148 |
-
def get_depth_map(self, image):
|
| 149 |
-
"""Generate depth map using Zoe Depth"""
|
| 150 |
-
if self.zoe_depth is not None:
|
| 151 |
-
try:
|
| 152 |
-
if image.mode != 'RGB':
|
| 153 |
-
image = image.convert('RGB')
|
| 154 |
-
|
| 155 |
-
orig_width, orig_height = image.size
|
| 156 |
-
# **FIX 1 START: Ensure all size variables are standard Python int**
|
| 157 |
-
orig_width = int(orig_width)
|
| 158 |
-
orig_height = int(orig_height)
|
| 159 |
-
|
| 160 |
-
# FIXED: Use multiples of 64 (not 32)
|
| 161 |
-
target_width = int((orig_width // 64) * 64)
|
| 162 |
-
target_height = int((orig_height // 64) * 64)
|
| 163 |
-
|
| 164 |
-
target_width = int(max(64, target_width))
|
| 165 |
-
target_height = int(max(64, target_height))
|
| 166 |
-
|
| 167 |
-
# Create an explicit tuple of standard ints
|
| 168 |
-
size_for_depth = (int(target_width), int(target_height))
|
| 169 |
-
|
| 170 |
-
# Always resize using the explicit int tuple to avoid numpy.int64 issues
|
| 171 |
-
# This replaces the conditional resize
|
| 172 |
-
image_for_depth = image.resize(size_for_depth, Image.LANCZOS)
|
| 173 |
-
|
| 174 |
-
if target_width != orig_width or target_height != orig_height:
|
| 175 |
-
print(f"[DEPTH] Resized for ZoeDetector: {orig_width}x{orig_height} -> {target_width}x{target_height}")
|
| 176 |
-
|
| 177 |
-
# FIXED: Add torch.no_grad() wrapper
|
| 178 |
-
with torch.no_grad():
|
| 179 |
-
depth_image = self.zoe_depth(image_for_depth) # Use the correctly-typed resized image
|
| 180 |
-
|
| 181 |
-
depth_width, depth_height = depth_image.size
|
| 182 |
-
if depth_width != orig_width or depth_height != orig_height:
|
| 183 |
-
# Resize back to the original size that get_depth_map received
|
| 184 |
-
depth_image = depth_image.resize((int(orig_width), int(orig_height)), Image.LANCZOS)
|
| 185 |
-
# **FIX 1 END**
|
| 186 |
-
|
| 187 |
-
print(f"[DEPTH] Zoe depth map generated: {orig_width}x{orig_height}")
|
| 188 |
-
return depth_image
|
| 189 |
-
|
| 190 |
-
except Exception as e:
|
| 191 |
-
print(f"[DEPTH] ZoeDetector failed ({e}), falling back to grayscale depth")
|
| 192 |
-
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 193 |
-
depth_colored = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
|
| 194 |
-
return Image.fromarray(depth_colored)
|
| 195 |
-
else:
|
| 196 |
-
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 197 |
-
depth_colored = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
|
| 198 |
-
return Image.fromarray(depth_colored)
|
| 199 |
-
|
| 200 |
|
| 201 |
def add_trigger_word(self, prompt):
|
| 202 |
"""Add trigger word to prompt if not present"""
|
| 203 |
if TRIGGER_WORD.lower() not in prompt.lower():
|
| 204 |
-
# **FIX 3 START: Handle empty or blank prompt**
|
| 205 |
if not prompt or not prompt.strip():
|
| 206 |
return TRIGGER_WORD
|
| 207 |
-
# **FIX 3 END**
|
| 208 |
return f"{TRIGGER_WORD}, {prompt}"
|
| 209 |
return prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
def
|
| 212 |
-
"""
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
# Extract features
|
| 229 |
-
scaled_array = cv2.cvtColor(np.array(scaled_crop), cv2.COLOR_RGB2BGR)
|
| 230 |
-
scaled_faces = self.face_app.get(scaled_array)
|
| 231 |
-
|
| 232 |
-
if len(scaled_faces) > 0:
|
| 233 |
-
multi_scale_embeds.append(scaled_faces[0].normed_embedding)
|
| 234 |
-
|
| 235 |
-
# Average embeddings
|
| 236 |
-
if len(multi_scale_embeds) > 0:
|
| 237 |
-
averaged = np.mean(multi_scale_embeds, axis=0)
|
| 238 |
-
# Renormalize
|
| 239 |
-
averaged = averaged / np.linalg.norm(averaged)
|
| 240 |
-
print(f"[MULTI-SCALE] Combined {len(multi_scale_embeds)} scales")
|
| 241 |
-
return averaged
|
| 242 |
-
|
| 243 |
-
return face.normed_embedding
|
| 244 |
-
|
| 245 |
-
except Exception as e:
|
| 246 |
-
print(f"[MULTI-SCALE] Failed: {e}, using single scale")
|
| 247 |
-
return face.normed_embedding
|
| 248 |
-
|
| 249 |
-
def detect_face_quality(self, face):
|
| 250 |
-
"""
|
| 251 |
-
Detect face quality and adaptively adjust parameters.
|
| 252 |
-
+2-3% consistency improvement.
|
| 253 |
-
"""
|
| 254 |
-
try:
|
| 255 |
-
bbox = face.bbox
|
| 256 |
-
face_size = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
|
| 257 |
-
det_score = float(face.det_score) if hasattr(face, 'det_score') else 1.0
|
| 258 |
-
|
| 259 |
-
# Small face -> boost identity preservation
|
| 260 |
-
if face_size < ADAPTIVE_THRESHOLDS['small_face_size']:
|
| 261 |
-
return ADAPTIVE_PARAMS['small_face'].copy()
|
| 262 |
-
|
| 263 |
-
# Low confidence -> boost preservation
|
| 264 |
-
elif det_score < ADAPTIVE_THRESHOLDS['low_confidence']:
|
| 265 |
-
return ADAPTIVE_PARAMS['low_confidence'].copy()
|
| 266 |
-
|
| 267 |
-
# Check for profile/side view (if pose available)
|
| 268 |
-
elif hasattr(face, 'pose') and len(face.pose) > 1:
|
| 269 |
-
try:
|
| 270 |
-
yaw = float(face.pose[1])
|
| 271 |
-
if abs(yaw) > ADAPTIVE_THRESHOLDS['profile_angle']:
|
| 272 |
-
return ADAPTIVE_PARAMS['profile_view'].copy()
|
| 273 |
-
except (ValueError, TypeError, IndexError):
|
| 274 |
-
pass
|
| 275 |
-
|
| 276 |
-
# Good quality face - use provided parameters
|
| 277 |
-
return None
|
| 278 |
-
|
| 279 |
-
except Exception as e:
|
| 280 |
-
print(f"[ADAPTIVE] Quality detection failed: {e}")
|
| 281 |
-
return None
|
| 282 |
-
|
| 283 |
-
def validate_and_adjust_parameters(self, strength, guidance_scale, lora_scale,
|
| 284 |
-
identity_preservation, identity_control_scale,
|
| 285 |
-
depth_control_scale, consistency_mode=True):
|
| 286 |
-
"""
|
| 287 |
-
Enhanced parameter validation with stricter rules for consistency.
|
| 288 |
-
"""
|
| 289 |
-
if consistency_mode:
|
| 290 |
-
print("[CONSISTENCY] Applying strict parameter validation...")
|
| 291 |
-
adjustments = []
|
| 292 |
-
|
| 293 |
-
# Rule 1: Strong inverse relationship between identity and LORA
|
| 294 |
-
if identity_preservation > 1.2:
|
| 295 |
-
original_lora = lora_scale
|
| 296 |
-
lora_scale = min(lora_scale, 1.0)
|
| 297 |
-
if abs(lora_scale - original_lora) > 0.01:
|
| 298 |
-
adjustments.append(f"LORA: {original_lora:.2f}->{lora_scale:.2f} (high identity)")
|
| 299 |
-
|
| 300 |
-
# Rule 2: Strength-based profile activation
|
| 301 |
-
if strength < 0.5:
|
| 302 |
-
# Maximum preservation mode
|
| 303 |
-
if identity_preservation < 1.3:
|
| 304 |
-
original_identity = identity_preservation
|
| 305 |
-
identity_preservation = 1.3
|
| 306 |
-
adjustments.append(f"Identity: {original_identity:.2f}->{identity_preservation:.2f} (max preservation)")
|
| 307 |
-
if lora_scale > 0.9:
|
| 308 |
-
original_lora = lora_scale
|
| 309 |
-
lora_scale = 0.9
|
| 310 |
-
adjustments.append(f"LORA: {original_lora:.2f}->{lora_scale:.2f} (max preservation)")
|
| 311 |
-
if guidance_scale > 1.3:
|
| 312 |
-
original_cfg = guidance_scale
|
| 313 |
-
guidance_scale = 1.3
|
| 314 |
-
adjustments.append(f"CFG: {original_cfg:.2f}->{guidance_scale:.2f} (max preservation)")
|
| 315 |
-
|
| 316 |
-
elif strength > 0.7:
|
| 317 |
-
# Artistic transformation mode
|
| 318 |
-
if identity_preservation > 1.0:
|
| 319 |
-
original_identity = identity_preservation
|
| 320 |
-
identity_preservation = 1.0
|
| 321 |
-
adjustments.append(f"Identity: {original_identity:.2f}->{identity_preservation:.2f} (artistic mode)")
|
| 322 |
-
if lora_scale < 1.2:
|
| 323 |
-
original_lora = lora_scale
|
| 324 |
-
lora_scale = 1.2
|
| 325 |
-
adjustments.append(f"LORA: {original_lora:.2f}->{lora_scale:.2f} (artistic mode)")
|
| 326 |
-
|
| 327 |
-
# Rule 3: CFG-LORA relationship
|
| 328 |
-
if guidance_scale > 1.4 and lora_scale > 1.2:
|
| 329 |
-
original_lora = lora_scale
|
| 330 |
-
lora_scale = 1.1
|
| 331 |
-
adjustments.append(f"LORA: {original_lora:.2f}->{lora_scale:.2f} (high CFG detected)")
|
| 332 |
-
|
| 333 |
-
# Rule 4: LCM sweet spot enforcement
|
| 334 |
-
original_cfg = guidance_scale
|
| 335 |
-
guidance_scale = max(1.0, min(guidance_scale, 1.5))
|
| 336 |
-
if abs(guidance_scale - original_cfg) > 0.01:
|
| 337 |
-
adjustments.append(f"CFG: {original_cfg:.2f}->{guidance_scale:.2f} (LCM optimal)")
|
| 338 |
-
|
| 339 |
-
# Rule 5: ControlNet balance
|
| 340 |
-
total_control = identity_control_scale + depth_control_scale
|
| 341 |
-
if total_control > 1.7:
|
| 342 |
-
scale_factor = 1.7 / total_control
|
| 343 |
-
original_id_ctrl = identity_control_scale
|
| 344 |
-
original_depth_ctrl = depth_control_scale
|
| 345 |
-
identity_control_scale *= scale_factor
|
| 346 |
-
depth_control_scale *= scale_factor
|
| 347 |
-
adjustments.append(f"ControlNets balanced: ID {original_id_ctrl:.2f}->{identity_control_scale:.2f}, Depth {original_depth_ctrl:.2f}->{depth_control_scale:.2f}")
|
| 348 |
-
|
| 349 |
-
# Report adjustments
|
| 350 |
-
if adjustments:
|
| 351 |
-
print(" [OK] Applied adjustments:")
|
| 352 |
-
for adj in adjustments:
|
| 353 |
-
print(f" - {adj}")
|
| 354 |
-
else:
|
| 355 |
-
print(" [OK] Parameters already optimal")
|
| 356 |
-
|
| 357 |
-
return strength, guidance_scale, lora_scale, identity_preservation, identity_control_scale, depth_control_scale
|
| 358 |
-
|
| 359 |
-
def generate_caption(self, image, max_length=None, num_beams=None):
|
| 360 |
-
"""Generate a descriptive caption for the image (supports BLIP-2, GIT, BLIP)."""
|
| 361 |
-
if not self.caption_enabled or self.caption_model is None:
|
| 362 |
-
return None
|
| 363 |
-
|
| 364 |
-
# Set defaults based on model type
|
| 365 |
-
if max_length is None:
|
| 366 |
-
if self.caption_model_type == "blip2":
|
| 367 |
-
max_length = 50 # BLIP-2 can handle longer captions
|
| 368 |
-
elif self.caption_model_type == "git":
|
| 369 |
-
max_length = 40 # GIT also produces good long captions
|
| 370 |
-
else:
|
| 371 |
-
max_length = CAPTION_CONFIG['max_length'] # BLIP base (20)
|
| 372 |
-
|
| 373 |
-
if num_beams is None:
|
| 374 |
-
num_beams = CAPTION_CONFIG['num_beams']
|
| 375 |
-
|
| 376 |
-
try:
|
| 377 |
-
if self.caption_model_type == "blip2":
|
| 378 |
-
# BLIP-2 specific processing
|
| 379 |
-
inputs = self.caption_processor(image, return_tensors="pt").to(self.device, self.dtype)
|
| 380 |
-
|
| 381 |
-
with torch.no_grad():
|
| 382 |
-
output = self.caption_model.generate(
|
| 383 |
-
**inputs,
|
| 384 |
-
max_length=max_length,
|
| 385 |
-
num_beams=num_beams,
|
| 386 |
-
min_length=10, # Encourage longer captions
|
| 387 |
-
length_penalty=1.0,
|
| 388 |
-
repetition_penalty=1.5,
|
| 389 |
-
early_stopping=True
|
| 390 |
-
)
|
| 391 |
-
|
| 392 |
-
caption = self.caption_processor.decode(output[0], skip_special_tokens=True)
|
| 393 |
-
|
| 394 |
-
elif self.caption_model_type == "git":
|
| 395 |
-
# GIT specific processing
|
| 396 |
-
inputs = self.caption_processor(images=image, return_tensors="pt").to(self.device, self.dtype)
|
| 397 |
-
|
| 398 |
-
with torch.no_grad():
|
| 399 |
-
output = self.caption_model.generate(
|
| 400 |
-
pixel_values=inputs.pixel_values,
|
| 401 |
-
max_length=max_length,
|
| 402 |
-
num_beams=num_beams,
|
| 403 |
-
min_length=10,
|
| 404 |
-
length_penalty=1.0,
|
| 405 |
-
repetition_penalty=1.5,
|
| 406 |
-
early_stopping=True
|
| 407 |
-
)
|
| 408 |
-
|
| 409 |
-
caption = self.caption_processor.batch_decode(output, skip_special_tokens=True)[0]
|
| 410 |
-
|
| 411 |
-
else:
|
| 412 |
-
# BLIP base processing
|
| 413 |
-
inputs = self.caption_processor(image, return_tensors="pt").to(self.device, self.dtype)
|
| 414 |
-
|
| 415 |
-
with torch.no_grad():
|
| 416 |
-
output = self.caption_model.generate(
|
| 417 |
-
**inputs,
|
| 418 |
-
max_length=max_length,
|
| 419 |
-
num_beams=num_beams,
|
| 420 |
-
early_stopping=True
|
| 421 |
-
)
|
| 422 |
-
|
| 423 |
-
caption = self.caption_processor.decode(output[0], skip_special_tokens=True)
|
| 424 |
-
|
| 425 |
-
return caption.strip()
|
| 426 |
-
|
| 427 |
-
except Exception as e:
|
| 428 |
-
print(f"Caption generation failed: {e}")
|
| 429 |
-
return None
|
| 430 |
-
|
| 431 |
-
def generate_retro_art(
|
| 432 |
self,
|
| 433 |
-
|
| 434 |
-
prompt="
|
| 435 |
-
negative_prompt="
|
| 436 |
-
num_inference_steps=12,
|
| 437 |
-
guidance_scale=1.
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
enable_color_matching=
|
| 444 |
consistency_mode=True,
|
| 445 |
seed=-1
|
| 446 |
):
|
| 447 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 448 |
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
|
|
|
|
|
|
| 452 |
|
| 453 |
-
# **FIX 3 START: Ensure blank negative prompts are empty strings for Compel**
|
| 454 |
-
if not negative_prompt or not negative_prompt.strip():
|
| 455 |
-
negative_prompt = ""
|
| 456 |
-
# **FIX 3 END**
|
| 457 |
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
print("\n[CONSISTENCY] Validating and adjusting parameters...")
|
| 461 |
-
strength, guidance_scale, lora_scale, identity_preservation, identity_control_scale, depth_control_scale = \
|
| 462 |
-
self.validate_and_adjust_parameters(
|
| 463 |
-
strength, guidance_scale, lora_scale, identity_preservation,
|
| 464 |
-
identity_control_scale, depth_control_scale, consistency_mode
|
| 465 |
-
)
|
| 466 |
|
| 467 |
-
#
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
# Calculate optimal size with flexible aspect ratio support
|
| 471 |
-
original_width, original_height = input_image.size
|
| 472 |
-
target_width, target_height = calculate_optimal_size(original_width, original_height)
|
| 473 |
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
print(f"Img2Img Strength: {strength}")
|
| 477 |
|
| 478 |
-
|
| 479 |
-
resized_image = input_image.resize((int(target_width), int(target_height)), Image.LANCZOS)
|
| 480 |
|
| 481 |
-
#
|
| 482 |
-
print("Generating
|
| 483 |
-
|
| 484 |
-
if
|
| 485 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
|
| 487 |
-
#
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
|
|
|
| 494 |
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
faces = self.face_app.get(img_array)
|
| 499 |
|
| 500 |
if len(faces) > 0:
|
| 501 |
-
|
| 502 |
-
print(f"
|
| 503 |
-
|
| 504 |
-
# Get largest face
|
| 505 |
-
face = sorted(faces, key=lambda x: (x.bbox[2] - x.bbox[0]) * (x.bbox[3] - x.bbox[1]))[-1]
|
| 506 |
-
|
| 507 |
-
# ADAPTIVE PARAMETERS
|
| 508 |
-
adaptive_params = self.detect_face_quality(face)
|
| 509 |
-
if adaptive_params is not None:
|
| 510 |
-
print(f"[ADAPTIVE] {adaptive_params['reason']}")
|
| 511 |
-
identity_preservation = adaptive_params['identity_preservation']
|
| 512 |
-
identity_control_scale = adaptive_params['identity_control_scale']
|
| 513 |
-
guidance_scale = adaptive_params['guidance_scale']
|
| 514 |
-
lora_scale = adaptive_params['lora_scale']
|
| 515 |
-
|
| 516 |
-
# Extract face embeddings
|
| 517 |
-
face_embeddings_base = face.normed_embedding
|
| 518 |
-
|
| 519 |
-
# Extract face crop
|
| 520 |
-
bbox = face.bbox.astype(int)
|
| 521 |
-
x1, y1, x2, y2 = bbox[0], bbox[1], bbox[2], bbox[3]
|
| 522 |
-
face_bbox_original = [x1, y1, x2, y2]
|
| 523 |
-
|
| 524 |
-
# Add padding
|
| 525 |
-
face_width = x2 - x1
|
| 526 |
-
face_height = y2 - y1
|
| 527 |
-
padding_x = int(face_width * 0.3)
|
| 528 |
-
padding_y = int(face_height * 0.3)
|
| 529 |
-
x1 = max(0, x1 - padding_x)
|
| 530 |
-
y1 = max(0, y1 - padding_y)
|
| 531 |
-
x2 = min(resized_image.width, x2 + padding_x)
|
| 532 |
-
y2 = min(resized_image.height, y2 + padding_y)
|
| 533 |
-
|
| 534 |
-
# Crop face region
|
| 535 |
-
face_crop = resized_image.crop((x1, y1, x2, y2))
|
| 536 |
-
|
| 537 |
-
# MULTI-SCALE PROCESSING
|
| 538 |
-
face_embeddings = self.extract_multi_scale_face(face_crop, face)
|
| 539 |
-
|
| 540 |
-
# Enhance face crop
|
| 541 |
-
face_crop_enhanced = enhance_face_crop(face_crop)
|
| 542 |
|
| 543 |
-
#
|
| 544 |
-
|
| 545 |
-
face_kps_image = draw_kps(resized_image, face_kps)
|
| 546 |
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
prompt = build_enhanced_prompt(prompt, facial_attrs, TRIGGER_WORD)
|
| 553 |
-
|
| 554 |
-
# Legacy output for compatibility
|
| 555 |
-
age = facial_attrs['age']
|
| 556 |
-
gender_code = facial_attrs['gender']
|
| 557 |
-
det_score = facial_attrs['quality']
|
| 558 |
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
|
| 563 |
-
|
|
|
|
|
|
|
|
|
|
| 564 |
if hasattr(self.pipe, 'set_adapters') and self.models_loaded['lora']:
|
| 565 |
try:
|
| 566 |
self.pipe.set_adapters(["retroart"], adapter_weights=[lora_scale])
|
| 567 |
-
print(f"LORA scale: {lora_scale}")
|
| 568 |
except Exception as e:
|
| 569 |
-
print(f"Could not set LORA scale: {e}")
|
| 570 |
-
|
| 571 |
-
# Prepare generation kwargs
|
| 572 |
-
pipe_kwargs = {
|
| 573 |
-
"image": resized_image,
|
| 574 |
-
"strength": strength,
|
| 575 |
-
"num_inference_steps": num_inference_steps,
|
| 576 |
-
"guidance_scale": guidance_scale,
|
| 577 |
-
}
|
| 578 |
|
| 579 |
-
#
|
|
|
|
| 580 |
if seed == -1:
|
| 581 |
generator = torch.Generator(device=self.device)
|
| 582 |
actual_seed = generator.seed()
|
|
@@ -586,142 +261,125 @@ class RetroArtConverter:
|
|
| 586 |
actual_seed = seed
|
| 587 |
print(f"[SEED] Using fixed seed: {actual_seed}")
|
| 588 |
|
| 589 |
-
pipe_kwargs["generator"] = generator
|
| 590 |
|
| 591 |
-
#
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 602 |
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
|
| 612 |
-
#
|
| 613 |
-
|
| 614 |
-
|
|
|
|
| 615 |
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 621 |
|
| 622 |
-
|
| 623 |
-
|
|
|
|
| 624 |
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
)
|
|
|
|
| 635 |
|
| 636 |
-
|
| 637 |
-
|
|
|
|
|
|
|
|
|
|
| 638 |
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
# Scale with identity preservation
|
| 643 |
-
boosted_scale = identity_preservation * IDENTITY_BOOST_MULTIPLIER
|
| 644 |
-
face_proj_embeds = face_proj_embeds * boosted_scale
|
| 645 |
-
|
| 646 |
-
print(f" - Face embedding: {face_emb_tensor.shape}")
|
| 647 |
-
print(f" - Resampler output: {face_proj_embeds.shape}")
|
| 648 |
-
print(f" - Scale: {boosted_scale:.2f}")
|
| 649 |
-
|
| 650 |
-
# CRITICAL: Concatenate with text embeddings (not separate kwargs!)
|
| 651 |
-
if 'prompt_embeds' in pipe_kwargs:
|
| 652 |
-
# Compel encoded prompts
|
| 653 |
-
original_embeds = pipe_kwargs['prompt_embeds']
|
| 654 |
-
|
| 655 |
-
# Handle CFG (classifier-free guidance)
|
| 656 |
-
if original_embeds.shape[0] > 1: # Has negative + positive
|
| 657 |
-
# Duplicate for negative + positive
|
| 658 |
-
face_proj_embeds = torch.cat([
|
| 659 |
-
torch.zeros_like(face_proj_embeds), # Negative
|
| 660 |
-
face_proj_embeds # Positive
|
| 661 |
-
], dim=0)
|
| 662 |
-
|
| 663 |
-
# Concatenate: [batch, text_tokens, 2048] + [batch, 16, 2048]
|
| 664 |
-
combined_embeds = torch.cat([original_embeds, face_proj_embeds], dim=1)
|
| 665 |
-
pipe_kwargs['prompt_embeds'] = combined_embeds
|
| 666 |
-
|
| 667 |
-
print(f" - Text embeds: {original_embeds.shape}")
|
| 668 |
-
print(f" - Combined embeds: {combined_embeds.shape}")
|
| 669 |
-
print(f" [OK] Face embeddings concatenated successfully!")
|
| 670 |
-
|
| 671 |
-
else:
|
| 672 |
-
print(f" [WARNING] Can't concatenate - no prompt_embeds (use Compel)")
|
| 673 |
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
elif using_multiple_controlnets and not has_detected_faces:
|
| 680 |
-
print("Multiple ControlNets available but no faces detected, using depth only")
|
| 681 |
-
control_images = [depth_image, depth_image]
|
| 682 |
-
conditioning_scales = [0.0, depth_control_scale]
|
| 683 |
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
else:
|
| 688 |
-
print("Using Depth ControlNet only")
|
| 689 |
-
pipe_kwargs["control_image"] = depth_image
|
| 690 |
-
pipe_kwargs["controlnet_conditioning_scale"] = depth_control_scale
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
# Generate
|
| 694 |
-
print(f"Generating with LCM: Steps={num_inference_steps}, CFG={guidance_scale}, Strength={strength}")
|
| 695 |
-
print(f"Controlnet scales - Identity: {identity_control_scale}, Depth: {depth_control_scale}")
|
| 696 |
-
result = self.pipe(**pipe_kwargs)
|
| 697 |
-
|
| 698 |
-
generated_image = result.images[0]
|
| 699 |
-
|
| 700 |
-
# Post-processing
|
| 701 |
-
if enable_color_matching and has_detected_faces:
|
| 702 |
-
print("Applying enhanced face-aware color matching...")
|
| 703 |
-
try:
|
| 704 |
-
if face_bbox_original is not None:
|
| 705 |
-
generated_image = enhanced_color_match(
|
| 706 |
-
generated_image,
|
| 707 |
-
resized_image,
|
| 708 |
-
face_bbox=face_bbox_original
|
| 709 |
-
)
|
| 710 |
-
print("[OK] Enhanced color matching applied (face-aware)")
|
| 711 |
-
else:
|
| 712 |
-
generated_image = color_match(generated_image, resized_image, mode='mkl')
|
| 713 |
-
print("[OK] Standard color matching applied")
|
| 714 |
-
except Exception as e:
|
| 715 |
-
print(f"Color matching failed: {e}")
|
| 716 |
-
elif enable_color_matching:
|
| 717 |
-
print("Applying standard color matching...")
|
| 718 |
-
try:
|
| 719 |
-
generated_image = color_match(generated_image, resized_image, mode='mkl')
|
| 720 |
-
print("[OK] Standard color matching applied")
|
| 721 |
-
except Exception as e:
|
| 722 |
-
print(f"Color matching failed: {e}")
|
| 723 |
-
|
| 724 |
-
return generated_image
|
| 725 |
|
| 726 |
|
| 727 |
-
print("[OK] Generator class ready")
|
|
|
|
| 1 |
"""
|
| 2 |
Generation logic for Pixagram AI Pixel Art Generator
|
| 3 |
+
MODIFIED for IP-Adapter-FaceIDXL (non-plus) and LCM
|
| 4 |
"""
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
|
|
|
| 8 |
from PIL import Image
|
| 9 |
import torch.nn.functional as F
|
| 10 |
from torchvision import transforms
|
| 11 |
+
# face_align is NO LONGER NEEDED for this class
|
| 12 |
+
from transformers import Pipeline
|
| 13 |
|
| 14 |
from config import (
|
| 15 |
device, dtype, TRIGGER_WORD, MULTI_SCALE_FACTORS,
|
| 16 |
+
ADAPTIVE_THRESHOLDS, ADAPTIVE_PARAMS, CAPTION_CONFIG, IDENTITY_BOOST_MULTIPLIER,
|
| 17 |
+
MODEL_REPO, MODEL_FILES
|
| 18 |
)
|
| 19 |
from utils import (
|
| 20 |
sanitize_text, enhanced_color_match, color_match, create_face_mask,
|
| 21 |
draw_kps, get_demographic_description, calculate_optimal_size, enhance_face_crop
|
| 22 |
)
|
| 23 |
from models import (
|
| 24 |
+
load_face_analysis, load_depth_detector, load_controlnets,
|
| 25 |
load_sdxl_pipeline, load_lora, setup_ip_adapter, setup_compel,
|
| 26 |
+
setup_scheduler, optimize_pipeline, load_caption_model, set_clip_skip,
|
| 27 |
+
load_canny_detector
|
| 28 |
+
# load_image_encoder (REMOVED)
|
| 29 |
)
|
| 30 |
|
| 31 |
|
| 32 |
class RetroArtConverter:
|
| 33 |
+
"""Main class for retro art generation - IP-Adapter-FaceIDXL / LCM VERSION"""
|
| 34 |
|
| 35 |
def __init__(self):
|
| 36 |
self.device = device
|
|
|
|
| 38 |
self.models_loaded = {
|
| 39 |
'custom_checkpoint': False,
|
| 40 |
'lora': False,
|
| 41 |
+
'ip_adapter': False,
|
| 42 |
+
'leres_depth': False,
|
| 43 |
+
'canny': False
|
| 44 |
}
|
| 45 |
|
| 46 |
+
# Initialize face analysis (buffalo_l)
|
| 47 |
self.face_app, self.face_detection_enabled = load_face_analysis()
|
| 48 |
|
| 49 |
+
# Load LeReS++ Depth detector
|
| 50 |
+
self.leres_detector, leres_success = load_depth_detector()
|
| 51 |
+
self.models_loaded['leres_depth'] = leres_success
|
| 52 |
+
|
| 53 |
+
# Load Canny detector
|
| 54 |
+
self.canny_detector, canny_success = load_canny_detector()
|
| 55 |
+
self.models_loaded['canny'] = canny_success
|
| 56 |
+
|
| 57 |
+
# Load ControlNets (Depth + Canny)
|
| 58 |
+
controlnet_depth, controlnet_canny, cn_canny_success = load_controlnets()
|
| 59 |
+
|
| 60 |
+
if cn_canny_success:
|
| 61 |
+
self.controlnet_depth = controlnet_depth
|
| 62 |
+
self.controlnet_canny = controlnet_canny
|
| 63 |
+
controlnets = [self.controlnet_depth, self.controlnet_canny]
|
| 64 |
+
print(f"Initializing with multiple ControlNets: Depth + Canny")
|
| 65 |
+
self.using_multiple_controlnets = True
|
|
|
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
+
self.controlnet_depth = controlnet_depth
|
| 68 |
controlnets = controlnet_depth
|
| 69 |
print(f"Initializing with single ControlNet: Depth only")
|
| 70 |
+
self.using_multiple_controlnets = False
|
| 71 |
+
|
| 72 |
# Load SDXL pipeline
|
| 73 |
self.pipe, checkpoint_success = load_sdxl_pipeline(controlnets)
|
| 74 |
self.models_loaded['custom_checkpoint'] = checkpoint_success
|
| 75 |
|
| 76 |
+
# Load LORA (retroart)
|
| 77 |
lora_success = load_lora(self.pipe)
|
| 78 |
self.models_loaded['lora'] = lora_success
|
| 79 |
|
| 80 |
+
# --- [FIX] REMOVED load_image_encoder ---
|
| 81 |
+
# self.image_encoder_path = load_image_encoder()
|
| 82 |
+
|
| 83 |
+
# Setup IP-Adapter (FaceIDXL wrapper)
|
| 84 |
+
# --- [FIX] REMOVED image_encoder_path from call ---
|
| 85 |
+
self.ip_model, ip_adapter_success = setup_ip_adapter(self.pipe)
|
| 86 |
+
self.models_loaded['ip_adapter'] = ip_adapter_success
|
|
|
|
| 87 |
|
| 88 |
# Setup Compel
|
| 89 |
self.compel, self.use_compel = setup_compel(self.pipe)
|
|
|
|
| 97 |
# Load caption model
|
| 98 |
self.caption_processor, self.caption_model, self.caption_enabled, self.caption_model_type = load_caption_model()
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# Set CLIP skip
|
| 101 |
set_clip_skip(self.pipe)
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
# Print model status
|
| 104 |
self._print_status()
|
| 105 |
|
| 106 |
+
print(" [OK] Model initialization complete (FaceIDXL / LCM)!")
|
| 107 |
|
| 108 |
def _print_status(self):
|
| 109 |
"""Print model loading status"""
|
| 110 |
+
print("\n=== MODEL STATUS (FaceIDXL / LCM) ===")
|
| 111 |
for model, loaded in self.models_loaded.items():
|
| 112 |
+
if model == 'lora_path':
|
| 113 |
+
continue
|
| 114 |
status = "[OK] LOADED" if loaded else "[FALLBACK/DISABLED]"
|
| 115 |
print(f"{model}: {status}")
|
| 116 |
print("===================\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
def add_trigger_word(self, prompt):
|
| 119 |
"""Add trigger word to prompt if not present"""
|
| 120 |
if TRIGGER_WORD.lower() not in prompt.lower():
|
|
|
|
| 121 |
if not prompt or not prompt.strip():
|
| 122 |
return TRIGGER_WORD
|
|
|
|
| 123 |
return f"{TRIGGER_WORD}, {prompt}"
|
| 124 |
return prompt
|
| 125 |
+
|
| 126 |
+
def get_depth_map(self, image):
|
| 127 |
+
"""Generate depth map using LeReS++"""
|
| 128 |
+
if self.leres_detector is not None:
|
| 129 |
+
try:
|
| 130 |
+
if image.mode != 'RGB':
|
| 131 |
+
image = image.convert('RGB')
|
| 132 |
+
print("Generating LeReS++ depth map...")
|
| 133 |
+
depth_map = self.leres_detector(image)
|
| 134 |
+
print(" [OK] LeReS++ map generated")
|
| 135 |
+
return depth_map
|
| 136 |
+
except Exception as e:
|
| 137 |
+
print(f"LeReS++ depth generation failed: {e}")
|
| 138 |
+
print("[WARNING] LeReS detector not loaded, returning blank image.")
|
| 139 |
+
return Image.new("RGB", image.size, (128, 128, 128))
|
| 140 |
|
| 141 |
+
def get_canny_map(self, image, low_threshold=100, high_threshold=200):
|
| 142 |
+
"""Generate canny map"""
|
| 143 |
+
if self.canny_detector is not None:
|
| 144 |
+
try:
|
| 145 |
+
if image.mode != 'RGB':
|
| 146 |
+
image = image.convert('RGB')
|
| 147 |
+
print("Generating Canny map...")
|
| 148 |
+
canny_map = self.canny_detector(image, low_threshold, high_threshold)
|
| 149 |
+
print(" [OK] Canny map generated")
|
| 150 |
+
return canny_map
|
| 151 |
+
except Exception as e:
|
| 152 |
+
print(f"Canny map generation failed: {e}")
|
| 153 |
+
print("[WARNING] Canny detector not loaded, returning blank image.")
|
| 154 |
+
return Image.new("RGB", image.size, (0, 0, 0))
|
| 155 |
+
|
| 156 |
+
def generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
self,
|
| 158 |
+
image,
|
| 159 |
+
prompt="a person",
|
| 160 |
+
negative_prompt="",
|
| 161 |
+
num_inference_steps=12, # LCM Default
|
| 162 |
+
guidance_scale=1.5, # LCM Default
|
| 163 |
+
strength=0.6,
|
| 164 |
+
lora_scale=1.0, # Re-added lora_scale
|
| 165 |
+
depth_control_scale=0.7,
|
| 166 |
+
canny_control_scale=0.5,
|
| 167 |
+
ip_adapter_scale=1.0, # This will be 'scale'
|
| 168 |
+
enable_color_matching=True,
|
| 169 |
consistency_mode=True,
|
| 170 |
seed=-1
|
| 171 |
):
|
| 172 |
+
"""
|
| 173 |
+
Generate retro art with IP-Adapter-FaceIDXL.
|
| 174 |
+
Falls back to standard pipeline if no face is detected.
|
| 175 |
+
"""
|
| 176 |
|
| 177 |
+
print(f"\n{'='*60}")
|
| 178 |
+
print(f"Starting FaceID-XL (LCM) generation with:")
|
| 179 |
+
print(f" Steps: {num_inference_steps}, CFG: {guidance_scale}, Strength: {strength}")
|
| 180 |
+
print(f" LoRA Scale: {lora_scale}, IP-Adapter Scale: {ip_adapter_scale}")
|
| 181 |
+
print(f" ControlNets: Depth ({depth_control_scale}), Canny ({canny_control_scale})")
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
+
if not self.models_loaded['ip_adapter']:
|
| 185 |
+
print("[WARNING] IP-Adapter-FaceID model is not loaded. Face generation will be disabled.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
+
# Prepare input image
|
| 188 |
+
if image.mode != 'RGB':
|
| 189 |
+
image = image.convert('RGB')
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
optimal_width, optimal_height = calculate_optimal_size(image.size[0], image.size[1])
|
| 192 |
+
resized_image = image.resize((optimal_width, optimal_height), Image.LANCZOS)
|
|
|
|
| 193 |
|
| 194 |
+
print(f"Image resized: {image.size} → {resized_image.size}")
|
|
|
|
| 195 |
|
| 196 |
+
# --- Prompt Enhancement ---
|
| 197 |
+
print("Generating caption for prompt enhancement...")
|
| 198 |
+
input_caption = self.generate_caption(image)
|
| 199 |
+
if input_caption:
|
| 200 |
+
print(f" [OK] Caption: {input_caption}")
|
| 201 |
+
if not prompt or not prompt.strip():
|
| 202 |
+
prompt = input_caption
|
| 203 |
+
else:
|
| 204 |
+
prompt = f"{input_caption}, {prompt}"
|
| 205 |
|
| 206 |
+
# Add retroart trigger word
|
| 207 |
+
prompt = self.add_trigger_word(prompt)
|
| 208 |
+
print(f" Final prompt: {prompt}")
|
| 209 |
+
|
| 210 |
+
# --- Face Preparation (NOW OPTIONAL) ---
|
| 211 |
+
print("Detecting faces (buffalo_l)...")
|
| 212 |
+
faceid_embeds = None
|
| 213 |
+
has_face = False
|
| 214 |
|
| 215 |
+
try:
|
| 216 |
+
image_np = cv2.cvtColor(np.array(resized_image), cv2.COLOR_RGB2BGR)
|
| 217 |
+
faces = self.face_app.get(image_np)
|
|
|
|
| 218 |
|
| 219 |
if len(faces) > 0:
|
| 220 |
+
face = faces[0]
|
| 221 |
+
print(f" [OK] Face detected (score: {face.det_score:.3f})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
+
# Get FaceID embeddings ONLY
|
| 224 |
+
faceid_embeds = torch.from_numpy(face.normed_embedding).unsqueeze(0).to(self.device)
|
|
|
|
| 225 |
|
| 226 |
+
print(" [OK] Face embeddings extracted.")
|
| 227 |
+
has_face = True
|
| 228 |
+
else:
|
| 229 |
+
print(" [INFO] No face detected. Proceeding without face identity.")
|
| 230 |
+
has_face = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
+
except Exception as e:
|
| 233 |
+
print(f" [WARNING] Face detection/prep failed: {e}. Proceeding without face identity.")
|
| 234 |
+
has_face = False
|
| 235 |
+
|
| 236 |
+
# --- ControlNet Maps ---
|
| 237 |
+
print("Generating depth map (LeReS++)...")
|
| 238 |
+
depth_image = self.get_depth_map(resized_image)
|
| 239 |
+
print("Generating canny map...")
|
| 240 |
+
canny_image = self.get_canny_map(resized_image)
|
| 241 |
|
| 242 |
+
control_image = [depth_image, canny_image]
|
| 243 |
+
conditioning_scales = [depth_control_scale, canny_control_scale]
|
| 244 |
+
|
| 245 |
+
# --- LORA (RetroArt) Setup ---
|
| 246 |
if hasattr(self.pipe, 'set_adapters') and self.models_loaded['lora']:
|
| 247 |
try:
|
| 248 |
self.pipe.set_adapters(["retroart"], adapter_weights=[lora_scale])
|
| 249 |
+
print(f"RetroArt LORA scale set: {lora_scale}")
|
| 250 |
except Exception as e:
|
| 251 |
+
print(f" [WARNING] Could not set LORA scale: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
+
# --- Generator (Seed) Setup ---
|
| 254 |
+
# (Moved before the if/else block to be available for both paths)
|
| 255 |
if seed == -1:
|
| 256 |
generator = torch.Generator(device=self.device)
|
| 257 |
actual_seed = generator.seed()
|
|
|
|
| 261 |
actual_seed = seed
|
| 262 |
print(f"[SEED] Using fixed seed: {actual_seed}")
|
| 263 |
|
|
|
|
| 264 |
|
| 265 |
+
# --- Generate (Conditional Path) ---
|
| 266 |
+
try:
|
| 267 |
+
if self.models_loaded['ip_adapter'] and has_face:
|
| 268 |
+
# --- PATH 1: FACE DETECTED ---
|
| 269 |
+
print(f"\nGenerating with IPAdapterFaceIDXL (Face Detected):")
|
| 270 |
+
print(f" IP-Adapter scale (Face): {ip_adapter_scale}")
|
| 271 |
|
| 272 |
+
generated_images = self.ip_model.generate(
|
| 273 |
+
prompt=prompt,
|
| 274 |
+
negative_prompt=negative_prompt,
|
| 275 |
+
faceid_embeds=faceid_embeds,
|
| 276 |
+
scale=ip_adapter_scale, # Use 'scale' not 's_scale'
|
| 277 |
+
num_samples=4,
|
| 278 |
+
width=optimal_width,
|
| 279 |
+
height=optimal_height,
|
| 280 |
+
num_inference_steps=num_inference_steps,
|
| 281 |
+
seed=actual_seed,
|
| 282 |
+
|
| 283 |
+
# These are passed via **kwargs to self.pipe()
|
| 284 |
+
image=resized_image,
|
| 285 |
+
strength=strength,
|
| 286 |
+
control_image=control_image,
|
| 287 |
+
controlnet_conditioning_scale=conditioning_scales,
|
| 288 |
+
guidance_scale=guidance_scale # Pass CFG
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
else:
|
| 292 |
+
# --- PATH 2: NO FACE DETECTED ---
|
| 293 |
+
print(f"\nGenerating with Standard Pipeline (No Face Detected):")
|
| 294 |
|
| 295 |
+
# We must encode prompts ourselves, as we aren't using the wrapper
|
| 296 |
+
kwargs = {
|
| 297 |
+
"width": optimal_width,
|
| 298 |
+
"height": optimal_height,
|
| 299 |
+
"num_inference_steps": num_inference_steps,
|
| 300 |
+
"generator": generator,
|
| 301 |
+
"image": resized_image,
|
| 302 |
+
"strength": strength,
|
| 303 |
+
"control_image": control_image,
|
| 304 |
+
"controlnet_conditioning_scale": conditioning_scales,
|
| 305 |
+
"guidance_scale": guidance_scale,
|
| 306 |
+
"num_images_per_prompt": 4
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
if self.use_compel and self.compel is not None:
|
| 310 |
+
print(" Encoding prompts with Compel...")
|
| 311 |
+
conditioning, pooled = self.compel(prompt)
|
| 312 |
+
negative_conditioning, negative_pooled = self.compel(negative_prompt)
|
| 313 |
+
kwargs["prompt_embeds"] = conditioning
|
| 314 |
+
kwargs["pooled_prompt_embeds"] = pooled
|
| 315 |
+
kwargs["negative_prompt_embeds"] = negative_conditioning
|
| 316 |
+
kwargs["negative_pooled_prompt_embeds"] = negative_pooled
|
| 317 |
+
else:
|
| 318 |
+
print(" Compel not available, using standard prompts.")
|
| 319 |
+
kwargs["prompt"] = prompt
|
| 320 |
+
kwargs["negative_prompt"] = negative_prompt
|
| 321 |
+
|
| 322 |
+
generated_images = self.pipe(**kwargs).images
|
| 323 |
+
|
| 324 |
+
except Exception as e:
|
| 325 |
+
print(f"[ERROR] Generation failed: {e}")
|
| 326 |
+
import traceback
|
| 327 |
+
traceback.print_exc()
|
| 328 |
+
raise
|
| 329 |
|
| 330 |
+
# Post-processing
|
| 331 |
+
print(f"\n{'='*60}")
|
| 332 |
+
print("Generation complete! (4 images)")
|
| 333 |
+
print(f"{'='*60}\n")
|
| 334 |
|
| 335 |
+
return generated_images
|
| 336 |
+
|
| 337 |
+
def generate_caption(self, image):
|
| 338 |
+
"""
|
| 339 |
+
Generate a caption for an image.
|
| 340 |
+
Returns None if caption generation is disabled.
|
| 341 |
+
"""
|
| 342 |
+
if not self.caption_enabled or self.caption_model is None:
|
| 343 |
+
return None
|
| 344 |
+
|
| 345 |
+
try:
|
| 346 |
+
# Ensure image is PIL Image
|
| 347 |
+
if not isinstance(image, Image.Image):
|
| 348 |
+
image = Image.fromarray(image)
|
| 349 |
|
| 350 |
+
# Convert to RGB if needed
|
| 351 |
+
if image.mode != 'RGB':
|
| 352 |
+
image = image.convert('RGB')
|
| 353 |
|
| 354 |
+
print("Generating caption...")
|
| 355 |
+
|
| 356 |
+
with torch.no_grad():
|
| 357 |
+
if self.caption_model_type == 'git':
|
| 358 |
+
# GIT model
|
| 359 |
+
inputs = self.caption_processor(images=image, return_tensors="pt").to(self.device)
|
| 360 |
+
generated_ids = self.caption_model.generate(
|
| 361 |
+
pixel_values=inputs.pixel_values,
|
| 362 |
+
max_length=50
|
| 363 |
)
|
| 364 |
+
caption = self.caption_processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 365 |
|
| 366 |
+
elif self.caption_model_type == 'blip':
|
| 367 |
+
# BLIP model
|
| 368 |
+
inputs = self.caption_processor(image, return_tensors="pt").to(self.device)
|
| 369 |
+
generated_ids = self.caption_model.generate(**inputs, max_length=50)
|
| 370 |
+
caption = self.caption_processor.decode(generated_ids[0], skip_special_tokens=True)
|
| 371 |
|
| 372 |
+
else:
|
| 373 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
|
| 375 |
+
# Sanitize caption
|
| 376 |
+
caption = caption.strip()
|
| 377 |
+
print(f" [OK] Caption: {caption}")
|
| 378 |
+
return caption
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
| 380 |
+
except Exception as e:
|
| 381 |
+
print(f" [WARNING] Caption generation failed: {e}")
|
| 382 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
|
| 385 |
+
print("[OK] Generator class ready (IP-Adapter-FaceIDXL / LCM VERSION)")
|