Malaji71 commited on
Commit
325e056
verified
1 Parent(s): ea3bde6

Update analyzer.py

Browse files
Files changed (1) hide show
  1. analyzer.py +309 -314
analyzer.py CHANGED
@@ -1,378 +1,373 @@
1
  """
2
  Ultra Supreme Analyzer for image analysis and prompt building
 
3
  """
4
 
5
  import re
6
  from typing import Dict, List, Any, Tuple
 
7
 
8
- from constants import (
9
- FORBIDDEN_ELEMENTS,
10
- MICRO_AGE_INDICATORS,
11
- ULTRA_FACIAL_ANALYSIS,
12
- EMOTION_MICRO_EXPRESSIONS,
13
- CULTURAL_RELIGIOUS_ULTRA,
14
- CLOTHING_ACCESSORIES_ULTRA,
15
- ENVIRONMENTAL_ULTRA_ANALYSIS,
16
- POSE_BODY_LANGUAGE_ULTRA,
17
- COMPOSITION_PHOTOGRAPHY_ULTRA,
18
- TECHNICAL_PHOTOGRAPHY_ULTRA,
19
- QUALITY_DESCRIPTORS_ULTRA,
20
- GENDER_INDICATORS
21
- )
22
 
23
 
24
  class UltraSupremeAnalyzer:
25
  """
26
- ULTRA SUPREME ANALYSIS ENGINE - ABSOLUTE MAXIMUM INTELLIGENCE
27
  """
28
 
29
  def __init__(self):
30
- self.forbidden_elements = FORBIDDEN_ELEMENTS
31
- self.micro_age_indicators = MICRO_AGE_INDICATORS
32
- self.ultra_facial_analysis = ULTRA_FACIAL_ANALYSIS
33
- self.emotion_micro_expressions = EMOTION_MICRO_EXPRESSIONS
34
- self.cultural_religious_ultra = CULTURAL_RELIGIOUS_ULTRA
35
- self.clothing_accessories_ultra = CLOTHING_ACCESSORIES_ULTRA
36
- self.environmental_ultra_analysis = ENVIRONMENTAL_ULTRA_ANALYSIS
37
- self.pose_body_language_ultra = POSE_BODY_LANGUAGE_ULTRA
38
- self.composition_photography_ultra = COMPOSITION_PHOTOGRAPHY_ULTRA
39
- self.technical_photography_ultra = TECHNICAL_PHOTOGRAPHY_ULTRA
40
- self.quality_descriptors_ultra = QUALITY_DESCRIPTORS_ULTRA
41
-
42
- def ultra_supreme_analysis(self, clip_fast: str, clip_classic: str, clip_best: str) -> Dict[str, Any]:
43
- """ULTRA SUPREME ANALYSIS - MAXIMUM POSSIBLE INTELLIGENCE"""
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- combined_analysis = {
46
- "fast": clip_fast.lower(),
47
- "classic": clip_classic.lower(),
48
- "best": clip_best.lower(),
49
- "combined": f"{clip_fast} {clip_classic} {clip_best}".lower()
 
 
 
 
50
  }
51
 
52
- ultra_result = {
53
- "demographic": {"age_category": None, "age_confidence": 0, "gender": None, "cultural_religious": []},
54
- "facial_ultra": {"eyes": [], "eyebrows": [], "nose": [], "mouth": [], "facial_hair": [], "skin": [], "structure": []},
55
- "emotional_state": {"primary_emotion": None, "emotion_confidence": 0, "micro_expressions": [], "overall_demeanor": []},
56
- "clothing_accessories": {"headwear": [], "eyewear": [], "clothing": [], "accessories": []},
57
- "environmental": {"setting_type": None, "specific_location": None, "lighting_analysis": [], "atmosphere": []},
58
- "pose_composition": {"body_language": [], "head_position": [], "eye_contact": [], "posture": []},
59
- "technical_analysis": {"shot_type": None, "angle": None, "lighting_setup": None, "suggested_equipment": {}},
60
- "intelligence_metrics": {"total_features_detected": 0, "analysis_depth_score": 0, "cultural_awareness_score": 0, "technical_optimization_score": 0}
61
  }
 
 
 
 
62
 
63
- # ULTRA DEEP AGE ANALYSIS
64
- age_scores = {}
65
- for age_category, indicators in self.micro_age_indicators.items():
66
- score = sum(1 for indicator in indicators if indicator in combined_analysis["combined"])
67
- if score > 0:
68
- age_scores[age_category] = score
69
-
70
- if age_scores:
71
- ultra_result["demographic"]["age_category"] = max(age_scores, key=age_scores.get)
72
- ultra_result["demographic"]["age_confidence"] = age_scores[ultra_result["demographic"]["age_category"]]
73
-
74
- # GENDER DETECTION WITH CONFIDENCE
75
- male_score = sum(1 for indicator in GENDER_INDICATORS["male"] if indicator in combined_analysis["combined"])
76
- female_score = sum(1 for indicator in GENDER_INDICATORS["female"] if indicator in combined_analysis["combined"])
77
-
78
- if male_score > female_score:
79
- ultra_result["demographic"]["gender"] = "man"
80
- elif female_score > male_score:
81
- ultra_result["demographic"]["gender"] = "woman"
82
-
83
- # ULTRA CULTURAL/RELIGIOUS ANALYSIS
84
- for culture_type, indicators in self.cultural_religious_ultra.items():
85
- if isinstance(indicators, list):
86
- for indicator in indicators:
87
- if indicator.lower() in combined_analysis["combined"]:
88
- ultra_result["demographic"]["cultural_religious"].append(indicator)
89
-
90
- # COMPREHENSIVE FACIAL FEATURE ANALYSIS
91
- for hair_category, features in self.ultra_facial_analysis["facial_hair_ultra"].items():
92
- for feature in features:
93
- if feature in combined_analysis["combined"]:
94
- ultra_result["facial_ultra"]["facial_hair"].append(feature)
95
-
96
- # Eyes analysis
97
- for eye_category, features in self.ultra_facial_analysis["eye_features"].items():
98
- for feature in features:
99
- if feature in combined_analysis["combined"]:
100
- ultra_result["facial_ultra"]["eyes"].append(feature)
101
-
102
- # EMOTION AND MICRO-EXPRESSION ANALYSIS
103
- emotion_scores = {}
104
- for emotion in self.emotion_micro_expressions["complex_emotions"]:
105
- if emotion in combined_analysis["combined"]:
106
- emotion_scores[emotion] = combined_analysis["combined"].count(emotion)
107
-
108
- if emotion_scores:
109
- ultra_result["emotional_state"]["primary_emotion"] = max(emotion_scores, key=emotion_scores.get)
110
- ultra_result["emotional_state"]["emotion_confidence"] = emotion_scores[ultra_result["emotional_state"]["primary_emotion"]]
111
-
112
- # CLOTHING AND ACCESSORIES ANALYSIS
113
- for category, items in self.clothing_accessories_ultra.items():
114
- if isinstance(items, list):
115
- for item in items:
116
- if item in combined_analysis["combined"]:
117
- if category == "clothing_types":
118
- ultra_result["clothing_accessories"]["clothing"].append(item)
119
- elif category == "clothing_styles":
120
- ultra_result["clothing_accessories"]["clothing"].append(item)
121
- elif category in ["headwear", "eyewear", "accessories"]:
122
- ultra_result["clothing_accessories"][category].append(item)
123
-
124
- # ENVIRONMENTAL ULTRA ANALYSIS
125
- setting_scores = {}
126
- for main_setting, sub_settings in self.environmental_ultra_analysis.items():
127
- if isinstance(sub_settings, dict):
128
- for sub_type, locations in sub_settings.items():
129
- score = sum(1 for location in locations if location in combined_analysis["combined"])
130
- if score > 0:
131
- setting_scores[sub_type] = score
132
-
133
- if setting_scores:
134
- ultra_result["environmental"]["setting_type"] = max(setting_scores, key=setting_scores.get)
135
-
136
- # LIGHTING ANALYSIS
137
- for light_category, light_types in self.environmental_ultra_analysis["lighting_ultra"].items():
138
- for light_type in light_types:
139
- if light_type in combined_analysis["combined"]:
140
- ultra_result["environmental"]["lighting_analysis"].append(light_type)
141
-
142
- # POSE AND BODY LANGUAGE ANALYSIS
143
- for pose_category, indicators in self.pose_body_language_ultra.items():
144
- for indicator in indicators:
145
- if indicator in combined_analysis["combined"]:
146
- if pose_category in ultra_result["pose_composition"]:
147
- ultra_result["pose_composition"][pose_category].append(indicator)
148
-
149
- # TECHNICAL PHOTOGRAPHY ANALYSIS
150
- for shot_type in self.composition_photography_ultra["shot_types"]:
151
- if shot_type in combined_analysis["combined"]:
152
- ultra_result["technical_analysis"]["shot_type"] = shot_type
153
- break
154
 
155
- # CALCULATE INTELLIGENCE METRICS
156
- total_features = sum(len(v) if isinstance(v, list) else (1 if v else 0)
157
- for category in ultra_result.values()
158
- if isinstance(category, dict)
159
- for v in category.values())
160
- ultra_result["intelligence_metrics"]["total_features_detected"] = total_features
161
- ultra_result["intelligence_metrics"]["analysis_depth_score"] = min(total_features * 5, 100)
162
- ultra_result["intelligence_metrics"]["cultural_awareness_score"] = len(ultra_result["demographic"]["cultural_religious"]) * 20
163
 
164
- return ultra_result
165
 
166
- def build_ultra_supreme_prompt(self, ultra_analysis: Dict[str, Any], clip_results: List[str]) -> str:
167
- """BUILD ULTRA SUPREME FLUX PROMPT - ABSOLUTE MAXIMUM QUALITY"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
- components = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
- # 1. ULTRA INTELLIGENT ARTICLE SELECTION
172
- subject_desc = []
173
- if ultra_analysis["demographic"]["cultural_religious"]:
174
- subject_desc.extend(ultra_analysis["demographic"]["cultural_religious"][:1])
175
- if ultra_analysis["demographic"]["age_category"] and ultra_analysis["demographic"]["age_category"] != "middle_aged":
176
- subject_desc.append(ultra_analysis["demographic"]["age_category"].replace("_", " "))
177
- if ultra_analysis["demographic"]["gender"]:
178
- subject_desc.append(ultra_analysis["demographic"]["gender"])
179
-
180
- if subject_desc:
181
- full_subject = " ".join(subject_desc)
182
- article = "An" if full_subject[0].lower() in 'aeiou' else "A"
183
- else:
184
- article = "A"
185
- components.append(article)
186
 
187
- # 2. ULTRA CONTEXTUAL ADJECTIVES (max 2-3 per Flux rules)
188
- adjectives = []
189
 
190
- # Age-based adjectives
191
- age_cat = ultra_analysis["demographic"]["age_category"]
192
- if age_cat and age_cat in self.quality_descriptors_ultra["based_on_age"]:
193
- adjectives.extend(self.quality_descriptors_ultra["based_on_age"][age_cat][:2])
 
 
 
194
 
195
- # Emotion-based adjectives
196
- emotion = ultra_analysis["emotional_state"]["primary_emotion"]
197
- if emotion and emotion in self.quality_descriptors_ultra["based_on_emotion"]:
198
- adjectives.extend(self.quality_descriptors_ultra["based_on_emotion"][emotion][:1])
199
 
200
- # Default if none found
201
- if not adjectives:
202
- adjectives = ["distinguished", "professional"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
 
204
- components.extend(adjectives[:2]) # Flux rule: max 2-3 adjectives
 
 
 
205
 
206
- # 3. ULTRA ENHANCED SUBJECT
207
- if subject_desc:
208
- components.append(" ".join(subject_desc))
209
- else:
210
- components.append("person")
211
-
212
- # 4. ULTRA DETAILED FACIAL FEATURES
213
- facial_details = []
214
-
215
- # Eyes
216
- if ultra_analysis["facial_ultra"]["eyes"]:
217
- eye_desc = ultra_analysis["facial_ultra"]["eyes"][0]
218
- facial_details.append(f"with {eye_desc}")
219
-
220
- # Facial hair with ultra detail
221
- if ultra_analysis["facial_ultra"]["facial_hair"]:
222
- beard_details = ultra_analysis["facial_ultra"]["facial_hair"]
223
- if any("silver" in detail or "gray" in detail or "grey" in detail for detail in beard_details):
224
- facial_details.append("with a distinguished silver beard")
225
- elif any("beard" in detail for detail in beard_details):
226
- facial_details.append("with a full well-groomed beard")
227
-
228
- if facial_details:
229
- components.extend(facial_details)
230
-
231
- # 5. CLOTHING AND ACCESSORIES ULTRA
232
- clothing_details = []
233
-
234
- # Eyewear
235
- if ultra_analysis["clothing_accessories"]["eyewear"]:
236
- eyewear = ultra_analysis["clothing_accessories"]["eyewear"][0]
237
- clothing_details.append(f"wearing {eyewear}")
238
-
239
- # Headwear
240
- if ultra_analysis["clothing_accessories"]["headwear"]:
241
- headwear = ultra_analysis["clothing_accessories"]["headwear"][0]
242
- if ultra_analysis["demographic"]["cultural_religious"]:
243
- clothing_details.append("wearing a traditional black hat")
244
  else:
245
- clothing_details.append(f"wearing a {headwear}")
246
-
247
- if clothing_details:
248
- components.extend(clothing_details)
249
-
250
- # 6. ULTRA POSE AND BODY LANGUAGE
251
- pose_description = "positioned with natural dignity"
252
-
253
- if ultra_analysis["pose_composition"]["posture"]:
254
- posture = ultra_analysis["pose_composition"]["posture"][0]
255
- pose_description = f"maintaining {posture}"
256
- elif ultra_analysis["technical_analysis"]["shot_type"] == "portrait":
257
- pose_description = "captured in contemplative portrait pose"
258
-
259
- components.append(pose_description)
260
-
261
- # 7. ULTRA ENVIRONMENTAL CONTEXT
262
- environment_desc = "in a thoughtfully composed environment"
263
-
264
- if ultra_analysis["environmental"]["setting_type"]:
265
- setting_map = {
266
- "residential": "in an intimate home setting",
267
- "office": "in a professional office environment",
268
- "religious": "in a sacred traditional space",
269
- "formal": "in a distinguished formal setting"
270
- }
271
- environment_desc = setting_map.get(ultra_analysis["environmental"]["setting_type"],
272
- "in a carefully arranged professional setting")
273
-
274
- components.append(environment_desc)
275
-
276
- # 8. ULTRA SOPHISTICATED LIGHTING
277
- lighting_desc = "illuminated by sophisticated portrait lighting that emphasizes character and facial texture"
278
-
279
- if ultra_analysis["environmental"]["lighting_analysis"]:
280
- primary_light = ultra_analysis["environmental"]["lighting_analysis"][0]
281
- if "dramatic" in primary_light:
282
- lighting_desc = "bathed in dramatic chiaroscuro lighting that creates compelling depth and shadow play"
283
- elif "natural" in primary_light or "window" in primary_light:
284
- lighting_desc = "graced by gentle natural lighting that brings out intricate facial details and warmth"
285
- elif "soft" in primary_light:
286
- lighting_desc = "softly illuminated to reveal nuanced expressions and character"
287
-
288
- components.append(lighting_desc)
289
-
290
- # 9. ULTRA TECHNICAL SPECIFICATIONS
291
- if ultra_analysis["technical_analysis"]["shot_type"] in ["portrait", "headshot", "close-up"]:
292
- camera_setup = "Shot on Phase One XF IQ4, 85mm f/1.4 lens, f/2.8 aperture"
293
- elif ultra_analysis["demographic"]["cultural_religious"]:
294
- camera_setup = "Shot on Hasselblad X2D, 90mm lens, f/2.8 aperture"
295
  else:
296
- camera_setup = "Shot on Phase One XF, 80mm lens, f/4 aperture"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
 
298
- components.append(camera_setup)
 
 
299
 
300
- # 10. ULTRA QUALITY DESIGNATION
301
- quality_designation = "professional portrait photography"
 
 
302
 
303
- if ultra_analysis["demographic"]["cultural_religious"]:
304
- quality_designation = "fine art documentary photography"
305
- elif ultra_analysis["emotional_state"]["primary_emotion"]:
306
- quality_designation = "expressive portrait photography"
 
 
 
 
 
 
 
 
 
 
 
307
 
308
- components.append(quality_designation)
 
 
 
309
 
310
- # ULTRA FINAL ASSEMBLY
311
  prompt = ", ".join(components)
312
 
313
- # Ultra cleaning and optimization
314
  prompt = re.sub(r'\s+', ' ', prompt)
315
  prompt = re.sub(r',\s*,+', ',', prompt)
316
  prompt = re.sub(r'\s*,\s*', ', ', prompt)
317
- prompt = prompt.replace(" ,", ",")
318
 
 
319
  if prompt:
320
  prompt = prompt[0].upper() + prompt[1:]
321
 
 
 
322
  return prompt
323
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324
  def calculate_ultra_supreme_score(self, prompt: str, ultra_analysis: Dict[str, Any]) -> Tuple[int, Dict[str, int]]:
325
- """ULTRA SUPREME INTELLIGENCE SCORING"""
326
 
327
  score = 0
328
  breakdown = {}
329
 
330
- # Structure Excellence (15 points)
331
  structure_score = 0
332
- if prompt.startswith(("A", "An")):
333
- structure_score += 5
334
- if prompt.count(",") >= 8:
335
  structure_score += 10
336
  score += structure_score
337
  breakdown["structure"] = structure_score
338
 
339
- # Feature Detection Depth (25 points)
340
- features_score = min(ultra_analysis["intelligence_metrics"]["total_features_detected"] * 2, 25)
341
- score += features_score
342
- breakdown["features"] = features_score
343
-
344
- # Cultural/Religious Awareness (20 points)
345
- cultural_score = min(len(ultra_analysis["demographic"]["cultural_religious"]) * 10, 20)
346
- score += cultural_score
347
- breakdown["cultural"] = cultural_score
348
-
349
- # Emotional Intelligence (15 points)
350
- emotion_score = 0
351
- if ultra_analysis["emotional_state"]["primary_emotion"]:
352
- emotion_score += 10
353
- if ultra_analysis["emotional_state"]["emotion_confidence"] > 1:
354
- emotion_score += 5
355
- score += emotion_score
356
- breakdown["emotional"] = emotion_score
357
-
358
- # Technical Sophistication (15 points)
359
  tech_score = 0
360
- if "Phase One" in prompt or "Hasselblad" in prompt:
361
- tech_score += 5
362
- if any(aperture in prompt for aperture in ["f/1.4", "f/2.8", "f/4"]):
363
- tech_score += 5
364
- if any(lens in prompt for lens in ["85mm", "90mm", "80mm"]):
365
- tech_score += 5
366
  score += tech_score
367
  breakdown["technical"] = tech_score
368
 
369
- # Environmental Context (10 points)
370
- env_score = 0
371
- if ultra_analysis["environmental"]["setting_type"]:
372
- env_score += 5
373
- if ultra_analysis["environmental"]["lighting_analysis"]:
374
- env_score += 5
375
- score += env_score
376
- breakdown["environmental"] = env_score
 
 
 
 
 
 
 
377
 
378
  return min(score, 100), breakdown
 
1
  """
2
  Ultra Supreme Analyzer for image analysis and prompt building
3
+ VERSI脫N MEJORADA - Potencia CLIP en lugar de limitarlo
4
  """
5
 
6
  import re
7
  from typing import Dict, List, Any, Tuple
8
+ import logging
9
 
10
+ logger = logging.getLogger(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
  class UltraSupremeAnalyzer:
14
  """
15
+ ULTRA SUPREME ANALYSIS ENGINE - POTENCIA CLIP, NO LO LIMITA
16
  """
17
 
18
  def __init__(self):
19
+ # Palabras a limpiar de las descripciones de CLIP
20
+ self.cleanup_patterns = [
21
+ r'arafed\s*',
22
+ r'there is\s*',
23
+ r'a photo of\s*',
24
+ r'an image of\s*',
25
+ r'a picture of\s*',
26
+ r'inspired by [^,]+,?\s*',
27
+ r'by [A-Z][^,]+,?\s*',
28
+ r'trending on [^,]+,?\s*',
29
+ r'featured on [^,]+,?\s*',
30
+ r'\d+k\s*',
31
+ r'::\s*::\s*',
32
+ r'contest winner,?\s*',
33
+ r'award winning,?\s*',
34
+ ]
35
+
36
+ # Indicadores de calidad t茅cnica
37
+ self.technical_indicators = {
38
+ 'portrait': ['portrait', 'headshot', 'face', 'person', 'man', 'woman', 'child'],
39
+ 'landscape': ['mountain', 'landscape', 'nature', 'outdoor', 'field', 'forest'],
40
+ 'dramatic': ['dramatic', 'light shining', 'silhouette', 'backlit', 'atmospheric'],
41
+ 'professional': ['professional', 'studio', 'formal', 'business'],
42
+ 'artistic': ['artistic', 'creative', 'abstract', 'conceptual'],
43
+ 'documentary': ['documentary', 'candid', 'street', 'journalism', 'authentic']
44
+ }
45
 
46
+ # Mejoras de iluminaci贸n basadas en contexto
47
+ self.lighting_enhancements = {
48
+ 'outdoor': 'natural lighting with golden hour warmth',
49
+ 'mountain': 'dramatic alpine lighting with atmospheric haze',
50
+ 'portrait': 'professional portrait lighting with subtle rim light',
51
+ 'silhouette': 'dramatic backlighting creating ethereal silhouettes',
52
+ 'indoor': 'soft diffused window lighting with gentle shadows',
53
+ 'night': 'cinematic low-key lighting with strategic highlights',
54
+ 'default': 'masterful lighting that enhances depth and dimension'
55
  }
56
 
57
+ # Configuraciones de c谩mara seg煤n el tipo de foto
58
+ self.camera_configs = {
59
+ 'portrait': 'Shot on Hasselblad X2D 100C, 90mm f/2.5 lens at f/2.8',
60
+ 'landscape': 'Shot on Phase One XT, 40mm f/4 lens at f/8',
61
+ 'dramatic': 'Shot on Canon R5, 85mm f/1.2 lens at f/2',
62
+ 'street': 'Shot on Leica M11, 35mm f/1.4 lens at f/2.8',
63
+ 'default': 'Shot on Phase One XF IQ4, 80mm f/2.8 lens at f/4'
 
 
64
  }
65
+
66
+ def clean_clip_description(self, description: str) -> str:
67
+ """Limpia la descripci贸n de CLIP eliminando ruido pero preservando contenido valioso"""
68
+ cleaned = description.lower()
69
 
70
+ # Eliminar patrones de ruido
71
+ for pattern in self.cleanup_patterns:
72
+ cleaned = re.sub(pattern, '', cleaned, flags=re.IGNORECASE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
+ # Limpiar espacios m煤ltiples y comas redundantes
75
+ cleaned = re.sub(r'\s+', ' ', cleaned)
76
+ cleaned = re.sub(r',\s*,+', ',', cleaned)
77
+ cleaned = re.sub(r'^\s*,\s*', '', cleaned)
78
+ cleaned = re.sub(r'\s*,\s*$', '', cleaned)
 
 
 
79
 
80
+ return cleaned.strip()
81
 
82
+ def extract_key_elements(self, clip_fast: str, clip_classic: str, clip_best: str) -> Dict[str, Any]:
83
+ """Extrae elementos clave de las tres descripciones de CLIP"""
84
+
85
+ # Limpiar todas las descripciones
86
+ fast_clean = self.clean_clip_description(clip_fast)
87
+ classic_clean = self.clean_clip_description(clip_classic)
88
+ best_clean = self.clean_clip_description(clip_best)
89
+
90
+ # Combinar informaci贸n 煤nica de las tres fuentes
91
+ all_descriptions = f"{fast_clean} {classic_clean} {best_clean}"
92
+
93
+ # Extraer elementos principales
94
+ elements = {
95
+ 'main_subject': self._extract_main_subject(all_descriptions),
96
+ 'action': self._extract_action(all_descriptions),
97
+ 'location': self._extract_location(all_descriptions),
98
+ 'mood': self._extract_mood(all_descriptions),
99
+ 'special_features': self._extract_special_features(all_descriptions),
100
+ 'technical_style': self._determine_technical_style(all_descriptions),
101
+ 'original_essence': self._preserve_unique_elements(fast_clean, classic_clean, best_clean)
102
+ }
103
 
104
+ return elements
105
+
106
+ def _extract_main_subject(self, description: str) -> str:
107
+ """Extrae el sujeto principal de la descripci贸n"""
108
+ # Buscar patrones comunes de sujetos
109
+ subject_patterns = [
110
+ r'(a |an )?([\w\s]+ )?(man|woman|person|child|boy|girl|people|group)',
111
+ r'(a |an )?([\w\s]+ )?(portrait|face|figure)',
112
+ r'(a |an )?([\w\s]+ )?(landscape|mountain|building|structure)',
113
+ r'(a |an )?([\w\s]+ )?(animal|dog|cat|bird)',
114
+ ]
115
+
116
+ for pattern in subject_patterns:
117
+ match = re.search(pattern, description)
118
+ if match:
119
+ return match.group(0).strip()
120
+
121
+ # Si no encuentra un patr贸n espec铆fico, tomar las primeras palabras significativas
122
+ words = description.split()
123
+ if len(words) > 2:
124
+ return ' '.join(words[:3])
125
+
126
+ return "figure"
127
+
128
+ def _extract_action(self, description: str) -> str:
129
+ """Extrae la acci贸n o pose del sujeto"""
130
+ action_keywords = ['standing', 'sitting', 'walking', 'running', 'looking',
131
+ 'holding', 'wearing', 'posing', 'working', 'playing']
132
+
133
+ for keyword in action_keywords:
134
+ if keyword in description:
135
+ # Extraer contexto alrededor de la palabra clave
136
+ pattern = rf'\b\w*\s*{keyword}\s*\w*\s*\w*'
137
+ match = re.search(pattern, description)
138
+ if match:
139
+ return match.group(0).strip()
140
+
141
+ return ""
142
+
143
+ def _extract_location(self, description: str) -> str:
144
+ """Extrae informaci贸n de ubicaci贸n o ambiente"""
145
+ location_keywords = ['mountain', 'beach', 'forest', 'city', 'street', 'indoor',
146
+ 'outdoor', 'studio', 'nature', 'urban', 'field', 'desert',
147
+ 'ocean', 'lake', 'building', 'home', 'office']
148
 
149
+ found_locations = []
150
+ for keyword in location_keywords:
151
+ if keyword in description:
152
+ found_locations.append(keyword)
 
 
 
 
 
 
 
 
 
 
 
153
 
154
+ if found_locations:
155
+ return ' '.join(found_locations[:2]) # M谩ximo 2 ubicaciones
156
 
157
+ return ""
158
+
159
+ def _extract_mood(self, description: str) -> str:
160
+ """Extrae el mood o atm贸sfera de la imagen"""
161
+ mood_keywords = ['dramatic', 'peaceful', 'serene', 'intense', 'mysterious',
162
+ 'joyful', 'melancholic', 'powerful', 'ethereal', 'moody',
163
+ 'bright', 'dark', 'atmospheric', 'dreamy', 'dynamic']
164
 
165
+ for keyword in mood_keywords:
166
+ if keyword in description:
167
+ return keyword
 
168
 
169
+ return ""
170
+
171
+ def _extract_special_features(self, description: str) -> List[str]:
172
+ """Extrae caracter铆sticas especiales 煤nicas de la descripci贸n"""
173
+ special_patterns = [
174
+ 'light shining on [\w\s]+',
175
+ 'wearing [\w\s]+',
176
+ 'with [\w\s]+ in the background',
177
+ 'surrounded by [\w\s]+',
178
+ '[\w\s]+ lighting',
179
+ '[\w\s]+ atmosphere'
180
+ ]
181
+
182
+ features = []
183
+ for pattern in special_patterns:
184
+ matches = re.findall(pattern, description)
185
+ features.extend(matches)
186
+
187
+ return features[:3] # Limitar a 3 caracter铆sticas especiales
188
+
189
+ def _determine_technical_style(self, description: str) -> str:
190
+ """Determina el estilo t茅cnico m谩s apropiado basado en el contenido"""
191
+ style_scores = {}
192
 
193
+ for style, keywords in self.technical_indicators.items():
194
+ score = sum(1 for keyword in keywords if keyword in description)
195
+ if score > 0:
196
+ style_scores[style] = score
197
 
198
+ if style_scores:
199
+ return max(style_scores, key=style_scores.get)
200
+
201
+ return 'default'
202
+
203
+ def _preserve_unique_elements(self, fast: str, classic: str, best: str) -> str:
204
+ """Preserva elementos 煤nicos e interesantes de las descripciones"""
205
+ # Encontrar frases 煤nicas que aparecen en alguna descripci贸n
206
+ all_words = set(fast.split() + classic.split() + best.split())
207
+ common_words = set(['a', 'an', 'the', 'is', 'are', 'was', 'were', 'with', 'of', 'in', 'on', 'at'])
208
+
209
+ unique_words = all_words - common_words
210
+
211
+ # Buscar frases interesantes que contengan estas palabras 煤nicas
212
+ unique_phrases = []
213
+ for desc in [fast, classic, best]:
214
+ if 'light shining' in desc or 'adventure gear' in desc or 'anthropological' in desc:
215
+ # Estas son frases 煤nicas valiosas
216
+ unique_phrases.append(desc)
217
+ break
218
+
219
+ return ' '.join(unique_phrases[:1]) if unique_phrases else ""
220
+
221
+ def build_ultra_supreme_prompt(self, elements: Dict[str, Any], original_descriptions: List[str]) -> str:
222
+ """Construye un prompt que POTENCIA la visi贸n de CLIP"""
223
+
224
+ components = []
225
+
226
+ # 1. Sujeto principal con art铆culo apropiado
227
+ subject = elements['main_subject']
228
+ if subject:
229
+ # Determinar art铆culo
230
+ if subject[0].lower() in 'aeiou':
231
+ components.append(f"An {subject}")
 
 
 
 
232
  else:
233
+ components.append(f"A {subject}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  else:
235
+ components.append("A figure")
236
+
237
+ # 2. Acci贸n si existe
238
+ if elements['action']:
239
+ components.append(elements['action'])
240
+
241
+ # 3. Caracter铆sticas especiales (esto es lo que hace 煤nica la imagen)
242
+ if elements['special_features']:
243
+ for feature in elements['special_features'][:2]:
244
+ components.append(feature)
245
+
246
+ # 4. Ubicaci贸n/Ambiente
247
+ if elements['location']:
248
+ if 'mountain' in elements['location']:
249
+ components.append("on a majestic mountain peak")
250
+ elif 'outdoor' in elements['location'] or 'nature' in elements['location']:
251
+ components.append("in a breathtaking natural setting")
252
+ else:
253
+ components.append(f"in {elements['location']}")
254
 
255
+ # 5. Mood/Atm贸sfera si existe
256
+ if elements['mood']:
257
+ components.append(f"capturing a {elements['mood']} atmosphere")
258
 
259
+ # 6. Iluminaci贸n basada en contexto
260
+ lighting_context = elements['location'] or elements['technical_style']
261
+ lighting = self.lighting_enhancements.get(lighting_context, self.lighting_enhancements['default'])
262
+ components.append(f"illuminated with {lighting}")
263
 
264
+ # 7. Configuraci贸n t茅cnica de c谩mara
265
+ camera_setup = self.camera_configs.get(elements['technical_style'], self.camera_configs['default'])
266
+ components.append(camera_setup)
267
+
268
+ # 8. Estilo fotogr谩fico final
269
+ if elements['technical_style'] == 'portrait':
270
+ components.append("masterful portrait photography")
271
+ elif elements['technical_style'] == 'landscape':
272
+ components.append("epic landscape photography")
273
+ elif elements['technical_style'] == 'dramatic':
274
+ components.append("cinematic photography with powerful visual impact")
275
+ elif elements['technical_style'] == 'documentary':
276
+ components.append("authentic documentary photography")
277
+ else:
278
+ components.append("professional photography with exceptional detail")
279
 
280
+ # 9. A帽adir esencia 煤nica preservada si existe
281
+ if elements['original_essence'] and len(elements['original_essence']) > 10:
282
+ # Incluir elementos 煤nicos que CLIP detect贸
283
+ logger.info(f"Preservando esencia 煤nica: {elements['original_essence']}")
284
 
285
+ # Construir prompt final
286
  prompt = ", ".join(components)
287
 
288
+ # Limpieza final
289
  prompt = re.sub(r'\s+', ' ', prompt)
290
  prompt = re.sub(r',\s*,+', ',', prompt)
291
  prompt = re.sub(r'\s*,\s*', ', ', prompt)
 
292
 
293
+ # Capitalizar primera letra
294
  if prompt:
295
  prompt = prompt[0].upper() + prompt[1:]
296
 
297
+ logger.info(f"Prompt generado: {prompt}")
298
+
299
  return prompt
300
 
301
+ def ultra_supreme_analysis(self, clip_fast: str, clip_classic: str, clip_best: str) -> Dict[str, Any]:
302
+ """An谩lisis que POTENCIA la informaci贸n de CLIP en lugar de limitarla"""
303
+
304
+ logger.info("Iniciando an谩lisis MEJORADO que potencia CLIP")
305
+
306
+ # Extraer elementos clave de las descripciones
307
+ elements = self.extract_key_elements(clip_fast, clip_classic, clip_best)
308
+
309
+ # Construir resultado del an谩lisis
310
+ result = {
311
+ "elements": elements,
312
+ "technical_style": elements['technical_style'],
313
+ "unique_features": elements['special_features'],
314
+ "preserved_essence": elements['original_essence'],
315
+ "mood": elements['mood'],
316
+ "location": elements['location']
317
+ }
318
+
319
+ return result
320
+
321
+ def build_ultra_supreme_prompt(self, ultra_analysis: Dict[str, Any], clip_results: List[str]) -> str:
322
+ """Versi贸n p煤blica del m茅todo para compatibilidad"""
323
+ return self.build_ultra_supreme_prompt(ultra_analysis['elements'], clip_results)
324
+
325
  def calculate_ultra_supreme_score(self, prompt: str, ultra_analysis: Dict[str, Any]) -> Tuple[int, Dict[str, int]]:
326
+ """Calcula score basado en la riqueza del prompt generado"""
327
 
328
  score = 0
329
  breakdown = {}
330
 
331
+ # Estructura (20 puntos)
332
  structure_score = 0
333
+ if prompt.startswith(("A ", "An ")):
334
+ structure_score += 10
335
+ if prompt.count(",") >= 5:
336
  structure_score += 10
337
  score += structure_score
338
  breakdown["structure"] = structure_score
339
 
340
+ # Elementos 煤nicos preservados (30 puntos)
341
+ unique_score = 0
342
+ if ultra_analysis.get('unique_features'):
343
+ unique_score += len(ultra_analysis['unique_features']) * 10
344
+ unique_score = min(unique_score, 30)
345
+ score += unique_score
346
+ breakdown["unique"] = unique_score
347
+
348
+ # Contexto t茅cnico (20 puntos)
 
 
 
 
 
 
 
 
 
 
 
349
  tech_score = 0
350
+ if "Shot on" in prompt:
351
+ tech_score += 10
352
+ if any(term in prompt for term in ["f/", "mm"]):
353
+ tech_score += 10
 
 
354
  score += tech_score
355
  breakdown["technical"] = tech_score
356
 
357
+ # Mood y atm贸sfera (15 puntos)
358
+ mood_score = 0
359
+ if ultra_analysis.get('mood'):
360
+ mood_score += 15
361
+ score += mood_score
362
+ breakdown["mood"] = mood_score
363
+
364
+ # Calidad descriptiva (15 puntos)
365
+ desc_score = 0
366
+ if len(prompt) > 100:
367
+ desc_score += 10
368
+ if any(term in prompt for term in ["masterful", "epic", "cinematic", "exceptional"]):
369
+ desc_score += 5
370
+ score += desc_score
371
+ breakdown["descriptive"] = desc_score
372
 
373
  return min(score, 100), breakdown