Spaces:
Running
on
Zero
Running
on
Zero
File size: 22,145 Bytes
ce98582 30c8cdc cf7819d 30c8cdc 3f0776a 30c8cdc 3f0776a ce98582 3f0776a 30c8cdc cf7819d 30c8cdc 3f0776a ce98582 3f0776a d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c ce98582 b3f99d4 30c8cdc ce98582 30c8cdc 3f0776a d8ac97c 30c8cdc 3f0776a 30c8cdc b3f99d4 3f0776a 30c8cdc cf7819d 3f0776a 30c8cdc 3f0776a 30c8cdc 3f0776a 30c8cdc ce98582 3f0776a 30c8cdc 3f0776a cf7819d d8ac97c 30c8cdc 3f0776a b3f99d4 3f0776a 30c8cdc b3f99d4 30c8cdc 3f0776a b3f99d4 30c8cdc 3f0776a 30c8cdc d8ac97c ce98582 d8ac97c ce98582 d8ac97c 30c8cdc 3f0776a ce98582 3f0776a d8ac97c ce98582 cf7819d d8ac97c 3f0776a d8ac97c ce98582 30c8cdc d8ac97c 30c8cdc ce98582 30c8cdc b3f99d4 3f0776a ce98582 3f0776a d8ac97c b3f99d4 d8ac97c b3f99d4 d8ac97c b3f99d4 30c8cdc 3f0776a d8ac97c 30c8cdc cf7819d ce98582 30c8cdc ce98582 30c8cdc ce98582 30c8cdc ce98582 30c8cdc ce98582 30c8cdc ce98582 30c8cdc ce98582 30c8cdc ce98582 30c8cdc ce98582 30c8cdc cf7819d ce98582 d8ac97c ce98582 30c8cdc d8ac97c ce98582 30c8cdc ce98582 30c8cdc d8ac97c 30c8cdc ce98582 d8ac97c ce98582 d8ac97c 30c8cdc d8ac97c ce98582 30c8cdc d8ac97c 30c8cdc b3f99d4 30c8cdc ce98582 d8ac97c ce98582 b3f99d4 30c8cdc d8ac97c 30c8cdc cf7819d ce98582 d8ac97c ce98582 d8ac97c ce98582 d8ac97c 30c8cdc d8ac97c ce98582 b3f99d4 d8ac97c ce98582 30c8cdc 3f0776a ce98582 30c8cdc d8ac97c 30c8cdc cf7819d 30c8cdc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
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
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 DeepFluxAnalyzer:
"""
Deep analysis engine that understands image content and applies Flux rules intelligently
"""
def __init__(self):
self.forbidden_elements = ["++", "weights", "white background [en dev]"]
# Deep vocabulary for intelligent analysis
self.age_descriptors = {
"young": ["young", "youthful", "fresh-faced"],
"middle": ["middle-aged", "mature"],
"elderly": ["elderly", "aged", "distinguished", "weathered"]
}
self.facial_features = {
"beard": ["bearded", "with a full beard", "with facial hair", "with a silver beard", "with a gray beard"],
"glasses": ["wearing glasses", "with wire-frame glasses", "with spectacles", "with eyeglasses"],
"eyes": ["intense gaze", "piercing eyes", "contemplative expression", "focused stare"]
}
self.clothing_religious = {
"hat": ["black hat", "traditional hat", "religious headwear", "Orthodox hat"],
"clothing": ["traditional clothing", "religious attire", "formal wear", "dark clothing"]
}
self.settings_detailed = {
"indoor": ["indoor setting", "interior space", "indoor environment"],
"outdoor": ["outdoor setting", "natural environment", "exterior location"],
"studio": ["studio setting", "controlled environment", "professional backdrop"]
}
self.lighting_advanced = {
"portrait": ["dramatic portrait lighting", "studio portrait lighting", "professional portrait setup"],
"natural": ["natural lighting", "window light", "ambient illumination"],
"dramatic": ["dramatic lighting", "high contrast lighting", "chiaroscuro lighting"]
}
self.technical_professional = {
"portrait_lens": ["85mm lens", "135mm lens", "medium telephoto"],
"standard_lens": ["50mm lens", "35mm lens", "standard focal length"],
"aperture": ["f/1.4 aperture", "f/2.8 aperture", "f/4 aperture"],
"camera": ["Shot on Phase One XF", "Shot on Hasselblad", "Shot on Canon EOS R5"]
}
def analyze_clip_deeply(self, clip_result):
"""Extract detailed information from CLIP analysis"""
clip_lower = clip_result.lower()
analysis = {
"subjects": [],
"age": None,
"features": [],
"clothing": [],
"setting": None,
"mood": None,
"composition": None
}
# Subject and age detection
if any(word in clip_lower for word in ["man", "person", "male"]):
if any(word in clip_lower for word in ["old", "elderly", "aged", "gray", "grey", "silver"]):
analysis["subjects"].append("elderly man")
analysis["age"] = "elderly"
elif any(word in clip_lower for word in ["young", "youth", "boy"]):
analysis["subjects"].append("young man")
analysis["age"] = "young"
else:
analysis["subjects"].append("man")
analysis["age"] = "middle"
if any(word in clip_lower for word in ["woman", "female", "lady"]):
if any(word in clip_lower for word in ["old", "elderly", "aged"]):
analysis["subjects"].append("elderly woman")
analysis["age"] = "elderly"
else:
analysis["subjects"].append("woman")
# Facial features detection
if any(word in clip_lower for word in ["beard", "facial hair", "mustache"]):
if any(word in clip_lower for word in ["gray", "grey", "silver", "white"]):
analysis["features"].append("silver beard")
else:
analysis["features"].append("beard")
if any(word in clip_lower for word in ["glasses", "spectacles", "eyeglasses"]):
analysis["features"].append("glasses")
# Clothing and accessories
if any(word in clip_lower for word in ["hat", "cap", "headwear"]):
analysis["clothing"].append("hat")
if any(word in clip_lower for word in ["suit", "formal", "dress", "shirt"]):
analysis["clothing"].append("formal wear")
# Setting detection
if any(word in clip_lower for word in ["indoor", "inside", "interior", "room"]):
analysis["setting"] = "indoor"
elif any(word in clip_lower for word in ["outdoor", "outside", "landscape", "street"]):
analysis["setting"] = "outdoor"
elif any(word in clip_lower for word in ["studio", "backdrop"]):
analysis["setting"] = "studio"
# Mood and composition
if any(word in clip_lower for word in ["portrait", "headshot", "face", "close-up"]):
analysis["composition"] = "portrait"
elif any(word in clip_lower for word in ["sitting", "seated", "chair"]):
analysis["composition"] = "seated"
elif any(word in clip_lower for word in ["standing", "upright"]):
analysis["composition"] = "standing"
return analysis
def build_flux_prompt(self, analysis, clip_base):
"""Build optimized Flux prompt using deep analysis"""
components = []
# 1. Article (intelligent selection)
if analysis["subjects"]:
subject = analysis["subjects"][0]
article = "An" if subject[0] in 'aeiou' else "A"
else:
article = "A"
components.append(article)
# 2. Descriptive adjectives (context-aware)
adjectives = []
if analysis["age"] == "elderly":
adjectives.extend(["distinguished", "weathered"])
elif analysis["age"] == "young":
adjectives.extend(["young", "fresh-faced"])
else:
adjectives.extend(["professional", "elegant"])
# Add up to 2-3 adjectives as per Flux rules
components.extend(adjectives[:2])
# 3. Main subject (enhanced with details)
if analysis["subjects"]:
main_subject = analysis["subjects"][0]
# Add religious/cultural context if detected
if "hat" in analysis["clothing"] and "beard" in [f.split()[0] for f in analysis["features"]]:
main_subject = "Orthodox Jewish " + main_subject
else:
main_subject = "subject"
components.append(main_subject)
# 4. Features integration (intelligent placement)
feature_descriptions = []
if "glasses" in analysis["features"]:
feature_descriptions.append("with distinctive wire-frame glasses")
if any("beard" in f for f in analysis["features"]):
if "silver beard" in analysis["features"]:
feature_descriptions.append("with a distinguished silver beard")
else:
feature_descriptions.append("with a full beard")
if feature_descriptions:
components.extend(feature_descriptions)
# 5. Clothing and accessories
clothing_desc = []
if "hat" in analysis["clothing"]:
clothing_desc.append("wearing a traditional black hat")
if "formal wear" in analysis["clothing"]:
clothing_desc.append("in formal attire")
if clothing_desc:
components.extend(clothing_desc)
# 6. Verb/Action (based on composition analysis)
if analysis["composition"] == "seated":
action = "seated contemplatively"
elif analysis["composition"] == "standing":
action = "standing with dignity"
else:
action = "positioned thoughtfully"
components.append(action)
# 7. Context/Location (enhanced setting)
setting_map = {
"indoor": "in an intimate indoor setting",
"outdoor": "in a natural outdoor environment",
"studio": "in a professional studio environment"
}
if analysis["setting"]:
context = setting_map.get(analysis["setting"], "in a carefully composed environment")
else:
context = "in a thoughtfully arranged scene"
components.append(context)
# 8. Environmental details (lighting-aware)
if analysis["composition"] == "portrait":
env_detail = "with dramatic portrait lighting that emphasizes facial features and texture"
else:
env_detail = "captured with sophisticated atmospheric lighting"
components.append(env_detail)
# 9. Technical specifications (composition-appropriate)
if analysis["composition"] == "portrait":
tech_spec = "Shot on Phase One XF, 85mm lens, f/2.8 aperture"
else:
tech_spec = "Shot on Phase One, 50mm lens, f/4 aperture"
components.append(tech_spec)
# 10. Quality marker (always professional)
components.append("professional photography")
# Join with proper punctuation
prompt = ", ".join(components)
# Clean up and optimize
prompt = re.sub(r'\s+', ' ', prompt) # Remove extra spaces
prompt = prompt.replace(", ,", ",") # Remove double commas
return prompt
def calculate_intelligence_score(self, prompt, analysis):
"""Calculate how well the prompt reflects intelligent analysis"""
score = 0
# Structure compliance (Flux rules 1-10)
if prompt.startswith(("A", "An")):
score += 10
# Feature recognition accuracy
if len(analysis["features"]) > 0:
score += 15
# Context understanding
if analysis["setting"]:
score += 15
# Subject detail depth
if len(analysis["subjects"]) > 0:
score += 15
# Technical specs presence
if "Phase One" in prompt and "lens" in prompt:
score += 15
# Lighting specification
if "lighting" in prompt:
score += 10
# Composition awareness
if analysis["composition"]:
score += 10
# Forbidden elements check
if not any(forbidden in prompt for forbidden in self.forbidden_elements):
score += 10
return min(score, 100)
class FluxPromptOptimizer:
def __init__(self):
self.interrogator = None
self.analyzer = DeepFluxAnalyzer()
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_optimized_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()
# Get comprehensive CLIP analysis
clip_result = self.interrogator.interrogate(image)
# Deep analysis of the CLIP result
deep_analysis = self.analyzer.analyze_clip_deeply(clip_result)
# Build optimized Flux prompt
optimized_prompt = self.analyzer.build_flux_prompt(deep_analysis, clip_result)
# Calculate intelligence score
score = self.analyzer.calculate_intelligence_score(optimized_prompt, deep_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()
# Generate detailed analysis info
gpu_status = "β‘ ZeroGPU" if torch.cuda.is_available() else "π» CPU"
features_detected = ", ".join(deep_analysis["features"]) if deep_analysis["features"] else "None"
subjects_detected = ", ".join(deep_analysis["subjects"]) if deep_analysis["subjects"] else "Generic"
analysis_info = f"""**Deep Analysis Complete**
**Processing:** {gpu_status} β’ {duration:.1f}s
**Intelligence Score:** {score}/100
**Generation:** #{self.usage_count}
**Detected Elements:**
β’ **Subjects:** {subjects_detected}
β’ **Features:** {features_detected}
β’ **Setting:** {deep_analysis["setting"] or "Unspecified"}
β’ **Composition:** {deep_analysis["composition"] or "Standard"}
**CLIP Base:** {clip_result[:80]}...
**Flux Enhancement:** Applied deep analysis with Pariente AI rules"""
return optimized_prompt, analysis_info, score
except Exception as e:
logger.error(f"Generation error: {e}")
return f"β Error: {str(e)}", "Please try with a different image.", 0
optimizer = FluxPromptOptimizer()
def process_image_wrapper(image):
"""Simplified wrapper - no unnecessary options"""
try:
prompt, info, score = optimizer.generate_optimized_prompt(image)
# Create score HTML
color = "#22c55e" if score >= 80 else "#f59e0b" if score >= 60 else "#ef4444"
score_html = f'''
<div style="text-align: center; padding: 1rem; background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%); border: 2px solid {color}; border-radius: 12px; margin: 1rem 0;">
<div style="font-size: 2rem; font-weight: 700; color: {color}; margin: 0;">{score}</div>
<div style="font-size: 0.875rem; color: #15803d; margin: 0; text-transform: uppercase; letter-spacing: 0.05em;">Intelligence Score</div>
</div>
'''
return prompt, info, score_html
except Exception as e:
logger.error(f"Wrapper error: {e}")
return "β Processing failed", f"Error: {str(e)}", '<div style="text-align: center; color: red;">Error</div>'
def clear_outputs():
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
return "", "", '<div style="text-align: center; padding: 1rem;"><div style="font-size: 2rem; color: #ccc;">--</div><div style="font-size: 0.875rem; color: #999;">Intelligence Score</div></div>'
def create_interface():
css = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
.gradio-container {
max-width: 1200px !important;
margin: 0 auto !important;
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%) !important;
}
.main-header {
text-align: center;
padding: 2rem 0 3rem 0;
background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
color: white;
margin: -2rem -2rem 2rem -2rem;
border-radius: 0 0 24px 24px;
}
.main-title {
font-size: 2.5rem !important;
font-weight: 700 !important;
margin: 0 0 0.5rem 0 !important;
letter-spacing: -0.025em !important;
background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.subtitle {
font-size: 1.125rem !important;
font-weight: 400 !important;
opacity: 0.8 !important;
margin: 0 !important;
}
.prompt-output {
font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace !important;
font-size: 14px !important;
line-height: 1.6 !important;
background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%) !important;
border: 1px solid #e2e8f0 !important;
border-radius: 12px !important;
padding: 1.5rem !important;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
}
"""
with gr.Blocks(
theme=gr.themes.Soft(),
title="Flux Prompt Optimizer",
css=css
) as interface:
gr.HTML("""
<div class="main-header">
<div class="main-title">β‘ Flux Prompt Optimizer</div>
<div class="subtitle">Deep AI analysis β’ Intelligent prompt generation β’ Research-based optimization</div>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## π· Image Analysis")
image_input = gr.Image(
label="Upload your image",
type="pil",
height=400
)
optimize_btn = gr.Button(
"π§ Analyze & Optimize",
variant="primary",
size="lg"
)
gr.Markdown("""
### Deep Analysis Engine
This system performs comprehensive image analysis:
β’ **Subject Recognition** - Identifies people, objects, context
β’ **Feature Detection** - Facial features, clothing, accessories
β’ **Composition Analysis** - Lighting, setting, mood
β’ **Flux Optimization** - Applies research-validated rules
No options needed - the AI decides what's optimal.
""")
with gr.Column(scale=1):
gr.Markdown("## π― Optimized Result")
prompt_output = gr.Textbox(
label="Flux-Optimized Prompt",
placeholder="Upload an image to see the intelligent analysis and optimization...",
lines=8,
max_lines=12,
elem_classes=["prompt-output"],
show_copy_button=True
)
score_output = gr.HTML(
value='<div style="text-align: center; padding: 1rem;"><div style="font-size: 2rem; color: #ccc;">--</div><div style="font-size: 0.875rem; color: #999;">Intelligence Score</div></div>'
)
info_output = gr.Markdown(value="")
clear_btn = gr.Button("ποΈ Clear", size="sm")
gr.Markdown("""
---
### π¬ Pariente AI Research Foundation
This optimizer implements deep computer vision analysis combined with validated Flux prompt engineering rules.
The system intelligently recognizes image content and applies structured optimization without requiring user configuration.
**Research-based β’ Intelligence-driven β’ Zero configuration needed**
""")
# Simple event handlers
optimize_btn.click(
fn=process_image_wrapper,
inputs=[image_input],
outputs=[prompt_output, info_output, score_output]
)
clear_btn.click(
fn=clear_outputs,
outputs=[prompt_output, info_output, score_output]
)
return interface
if __name__ == "__main__":
logger.info("π Starting Deep Flux Prompt Optimizer")
interface = create_interface()
interface.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True
) |