Spaces:
Runtime error
Runtime error
Update models.py
Browse files
models.py
CHANGED
|
@@ -13,7 +13,7 @@ from diffusers import (
|
|
| 13 |
from diffusers.models.attention_processor import AttnProcessor2_0
|
| 14 |
from transformers import CLIPVisionModelWithProjection
|
| 15 |
from insightface.app import FaceAnalysis
|
| 16 |
-
from controlnet_aux import ZoeDetector, OpenposeDetector
|
| 17 |
from huggingface_hub import hf_hub_download
|
| 18 |
from compel import Compel, ReturnedEmbeddingsType
|
| 19 |
|
|
@@ -82,16 +82,44 @@ def load_face_analysis():
|
|
| 82 |
|
| 83 |
|
| 84 |
def load_depth_detector():
|
| 85 |
-
"""
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
zoe_depth = ZoeDetector.from_pretrained("lllyasviel/Annotators")
|
| 89 |
zoe_depth.to(device)
|
| 90 |
-
print(" [OK]
|
| 91 |
-
return zoe_depth, True
|
| 92 |
except Exception as e:
|
| 93 |
-
print(f" [
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# --- NEW FUNCTION ---
|
| 97 |
def load_openpose_detector():
|
|
@@ -107,6 +135,19 @@ def load_openpose_detector():
|
|
| 107 |
return None, False
|
| 108 |
# --- END NEW FUNCTION ---
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
def load_controlnets():
|
| 111 |
"""Load ControlNet models."""
|
| 112 |
print("Loading ControlNet Zoe Depth model...")
|
|
@@ -187,17 +228,37 @@ def load_sdxl_pipeline(controlnets):
|
|
| 187 |
return pipe, False
|
| 188 |
|
| 189 |
|
| 190 |
-
def
|
| 191 |
-
"""Load
|
| 192 |
-
print("Loading
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
|
| 203 |
def setup_ip_adapter(pipe, image_encoder):
|
|
|
|
| 13 |
from diffusers.models.attention_processor import AttnProcessor2_0
|
| 14 |
from transformers import CLIPVisionModelWithProjection
|
| 15 |
from insightface.app import FaceAnalysis
|
| 16 |
+
from controlnet_aux import ZoeDetector, OpenposeDetector, LeresDetector, MidasDetector, MediapipeFaceDetector
|
| 17 |
from huggingface_hub import hf_hub_download
|
| 18 |
from compel import Compel, ReturnedEmbeddingsType
|
| 19 |
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
def load_depth_detector():
|
| 85 |
+
"""
|
| 86 |
+
Load depth detector with fallback hierarchy: Leres → Zoe → Midas.
|
| 87 |
+
Returns (detector, detector_type, success).
|
| 88 |
+
"""
|
| 89 |
+
print("Loading depth detector with fallback hierarchy...")
|
| 90 |
+
|
| 91 |
+
# Try LeresDetector first (best quality)
|
| 92 |
try:
|
| 93 |
+
print(" Attempting LeresDetector (highest quality)...")
|
| 94 |
+
leres_depth = LeresDetector.from_pretrained("lllyasviel/Annotators")
|
| 95 |
+
leres_depth.to(device)
|
| 96 |
+
print(" [OK] LeresDetector loaded successfully")
|
| 97 |
+
return leres_depth, 'leres', True
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f" [INFO] LeresDetector not available: {e}")
|
| 100 |
+
|
| 101 |
+
# Fallback to ZoeDetector
|
| 102 |
+
try:
|
| 103 |
+
print(" Attempting ZoeDetector (fallback #1)...")
|
| 104 |
zoe_depth = ZoeDetector.from_pretrained("lllyasviel/Annotators")
|
| 105 |
zoe_depth.to(device)
|
| 106 |
+
print(" [OK] ZoeDetector loaded successfully")
|
| 107 |
+
return zoe_depth, 'zoe', True
|
| 108 |
except Exception as e:
|
| 109 |
+
print(f" [INFO] ZoeDetector not available: {e}")
|
| 110 |
+
|
| 111 |
+
# Final fallback to MidasDetector
|
| 112 |
+
try:
|
| 113 |
+
print(" Attempting MidasDetector (fallback #2)...")
|
| 114 |
+
midas_depth = MidasDetector.from_pretrained("lllyasviel/Annotators")
|
| 115 |
+
midas_depth.to(device)
|
| 116 |
+
print(" [OK] MidasDetector loaded successfully")
|
| 117 |
+
return midas_depth, 'midas', True
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f" [WARNING] MidasDetector not available: {e}")
|
| 120 |
+
|
| 121 |
+
print(" [ERROR] No depth detector available")
|
| 122 |
+
return None, None, False
|
| 123 |
|
| 124 |
# --- NEW FUNCTION ---
|
| 125 |
def load_openpose_detector():
|
|
|
|
| 135 |
return None, False
|
| 136 |
# --- END NEW FUNCTION ---
|
| 137 |
|
| 138 |
+
# --- NEW FUNCTION ---
|
| 139 |
+
def load_mediapipe_face_detector():
|
| 140 |
+
"""Load MediapipeFaceDetector for advanced face detection."""
|
| 141 |
+
print("Loading MediapipeFaceDetector...")
|
| 142 |
+
try:
|
| 143 |
+
face_detector = MediapipeFaceDetector()
|
| 144 |
+
print(" [OK] MediapipeFaceDetector loaded successfully")
|
| 145 |
+
return face_detector, True
|
| 146 |
+
except Exception as e:
|
| 147 |
+
print(f" [WARNING] MediapipeFaceDetector not available: {e}")
|
| 148 |
+
return None, False
|
| 149 |
+
# --- END NEW FUNCTION ---
|
| 150 |
+
|
| 151 |
def load_controlnets():
|
| 152 |
"""Load ControlNet models."""
|
| 153 |
print("Loading ControlNet Zoe Depth model...")
|
|
|
|
| 228 |
return pipe, False
|
| 229 |
|
| 230 |
|
| 231 |
+
def load_loras(pipe):
|
| 232 |
+
"""Load all LORAs from HuggingFace Hub."""
|
| 233 |
+
print("Loading all LORAs from HuggingFace Hub...")
|
| 234 |
+
loaded_loras = {}
|
| 235 |
+
|
| 236 |
+
lora_files = {
|
| 237 |
+
"retroart": MODEL_FILES.get("lora_retroart"),
|
| 238 |
+
"vga": MODEL_FILES.get("lora_vga"),
|
| 239 |
+
"lucasart": MODEL_FILES.get("lora_lucasart")
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
for adapter_name, filename in lora_files.items():
|
| 243 |
+
if not filename:
|
| 244 |
+
print(f" [INFO] No file specified for LORA '{adapter_name}', skipping.")
|
| 245 |
+
loaded_loras[adapter_name] = False
|
| 246 |
+
continue
|
| 247 |
+
|
| 248 |
+
try:
|
| 249 |
+
lora_path = download_model_with_retry(MODEL_REPO, filename)
|
| 250 |
+
pipe.load_lora_weights(lora_path, adapter_name=adapter_name)
|
| 251 |
+
print(f" [OK] LORA loaded successfully: {filename} as '{adapter_name}'")
|
| 252 |
+
loaded_loras[adapter_name] = True
|
| 253 |
+
except Exception as e:
|
| 254 |
+
print(f" [WARNING] Could not load LORA {filename}: {e}")
|
| 255 |
+
loaded_loras[adapter_name] = False
|
| 256 |
+
|
| 257 |
+
success = any(loaded_loras.values())
|
| 258 |
+
if not success:
|
| 259 |
+
print(" [WARNING] No LORAs were loaded successfully.")
|
| 260 |
+
|
| 261 |
+
return loaded_loras, success
|
| 262 |
|
| 263 |
|
| 264 |
def setup_ip_adapter(pipe, image_encoder):
|