fix: manejo de errores CUDA para FLUX.1-schnell y mapeo correcto de FLUX.1-Kontext-dev - Manejo específico para error NVML_SUCCESS - Fallback automático a FLUX.1-dev - Mapeo de Kontext-dev a Replicate
Browse files
app.py
CHANGED
|
@@ -225,20 +225,69 @@ def load_image_model(model_name):
|
|
| 225 |
print(f"❌ Error cargando FLUX: {e}")
|
| 226 |
print(f"🔍 Tipo de error: {type(e).__name__}")
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
# Si es un error de autenticación, dar instrucciones específicas
|
| 229 |
-
|
| 230 |
print("🔐 Error de autenticación. Asegúrate de:")
|
| 231 |
print(" 1. Tener acceso al modelo FLUX en Hugging Face")
|
| 232 |
print(" 2. Configurar HF_TOKEN en las variables de entorno del Space")
|
| 233 |
print(" 3. Que el token tenga permisos para acceder a modelos gated")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
-
#
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
| 242 |
|
| 243 |
# Configuración especial para SD 3.5 Large (Premium)
|
| 244 |
elif "stable-diffusion-3.5-large" in model_name:
|
|
|
|
| 225 |
print(f"❌ Error cargando FLUX: {e}")
|
| 226 |
print(f"🔍 Tipo de error: {type(e).__name__}")
|
| 227 |
|
| 228 |
+
# Manejo específico para error de CUDA en FLUX.1-schnell
|
| 229 |
+
if "NVML_SUCCESS" in str(e) or "CUDACachingAllocator" in str(e):
|
| 230 |
+
print("🚨 Error de CUDA detectado en FLUX.1-schnell")
|
| 231 |
+
print("🔧 Intentando solución alternativa...")
|
| 232 |
+
|
| 233 |
+
try:
|
| 234 |
+
# Limpiar memoria CUDA
|
| 235 |
+
if torch.cuda.is_available():
|
| 236 |
+
torch.cuda.empty_cache()
|
| 237 |
+
print("🧹 Memoria CUDA limpiada")
|
| 238 |
+
|
| 239 |
+
# Intentar cargar sin optimizaciones
|
| 240 |
+
pipe = FluxPipeline.from_pretrained(
|
| 241 |
+
model_name,
|
| 242 |
+
torch_dtype=torch.float32, # Usar float32 en lugar de float16
|
| 243 |
+
use_auth_token=HF_TOKEN,
|
| 244 |
+
device_map="auto" # Dejar que PyTorch maneje la asignación
|
| 245 |
+
)
|
| 246 |
+
print("✅ FLUX cargado con configuración alternativa")
|
| 247 |
+
|
| 248 |
+
except Exception as e2:
|
| 249 |
+
print(f"❌ Error persistente: {e2}")
|
| 250 |
+
print("🔄 Fallback a FLUX.1-dev...")
|
| 251 |
+
|
| 252 |
+
try:
|
| 253 |
+
pipe = FluxPipeline.from_pretrained(
|
| 254 |
+
"black-forest-labs/FLUX.1-dev",
|
| 255 |
+
torch_dtype=torch.float32,
|
| 256 |
+
use_auth_token=HF_TOKEN
|
| 257 |
+
)
|
| 258 |
+
print("✅ FLUX.1-dev cargado como fallback")
|
| 259 |
+
except Exception as e3:
|
| 260 |
+
print(f"❌ Error con FLUX.1-dev: {e3}")
|
| 261 |
+
print("🔄 Fallback final a Stable Diffusion...")
|
| 262 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 263 |
+
"CompVis/stable-diffusion-v1-4",
|
| 264 |
+
torch_dtype=torch_dtype,
|
| 265 |
+
safety_checker=None
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
# Si es un error de autenticación, dar instrucciones específicas
|
| 269 |
+
elif "401" in str(e) or "unauthorized" in str(e).lower():
|
| 270 |
print("🔐 Error de autenticación. Asegúrate de:")
|
| 271 |
print(" 1. Tener acceso al modelo FLUX en Hugging Face")
|
| 272 |
print(" 2. Configurar HF_TOKEN en las variables de entorno del Space")
|
| 273 |
print(" 3. Que el token tenga permisos para acceder a modelos gated")
|
| 274 |
+
|
| 275 |
+
# Fallback a Stable Diffusion
|
| 276 |
+
print("🔄 Fallback a Stable Diffusion...")
|
| 277 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 278 |
+
"CompVis/stable-diffusion-v1-4",
|
| 279 |
+
torch_dtype=torch_dtype,
|
| 280 |
+
safety_checker=None
|
| 281 |
+
)
|
| 282 |
|
| 283 |
+
# Otros errores
|
| 284 |
+
else:
|
| 285 |
+
print("🔄 Fallback a Stable Diffusion...")
|
| 286 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 287 |
+
"CompVis/stable-diffusion-v1-4",
|
| 288 |
+
torch_dtype=torch_dtype,
|
| 289 |
+
safety_checker=None
|
| 290 |
+
)
|
| 291 |
|
| 292 |
# Configuración especial para SD 3.5 Large (Premium)
|
| 293 |
elif "stable-diffusion-3.5-large" in model_name:
|