Spaces:
Running
on
Zero
Running
on
Zero
Update optimizer.py
Browse files- optimizer.py +46 -23
optimizer.py
CHANGED
|
@@ -30,7 +30,10 @@ class UltraSupremeOptimizer:
|
|
| 30 |
self.usage_count = 0
|
| 31 |
self.device = self._get_device()
|
| 32 |
self.is_initialized = False
|
| 33 |
-
#
|
|
|
|
|
|
|
|
|
|
| 34 |
self.initialize_model()
|
| 35 |
|
| 36 |
@staticmethod
|
|
@@ -44,21 +47,31 @@ class UltraSupremeOptimizer:
|
|
| 44 |
return "cpu"
|
| 45 |
|
| 46 |
def initialize_model(self) -> bool:
|
| 47 |
-
"""Initialize the CLIP interrogator model -
|
| 48 |
if self.is_initialized:
|
| 49 |
return True
|
| 50 |
|
| 51 |
try:
|
| 52 |
-
#
|
| 53 |
config = Config(
|
| 54 |
clip_model_name="ViT-L-14/openai",
|
| 55 |
download_cache=True,
|
| 56 |
chunk_size=2048,
|
| 57 |
quiet=True,
|
| 58 |
-
device="cpu" # Inicializar en CPU
|
| 59 |
)
|
| 60 |
|
| 61 |
self.interrogator = Interrogator(config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
self.is_initialized = True
|
| 63 |
|
| 64 |
# Clean up memory after initialization
|
|
@@ -86,8 +99,8 @@ class UltraSupremeOptimizer:
|
|
| 86 |
if image.mode != 'RGB':
|
| 87 |
image = image.convert('RGB')
|
| 88 |
|
| 89 |
-
# Resize if too large
|
| 90 |
-
max_size =
|
| 91 |
if image.size[0] > max_size or image.size[1] > max_size:
|
| 92 |
image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 93 |
|
|
@@ -150,16 +163,23 @@ class UltraSupremeOptimizer:
|
|
| 150 |
|
| 151 |
@spaces.GPU
|
| 152 |
def run_clip_inference(self, image: Image.Image) -> Tuple[str, str, str]:
|
| 153 |
-
"""Solo la inferencia CLIP usa GPU -
|
| 154 |
try:
|
| 155 |
-
# Mover modelo a GPU
|
| 156 |
if hasattr(self.interrogator, 'clip_model') and self.device == "cuda":
|
| 157 |
-
self.interrogator.clip_model = self.interrogator.clip_model.to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
#
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
| 163 |
|
| 164 |
return full_prompt, clip_fast, clip_classic
|
| 165 |
|
|
@@ -170,7 +190,7 @@ class UltraSupremeOptimizer:
|
|
| 170 |
def generate_ultra_supreme_prompt(self, image: Any) -> Tuple[str, str, int, Dict[str, int]]:
|
| 171 |
"""
|
| 172 |
Generate ultra supreme prompt from image usando el pipeline completo
|
| 173 |
-
|
| 174 |
|
| 175 |
Returns:
|
| 176 |
Tuple of (prompt, analysis_info, score, breakdown)
|
|
@@ -193,10 +213,10 @@ class UltraSupremeOptimizer:
|
|
| 193 |
|
| 194 |
start_time = datetime.now()
|
| 195 |
|
| 196 |
-
#
|
| 197 |
-
logger.info("ULTRA SUPREME ANALYSIS -
|
| 198 |
|
| 199 |
-
# Ejecutar inferencia CLIP en GPU
|
| 200 |
full_prompt, clip_fast, clip_classic = self.run_clip_inference(image)
|
| 201 |
|
| 202 |
logger.info(f"Prompt completo de CLIP Interrogator: {full_prompt}")
|
|
@@ -295,7 +315,7 @@ class UltraSupremeOptimizer:
|
|
| 295 |
duration: float) -> str:
|
| 296 |
"""Generate detailed analysis report"""
|
| 297 |
|
| 298 |
-
gpu_status = "⚡ ZeroGPU" if torch.cuda.is_available() else "💻 CPU"
|
| 299 |
|
| 300 |
# Extraer información clave
|
| 301 |
detected_style = analysis.get("detected_style", "general").title()
|
|
@@ -303,14 +323,15 @@ class UltraSupremeOptimizer:
|
|
| 303 |
base_prompt_preview = analysis.get("base_prompt", "")[:100] + "..." if len(analysis.get("base_prompt", "")) > 100 else analysis.get("base_prompt", "")
|
| 304 |
|
| 305 |
analysis_info = f"""**🚀 ULTRA SUPREME ANALYSIS COMPLETE**
|
| 306 |
-
**Processing:** {gpu_status} • {duration:.1f}s •
|
| 307 |
**Ultra Score:** {score}/100 • Breakdown: Base({breakdown.get('base_quality',0)}) Technical({breakdown.get('technical_enhancement',0)}) Lighting({breakdown.get('lighting_quality',0)}) Composition({breakdown.get('composition',0)})
|
| 308 |
**Generation:** #{self.usage_count}
|
| 309 |
|
| 310 |
**🧠 INTELLIGENT DETECTION:**
|
| 311 |
- **Detected Style:** {detected_style}
|
| 312 |
- **Main Subject:** {detected_subject}
|
| 313 |
-
- **
|
|
|
|
| 314 |
|
| 315 |
**📊 CLIP INTERROGATOR ANALYSIS:**
|
| 316 |
- **Base Prompt:** {base_prompt_preview}
|
|
@@ -318,13 +339,15 @@ class UltraSupremeOptimizer:
|
|
| 318 |
- **Classic Analysis:** {analysis.get('clip_classic', '')[:80]}...
|
| 319 |
|
| 320 |
**⚡ OPTIMIZATION APPLIED:**
|
| 321 |
-
- ✅
|
| 322 |
-
- ✅ GPU
|
|
|
|
|
|
|
| 323 |
- ✅ Added professional camera specifications
|
| 324 |
- ✅ Enhanced lighting descriptions
|
| 325 |
- ✅ Applied Flux-specific optimizations
|
| 326 |
- ✅ Removed redundant/generic elements
|
| 327 |
|
| 328 |
-
**🔬 Powered by Pariente AI Research + CLIP Interrogator**"""
|
| 329 |
|
| 330 |
return analysis_info
|
|
|
|
| 30 |
self.usage_count = 0
|
| 31 |
self.device = self._get_device()
|
| 32 |
self.is_initialized = False
|
| 33 |
+
# Forzar float32 en todo PyTorch
|
| 34 |
+
torch.backends.cuda.matmul.allow_tf32 = False
|
| 35 |
+
torch.backends.cudnn.allow_tf32 = False
|
| 36 |
+
# Inicializar modelo inmediatamente en CPU con float32
|
| 37 |
self.initialize_model()
|
| 38 |
|
| 39 |
@staticmethod
|
|
|
|
| 47 |
return "cpu"
|
| 48 |
|
| 49 |
def initialize_model(self) -> bool:
|
| 50 |
+
"""Initialize the CLIP interrogator model - FLOAT32 FORZADO"""
|
| 51 |
if self.is_initialized:
|
| 52 |
return True
|
| 53 |
|
| 54 |
try:
|
| 55 |
+
# FORZAR FLOAT32 EN TODO - Configuración máxima precisión
|
| 56 |
config = Config(
|
| 57 |
clip_model_name="ViT-L-14/openai",
|
| 58 |
download_cache=True,
|
| 59 |
chunk_size=2048,
|
| 60 |
quiet=True,
|
| 61 |
+
device="cpu" # Inicializar en CPU para controlar precisión
|
| 62 |
)
|
| 63 |
|
| 64 |
self.interrogator = Interrogator(config)
|
| 65 |
+
|
| 66 |
+
# FORZAR FLOAT32 EN TODOS LOS COMPONENTES DEL MODELO
|
| 67 |
+
if hasattr(self.interrogator, 'clip_model') and self.interrogator.clip_model is not None:
|
| 68 |
+
self.interrogator.clip_model = self.interrogator.clip_model.float()
|
| 69 |
+
logger.info("CLIP model forced to float32")
|
| 70 |
+
|
| 71 |
+
if hasattr(self.interrogator, 'blip_model') and self.interrogator.blip_model is not None:
|
| 72 |
+
self.interrogator.blip_model = self.interrogator.blip_model.float()
|
| 73 |
+
logger.info("BLIP model forced to float32")
|
| 74 |
+
|
| 75 |
self.is_initialized = True
|
| 76 |
|
| 77 |
# Clean up memory after initialization
|
|
|
|
| 99 |
if image.mode != 'RGB':
|
| 100 |
image = image.convert('RGB')
|
| 101 |
|
| 102 |
+
# Resize if too large - usar tamaño generoso para máxima calidad
|
| 103 |
+
max_size = 1024 if self.device != "cpu" else 768
|
| 104 |
if image.size[0] > max_size or image.size[1] > max_size:
|
| 105 |
image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 106 |
|
|
|
|
| 163 |
|
| 164 |
@spaces.GPU
|
| 165 |
def run_clip_inference(self, image: Image.Image) -> Tuple[str, str, str]:
|
| 166 |
+
"""Solo la inferencia CLIP usa GPU - FLOAT32 FORZADO"""
|
| 167 |
try:
|
| 168 |
+
# Mover modelo a GPU MANTENIENDO FLOAT32
|
| 169 |
if hasattr(self.interrogator, 'clip_model') and self.device == "cuda":
|
| 170 |
+
self.interrogator.clip_model = self.interrogator.clip_model.to("cuda").float()
|
| 171 |
+
logger.info("CLIP model moved to GPU with float32 precision")
|
| 172 |
+
|
| 173 |
+
if hasattr(self.interrogator, 'blip_model') and self.device == "cuda":
|
| 174 |
+
self.interrogator.blip_model = self.interrogator.blip_model.to("cuda").float()
|
| 175 |
+
logger.info("BLIP model moved to GPU with float32 precision")
|
| 176 |
|
| 177 |
+
# FORZAR que las inferencias usen float32
|
| 178 |
+
with torch.cuda.amp.autocast(enabled=False): # Deshabilitar autocast para forzar float32
|
| 179 |
+
# Ejecutar inferencias CLIP en máxima precisión
|
| 180 |
+
full_prompt = self.interrogator.interrogate(image)
|
| 181 |
+
clip_fast = self.interrogator.interrogate_fast(image)
|
| 182 |
+
clip_classic = self.interrogator.interrogate_classic(image)
|
| 183 |
|
| 184 |
return full_prompt, clip_fast, clip_classic
|
| 185 |
|
|
|
|
| 190 |
def generate_ultra_supreme_prompt(self, image: Any) -> Tuple[str, str, int, Dict[str, int]]:
|
| 191 |
"""
|
| 192 |
Generate ultra supreme prompt from image usando el pipeline completo
|
| 193 |
+
MÁXIMA PRECISIÓN FLOAT32 EN TODO
|
| 194 |
|
| 195 |
Returns:
|
| 196 |
Tuple of (prompt, analysis_info, score, breakdown)
|
|
|
|
| 213 |
|
| 214 |
start_time = datetime.now()
|
| 215 |
|
| 216 |
+
# PIPELINE CON MÁXIMA PRECISIÓN FLOAT32
|
| 217 |
+
logger.info("ULTRA SUPREME ANALYSIS - Float32 máxima precisión")
|
| 218 |
|
| 219 |
+
# Ejecutar inferencia CLIP en GPU con float32 forzado
|
| 220 |
full_prompt, clip_fast, clip_classic = self.run_clip_inference(image)
|
| 221 |
|
| 222 |
logger.info(f"Prompt completo de CLIP Interrogator: {full_prompt}")
|
|
|
|
| 315 |
duration: float) -> str:
|
| 316 |
"""Generate detailed analysis report"""
|
| 317 |
|
| 318 |
+
gpu_status = "⚡ ZeroGPU (Float32)" if torch.cuda.is_available() else "💻 CPU (Float32)"
|
| 319 |
|
| 320 |
# Extraer información clave
|
| 321 |
detected_style = analysis.get("detected_style", "general").title()
|
|
|
|
| 323 |
base_prompt_preview = analysis.get("base_prompt", "")[:100] + "..." if len(analysis.get("base_prompt", "")) > 100 else analysis.get("base_prompt", "")
|
| 324 |
|
| 325 |
analysis_info = f"""**🚀 ULTRA SUPREME ANALYSIS COMPLETE**
|
| 326 |
+
**Processing:** {gpu_status} • {duration:.1f}s • Maximum Precision Pipeline
|
| 327 |
**Ultra Score:** {score}/100 • Breakdown: Base({breakdown.get('base_quality',0)}) Technical({breakdown.get('technical_enhancement',0)}) Lighting({breakdown.get('lighting_quality',0)}) Composition({breakdown.get('composition',0)})
|
| 328 |
**Generation:** #{self.usage_count}
|
| 329 |
|
| 330 |
**🧠 INTELLIGENT DETECTION:**
|
| 331 |
- **Detected Style:** {detected_style}
|
| 332 |
- **Main Subject:** {detected_subject}
|
| 333 |
+
- **Precision:** Float32 máxima precisión en CPU+GPU
|
| 334 |
+
- **Quality:** Maximum resolution processing (1024px)
|
| 335 |
|
| 336 |
**📊 CLIP INTERROGATOR ANALYSIS:**
|
| 337 |
- **Base Prompt:** {base_prompt_preview}
|
|
|
|
| 339 |
- **Classic Analysis:** {analysis.get('clip_classic', '')[:80]}...
|
| 340 |
|
| 341 |
**⚡ OPTIMIZATION APPLIED:**
|
| 342 |
+
- ✅ Float32 forzado en todos los modelos
|
| 343 |
+
- ✅ GPU inference con máxima precisión
|
| 344 |
+
- ✅ TensorFloat-32 deshabilitado
|
| 345 |
+
- ✅ Mixed precision deshabilitado
|
| 346 |
- ✅ Added professional camera specifications
|
| 347 |
- ✅ Enhanced lighting descriptions
|
| 348 |
- ✅ Applied Flux-specific optimizations
|
| 349 |
- ✅ Removed redundant/generic elements
|
| 350 |
|
| 351 |
+
**🔬 Powered by Pariente AI Research + CLIP Interrogator (Float32 Max)**"""
|
| 352 |
|
| 353 |
return analysis_info
|