Spaces:
Runtime error
Runtime error
Update models.py
Browse files
models.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
-
Model loading for Pixagram -
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
import torch
|
| 6 |
import time
|
|
@@ -34,38 +34,31 @@ def download_model_with_retry(repo_id, filename, max_retries=None):
|
|
| 34 |
|
| 35 |
for attempt in range(max_retries):
|
| 36 |
try:
|
| 37 |
-
print(f" Attempting download {filename} (attempt {attempt + 1}/{max_retries})...")
|
| 38 |
-
|
| 39 |
kwargs = {"repo_type": "model"}
|
| 40 |
if HUGGINGFACE_TOKEN:
|
| 41 |
kwargs["token"] = HUGGINGFACE_TOKEN
|
| 42 |
|
| 43 |
path = hf_hub_download(repo_id=repo_id, filename=filename, **kwargs)
|
| 44 |
-
print(f" [OK] Downloaded: {filename}")
|
| 45 |
return path
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
-
print(f" [WARNING] Attempt {attempt + 1} failed: {e}")
|
| 49 |
if attempt < max_retries - 1:
|
| 50 |
-
print(f" Retrying in {DOWNLOAD_CONFIG['retry_delay']} seconds...")
|
| 51 |
time.sleep(DOWNLOAD_CONFIG['retry_delay'])
|
| 52 |
else:
|
| 53 |
-
print(f" [ERROR] Failed after {max_retries} attempts")
|
| 54 |
raise
|
| 55 |
return None
|
| 56 |
|
| 57 |
|
| 58 |
def load_face_analysis():
|
| 59 |
-
"""Load face analysis -
|
| 60 |
print("Loading face analysis...")
|
| 61 |
try:
|
| 62 |
snapshot_download(
|
| 63 |
repo_id=FACE_DETECTION_CONFIG['download_repo'],
|
| 64 |
local_dir=FACE_DETECTION_CONFIG['local_dir']
|
| 65 |
)
|
| 66 |
-
print(" [OK] Antelopev2 downloaded")
|
| 67 |
|
| 68 |
-
#
|
| 69 |
app = FaceAnalysis(name='antelopev2', root='/data', providers=['CPUExecutionProvider'])
|
| 70 |
app.prepare(ctx_id=0, det_size=(640, 640))
|
| 71 |
|
|
@@ -81,7 +74,7 @@ def load_depth_detector():
|
|
| 81 |
print("Loading Zoe Depth...")
|
| 82 |
try:
|
| 83 |
zoe = ZoeDetector.from_pretrained("lllyasviel/Annotators")
|
| 84 |
-
zoe = zoe.to("cpu")
|
| 85 |
print(" [OK] Zoe Depth loaded")
|
| 86 |
return zoe, True
|
| 87 |
except Exception as e:
|
|
@@ -117,123 +110,107 @@ def load_sdxl_pipeline(controlnets):
|
|
| 117 |
print("Loading SDXL pipeline...")
|
| 118 |
|
| 119 |
# Load VAE (line 128)
|
| 120 |
-
print(" Loading VAE...")
|
| 121 |
vae = AutoencoderKL.from_pretrained(
|
| 122 |
"madebyollin/sdxl-vae-fp16-fix",
|
| 123 |
torch_dtype=dtype
|
| 124 |
)
|
| 125 |
print(" [OK] VAE loaded")
|
| 126 |
|
| 127 |
-
# Load pipeline (line 134) -
|
| 128 |
-
print(" Creating pipeline...")
|
| 129 |
pipe = StableDiffusionXLInstantIDImg2ImgPipeline.from_pretrained(
|
| 130 |
"frankjoshua/albedobaseXL_v21",
|
| 131 |
vae=vae,
|
| 132 |
-
controlnet=controlnets, # Direct list
|
| 133 |
torch_dtype=dtype
|
| 134 |
)
|
| 135 |
|
| 136 |
-
#
|
| 137 |
-
print(" Setting up LCM scheduler...")
|
| 138 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
|
|
|
| 139 |
|
| 140 |
# Load IP-Adapter (line 139)
|
| 141 |
-
print(" Loading IP-Adapter...")
|
| 142 |
ip_adapter_path = download_model_with_retry("InstantX/InstantID", "ip-adapter.bin")
|
| 143 |
pipe.load_ip_adapter_instantid(ip_adapter_path)
|
| 144 |
-
pipe.set_ip_adapter_scale(0.8)
|
|
|
|
| 145 |
|
| 146 |
# Move to device
|
| 147 |
pipe = pipe.to(device)
|
| 148 |
|
| 149 |
-
print(" [OK] Pipeline
|
| 150 |
return pipe, True
|
| 151 |
|
| 152 |
|
| 153 |
-
# Global LoRA
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
last_fused = False
|
| 157 |
|
| 158 |
|
| 159 |
def load_lora(pipe):
|
| 160 |
"""
|
| 161 |
-
Load LoRA
|
| 162 |
-
KEY: Load as state_dict, NOT path!
|
| 163 |
"""
|
| 164 |
-
print("Loading LoRA
|
| 165 |
-
global
|
| 166 |
|
| 167 |
try:
|
| 168 |
lora_path = download_model_with_retry(MODEL_REPO, MODEL_FILES['lora'])
|
|
|
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
loaded_lora_state_dict = load_file(lora_path)
|
| 173 |
-
else:
|
| 174 |
-
loaded_lora_state_dict = torch.load(lora_path)
|
| 175 |
-
|
| 176 |
-
print(" [OK] LoRA state_dict loaded")
|
| 177 |
return True
|
| 178 |
except Exception as e:
|
| 179 |
print(f" [WARNING] LoRA load failed: {e}")
|
| 180 |
-
|
| 181 |
return False
|
| 182 |
|
| 183 |
|
| 184 |
def fuse_lora_with_scale(pipe, lora_scale):
|
| 185 |
"""
|
| 186 |
-
Fuse LoRA with scale
|
| 187 |
-
|
| 188 |
-
examplewithface.py calls fuse_lora(lora_scale) but that's old API.
|
| 189 |
-
Modern API: load → set_adapters → fuse
|
| 190 |
"""
|
| 191 |
-
global
|
| 192 |
|
| 193 |
-
if
|
| 194 |
-
print(" [WARNING] No LoRA
|
| 195 |
return False
|
| 196 |
|
| 197 |
try:
|
| 198 |
-
#
|
| 199 |
-
if
|
| 200 |
-
print(" [LORA]
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
except:
|
| 204 |
-
pass
|
| 205 |
try:
|
| 206 |
pipe.unload_lora_weights()
|
| 207 |
except:
|
| 208 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
# Load state_dict with adapter name
|
| 211 |
-
print(" [LORA] Loading state_dict...")
|
| 212 |
-
pipe.load_lora_weights(loaded_lora_state_dict, adapter_name="pixel_lora")
|
| 213 |
-
|
| 214 |
-
# Set scale using modern API
|
| 215 |
-
print(f" [LORA] Setting scale to {lora_scale}...")
|
| 216 |
-
try:
|
| 217 |
-
pipe.set_adapters(["pixel_lora"], adapter_weights=[lora_scale])
|
| 218 |
-
except AttributeError:
|
| 219 |
-
# If set_adapters doesn't exist, scale will be 1.0
|
| 220 |
-
print(" [INFO] set_adapters not available, using scale 1.0")
|
| 221 |
-
|
| 222 |
-
# Fuse - NO scale argument
|
| 223 |
-
print(f" [LORA] Fusing...")
|
| 224 |
-
pipe.fuse_lora()
|
| 225 |
-
|
| 226 |
-
last_fused = True
|
| 227 |
-
print(f" [OK] LoRA fused with scale {lora_scale}")
|
| 228 |
return True
|
| 229 |
|
| 230 |
except Exception as e:
|
| 231 |
-
print(f" [ERROR] LoRA
|
| 232 |
import traceback
|
| 233 |
traceback.print_exc()
|
| 234 |
return False
|
| 235 |
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
def setup_compel(pipe):
|
| 238 |
"""Setup Compel - examplewithface.py line 145"""
|
| 239 |
print("Setting up Compel...")
|
|
@@ -297,6 +274,6 @@ def set_clip_skip(pipe):
|
|
| 297 |
print(f" [OK] CLIP skip set to {CLIP_SKIP}")
|
| 298 |
|
| 299 |
|
| 300 |
-
__all__ = ['draw_kps', 'fuse_lora_with_scale', '
|
| 301 |
|
| 302 |
-
print("[OK] Models ready (examplewithface.py pattern + modern
|
|
|
|
| 1 |
"""
|
| 2 |
+
Model loading for Pixagram - WORKING VERSION
|
| 3 |
+
Following examplewithface.py pattern with modern diffusers compatibility
|
| 4 |
"""
|
| 5 |
import torch
|
| 6 |
import time
|
|
|
|
| 34 |
|
| 35 |
for attempt in range(max_retries):
|
| 36 |
try:
|
|
|
|
|
|
|
| 37 |
kwargs = {"repo_type": "model"}
|
| 38 |
if HUGGINGFACE_TOKEN:
|
| 39 |
kwargs["token"] = HUGGINGFACE_TOKEN
|
| 40 |
|
| 41 |
path = hf_hub_download(repo_id=repo_id, filename=filename, **kwargs)
|
|
|
|
| 42 |
return path
|
| 43 |
|
| 44 |
except Exception as e:
|
|
|
|
| 45 |
if attempt < max_retries - 1:
|
|
|
|
| 46 |
time.sleep(DOWNLOAD_CONFIG['retry_delay'])
|
| 47 |
else:
|
|
|
|
| 48 |
raise
|
| 49 |
return None
|
| 50 |
|
| 51 |
|
| 52 |
def load_face_analysis():
|
| 53 |
+
"""Load face analysis - examplewithface.py line 113"""
|
| 54 |
print("Loading face analysis...")
|
| 55 |
try:
|
| 56 |
snapshot_download(
|
| 57 |
repo_id=FACE_DETECTION_CONFIG['download_repo'],
|
| 58 |
local_dir=FACE_DETECTION_CONFIG['local_dir']
|
| 59 |
)
|
|
|
|
| 60 |
|
| 61 |
+
# examplewithface.py line 113
|
| 62 |
app = FaceAnalysis(name='antelopev2', root='/data', providers=['CPUExecutionProvider'])
|
| 63 |
app.prepare(ctx_id=0, det_size=(640, 640))
|
| 64 |
|
|
|
|
| 74 |
print("Loading Zoe Depth...")
|
| 75 |
try:
|
| 76 |
zoe = ZoeDetector.from_pretrained("lllyasviel/Annotators")
|
| 77 |
+
zoe = zoe.to("cpu")
|
| 78 |
print(" [OK] Zoe Depth loaded")
|
| 79 |
return zoe, True
|
| 80 |
except Exception as e:
|
|
|
|
| 110 |
print("Loading SDXL pipeline...")
|
| 111 |
|
| 112 |
# Load VAE (line 128)
|
|
|
|
| 113 |
vae = AutoencoderKL.from_pretrained(
|
| 114 |
"madebyollin/sdxl-vae-fp16-fix",
|
| 115 |
torch_dtype=dtype
|
| 116 |
)
|
| 117 |
print(" [OK] VAE loaded")
|
| 118 |
|
| 119 |
+
# Load pipeline (line 134) - controlnets as list!
|
|
|
|
| 120 |
pipe = StableDiffusionXLInstantIDImg2ImgPipeline.from_pretrained(
|
| 121 |
"frankjoshua/albedobaseXL_v21",
|
| 122 |
vae=vae,
|
| 123 |
+
controlnet=controlnets, # Direct list!
|
| 124 |
torch_dtype=dtype
|
| 125 |
)
|
| 126 |
|
| 127 |
+
# LCM scheduler (user requested LCM)
|
|
|
|
| 128 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 129 |
+
print(" [OK] LCM scheduler set")
|
| 130 |
|
| 131 |
# Load IP-Adapter (line 139)
|
|
|
|
| 132 |
ip_adapter_path = download_model_with_retry("InstantX/InstantID", "ip-adapter.bin")
|
| 133 |
pipe.load_ip_adapter_instantid(ip_adapter_path)
|
| 134 |
+
pipe.set_ip_adapter_scale(0.8)
|
| 135 |
+
print(" [OK] IP-Adapter loaded")
|
| 136 |
|
| 137 |
# Move to device
|
| 138 |
pipe = pipe.to(device)
|
| 139 |
|
| 140 |
+
print(" [OK] Pipeline ready")
|
| 141 |
return pipe, True
|
| 142 |
|
| 143 |
|
| 144 |
+
# Global LoRA tracking
|
| 145 |
+
loaded_lora_path = None
|
| 146 |
+
current_lora_scale = None
|
|
|
|
| 147 |
|
| 148 |
|
| 149 |
def load_lora(pipe):
|
| 150 |
"""
|
| 151 |
+
Load LoRA - Don't fuse yet, will fuse per-generation
|
|
|
|
| 152 |
"""
|
| 153 |
+
print("Loading LoRA...")
|
| 154 |
+
global loaded_lora_path
|
| 155 |
|
| 156 |
try:
|
| 157 |
lora_path = download_model_with_retry(MODEL_REPO, MODEL_FILES['lora'])
|
| 158 |
+
loaded_lora_path = lora_path
|
| 159 |
|
| 160 |
+
print(f" [OK] LoRA path stored: {lora_path}")
|
| 161 |
+
print(f" [INFO] LoRA will be fused before each generation")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
return True
|
| 163 |
except Exception as e:
|
| 164 |
print(f" [WARNING] LoRA load failed: {e}")
|
| 165 |
+
loaded_lora_path = None
|
| 166 |
return False
|
| 167 |
|
| 168 |
|
| 169 |
def fuse_lora_with_scale(pipe, lora_scale):
|
| 170 |
"""
|
| 171 |
+
Fuse LoRA with scale for generation
|
| 172 |
+
Modern approach: Don't fuse, use cross_attention_kwargs instead
|
|
|
|
|
|
|
| 173 |
"""
|
| 174 |
+
global loaded_lora_path, current_lora_scale
|
| 175 |
|
| 176 |
+
if loaded_lora_path is None:
|
| 177 |
+
print(" [WARNING] No LoRA available")
|
| 178 |
return False
|
| 179 |
|
| 180 |
try:
|
| 181 |
+
# Check if we need to reload
|
| 182 |
+
if current_lora_scale is None or current_lora_scale != lora_scale:
|
| 183 |
+
print(f" [LORA] Loading LoRA with scale {lora_scale}...")
|
| 184 |
+
|
| 185 |
+
# Unload previous if exists
|
|
|
|
|
|
|
| 186 |
try:
|
| 187 |
pipe.unload_lora_weights()
|
| 188 |
except:
|
| 189 |
pass
|
| 190 |
+
|
| 191 |
+
# Load LoRA weights from path
|
| 192 |
+
pipe.load_lora_weights(loaded_lora_path)
|
| 193 |
+
current_lora_scale = lora_scale
|
| 194 |
+
|
| 195 |
+
print(f" [OK] LoRA loaded with scale {lora_scale}")
|
| 196 |
+
print(f" [INFO] Scale will be applied via cross_attention_kwargs at inference")
|
| 197 |
+
else:
|
| 198 |
+
print(f" [INFO] LoRA already loaded with scale {lora_scale}")
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
return True
|
| 201 |
|
| 202 |
except Exception as e:
|
| 203 |
+
print(f" [ERROR] LoRA loading failed: {e}")
|
| 204 |
import traceback
|
| 205 |
traceback.print_exc()
|
| 206 |
return False
|
| 207 |
|
| 208 |
|
| 209 |
+
def get_lora_scale():
|
| 210 |
+
"""Get current LoRA scale for cross_attention_kwargs"""
|
| 211 |
+
return current_lora_scale if current_lora_scale is not None else 1.0
|
| 212 |
+
|
| 213 |
+
|
| 214 |
def setup_compel(pipe):
|
| 215 |
"""Setup Compel - examplewithface.py line 145"""
|
| 216 |
print("Setting up Compel...")
|
|
|
|
| 274 |
print(f" [OK] CLIP skip set to {CLIP_SKIP}")
|
| 275 |
|
| 276 |
|
| 277 |
+
__all__ = ['draw_kps', 'fuse_lora_with_scale', 'get_lora_scale']
|
| 278 |
|
| 279 |
+
print("[OK] Models ready (examplewithface.py pattern + modern API)")
|