import spaces import gradio as gr import torch from PIL import Image import numpy as np from clip_interrogator import Config, Interrogator import logging import os import warnings from datetime import datetime import gc import re import math warnings.filterwarnings("ignore", category=FutureWarning) warnings.filterwarnings("ignore", category=UserWarning) os.environ["TOKENIZERS_PARALLELISM"] = "false" logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def get_device(): if torch.cuda.is_available(): return "cuda" elif torch.backends.mps.is_available(): return "mps" else: return "cpu" DEVICE = get_device() class UltraSupremeAnalyzer: """ ULTRA SUPREME ANALYSIS ENGINE - ABSOLUTE MAXIMUM INTELLIGENCE """ def __init__(self): self.forbidden_elements = ["++", "weights", "white background [en dev]"] # ULTRA COMPREHENSIVE VOCABULARIES - MAXIMUM DEPTH self.micro_age_indicators = { "infant": ["baby", "infant", "newborn", "toddler"], "child": ["child", "kid", "young", "little", "small", "youth"], "teen": ["teenager", "teen", "adolescent", "young adult", "student"], "young_adult": ["young adult", "twenties", "thirty", "youthful", "fresh"], "middle_aged": ["middle-aged", "forties", "fifties", "mature", "experienced"], "senior": ["senior", "older", "elderly", "aged", "vintage", "seasoned"], "elderly": ["elderly", "old", "ancient", "weathered", "aged", "gray", "grey", "white hair", "silver", "wrinkled", "lined", "creased", "time-worn", "distinguished by age"] } self.ultra_facial_analysis = { "eye_features": { "shape": ["round eyes", "almond eyes", "narrow eyes", "wide eyes", "deep-set eyes", "prominent eyes"], "expression": ["intense gaze", "piercing stare", "gentle eyes", "wise eyes", "tired eyes", "alert eyes", "contemplative stare", "focused gaze", "distant look"], "color": ["brown eyes", "blue eyes", "green eyes", "hazel eyes", "dark eyes", "light eyes"], "condition": ["clear eyes", "bloodshot", "bright eyes", "dull eyes", "sparkling eyes"] }, "eyebrow_analysis": ["thick eyebrows", "thin eyebrows", "bushy eyebrows", "arched eyebrows", "straight eyebrows", "gray eyebrows"], "nose_features": ["prominent nose", "straight nose", "aquiline nose", "small nose", "wide nose", "narrow nose"], "mouth_expression": { "shape": ["thin lips", "full lips", "small mouth", "wide mouth"], "expression": ["slight smile", "serious expression", "frown", "neutral expression", "contemplative look", "stern look", "gentle expression"] }, "facial_hair_ultra": { "beard_types": ["full beard", "goatee", "mustache", "stubble", "clean-shaven", "five o'clock shadow"], "beard_texture": ["thick beard", "thin beard", "coarse beard", "fine beard", "well-groomed beard", "unkempt beard"], "beard_color": ["black beard", "brown beard", "gray beard", "grey beard", "silver beard", "white beard", "salt-and-pepper beard", "graying beard"], "beard_length": ["long beard", "short beard", "trimmed beard", "full-length beard"] }, "skin_analysis": ["smooth skin", "weathered skin", "wrinkled skin", "clear skin", "rough skin", "aged skin", "youthful skin", "tanned skin", "pale skin", "olive skin"], "facial_structure": ["angular face", "round face", "oval face", "square jaw", "defined cheekbones", "high cheekbones", "strong jawline", "soft features", "sharp features"] } self.emotion_micro_expressions = { "primary_emotions": ["happy", "sad", "angry", "fearful", "surprised", "disgusted", "contemptuous"], "complex_emotions": ["contemplative", "melancholic", "serene", "intense", "peaceful", "troubled", "confident", "uncertain", "wise", "stern", "gentle", "authoritative"], "emotional_indicators": ["furrowed brow", "raised eyebrows", "squinted eyes", "pursed lips", "relaxed expression", "tense jaw", "soft eyes", "hard stare"] } self.cultural_religious_ultra = { "jewish_orthodox": ["Orthodox Jewish", "Hasidic", "Ultra-Orthodox", "religious Jewish", "traditional Jewish", "devout Jewish"], "christian": ["Christian", "Catholic", "Protestant", "Orthodox Christian", "religious Christian"], "muslim": ["Muslim", "Islamic", "religious Muslim", "devout Muslim"], "buddhist": ["Buddhist", "monk", "religious Buddhist"], "general_religious": ["religious", "devout", "pious", "spiritual", "faithful", "observant"], "traditional_clothing": { "jewish": ["yarmulke", "kippah", "tallit", "tzitzit", "black hat", "Orthodox hat", "religious hat", "traditional Jewish hat"], "general": ["religious garment", "traditional clothing", "ceremonial dress", "formal religious attire"] } } self.clothing_accessories_ultra = { "headwear": ["hat", "cap", "beret", "headband", "turban", "hood", "helmet", "crown", "headpiece"], "eyewear": ["glasses", "spectacles", "sunglasses", "reading glasses", "wire-frame glasses", "thick-rimmed glasses", "designer glasses", "vintage glasses"], "clothing_types": ["suit", "jacket", "shirt", "dress", "robe", "uniform", "casual wear", "formal wear", "business attire"], "clothing_colors": ["black", "white", "gray", "blue", "red", "green", "brown", "navy", "dark", "light"], "clothing_styles": ["formal", "casual", "business", "traditional", "modern", "vintage", "classic", "contemporary"], "accessories": ["jewelry", "watch", "necklace", "ring", "bracelet", "earrings", "pin", "brooch"] } self.environmental_ultra_analysis = { "indoor_settings": { "residential": ["home", "house", "apartment", "living room", "bedroom", "kitchen", "dining room"], "office": ["office", "workplace", "conference room", "meeting room", "boardroom", "desk"], "institutional": ["school", "hospital", "government building", "court", "library"], "religious": ["church", "synagogue", "mosque", "temple", "chapel", "sanctuary"], "commercial": ["store", "restaurant", "hotel", "mall", "shop"] }, "outdoor_settings": { "natural": ["park", "garden", "forest", "beach", "mountain", "countryside", "field"], "urban": ["street", "city", "downtown", "plaza", "square", "avenue"], "architectural": ["building", "monument", "bridge", "structure"] }, "lighting_ultra": { "natural_light": ["sunlight", "daylight", "morning light", "afternoon light", "evening light", "golden hour", "blue hour", "overcast light", "window light"], "artificial_light": ["indoor lighting", "electric light", "lamp light", "overhead lighting", "side lighting", "fluorescent", "LED lighting"], "dramatic_lighting": ["high contrast", "low key", "high key", "chiaroscuro", "dramatic shadows", "rim lighting", "backlighting", "spotlight"], "quality": ["soft lighting", "hard lighting", "diffused light", "direct light", "ambient light", "mood lighting"] } } self.pose_body_language_ultra = { "head_position": ["head up", "head down", "head tilted", "head straight", "head turned", "profile view", "three-quarter view"], "posture": ["upright posture", "slouched", "relaxed posture", "formal posture", "casual stance", "dignified bearing"], "hand_positions": ["hands clasped", "hands folded", "hands visible", "hands hidden", "gesturing", "pointing"], "sitting_positions": ["sitting upright", "leaning forward", "leaning back", "sitting casually", "formal sitting"], "eye_contact": ["looking at camera", "looking away", "direct gaze", "averted gaze", "looking down", "looking up"], "overall_demeanor": ["confident", "reserved", "approachable", "authoritative", "gentle", "stern", "relaxed", "tense"] } self.composition_photography_ultra = { "shot_types": ["close-up", "medium shot", "wide shot", "extreme close-up", "portrait shot", "headshot", "bust shot", "full body"], "angles": ["eye level", "high angle", "low angle", "bird's eye", "worm's eye", "Dutch angle"], "framing": ["centered", "off-center", "rule of thirds", "tight framing", "loose framing"], "depth_of_field": ["shallow depth", "deep focus", "bokeh", "sharp focus", "soft focus"], "camera_movement": ["static", "handheld", "stabilized", "smooth"] } self.technical_photography_ultra = { "camera_systems": { "professional": ["Phase One XF", "Phase One XT", "Hasselblad X2D", "Fujifilm GFX", "Canon EOS R5", "Nikon Z9"], "medium_format": ["Phase One", "Hasselblad", "Fujifilm GFX", "Pentax 645"], "full_frame": ["Canon EOS R", "Nikon Z", "Sony A7", "Leica SL"] }, "lenses_ultra": { "portrait": ["85mm f/1.4", "135mm f/2", "105mm f/1.4", "200mm f/2.8"], "standard": ["50mm f/1.4", "35mm f/1.4", "24-70mm f/2.8"], "wide": ["24mm f/1.4", "16-35mm f/2.8", "14mm f/2.8"] }, "aperture_settings": ["f/1.4", "f/2", "f/2.8", "f/4", "f/5.6", "f/8"], "photography_styles": ["portrait photography", "documentary photography", "fine art photography", "commercial photography", "editorial photography"] } self.quality_descriptors_ultra = { "based_on_age": { "elderly": ["distinguished", "venerable", "dignified", "wise", "experienced", "seasoned", "time-honored", "revered", "weathered", "sage-like"], "middle_aged": ["professional", "accomplished", "established", "confident", "mature", "refined", "sophisticated"], "young_adult": ["vibrant", "energetic", "fresh", "youthful", "dynamic", "spirited", "lively"] }, "based_on_emotion": { "contemplative": ["thoughtful", "reflective", "meditative", "introspective"], "confident": ["assured", "self-possessed", "commanding", "authoritative"], "gentle": ["kind", "warm", "compassionate", "tender"], "stern": ["serious", "grave", "solemn", "austere"] }, "based_on_setting": { "formal": ["professional", "official", "ceremonial", "dignified"], "casual": ["relaxed", "informal", "comfortable", "natural"], "artistic": ["creative", "expressive", "aesthetic", "artistic"] } } def ultra_supreme_analysis(self, clip_fast, clip_classic, clip_best): """ULTRA SUPREME ANALYSIS - MAXIMUM POSSIBLE INTELLIGENCE""" combined_analysis = { "fast": clip_fast.lower(), "classic": clip_classic.lower(), "best": clip_best.lower(), "combined": f"{clip_fast} {clip_classic} {clip_best}".lower() } ultra_result = { "demographic": {"age_category": None, "age_confidence": 0, "gender": None, "cultural_religious": []}, "facial_ultra": {"eyes": [], "eyebrows": [], "nose": [], "mouth": [], "facial_hair": [], "skin": [], "structure": []}, "emotional_state": {"primary_emotion": None, "emotion_confidence": 0, "micro_expressions": [], "overall_demeanor": []}, "clothing_accessories": {"headwear": [], "eyewear": [], "clothing": [], "accessories": []}, "environmental": {"setting_type": None, "specific_location": None, "lighting_analysis": [], "atmosphere": []}, "pose_composition": {"body_language": [], "head_position": [], "eye_contact": [], "posture": []}, "technical_analysis": {"shot_type": None, "angle": None, "lighting_setup": None, "suggested_equipment": {}}, "intelligence_metrics": {"total_features_detected": 0, "analysis_depth_score": 0, "cultural_awareness_score": 0, "technical_optimization_score": 0} } # ULTRA DEEP AGE ANALYSIS age_scores = {} for age_category, indicators in self.micro_age_indicators.items(): score = sum(1 for indicator in indicators if indicator in combined_analysis["combined"]) if score > 0: age_scores[age_category] = score if age_scores: ultra_result["demographic"]["age_category"] = max(age_scores, key=age_scores.get) ultra_result["demographic"]["age_confidence"] = age_scores[ultra_result["demographic"]["age_category"]] # GENDER DETECTION WITH CONFIDENCE male_indicators = ["man", "male", "gentleman", "guy", "he", "his", "masculine"] female_indicators = ["woman", "female", "lady", "she", "her", "feminine"] male_score = sum(1 for indicator in male_indicators if indicator in combined_analysis["combined"]) female_score = sum(1 for indicator in female_indicators if indicator in combined_analysis["combined"]) if male_score > female_score: ultra_result["demographic"]["gender"] = "man" elif female_score > male_score: ultra_result["demographic"]["gender"] = "woman" # ULTRA CULTURAL/RELIGIOUS ANALYSIS for culture_type, indicators in self.cultural_religious_ultra.items(): if isinstance(indicators, list): for indicator in indicators: if indicator.lower() in combined_analysis["combined"]: ultra_result["demographic"]["cultural_religious"].append(indicator) # COMPREHENSIVE FACIAL FEATURE ANALYSIS for hair_category, features in self.ultra_facial_analysis["facial_hair_ultra"].items(): for feature in features: if feature in combined_analysis["combined"]: ultra_result["facial_ultra"]["facial_hair"].append(feature) # Eyes analysis for eye_category, features in self.ultra_facial_analysis["eye_features"].items(): for feature in features: if feature in combined_analysis["combined"]: ultra_result["facial_ultra"]["eyes"].append(feature) # EMOTION AND MICRO-EXPRESSION ANALYSIS emotion_scores = {} for emotion in self.emotion_micro_expressions["complex_emotions"]: if emotion in combined_analysis["combined"]: emotion_scores[emotion] = combined_analysis["combined"].count(emotion) if emotion_scores: ultra_result["emotional_state"]["primary_emotion"] = max(emotion_scores, key=emotion_scores.get) ultra_result["emotional_state"]["emotion_confidence"] = emotion_scores[ultra_result["emotional_state"]["primary_emotion"]] # CLOTHING AND ACCESSORIES ANALYSIS for category, items in self.clothing_accessories_ultra.items(): if isinstance(items, list): for item in items: if item in combined_analysis["combined"]: ultra_result["clothing_accessories"][category].append(item) # ENVIRONMENTAL ULTRA ANALYSIS setting_scores = {} for main_setting, sub_settings in self.environmental_ultra_analysis.items(): if isinstance(sub_settings, dict): for sub_type, locations in sub_settings.items(): score = sum(1 for location in locations if location in combined_analysis["combined"]) if score > 0: setting_scores[sub_type] = score if setting_scores: ultra_result["environmental"]["setting_type"] = max(setting_scores, key=setting_scores.get) # LIGHTING ANALYSIS for light_category, light_types in self.environmental_ultra_analysis["lighting_ultra"].items(): for light_type in light_types: if light_type in combined_analysis["combined"]: ultra_result["environmental"]["lighting_analysis"].append(light_type) # POSE AND BODY LANGUAGE ANALYSIS for pose_category, indicators in self.pose_body_language_ultra.items(): for indicator in indicators: if indicator in combined_analysis["combined"]: ultra_result["pose_composition"][pose_category].append(indicator) # TECHNICAL PHOTOGRAPHY ANALYSIS for shot_type in self.composition_photography_ultra["shot_types"]: if shot_type in combined_analysis["combined"]: ultra_result["technical_analysis"]["shot_type"] = shot_type break # CALCULATE INTELLIGENCE METRICS total_features = sum(len(v) if isinstance(v, list) else (1 if v else 0) for category in ultra_result.values() if isinstance(category, dict) for v in category.values()) ultra_result["intelligence_metrics"]["total_features_detected"] = total_features ultra_result["intelligence_metrics"]["analysis_depth_score"] = min(total_features * 5, 100) ultra_result["intelligence_metrics"]["cultural_awareness_score"] = len(ultra_result["demographic"]["cultural_religious"]) * 20 return ultra_result def build_ultra_supreme_prompt(self, ultra_analysis, clip_results): """BUILD ULTRA SUPREME FLUX PROMPT - ABSOLUTE MAXIMUM QUALITY""" components = [] # 1. ULTRA INTELLIGENT ARTICLE SELECTION subject_desc = [] if ultra_analysis["demographic"]["cultural_religious"]: subject_desc.extend(ultra_analysis["demographic"]["cultural_religious"][:1]) if ultra_analysis["demographic"]["age_category"] and ultra_analysis["demographic"]["age_category"] != "middle_aged": subject_desc.append(ultra_analysis["demographic"]["age_category"].replace("_", " ")) if ultra_analysis["demographic"]["gender"]: subject_desc.append(ultra_analysis["demographic"]["gender"]) if subject_desc: full_subject = " ".join(subject_desc) article = "An" if full_subject[0].lower() in 'aeiou' else "A" else: article = "A" components.append(article) # 2. ULTRA CONTEXTUAL ADJECTIVES (max 2-3 per Flux rules) adjectives = [] # Age-based adjectives age_cat = ultra_analysis["demographic"]["age_category"] if age_cat and age_cat in self.quality_descriptors_ultra["based_on_age"]: adjectives.extend(self.quality_descriptors_ultra["based_on_age"][age_cat][:2]) # Emotion-based adjectives emotion = ultra_analysis["emotional_state"]["primary_emotion"] if emotion and emotion in self.quality_descriptors_ultra["based_on_emotion"]: adjectives.extend(self.quality_descriptors_ultra["based_on_emotion"][emotion][:1]) # Default if none found if not adjectives: adjectives = ["distinguished", "professional"] components.extend(adjectives[:2]) # Flux rule: max 2-3 adjectives # 3. ULTRA ENHANCED SUBJECT if subject_desc: components.append(" ".join(subject_desc)) else: components.append("person") # 4. ULTRA DETAILED FACIAL FEATURES facial_details = [] # Eyes if ultra_analysis["facial_ultra"]["eyes"]: eye_desc = ultra_analysis["facial_ultra"]["eyes"][0] facial_details.append(f"with {eye_desc}") # Facial hair with ultra detail if ultra_analysis["facial_ultra"]["facial_hair"]: beard_details = ultra_analysis["facial_ultra"]["facial_hair"] if any("silver" in detail or "gray" in detail or "grey" in detail for detail in beard_details): facial_details.append("with a distinguished silver beard") elif any("beard" in detail for detail in beard_details): facial_details.append("with a full well-groomed beard") if facial_details: components.extend(facial_details) # 5. CLOTHING AND ACCESSORIES ULTRA clothing_details = [] # Eyewear if ultra_analysis["clothing_accessories"]["eyewear"]: eyewear = ultra_analysis["clothing_accessories"]["eyewear"][0] clothing_details.append(f"wearing {eyewear}") # Headwear if ultra_analysis["clothing_accessories"]["headwear"]: headwear = ultra_analysis["clothing_accessories"]["headwear"][0] if ultra_analysis["demographic"]["cultural_religious"]: clothing_details.append("wearing a traditional black hat") else: clothing_details.append(f"wearing a {headwear}") if clothing_details: components.extend(clothing_details) # 6. ULTRA POSE AND BODY LANGUAGE pose_description = "positioned with natural dignity" if ultra_analysis["pose_composition"]["posture"]: posture = ultra_analysis["pose_composition"]["posture"][0] pose_description = f"maintaining {posture}" elif ultra_analysis["technical_analysis"]["shot_type"] == "portrait": pose_description = "captured in contemplative portrait pose" components.append(pose_description) # 7. ULTRA ENVIRONMENTAL CONTEXT environment_desc = "in a thoughtfully composed environment" if ultra_analysis["environmental"]["setting_type"]: setting_map = { "residential": "in an intimate home setting", "office": "in a professional office environment", "religious": "in a sacred traditional space", "formal": "in a distinguished formal setting" } environment_desc = setting_map.get(ultra_analysis["environmental"]["setting_type"], "in a carefully arranged professional setting") components.append(environment_desc) # 8. ULTRA SOPHISTICATED LIGHTING lighting_desc = "illuminated by sophisticated portrait lighting that emphasizes character and facial texture" if ultra_analysis["environmental"]["lighting_analysis"]: primary_light = ultra_analysis["environmental"]["lighting_analysis"][0] if "dramatic" in primary_light: lighting_desc = "bathed in dramatic chiaroscuro lighting that creates compelling depth and shadow play" elif "natural" in primary_light or "window" in primary_light: lighting_desc = "graced by gentle natural lighting that brings out intricate facial details and warmth" elif "soft" in primary_light: lighting_desc = "softly illuminated to reveal nuanced expressions and character" components.append(lighting_desc) # 9. ULTRA TECHNICAL SPECIFICATIONS if ultra_analysis["technical_analysis"]["shot_type"] in ["portrait", "headshot", "close-up"]: camera_setup = "Shot on Phase One XF IQ4, 85mm f/1.4 lens, f/2.8 aperture" elif ultra_analysis["demographic"]["cultural_religious"]: camera_setup = "Shot on Hasselblad X2D, 90mm lens, f/2.8 aperture" else: camera_setup = "Shot on Phase One XF, 80mm lens, f/4 aperture" components.append(camera_setup) # 10. ULTRA QUALITY DESIGNATION quality_designation = "professional portrait photography" if ultra_analysis["demographic"]["cultural_religious"]: quality_designation = "fine art documentary photography" elif ultra_analysis["emotional_state"]["primary_emotion"]: quality_designation = "expressive portrait photography" components.append(quality_designation) # ULTRA FINAL ASSEMBLY prompt = ", ".join(components) # Ultra cleaning and optimization prompt = re.sub(r'\s+', ' ', prompt) prompt = re.sub(r',\s*,+', ',', prompt) prompt = re.sub(r'\s*,\s*', ', ', prompt) prompt = prompt.replace(" ,", ",") if prompt: prompt = prompt[0].upper() + prompt[1:] return prompt def calculate_ultra_supreme_score(self, prompt, ultra_analysis): """ULTRA SUPREME INTELLIGENCE SCORING""" score = 0 breakdown = {} # Structure Excellence (15 points) structure_score = 0 if prompt.startswith(("A", "An")): structure_score += 5 if prompt.count(",") >= 8: structure_score += 10 score += structure_score breakdown["structure"] = structure_score # Feature Detection Depth (25 points) features_score = min(ultra_analysis["intelligence_metrics"]["total_features_detected"] * 2, 25) score += features_score breakdown["features"] = features_score # Cultural/Religious Awareness (20 points) cultural_score = min(len(ultra_analysis["demographic"]["cultural_religious"]) * 10, 20) score += cultural_score breakdown["cultural"] = cultural_score # Emotional Intelligence (15 points) emotion_score = 0 if ultra_analysis["emotional_state"]["primary_emotion"]: emotion_score += 10 if ultra_analysis["emotional_state"]["emotion_confidence"] > 1: emotion_score += 5 score += emotion_score breakdown["emotional"] = emotion_score # Technical Sophistication (15 points) tech_score = 0 if "Phase One" in prompt or "Hasselblad" in prompt: tech_score += 5 if any(aperture in prompt for aperture in ["f/1.4", "f/2.8", "f/4"]): tech_score += 5 if any(lens in prompt for lens in ["85mm", "90mm", "80mm"]): tech_score += 5 score += tech_score breakdown["technical"] = tech_score # Environmental Context (10 points) env_score = 0 if ultra_analysis["environmental"]["setting_type"]: env_score += 5 if ultra_analysis["environmental"]["lighting_analysis"]: env_score += 5 score += env_score breakdown["environmental"] = env_score return min(score, 100), breakdown class UltraSupremeOptimizer: def __init__(self): self.interrogator = None self.analyzer = UltraSupremeAnalyzer() self.usage_count = 0 self.device = DEVICE self.is_initialized = False def initialize_model(self): if self.is_initialized: return True try: config = Config( clip_model_name="ViT-L-14/openai", download_cache=True, chunk_size=2048, quiet=True, device=self.device ) self.interrogator = Interrogator(config) self.is_initialized = True if self.device == "cpu": gc.collect() else: torch.cuda.empty_cache() return True except Exception as e: logger.error(f"Initialization error: {e}") return False def optimize_image(self, image): if image is None: return None if isinstance(image, np.ndarray): image = Image.fromarray(image) elif not isinstance(image, Image.Image): image = Image.open(image) if image.mode != 'RGB': image = image.convert('RGB') max_size = 768 if self.device != "cpu" else 512 if image.size[0] > max_size or image.size[1] > max_size: image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS) return image @spaces.GPU def generate_ultra_supreme_prompt(self, image): try: if not self.is_initialized: if not self.initialize_model(): return "❌ Model initialization failed.", "Please refresh and try again.", 0, {} if image is None: return "❌ Please upload an image.", "No image provided.", 0, {} self.usage_count += 1 image = self.optimize_image(image) if image is None: return "❌ Image processing failed.", "Invalid image format.", 0, {} start_time = datetime.now() # ULTRA SUPREME TRIPLE CLIP ANALYSIS logger.info("ULTRA SUPREME ANALYSIS - Maximum intelligence deployment") clip_fast = self.interrogator.interrogate_fast(image) clip_classic = self.interrogator.interrogate_classic(image) clip_best = self.interrogator.interrogate(image) logger.info(f"ULTRA CLIP Results:\nFast: {clip_fast}\nClassic: {clip_classic}\nBest: {clip_best}") # ULTRA SUPREME ANALYSIS ultra_analysis = self.analyzer.ultra_supreme_analysis(clip_fast, clip_classic, clip_best) # BUILD ULTRA SUPREME FLUX PROMPT optimized_prompt = self.analyzer.build_ultra_supreme_prompt(ultra_analysis, [clip_fast, clip_classic, clip_best]) # CALCULATE ULTRA SUPREME SCORE score, breakdown = self.analyzer.calculate_ultra_supreme_score(optimized_prompt, ultra_analysis) end_time = datetime.now() duration = (end_time - start_time).total_seconds() # Memory cleanup if self.device == "cpu": gc.collect() else: torch.cuda.empty_cache() # ULTRA COMPREHENSIVE ANALYSIS REPORT gpu_status = "⚡ ZeroGPU" if torch.cuda.is_available() else "💻 CPU" # Format detected elements features = ", ".join(ultra_analysis["facial_ultra"]["facial_hair"]) if ultra_analysis["facial_ultra"]["facial_hair"] else "None detected" cultural = ", ".join(ultra_analysis["demographic"]["cultural_religious"]) if ultra_analysis["demographic"]["cultural_religious"] else "None detected" clothing = ", ".join(ultra_analysis["clothing_accessories"]["eyewear"] + ultra_analysis["clothing_accessories"]["headwear"]) if ultra_analysis["clothing_accessories"]["eyewear"] or ultra_analysis["clothing_accessories"]["headwear"] else "None detected" analysis_info = f"""**🚀 ULTRA SUPREME ANALYSIS COMPLETE** **Processing:** {gpu_status} • {duration:.1f}s • Triple CLIP Ultra Intelligence **Ultra Score:** {score}/100 • Breakdown: Structure({breakdown.get('structure',0)}) Features({breakdown.get('features',0)}) Cultural({breakdown.get('cultural',0)}) Emotional({breakdown.get('emotional',0)}) Technical({breakdown.get('technical',0)}) **Generation:** #{self.usage_count} **🧠 ULTRA DEEP DETECTION:** - **Age Category:** {ultra_analysis["demographic"].get("age_category", "Unspecified").replace("_", " ").title()} (Confidence: {ultra_analysis["demographic"].get("age_confidence", 0)}) - **Cultural Context:** {cultural} - **Facial Features:** {features} - **Accessories:** {clothing} - **Setting:** {ultra_analysis["environmental"].get("setting_type", "Standard").title()} - **Emotion:** {ultra_analysis["emotional_state"].get("primary_emotion", "Neutral").title()} - **Total Features:** {ultra_analysis["intelligence_metrics"]["total_features_detected"]} **📊 CLIP ANALYSIS SOURCES:** - **Fast:** {clip_fast[:50]}... - **Classic:** {clip_classic[:50]}... - **Best:** {clip_best[:50]}... **⚡ ULTRA OPTIMIZATION:** Applied absolute maximum depth analysis with Pariente AI research rules""" return optimized_prompt, analysis_info, score, breakdown except Exception as e: logger.error(f"Ultra supreme generation error: {e}") return f"❌ Error: {str(e)}", "Please try with a different image.", 0, {} # Initialize the optimizer optimizer = UltraSupremeOptimizer() def process_ultra_supreme_analysis(image): """Ultra supreme analysis wrapper""" try: prompt, info, score, breakdown = optimizer.generate_ultra_supreme_prompt(image) # Ultra enhanced score display if score >= 95: color = "#059669" grade = "LEGENDARY" elif score >= 90: color = "#10b981" grade = "EXCELLENT" elif score >= 80: color = "#22c55e" grade = "VERY GOOD" elif score >= 70: color = "#f59e0b" grade = "GOOD" elif score >= 60: color = "#f97316" grade = "FAIR" else: color = "#ef4444" grade = "NEEDS WORK" score_html = f'''