Malaji71 commited on
Commit
d99e44d
·
verified ·
1 Parent(s): 901a1c4

Update optimizer.py

Browse files
Files changed (1) hide show
  1. optimizer.py +137 -61
optimizer.py CHANGED
@@ -279,7 +279,7 @@ class UltraSupremeOptimizer:
279
 
280
  start_time = datetime.now()
281
 
282
- logger.info("ULTRA SUPREME ANALYSIS - Starting pipeline")
283
 
284
  # Ejecutar inferencia CLIP
285
  full_prompt, clip_fast, clip_classic = self.run_clip_inference(image)
@@ -291,32 +291,47 @@ class UltraSupremeOptimizer:
291
  clip_fast = "image"
292
  clip_classic = "picture"
293
 
294
- logger.info(f"Prompt completo: {full_prompt[:100]}...")
295
- logger.info(f"Fast: {clip_fast[:50]}...")
296
- logger.info(f"Classic: {clip_classic[:50]}...")
297
-
298
- # Aplicar reglas de Flux al prompt completo
299
- optimized_prompt = self.apply_flux_rules(full_prompt)
300
-
301
- # Crear análisis para el reporte
302
- analysis_summary = {
303
- "base_prompt": full_prompt,
304
- "clip_fast": clip_fast,
305
- "clip_classic": clip_classic,
306
- "optimized": optimized_prompt,
307
- "detected_style": self._detect_style(full_prompt),
308
- "detected_subject": self._detect_subject(full_prompt)
309
- }
310
-
311
- # Calcular score
312
- score = self._calculate_score(optimized_prompt, full_prompt)
313
- breakdown = {
314
- "base_quality": min(len(full_prompt) // 10, 25),
315
- "technical_enhancement": 25 if "Shot on" in optimized_prompt else 0,
316
- "lighting_quality": 25 if "lighting" in optimized_prompt.lower() else 0,
317
- "composition": 25 if any(word in optimized_prompt.lower() for word in ["professional", "masterful", "epic"]) else 0
318
- }
319
- score = sum(breakdown.values())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
320
 
321
  end_time = datetime.now()
322
  duration = (end_time - start_time).total_seconds()
@@ -326,9 +341,9 @@ class UltraSupremeOptimizer:
326
  if torch.cuda.is_available():
327
  torch.cuda.empty_cache()
328
 
329
- # Generate analysis report
330
- analysis_info = self._generate_analysis_report(
331
- analysis_summary, score, breakdown, duration
332
  )
333
 
334
  return optimized_prompt, analysis_info, score, breakdown
@@ -386,45 +401,106 @@ class UltraSupremeOptimizer:
386
 
387
  return min(score, 100)
388
 
389
- def _generate_analysis_report(self, analysis: Dict[str, Any],
390
- score: int, breakdown: Dict[str, int],
391
- duration: float) -> str:
392
- """Generate detailed analysis report"""
393
 
394
  device_used = "cuda" if torch.cuda.is_available() else "cpu"
395
  gpu_status = "⚡ ZeroGPU" if device_used == "cuda" else "💻 CPU"
396
- precision_info = "Half Precision (FP16)" if device_used == "cuda" else "Full Precision (FP32)"
397
 
398
- # Extraer información clave
399
- detected_style = analysis.get("detected_style", "general").title()
400
- detected_subject = analysis.get("detected_subject", "Unknown")
401
- base_prompt_preview = analysis.get("base_prompt", "")[:100] + "..." if len(analysis.get("base_prompt", "")) > 100 else analysis.get("base_prompt", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402
 
403
- analysis_info = f"""**🚀 ULTRA SUPREME ANALYSIS COMPLETE**
404
- **Processing:** {gpu_status} {duration:.1f}s {precision_info}
405
- **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)})
406
- **Generation:** #{self.usage_count}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
407
 
408
- **🧠 INTELLIGENT DETECTION:**
409
- - **Detected Style:** {detected_style}
410
- - **Main Subject:** {detected_subject}
411
- - **Precision:** Using {precision_info} for optimal performance
412
- - **Quality:** Maximum resolution processing (768px)
413
 
414
- **📊 CLIP INTERROGATOR ANALYSIS:**
415
- - **Base Prompt:** {base_prompt_preview}
416
- - **Fast Analysis:** {analysis.get('clip_fast', '')[:80]}...
417
- - **Classic Analysis:** {analysis.get('clip_classic', '')[:80]}...
 
418
 
419
- **⚡ OPTIMIZATION APPLIED:**
420
- - Mixed precision handling for stability
421
- - Automatic GPU/CPU fallback
422
- - Memory-efficient processing
423
- - Added professional camera specifications
424
- - Enhanced lighting descriptions
425
- - ✅ Applied Flux-specific optimizations
426
- - ✅ Removed redundant/generic elements
427
 
428
- **🔬 Powered by Pariente AI Research + CLIP Interrogator**"""
429
 
430
  return analysis_info
 
279
 
280
  start_time = datetime.now()
281
 
282
+ logger.info("ULTRA SUPREME ANALYSIS - Starting complete pipeline with multi-model analysis")
283
 
284
  # Ejecutar inferencia CLIP
285
  full_prompt, clip_fast, clip_classic = self.run_clip_inference(image)
 
291
  clip_fast = "image"
292
  clip_classic = "picture"
293
 
294
+ logger.info(f"CLIP complete prompt: {full_prompt[:100]}...")
295
+
296
+ # NUEVO: Ejecutar análisis ultra supremo con múltiples modelos
297
+ logger.info("Running multi-model ultra supreme analysis...")
298
+ ultra_analysis = self.analyzer.ultra_supreme_analysis(
299
+ image, clip_fast, clip_classic, full_prompt
300
+ )
301
+
302
+ # Construir prompt mejorado basado en análisis completo
303
+ enhanced_prompt_parts = []
304
+
305
+ # Base prompt de CLIP
306
+ enhanced_prompt_parts.append(full_prompt)
307
+
308
+ # Agregar información demográfica si está disponible
309
+ if ultra_analysis["demographic"]["gender"] and ultra_analysis["demographic"]["gender_confidence"] > 0.7:
310
+ gender = ultra_analysis["demographic"]["gender"]
311
+ age_cat = ultra_analysis["demographic"]["age_category"]
312
+ if age_cat:
313
+ enhanced_prompt_parts.append(f"{age_cat} {gender}")
314
+
315
+ # Agregar estado emocional principal
316
+ if ultra_analysis["emotional_state"]["primary_emotion"] and ultra_analysis["emotional_state"]["emotion_confidence"] > 0.6:
317
+ emotion = ultra_analysis["emotional_state"]["primary_emotion"]
318
+ enhanced_prompt_parts.append(f"{emotion} expression")
319
+
320
+ # Agregar información de pose si está disponible
321
+ if ultra_analysis["pose_composition"]["posture"]:
322
+ enhanced_prompt_parts.append(ultra_analysis["pose_composition"]["posture"][0])
323
+
324
+ # Combinar y aplicar reglas de Flux
325
+ combined_prompt = ", ".join(enhanced_prompt_parts)
326
+ optimized_prompt = self.apply_flux_rules(combined_prompt)
327
+
328
+ # Si el analyzer enriqueció el prompt, úsalo
329
+ analyzer_prompt = self.analyzer.build_ultra_supreme_prompt(ultra_analysis, [full_prompt])
330
+ if len(analyzer_prompt) > len(optimized_prompt):
331
+ optimized_prompt = self.apply_flux_rules(analyzer_prompt)
332
+
333
+ # Calcular score usando el analyzer
334
+ score, breakdown = self.analyzer.calculate_ultra_supreme_score(optimized_prompt, ultra_analysis)
335
 
336
  end_time = datetime.now()
337
  duration = (end_time - start_time).total_seconds()
 
341
  if torch.cuda.is_available():
342
  torch.cuda.empty_cache()
343
 
344
+ # Generate enhanced analysis report con datos de múltiples modelos
345
+ analysis_info = self._generate_ultra_analysis_report(
346
+ ultra_analysis, score, breakdown, duration
347
  )
348
 
349
  return optimized_prompt, analysis_info, score, breakdown
 
401
 
402
  return min(score, 100)
403
 
404
+ def _generate_ultra_analysis_report(self, analysis: Dict[str, Any],
405
+ score: int, breakdown: Dict[str, int],
406
+ duration: float) -> str:
407
+ """Generate ultra detailed analysis report with multi-model results"""
408
 
409
  device_used = "cuda" if torch.cuda.is_available() else "cpu"
410
  gpu_status = "⚡ ZeroGPU" if device_used == "cuda" else "💻 CPU"
 
411
 
412
+ # Demographic info
413
+ demo_info = ""
414
+ if analysis["demographic"]["age_category"]:
415
+ age = analysis["demographic"]["age_category"].replace("_", " ").title()
416
+ gender = analysis["demographic"]["gender"] or "person"
417
+ confidence = analysis["demographic"]["age_confidence"]
418
+ demo_info = f"**Detected:** {age} {gender} (confidence: {confidence:.0%})"
419
+
420
+ # Emotion info
421
+ emotion_info = ""
422
+ if analysis["emotional_state"]["primary_emotion"]:
423
+ emotion = analysis["emotional_state"]["primary_emotion"]
424
+ confidence = analysis["emotional_state"]["emotion_confidence"]
425
+ emotion_info = f"**Primary Emotion:** {emotion} ({confidence:.0%})"
426
+
427
+ # Add emotion distribution if available
428
+ if analysis["emotional_state"]["emotion_distribution"]:
429
+ top_emotions = sorted(
430
+ analysis["emotional_state"]["emotion_distribution"].items(),
431
+ key=lambda x: x[1], reverse=True
432
+ )[:3]
433
+ emotion_details = ", ".join([f"{e[0]}: {e[1]:.0%}" for e in top_emotions])
434
+ emotion_info += f"\n**Emotion Distribution:** {emotion_details}"
435
 
436
+ # Face analysis info
437
+ face_info = f"**Faces Detected:** {analysis['facial_ultra']['face_count']}"
438
+ if analysis['facial_ultra']['face_count'] > 0:
439
+ features = []
440
+ for feature_type in ['eyes', 'mouth', 'facial_hair', 'skin']:
441
+ if analysis['facial_ultra'].get(feature_type):
442
+ features.extend(analysis['facial_ultra'][feature_type])
443
+ if features:
444
+ face_info += f"\n**Facial Features:** {', '.join(features[:5])}"
445
+
446
+ # Pose info
447
+ pose_info = ""
448
+ if analysis["pose_composition"].get("pose_confidence", 0) > 0:
449
+ confidence = analysis["pose_composition"]["pose_confidence"]
450
+ pose_info = f"**Pose Analysis:** Body detected ({confidence:.0%} confidence)"
451
+ if analysis["pose_composition"]["posture"]:
452
+ pose_info += f"\n**Posture:** {', '.join(analysis['pose_composition']['posture'])}"
453
+
454
+ # Environment info
455
+ env_info = ""
456
+ if analysis["environmental"]["setting_type"]:
457
+ env_info = f"**Setting:** {analysis['environmental']['setting_type'].replace('_', ' ').title()}"
458
+ if analysis["environmental"]["lighting_analysis"]:
459
+ env_info += f"\n**Lighting:** {', '.join(analysis['environmental']['lighting_analysis'])}"
460
+
461
+ # Intelligence metrics
462
+ metrics = analysis["intelligence_metrics"]
463
+
464
+ analysis_info = f"""**🚀 ULTRA SUPREME MULTI-MODEL ANALYSIS COMPLETE**
465
+ **Processing:** {gpu_status} • {duration:.1f}s • Multi-Model Pipeline
466
+ **Ultra Score:** {score}/100 • Models: CLIP + DeepFace + MediaPipe + Transformers
467
+
468
+ **📊 BREAKDOWN:**
469
+ • Prompt Quality: {breakdown.get('prompt_quality', 0)}/25
470
+ • Analysis Depth: {breakdown.get('analysis_depth', 0)}/25
471
+ • Model Confidence: {breakdown.get('model_confidence', 0)}/25
472
+ • Feature Richness: {breakdown.get('feature_richness', 0)}/25
473
+
474
+ **🧠 DEEP ANALYSIS RESULTS:**
475
+
476
+ **👤 DEMOGRAPHICS & IDENTITY:**
477
+ {demo_info or "No face detected for demographic analysis"}
478
+
479
+ **😊 EMOTIONAL ANALYSIS:**
480
+ {emotion_info or "No emotional data available"}
481
+
482
+ **👁️ FACIAL ANALYSIS:**
483
+ {face_info}
484
+
485
+ **🚶 POSE & BODY LANGUAGE:**
486
+ {pose_info or "No pose data available"}
487
 
488
+ **🏞️ ENVIRONMENT & SCENE:**
489
+ {env_info or "No environmental data detected"}
 
 
 
490
 
491
+ **📊 INTELLIGENCE METRICS:**
492
+ **Total Features Detected:** {metrics['total_features_detected']}
493
+ **Analysis Depth Score:** {metrics['analysis_depth_score']}/100
494
+ **Model Confidence Average:** {metrics['model_confidence_average']:.0%}
495
+ • **Technical Optimization:** {metrics['technical_optimization_score']}/100
496
 
497
+ **✨ MULTI-MODEL ADVANTAGES:**
498
+ DeepFace: Accurate age, gender, emotion detection
499
+ MediaPipe: Body pose and gesture analysis
500
+ CLIP: Semantic understanding and context
501
+ Transformers: Advanced emotion classification
502
+ OpenCV: Robust face detection
 
 
503
 
504
+ **🔬 Powered by Pariente AI Research Ultra Supreme Intelligence Engine**"""
505
 
506
  return analysis_info