Spaces:
Running
on
Zero
Running
on
Zero
Update optimizer.py
Browse files- optimizer.py +51 -45
optimizer.py
CHANGED
@@ -30,10 +30,7 @@ class UltraSupremeOptimizer:
|
|
30 |
self.usage_count = 0
|
31 |
self.device = self._get_device()
|
32 |
self.is_initialized = False
|
33 |
-
#
|
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,31 +44,21 @@ class UltraSupremeOptimizer:
|
|
47 |
return "cpu"
|
48 |
|
49 |
def initialize_model(self) -> bool:
|
50 |
-
"""Initialize the CLIP interrogator model
|
51 |
if self.is_initialized:
|
52 |
return True
|
53 |
|
54 |
try:
|
55 |
-
#
|
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
|
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
|
@@ -163,34 +150,54 @@ class UltraSupremeOptimizer:
|
|
163 |
|
164 |
@spaces.GPU
|
165 |
def run_clip_inference(self, image: Image.Image) -> Tuple[str, str, str]:
|
166 |
-
"""Solo la inferencia CLIP usa GPU
|
167 |
try:
|
168 |
-
# Mover
|
169 |
-
if
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
self.interrogator
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
-
#
|
178 |
-
|
179 |
-
|
180 |
-
|
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 |
|
186 |
except Exception as e:
|
187 |
logger.error(f"CLIP inference error: {e}")
|
188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
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,10 +220,9 @@ class UltraSupremeOptimizer:
|
|
213 |
|
214 |
start_time = datetime.now()
|
215 |
|
216 |
-
|
217 |
-
logger.info("ULTRA SUPREME ANALYSIS - Float32 máxima precisión")
|
218 |
|
219 |
-
# Ejecutar inferencia CLIP en GPU
|
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,7 +321,8 @@ class UltraSupremeOptimizer:
|
|
315 |
duration: float) -> str:
|
316 |
"""Generate detailed analysis report"""
|
317 |
|
318 |
-
gpu_status = "⚡ ZeroGPU
|
|
|
319 |
|
320 |
# Extraer información clave
|
321 |
detected_style = analysis.get("detected_style", "general").title()
|
@@ -323,14 +330,14 @@ class UltraSupremeOptimizer:
|
|
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 •
|
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:**
|
334 |
- **Quality:** Maximum resolution processing (1024px)
|
335 |
|
336 |
**📊 CLIP INTERROGATOR ANALYSIS:**
|
@@ -339,15 +346,14 @@ class UltraSupremeOptimizer:
|
|
339 |
- **Classic Analysis:** {analysis.get('clip_classic', '')[:80]}...
|
340 |
|
341 |
**⚡ OPTIMIZATION APPLIED:**
|
342 |
-
- ✅
|
343 |
-
- ✅ GPU
|
344 |
-
- ✅
|
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
|
352 |
|
353 |
return analysis_info
|
|
|
30 |
self.usage_count = 0
|
31 |
self.device = self._get_device()
|
32 |
self.is_initialized = False
|
33 |
+
# Inicializar modelo inmediatamente
|
|
|
|
|
|
|
34 |
self.initialize_model()
|
35 |
|
36 |
@staticmethod
|
|
|
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 |
+
# Configuración estándar sin forzar precisión
|
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
|
|
|
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 modelos a GPU sin forzar precisión
|
156 |
+
if self.device == "cuda":
|
157 |
+
# Configurar el dispositivo en el interrogator
|
158 |
+
self.interrogator.config.device = "cuda"
|
159 |
+
|
160 |
+
# Mover modelos a GPU manteniendo su precisión nativa
|
161 |
+
if hasattr(self.interrogator, 'clip_model') and self.interrogator.clip_model is not None:
|
162 |
+
self.interrogator.clip_model = self.interrogator.clip_model.to("cuda")
|
163 |
+
logger.info("CLIP model moved to GPU with native precision")
|
164 |
+
|
165 |
+
if hasattr(self.interrogator, 'blip_model') and self.interrogator.blip_model is not None:
|
166 |
+
self.interrogator.blip_model = self.interrogator.blip_model.to("cuda")
|
167 |
+
logger.info("BLIP model moved to GPU with native precision")
|
168 |
|
169 |
+
# Ejecutar inferencias CLIP con precisión nativa
|
170 |
+
full_prompt = self.interrogator.interrogate(image)
|
171 |
+
clip_fast = self.interrogator.interrogate_fast(image)
|
172 |
+
clip_classic = self.interrogator.interrogate_classic(image)
|
|
|
|
|
173 |
|
174 |
return full_prompt, clip_fast, clip_classic
|
175 |
|
176 |
except Exception as e:
|
177 |
logger.error(f"CLIP inference error: {e}")
|
178 |
+
# Si falla en GPU, intentar en CPU
|
179 |
+
if self.device == "cuda":
|
180 |
+
logger.info("Falling back to CPU inference")
|
181 |
+
self.interrogator.config.device = "cpu"
|
182 |
+
|
183 |
+
if hasattr(self.interrogator, 'clip_model') and self.interrogator.clip_model is not None:
|
184 |
+
self.interrogator.clip_model = self.interrogator.clip_model.to("cpu")
|
185 |
+
|
186 |
+
if hasattr(self.interrogator, 'blip_model') and self.interrogator.blip_model is not None:
|
187 |
+
self.interrogator.blip_model = self.interrogator.blip_model.to("cpu")
|
188 |
+
|
189 |
+
# Reintentar en CPU
|
190 |
+
full_prompt = self.interrogator.interrogate(image)
|
191 |
+
clip_fast = self.interrogator.interrogate_fast(image)
|
192 |
+
clip_classic = self.interrogator.interrogate_classic(image)
|
193 |
+
|
194 |
+
return full_prompt, clip_fast, clip_classic
|
195 |
+
else:
|
196 |
+
raise e
|
197 |
|
198 |
def generate_ultra_supreme_prompt(self, image: Any) -> Tuple[str, str, int, Dict[str, int]]:
|
199 |
"""
|
200 |
Generate ultra supreme prompt from image usando el pipeline completo
|
|
|
201 |
|
202 |
Returns:
|
203 |
Tuple of (prompt, analysis_info, score, breakdown)
|
|
|
220 |
|
221 |
start_time = datetime.now()
|
222 |
|
223 |
+
logger.info("ULTRA SUPREME ANALYSIS - Starting pipeline")
|
|
|
224 |
|
225 |
+
# Ejecutar inferencia CLIP en GPU
|
226 |
full_prompt, clip_fast, clip_classic = self.run_clip_inference(image)
|
227 |
|
228 |
logger.info(f"Prompt completo de CLIP Interrogator: {full_prompt}")
|
|
|
321 |
duration: float) -> str:
|
322 |
"""Generate detailed analysis report"""
|
323 |
|
324 |
+
gpu_status = "⚡ ZeroGPU" if torch.cuda.is_available() else "💻 CPU"
|
325 |
+
precision_info = "Native Model Precision" if torch.cuda.is_available() else "CPU Processing"
|
326 |
|
327 |
# Extraer información clave
|
328 |
detected_style = analysis.get("detected_style", "general").title()
|
|
|
330 |
base_prompt_preview = analysis.get("base_prompt", "")[:100] + "..." if len(analysis.get("base_prompt", "")) > 100 else analysis.get("base_prompt", "")
|
331 |
|
332 |
analysis_info = f"""**🚀 ULTRA SUPREME ANALYSIS COMPLETE**
|
333 |
+
**Processing:** {gpu_status} • {duration:.1f}s • {precision_info}
|
334 |
**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)})
|
335 |
**Generation:** #{self.usage_count}
|
336 |
|
337 |
**🧠 INTELLIGENT DETECTION:**
|
338 |
- **Detected Style:** {detected_style}
|
339 |
- **Main Subject:** {detected_subject}
|
340 |
+
- **Precision:** Using native model precision for optimal performance
|
341 |
- **Quality:** Maximum resolution processing (1024px)
|
342 |
|
343 |
**📊 CLIP INTERROGATOR ANALYSIS:**
|
|
|
346 |
- **Classic Analysis:** {analysis.get('clip_classic', '')[:80]}...
|
347 |
|
348 |
**⚡ OPTIMIZATION APPLIED:**
|
349 |
+
- ✅ Native precision inference for stability
|
350 |
+
- ✅ GPU acceleration when available
|
351 |
+
- ✅ Automatic fallback to CPU if needed
|
|
|
352 |
- ✅ Added professional camera specifications
|
353 |
- ✅ Enhanced lighting descriptions
|
354 |
- ✅ Applied Flux-specific optimizations
|
355 |
- ✅ Removed redundant/generic elements
|
356 |
|
357 |
+
**🔬 Powered by Pariente AI Research + CLIP Interrogator**"""
|
358 |
|
359 |
return analysis_info
|