Spaces:
Running
on
Zero
Running
on
Zero
Update optimizer.py
Browse files- optimizer.py +288 -304
optimizer.py
CHANGED
@@ -1,333 +1,317 @@
|
|
1 |
"""
|
2 |
-
Ultra Supreme
|
|
|
3 |
"""
|
4 |
|
5 |
-
|
6 |
-
import
|
7 |
import gc
|
8 |
import logging
|
9 |
-
import
|
10 |
-
import
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
warnings.filterwarnings("ignore", category=FutureWarning)
|
17 |
-
warnings.filterwarnings("ignore", category=UserWarning)
|
18 |
-
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
19 |
|
20 |
-
# Configure logging
|
21 |
-
logging.basicConfig(level=logging.INFO)
|
22 |
logger = logging.getLogger(__name__)
|
23 |
|
24 |
-
# Initialize the optimizer globally
|
25 |
-
optimizer = UltraSupremeOptimizer()
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
def
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
# Find appropriate grade based on score
|
34 |
-
grade_info = None
|
35 |
-
for threshold, grade_data in sorted(SCORE_GRADES.items(), reverse=True):
|
36 |
-
if score >= threshold:
|
37 |
-
grade_info = grade_data
|
38 |
-
break
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
logger.error(f"Ultra supreme wrapper error: {e}")
|
55 |
-
return "❌ Processing failed", f"Error: {str(e)}", '<div style="text-align: center; color: red;">Error</div>'
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
def
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
def
|
67 |
-
|
68 |
-
|
69 |
-
css = """
|
70 |
-
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap');
|
71 |
-
|
72 |
-
.gradio-container {
|
73 |
-
max-width: 1600px !important;
|
74 |
-
margin: 0 auto !important;
|
75 |
-
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
|
76 |
-
background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%) !important;
|
77 |
-
}
|
78 |
-
|
79 |
-
/* FIX CRÍTICO PARA TEXTO BLANCO SOBRE BLANCO */
|
80 |
-
.markdown-text, .markdown-text *,
|
81 |
-
.prose, .prose *,
|
82 |
-
.gr-markdown, .gr-markdown *,
|
83 |
-
div[class*="markdown"], div[class*="markdown"] * {
|
84 |
-
color: #1f2937 !important;
|
85 |
-
}
|
86 |
-
|
87 |
-
.markdown-text h1, .markdown-text h2, .markdown-text h3,
|
88 |
-
.prose h1, .prose h2, .prose h3,
|
89 |
-
.gr-markdown h1, .gr-markdown h2, .gr-markdown h3 {
|
90 |
-
color: #111827 !important;
|
91 |
-
font-weight: 700 !important;
|
92 |
-
}
|
93 |
-
|
94 |
-
.markdown-text p, .markdown-text li, .markdown-text ul, .markdown-text ol,
|
95 |
-
.prose p, .prose li, .prose ul, .prose ol,
|
96 |
-
.gr-markdown p, .gr-markdown li, .gr-markdown ul, .gr-markdown ol {
|
97 |
-
color: #374151 !important;
|
98 |
-
}
|
99 |
-
|
100 |
-
.markdown-text strong, .prose strong, .gr-markdown strong {
|
101 |
-
color: #111827 !important;
|
102 |
-
font-weight: 700 !important;
|
103 |
-
}
|
104 |
-
|
105 |
-
/* Asegurar que las listas sean visibles */
|
106 |
-
ul, ol {
|
107 |
-
color: #374151 !important;
|
108 |
-
}
|
109 |
-
|
110 |
-
li {
|
111 |
-
color: #374151 !important;
|
112 |
-
}
|
113 |
-
|
114 |
-
/* Bullets de listas */
|
115 |
-
ul li::marker {
|
116 |
-
color: #374151 !important;
|
117 |
-
}
|
118 |
-
|
119 |
-
.main-header {
|
120 |
-
text-align: center;
|
121 |
-
padding: 3rem 0 4rem 0;
|
122 |
-
background: linear-gradient(135deg, #0c0a09 0%, #1c1917 30%, #292524 60%, #44403c 100%);
|
123 |
-
color: white;
|
124 |
-
margin: -2rem -2rem 3rem -2rem;
|
125 |
-
border-radius: 0 0 32px 32px;
|
126 |
-
box-shadow: 0 20px 50px -10px rgba(0, 0, 0, 0.25);
|
127 |
-
position: relative;
|
128 |
-
overflow: hidden;
|
129 |
-
}
|
130 |
-
|
131 |
-
.main-header::before {
|
132 |
-
content: '';
|
133 |
-
position: absolute;
|
134 |
-
top: 0;
|
135 |
-
left: 0;
|
136 |
-
right: 0;
|
137 |
-
bottom: 0;
|
138 |
-
background: linear-gradient(45deg, rgba(59, 130, 246, 0.1) 0%, rgba(147, 51, 234, 0.1) 50%, rgba(236, 72, 153, 0.1) 100%);
|
139 |
-
z-index: 1;
|
140 |
-
}
|
141 |
-
|
142 |
-
.main-title {
|
143 |
-
font-size: 4rem !important;
|
144 |
-
font-weight: 900 !important;
|
145 |
-
margin: 0 0 1rem 0 !important;
|
146 |
-
letter-spacing: -0.05em !important;
|
147 |
-
background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 25%, #8b5cf6 50%, #a855f7 75%, #ec4899 100%);
|
148 |
-
-webkit-background-clip: text;
|
149 |
-
-webkit-text-fill-color: transparent;
|
150 |
-
background-clip: text;
|
151 |
-
position: relative;
|
152 |
-
z-index: 2;
|
153 |
-
}
|
154 |
-
|
155 |
-
.subtitle {
|
156 |
-
font-size: 1.5rem !important;
|
157 |
-
font-weight: 500 !important;
|
158 |
-
opacity: 0.95 !important;
|
159 |
-
margin: 0 !important;
|
160 |
-
position: relative;
|
161 |
-
z-index: 2;
|
162 |
-
color: #ffffff !important;
|
163 |
-
}
|
164 |
-
|
165 |
-
.prompt-output {
|
166 |
-
font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace !important;
|
167 |
-
font-size: 15px !important;
|
168 |
-
line-height: 1.8 !important;
|
169 |
-
background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%) !important;
|
170 |
-
border: 2px solid #e2e8f0 !important;
|
171 |
-
border-radius: 20px !important;
|
172 |
-
padding: 2.5rem !important;
|
173 |
-
box-shadow: 0 20px 50px -10px rgba(0, 0, 0, 0.1) !important;
|
174 |
-
transition: all 0.3s ease !important;
|
175 |
-
color: #1f2937 !important;
|
176 |
-
}
|
177 |
-
|
178 |
-
.prompt-output:hover {
|
179 |
-
box-shadow: 0 25px 60px -5px rgba(0, 0, 0, 0.15) !important;
|
180 |
-
transform: translateY(-2px) !important;
|
181 |
-
}
|
182 |
-
|
183 |
-
/* Fix para el output de información */
|
184 |
-
.gr-textbox label {
|
185 |
-
color: #374151 !important;
|
186 |
-
}
|
187 |
-
|
188 |
-
/* Fix para footer */
|
189 |
-
footer, .footer, [class*="footer"] {
|
190 |
-
color: #374151 !important;
|
191 |
-
}
|
192 |
-
|
193 |
-
footer *, .footer *, [class*="footer"] * {
|
194 |
-
color: #374151 !important;
|
195 |
-
}
|
196 |
-
|
197 |
-
footer a, .footer a, [class*="footer"] a {
|
198 |
-
color: #3b82f6 !important;
|
199 |
-
text-decoration: underline;
|
200 |
-
}
|
201 |
-
|
202 |
-
footer a:hover, .footer a:hover, [class*="footer"] a:hover {
|
203 |
-
color: #2563eb !important;
|
204 |
-
}
|
205 |
-
|
206 |
-
/* Botones */
|
207 |
-
.gr-button-primary {
|
208 |
-
background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%) !important;
|
209 |
-
border: none !important;
|
210 |
-
color: white !important;
|
211 |
-
}
|
212 |
-
|
213 |
-
.gr-button-primary:hover {
|
214 |
-
background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%) !important;
|
215 |
-
transform: translateY(-1px);
|
216 |
-
box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
|
217 |
-
}
|
218 |
-
|
219 |
-
/* Asegurar que TODOS los elementos de texto sean visibles */
|
220 |
-
* {
|
221 |
-
-webkit-text-fill-color: initial !important;
|
222 |
-
}
|
223 |
-
|
224 |
-
/* Solo el título principal mantiene su gradiente */
|
225 |
-
.main-title {
|
226 |
-
-webkit-text-fill-color: transparent !important;
|
227 |
-
}
|
228 |
-
"""
|
229 |
-
|
230 |
-
with gr.Blocks(
|
231 |
-
theme=gr.themes.Soft(),
|
232 |
-
title="🚀 Ultra Supreme Flux Optimizer",
|
233 |
-
css=css
|
234 |
-
) as interface:
|
235 |
|
236 |
-
|
237 |
-
|
238 |
-
<div class="main-title">🚀 ULTRA SUPREME FLUX OPTIMIZER</div>
|
239 |
-
<div class="subtitle">Maximum Absolute Intelligence • Triple CLIP Analysis • Zero Compromise • Research Supremacy</div>
|
240 |
-
</div>
|
241 |
-
""")
|
242 |
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
height=500
|
251 |
-
)
|
252 |
-
|
253 |
-
analyze_btn = gr.Button(
|
254 |
-
"🚀 ULTRA SUPREME ANALYSIS",
|
255 |
-
variant="primary",
|
256 |
-
size="lg"
|
257 |
-
)
|
258 |
-
|
259 |
-
gr.Markdown("""
|
260 |
-
### 🔬 Maximum Absolute Intelligence
|
261 |
-
|
262 |
-
**🚀 Triple CLIP Interrogation:**
|
263 |
-
• Fast analysis for broad contextual mapping
|
264 |
-
• Classic analysis for detailed feature extraction
|
265 |
-
• Best analysis for maximum depth intelligence
|
266 |
-
|
267 |
-
**🧠 Ultra Deep Feature Extraction:**
|
268 |
-
• Micro-age detection with confidence scoring
|
269 |
-
• Cultural/religious context with semantic analysis
|
270 |
-
• Facial micro-features and expression mapping
|
271 |
-
• Emotional state and micro-expression detection
|
272 |
-
• Environmental lighting and atmospheric analysis
|
273 |
-
• Body language and pose interpretation
|
274 |
-
• Technical photography optimization
|
275 |
-
|
276 |
-
**⚡ Absolute Maximum Intelligence** - No configuration, no limits, no compromise.
|
277 |
-
""")
|
278 |
-
|
279 |
-
with gr.Column(scale=1):
|
280 |
-
gr.Markdown("## ⚡ Ultra Supreme Result")
|
281 |
-
|
282 |
-
prompt_output = gr.Textbox(
|
283 |
-
label="🚀 Ultra Supreme Optimized Flux Prompt",
|
284 |
-
placeholder="Upload an image to witness absolute maximum intelligence analysis...",
|
285 |
-
lines=12,
|
286 |
-
max_lines=20,
|
287 |
-
elem_classes=["prompt-output"],
|
288 |
-
show_copy_button=True
|
289 |
-
)
|
290 |
-
|
291 |
-
score_output = gr.HTML(
|
292 |
-
value='<div style="text-align: center; padding: 1rem;"><div style="font-size: 2rem; color: #ccc;">--</div><div style="font-size: 0.875rem; color: #999;">Ultra Supreme Score</div></div>'
|
293 |
-
)
|
294 |
-
|
295 |
-
info_output = gr.Markdown(value="")
|
296 |
-
|
297 |
-
clear_btn = gr.Button("🗑️ Clear Ultra Analysis", size="sm")
|
298 |
|
299 |
-
#
|
300 |
-
|
301 |
-
|
302 |
-
inputs=[image_input],
|
303 |
-
outputs=[prompt_output, info_output, score_output]
|
304 |
-
)
|
305 |
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
|
|
|
|
310 |
|
311 |
-
|
312 |
-
---
|
313 |
-
### 🏆 Ultra Supreme Research Foundation
|
314 |
|
315 |
-
|
316 |
-
|
317 |
-
|
|
|
318 |
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
|
|
|
|
|
|
|
|
|
|
323 |
|
|
|
|
|
|
|
|
|
324 |
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
|
|
|
1 |
"""
|
2 |
+
Ultra Supreme Optimizer - Main optimization engine for image analysis
|
3 |
+
VERSIÓN MEJORADA - Usa el prompt completo de CLIP Interrogator
|
4 |
"""
|
5 |
|
6 |
+
# IMPORTANT: spaces must be imported BEFORE torch or any CUDA-using library
|
7 |
+
import spaces
|
8 |
import gc
|
9 |
import logging
|
10 |
+
from datetime import datetime
|
11 |
+
from typing import Tuple, Dict, Any, Optional
|
12 |
|
13 |
+
import torch
|
14 |
+
import numpy as np
|
15 |
+
from PIL import Image
|
16 |
+
from clip_interrogator import Config, Interrogator
|
17 |
|
18 |
+
from analyzer import UltraSupremeAnalyzer
|
|
|
|
|
|
|
19 |
|
|
|
|
|
20 |
logger = logging.getLogger(__name__)
|
21 |
|
|
|
|
|
22 |
|
23 |
+
class UltraSupremeOptimizer:
|
24 |
+
"""Main optimizer class for ultra supreme image analysis"""
|
25 |
+
|
26 |
+
def __init__(self):
|
27 |
+
self.interrogator: Optional[Interrogator] = None
|
28 |
+
self.analyzer = UltraSupremeAnalyzer()
|
29 |
+
self.usage_count = 0
|
30 |
+
self.device = self._get_device()
|
31 |
+
self.is_initialized = False
|
32 |
+
|
33 |
+
@staticmethod
|
34 |
+
def _get_device() -> str:
|
35 |
+
"""Determine the best available device for computation"""
|
36 |
+
if torch.cuda.is_available():
|
37 |
+
return "cuda"
|
38 |
+
elif torch.backends.mps.is_available():
|
39 |
+
return "mps"
|
40 |
+
else:
|
41 |
+
return "cpu"
|
42 |
|
43 |
+
def initialize_model(self) -> bool:
|
44 |
+
"""Initialize the CLIP interrogator model"""
|
45 |
+
if self.is_initialized:
|
46 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
try:
|
49 |
+
config = Config(
|
50 |
+
clip_model_name="ViT-L-14/openai",
|
51 |
+
download_cache=True,
|
52 |
+
chunk_size=2048,
|
53 |
+
quiet=True,
|
54 |
+
device=self.device
|
55 |
+
)
|
56 |
+
|
57 |
+
self.interrogator = Interrogator(config)
|
58 |
+
self.is_initialized = True
|
59 |
+
|
60 |
+
# Clean up memory after initialization
|
61 |
+
if self.device == "cpu":
|
62 |
+
gc.collect()
|
63 |
+
else:
|
64 |
+
torch.cuda.empty_cache()
|
65 |
+
|
66 |
+
return True
|
67 |
+
|
68 |
+
except Exception as e:
|
69 |
+
logger.error(f"Initialization error: {e}")
|
70 |
+
return False
|
71 |
+
|
72 |
+
def optimize_image(self, image: Any) -> Optional[Image.Image]:
|
73 |
+
"""Optimize image for processing"""
|
74 |
+
if image is None:
|
75 |
+
return None
|
76 |
+
|
77 |
+
try:
|
78 |
+
# Convert to PIL Image if necessary
|
79 |
+
if isinstance(image, np.ndarray):
|
80 |
+
image = Image.fromarray(image)
|
81 |
+
elif not isinstance(image, Image.Image):
|
82 |
+
image = Image.open(image)
|
83 |
|
84 |
+
# Convert to RGB if necessary
|
85 |
+
if image.mode != 'RGB':
|
86 |
+
image = image.convert('RGB')
|
87 |
+
|
88 |
+
# Resize if too large
|
89 |
+
max_size = 768 if self.device != "cpu" else 512
|
90 |
+
if image.size[0] > max_size or image.size[1] > max_size:
|
91 |
+
image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
92 |
+
|
93 |
+
return image
|
94 |
+
|
95 |
+
except Exception as e:
|
96 |
+
logger.error(f"Image optimization error: {e}")
|
97 |
+
return None
|
98 |
+
|
99 |
+
def apply_flux_rules(self, base_prompt: str) -> str:
|
100 |
+
"""Aplica las reglas de Flux a un prompt base de CLIP Interrogator"""
|
101 |
+
|
102 |
+
# Limpiar el prompt de elementos no deseados
|
103 |
+
cleanup_patterns = [
|
104 |
+
r',\s*trending on artstation',
|
105 |
+
r',\s*trending on [^,]+',
|
106 |
+
r',\s*\d+k\s*',
|
107 |
+
r',\s*\d+k resolution',
|
108 |
+
r',\s*artstation',
|
109 |
+
r',\s*concept art',
|
110 |
+
r',\s*digital art',
|
111 |
+
r',\s*by greg rutkowski', # Remover artistas genéricos overused
|
112 |
+
]
|
113 |
+
|
114 |
+
cleaned_prompt = base_prompt
|
115 |
+
for pattern in cleanup_patterns:
|
116 |
+
cleaned_prompt = re.sub(pattern, '', cleaned_prompt, flags=re.IGNORECASE)
|
117 |
+
|
118 |
+
# Detectar el tipo de imagen para añadir configuración de cámara apropiada
|
119 |
+
camera_config = ""
|
120 |
+
if any(word in base_prompt.lower() for word in ['portrait', 'person', 'man', 'woman', 'face']):
|
121 |
+
camera_config = ", Shot on Hasselblad X2D 100C, 90mm f/2.5 lens at f/2.8, professional portrait photography"
|
122 |
+
elif any(word in base_prompt.lower() for word in ['landscape', 'mountain', 'nature', 'outdoor']):
|
123 |
+
camera_config = ", Shot on Phase One XT, 40mm f/4 lens at f/8, epic landscape photography"
|
124 |
+
elif any(word in base_prompt.lower() for word in ['street', 'urban', 'city']):
|
125 |
+
camera_config = ", Shot on Leica M11, 35mm f/1.4 lens at f/2.8, documentary street photography"
|
126 |
+
else:
|
127 |
+
camera_config = ", Shot on Phase One XF IQ4, 80mm f/2.8 lens at f/4, professional photography"
|
128 |
+
|
129 |
+
# Añadir mejoras de iluminación si no están presentes
|
130 |
+
if 'lighting' not in cleaned_prompt.lower():
|
131 |
+
if 'dramatic' in cleaned_prompt.lower():
|
132 |
+
cleaned_prompt += ", dramatic cinematic lighting"
|
133 |
+
elif 'portrait' in cleaned_prompt.lower():
|
134 |
+
cleaned_prompt += ", professional studio lighting with subtle rim light"
|
135 |
+
else:
|
136 |
+
cleaned_prompt += ", masterful natural lighting"
|
137 |
+
|
138 |
+
# Construir el prompt final
|
139 |
+
final_prompt = cleaned_prompt + camera_config
|
140 |
+
|
141 |
+
# Asegurar que empiece con mayúscula
|
142 |
+
final_prompt = final_prompt[0].upper() + final_prompt[1:] if final_prompt else final_prompt
|
143 |
|
144 |
+
# Limpiar espacios y comas duplicadas
|
145 |
+
final_prompt = re.sub(r'\s+', ' ', final_prompt)
|
146 |
+
final_prompt = re.sub(r',\s*,+', ',', final_prompt)
|
147 |
|
148 |
+
return final_prompt
|
|
|
|
|
149 |
|
150 |
+
@spaces.GPU
|
151 |
+
def generate_ultra_supreme_prompt(self, image: Any) -> Tuple[str, str, int, Dict[str, int]]:
|
152 |
+
"""
|
153 |
+
Generate ultra supreme prompt from image usando el pipeline completo
|
154 |
+
|
155 |
+
Returns:
|
156 |
+
Tuple of (prompt, analysis_info, score, breakdown)
|
157 |
+
"""
|
158 |
+
try:
|
159 |
+
# Initialize model if needed
|
160 |
+
if not self.is_initialized:
|
161 |
+
if not self.initialize_model():
|
162 |
+
return "❌ Model initialization failed.", "Please refresh and try again.", 0, {}
|
163 |
+
|
164 |
+
# Validate input
|
165 |
+
if image is None:
|
166 |
+
return "❌ Please upload an image.", "No image provided.", 0, {}
|
167 |
+
|
168 |
+
self.usage_count += 1
|
169 |
+
|
170 |
+
# Optimize image
|
171 |
+
image = self.optimize_image(image)
|
172 |
+
if image is None:
|
173 |
+
return "❌ Image processing failed.", "Invalid image format.", 0, {}
|
174 |
+
|
175 |
+
start_time = datetime.now()
|
176 |
+
|
177 |
+
# NUEVO PIPELINE: Usar CLIP Interrogator completo
|
178 |
+
logger.info("ULTRA SUPREME ANALYSIS - Usando pipeline completo de CLIP Interrogator")
|
179 |
+
|
180 |
+
# 1. Obtener el prompt COMPLETO de CLIP Interrogator (no solo análisis)
|
181 |
+
# Este incluye descripción + artistas + estilos + mediums
|
182 |
+
full_prompt = self.interrogator.interrogate(image)
|
183 |
+
logger.info(f"Prompt completo de CLIP Interrogator: {full_prompt}")
|
184 |
+
|
185 |
+
# 2. También obtener los análisis individuales para el reporte
|
186 |
+
clip_fast = self.interrogator.interrogate_fast(image)
|
187 |
+
clip_classic = self.interrogator.interrogate_classic(image)
|
188 |
+
|
189 |
+
logger.info(f"Análisis Fast: {clip_fast}")
|
190 |
+
logger.info(f"Análisis Classic: {clip_classic}")
|
191 |
+
|
192 |
+
# 3. Aplicar reglas de Flux al prompt completo
|
193 |
+
import re
|
194 |
+
optimized_prompt = self.apply_flux_rules(full_prompt)
|
195 |
+
|
196 |
+
# 4. Crear análisis para el reporte (simplificado)
|
197 |
+
analysis_summary = {
|
198 |
+
"base_prompt": full_prompt,
|
199 |
+
"clip_fast": clip_fast,
|
200 |
+
"clip_classic": clip_classic,
|
201 |
+
"optimized": optimized_prompt,
|
202 |
+
"detected_style": self._detect_style(full_prompt),
|
203 |
+
"detected_subject": self._detect_subject(full_prompt)
|
204 |
+
}
|
205 |
+
|
206 |
+
# 5. Calcular score basado en la riqueza del prompt
|
207 |
+
score = self._calculate_score(optimized_prompt, full_prompt)
|
208 |
+
breakdown = {
|
209 |
+
"base_quality": min(len(full_prompt) // 10, 25),
|
210 |
+
"technical_enhancement": 25 if "Shot on" in optimized_prompt else 0,
|
211 |
+
"lighting_quality": 25 if "lighting" in optimized_prompt.lower() else 0,
|
212 |
+
"composition": 25 if any(word in optimized_prompt.lower() for word in ["professional", "masterful", "epic"]) else 0
|
213 |
+
}
|
214 |
+
score = sum(breakdown.values())
|
215 |
+
|
216 |
+
end_time = datetime.now()
|
217 |
+
duration = (end_time - start_time).total_seconds()
|
218 |
+
|
219 |
+
# Memory cleanup
|
220 |
+
if self.device == "cpu":
|
221 |
+
gc.collect()
|
222 |
+
else:
|
223 |
+
torch.cuda.empty_cache()
|
224 |
+
|
225 |
+
# Generate analysis report
|
226 |
+
analysis_info = self._generate_analysis_report(
|
227 |
+
analysis_summary, score, breakdown, duration
|
228 |
+
)
|
229 |
+
|
230 |
+
return optimized_prompt, analysis_info, score, breakdown
|
231 |
+
|
232 |
+
except Exception as e:
|
233 |
+
logger.error(f"Ultra supreme generation error: {e}")
|
234 |
+
return f"❌ Error: {str(e)}", "Please try with a different image.", 0, {}
|
235 |
|
236 |
+
def _detect_style(self, prompt: str) -> str:
|
237 |
+
"""Detecta el estilo principal del prompt"""
|
238 |
+
styles = {
|
239 |
+
"portrait": ["portrait", "person", "face", "headshot"],
|
240 |
+
"landscape": ["landscape", "mountain", "nature", "scenery"],
|
241 |
+
"street": ["street", "urban", "city"],
|
242 |
+
"artistic": ["artistic", "abstract", "conceptual"],
|
243 |
+
"dramatic": ["dramatic", "cinematic", "moody"]
|
244 |
+
}
|
245 |
+
|
246 |
+
for style_name, keywords in styles.items():
|
247 |
+
if any(keyword in prompt.lower() for keyword in keywords):
|
248 |
+
return style_name
|
249 |
+
|
250 |
+
return "general"
|
251 |
|
252 |
+
def _detect_subject(self, prompt: str) -> str:
|
253 |
+
"""Detecta el sujeto principal del prompt"""
|
254 |
+
# Tomar las primeras palabras significativas
|
255 |
+
words = prompt.split(',')[0].split()
|
256 |
+
if len(words) > 3:
|
257 |
+
return ' '.join(words[:4])
|
258 |
+
return prompt.split(',')[0]
|
259 |
|
260 |
+
def _calculate_score(self, optimized_prompt: str, base_prompt: str) -> int:
|
261 |
+
"""Calcula el score basado en la calidad del prompt"""
|
262 |
+
score = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
|
264 |
+
# Base score por longitud y riqueza
|
265 |
+
score += min(len(base_prompt) // 10, 25)
|
|
|
|
|
|
|
|
|
266 |
|
267 |
+
# Technical enhancement
|
268 |
+
if "Shot on" in optimized_prompt:
|
269 |
+
score += 25
|
270 |
+
|
271 |
+
# Lighting quality
|
272 |
+
if "lighting" in optimized_prompt.lower():
|
273 |
+
score += 25
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
|
275 |
+
# Professional quality
|
276 |
+
if any(word in optimized_prompt.lower() for word in ["professional", "masterful", "epic", "cinematic"]):
|
277 |
+
score += 25
|
|
|
|
|
|
|
278 |
|
279 |
+
return min(score, 100)
|
280 |
+
|
281 |
+
def _generate_analysis_report(self, analysis: Dict[str, Any],
|
282 |
+
score: int, breakdown: Dict[str, int],
|
283 |
+
duration: float) -> str:
|
284 |
+
"""Generate detailed analysis report"""
|
285 |
|
286 |
+
gpu_status = "⚡ ZeroGPU" if torch.cuda.is_available() else "💻 CPU"
|
|
|
|
|
287 |
|
288 |
+
# Extraer información clave
|
289 |
+
detected_style = analysis.get("detected_style", "general").title()
|
290 |
+
detected_subject = analysis.get("detected_subject", "Unknown")
|
291 |
+
base_prompt_preview = analysis.get("base_prompt", "")[:100] + "..." if len(analysis.get("base_prompt", "")) > 100 else analysis.get("base_prompt", "")
|
292 |
|
293 |
+
analysis_info = f"""**🚀 ULTRA SUPREME ANALYSIS COMPLETE**
|
294 |
+
**Processing:** {gpu_status} • {duration:.1f}s • Full CLIP Interrogator Pipeline
|
295 |
+
**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)})
|
296 |
+
**Generation:** #{self.usage_count}
|
297 |
+
|
298 |
+
**🧠 INTELLIGENT DETECTION:**
|
299 |
+
- **Detected Style:** {detected_style}
|
300 |
+
- **Main Subject:** {detected_subject}
|
301 |
+
- **Pipeline:** CLIP Interrogator → Flux Optimization → Technical Enhancement
|
302 |
|
303 |
+
**📊 CLIP INTERROGATOR ANALYSIS:**
|
304 |
+
- **Base Prompt:** {base_prompt_preview}
|
305 |
+
- **Fast Analysis:** {analysis.get('clip_fast', '')[:80]}...
|
306 |
+
- **Classic Analysis:** {analysis.get('clip_classic', '')[:80]}...
|
307 |
|
308 |
+
**⚡ OPTIMIZATION APPLIED:**
|
309 |
+
- ✅ Preserved CLIP Interrogator's rich description
|
310 |
+
- ✅ Added professional camera specifications
|
311 |
+
- ✅ Enhanced lighting descriptions
|
312 |
+
- ✅ Applied Flux-specific optimizations
|
313 |
+
- ✅ Removed redundant/generic elements
|
314 |
+
|
315 |
+
**🔬 Powered by Pariente AI Research + CLIP Interrogator**"""
|
316 |
+
|
317 |
+
return analysis_info
|