Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
|
@@ -9,8 +9,9 @@ import os
|
|
| 9 |
import warnings
|
| 10 |
from datetime import datetime
|
| 11 |
import gc
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 15 |
warnings.filterwarnings("ignore", category=UserWarning)
|
| 16 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
|
@@ -18,33 +19,219 @@ os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
| 21 |
-
# Detectar dispositivo
|
| 22 |
def get_device():
|
| 23 |
if torch.cuda.is_available():
|
| 24 |
return "cuda"
|
| 25 |
elif torch.backends.mps.is_available():
|
| 26 |
-
return "mps"
|
| 27 |
else:
|
| 28 |
return "cpu"
|
| 29 |
|
| 30 |
DEVICE = get_device()
|
| 31 |
|
| 32 |
-
|
| 33 |
-
"
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
class
|
| 46 |
def __init__(self):
|
| 47 |
self.interrogator = None
|
|
|
|
| 48 |
self.usage_count = 0
|
| 49 |
self.device = DEVICE
|
| 50 |
self.is_initialized = False
|
|
@@ -55,7 +242,7 @@ class ImagePromptGenerator:
|
|
| 55 |
|
| 56 |
try:
|
| 57 |
if progress_callback:
|
| 58 |
-
progress_callback("
|
| 59 |
|
| 60 |
config = Config(
|
| 61 |
clip_model_name="ViT-L-14/openai",
|
|
@@ -76,7 +263,7 @@ class ImagePromptGenerator:
|
|
| 76 |
return True
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
-
logger.error(f"
|
| 80 |
return False
|
| 81 |
|
| 82 |
def optimize_image(self, image):
|
|
@@ -91,6 +278,7 @@ class ImagePromptGenerator:
|
|
| 91 |
if image.mode != 'RGB':
|
| 92 |
image = image.convert('RGB')
|
| 93 |
|
|
|
|
| 94 |
max_size = 768 if self.device != "cpu" else 512
|
| 95 |
if image.size[0] > max_size or image.size[1] > max_size:
|
| 96 |
image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
|
@@ -98,207 +286,327 @@ class ImagePromptGenerator:
|
|
| 98 |
return image
|
| 99 |
|
| 100 |
@spaces.GPU
|
| 101 |
-
def
|
| 102 |
try:
|
| 103 |
if not self.is_initialized:
|
| 104 |
if not self.initialize_model(progress_callback):
|
| 105 |
-
return "❌
|
| 106 |
|
| 107 |
if image is None:
|
| 108 |
-
return "❌
|
| 109 |
|
| 110 |
self.usage_count += 1
|
| 111 |
|
| 112 |
if progress_callback:
|
| 113 |
-
progress_callback("
|
| 114 |
|
| 115 |
image = self.optimize_image(image)
|
| 116 |
if image is None:
|
| 117 |
-
return "❌
|
| 118 |
|
| 119 |
if progress_callback:
|
| 120 |
-
progress_callback("
|
| 121 |
|
| 122 |
start_time = datetime.now()
|
| 123 |
|
|
|
|
| 124 |
try:
|
| 125 |
if mode == "fast":
|
| 126 |
-
|
| 127 |
elif mode == "classic":
|
| 128 |
-
|
| 129 |
else:
|
| 130 |
-
|
| 131 |
-
|
| 132 |
except Exception as e:
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
end_time = datetime.now()
|
| 136 |
duration = (end_time - start_time).total_seconds()
|
| 137 |
|
|
|
|
| 138 |
if self.device == "cpu":
|
| 139 |
gc.collect()
|
| 140 |
else:
|
| 141 |
torch.cuda.empty_cache()
|
| 142 |
|
| 143 |
-
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
|
| 148 |
-
{gpu_status}
|
| 149 |
-
**
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
|
|
|
|
| 152 |
"""
|
| 153 |
|
| 154 |
-
return
|
| 155 |
|
| 156 |
except Exception as e:
|
| 157 |
-
return f"❌ Error: {str(e)}", "
|
| 158 |
|
| 159 |
-
|
| 160 |
|
| 161 |
@spaces.GPU
|
| 162 |
-
def process_image_with_progress(image,
|
| 163 |
def progress_callback(message):
|
| 164 |
return message
|
| 165 |
|
| 166 |
-
yield "🚀
|
| 167 |
-
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
"""
|
| 173 |
|
| 174 |
-
prompt, info =
|
| 175 |
-
yield prompt, info
|
| 176 |
|
| 177 |
def clear_outputs():
|
| 178 |
gc.collect()
|
| 179 |
if torch.cuda.is_available():
|
| 180 |
torch.cuda.empty_cache()
|
| 181 |
-
return "", ""
|
| 182 |
|
| 183 |
def create_interface():
|
|
|
|
| 184 |
css = """
|
|
|
|
|
|
|
| 185 |
.gradio-container {
|
| 186 |
-
max-width:
|
| 187 |
-
|
|
|
|
|
|
|
| 188 |
}
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
background: linear-gradient(135deg, #
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
border:
|
| 197 |
}
|
|
|
|
| 198 |
.main-title {
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
| 201 |
-webkit-background-clip: text;
|
| 202 |
-webkit-text-fill-color: transparent;
|
| 203 |
-
|
| 204 |
-
font-weight: 800 !important;
|
| 205 |
-
margin-bottom: 0.3em !important;
|
| 206 |
}
|
|
|
|
| 207 |
.subtitle {
|
| 208 |
-
|
| 209 |
-
font-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
}
|
| 214 |
"""
|
| 215 |
|
| 216 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
gr.HTML("""
|
| 219 |
-
<div class="main-
|
| 220 |
-
|
|
|
|
|
|
|
| 221 |
""")
|
| 222 |
|
| 223 |
-
gr.Markdown("### 🎨 Image to Prompt - Research real, no marketing")
|
| 224 |
-
|
| 225 |
with gr.Row():
|
| 226 |
with gr.Column(scale=1):
|
| 227 |
-
gr.Markdown("##
|
|
|
|
| 228 |
image_input = gr.Image(
|
| 229 |
-
label="
|
| 230 |
type="pil",
|
| 231 |
-
height=
|
|
|
|
| 232 |
)
|
| 233 |
|
| 234 |
-
gr.Markdown("## ⚙️
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
| 239 |
)
|
| 240 |
|
| 241 |
mode_selector = gr.Dropdown(
|
| 242 |
-
choices=
|
| 243 |
-
value="best",
|
| 244 |
-
label="
|
|
|
|
| 245 |
)
|
| 246 |
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
with gr.Column(scale=1):
|
| 250 |
-
gr.Markdown("## 📝
|
|
|
|
| 251 |
prompt_output = gr.Textbox(
|
| 252 |
-
label="Prompt
|
| 253 |
-
placeholder="
|
| 254 |
-
lines=
|
|
|
|
| 255 |
elem_classes=["prompt-output"],
|
| 256 |
-
show_copy_button=True
|
|
|
|
| 257 |
)
|
| 258 |
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
with gr.Row():
|
| 262 |
-
clear_btn = gr.Button("🗑️
|
|
|
|
| 263 |
|
| 264 |
gr.Markdown("""
|
| 265 |
-
---
|
| 266 |
-
###
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
- Mostramos el código siempre
|
| 274 |
-
- Innovamos, no copiamos
|
| 275 |
-
|
| 276 |
-
**🤡 Startup típica hace marketing:**
|
| 277 |
-
- Copia código de GitHub
|
| 278 |
-
- Lo envuelve en CSS bonito
|
| 279 |
-
- Cobra como "innovación"
|
| 280 |
-
- Busca inversores con PowerPoints
|
| 281 |
-
|
| 282 |
-
---
|
| 283 |
-
|
| 284 |
-
**⚡ Powered by Pariente AI** - *Research real, no bullshit*
|
| 285 |
""")
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
)
|
| 292 |
|
| 293 |
clear_btn.click(
|
| 294 |
fn=clear_outputs,
|
| 295 |
-
outputs=[prompt_output, info_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
)
|
| 297 |
|
| 298 |
return interface
|
| 299 |
|
| 300 |
if __name__ == "__main__":
|
| 301 |
-
logger.info("🚀
|
| 302 |
interface = create_interface()
|
| 303 |
interface.launch(
|
| 304 |
server_name="0.0.0.0",
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
|
|
|
| 9 |
import warnings
|
| 10 |
from datetime import datetime
|
| 11 |
import gc
|
| 12 |
+
import re
|
| 13 |
|
| 14 |
+
# Suppress warnings
|
| 15 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 16 |
warnings.filterwarnings("ignore", category=UserWarning)
|
| 17 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
|
|
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
| 20 |
logger = logging.getLogger(__name__)
|
| 21 |
|
|
|
|
| 22 |
def get_device():
|
| 23 |
if torch.cuda.is_available():
|
| 24 |
return "cuda"
|
| 25 |
elif torch.backends.mps.is_available():
|
| 26 |
+
return "mps"
|
| 27 |
else:
|
| 28 |
return "cpu"
|
| 29 |
|
| 30 |
DEVICE = get_device()
|
| 31 |
|
| 32 |
+
class FluxRulesEngine:
|
| 33 |
+
"""
|
| 34 |
+
Flux prompt optimization based on Pariente AI research
|
| 35 |
+
Implements structured prompt generation following validated rules
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
def __init__(self):
|
| 39 |
+
self.forbidden_elements = ["++", "weights", "white background [en dev]"]
|
| 40 |
+
self.structure_order = {
|
| 41 |
+
1: "article",
|
| 42 |
+
2: "descriptive_adjectives",
|
| 43 |
+
3: "main_subject",
|
| 44 |
+
4: "verb_action",
|
| 45 |
+
5: "context_location",
|
| 46 |
+
6: "environmental_details",
|
| 47 |
+
7: "materials_textures",
|
| 48 |
+
8: "lighting_effects",
|
| 49 |
+
9: "technical_specs",
|
| 50 |
+
10: "quality_style"
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
self.articles = ["a", "an", "the"]
|
| 54 |
+
self.quality_adjectives = [
|
| 55 |
+
"majestic", "pristine", "sleek", "elegant", "dramatic",
|
| 56 |
+
"cinematic", "professional", "stunning", "refined"
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
self.lighting_types = [
|
| 60 |
+
"golden hour", "studio lighting", "dramatic lighting",
|
| 61 |
+
"ambient lighting", "natural light", "soft lighting",
|
| 62 |
+
"rim lighting", "volumetric lighting"
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
self.technical_specs = [
|
| 66 |
+
"Shot on Phase One", "f/2.8 aperture", "50mm lens",
|
| 67 |
+
"85mm lens", "35mm lens", "professional photography",
|
| 68 |
+
"medium format", "high resolution"
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
self.materials = [
|
| 72 |
+
"metallic", "glass", "chrome", "leather", "fabric",
|
| 73 |
+
"wood", "concrete", "steel", "ceramic"
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
def extract_subject(self, base_prompt):
|
| 77 |
+
"""Extract main subject from CLIP analysis"""
|
| 78 |
+
words = base_prompt.lower().split()
|
| 79 |
+
|
| 80 |
+
# Common subjects to identify
|
| 81 |
+
subjects = [
|
| 82 |
+
"car", "vehicle", "automobile", "person", "man", "woman",
|
| 83 |
+
"building", "house", "landscape", "mountain", "tree",
|
| 84 |
+
"flower", "animal", "dog", "cat", "bird"
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
+
for word in words:
|
| 88 |
+
if word in subjects:
|
| 89 |
+
return word
|
| 90 |
+
|
| 91 |
+
# Fallback to first noun-like word
|
| 92 |
+
return words[0] if words else "subject"
|
| 93 |
+
|
| 94 |
+
def detect_setting(self, base_prompt):
|
| 95 |
+
"""Detect environmental context"""
|
| 96 |
+
prompt_lower = base_prompt.lower()
|
| 97 |
+
|
| 98 |
+
settings = {
|
| 99 |
+
"studio": ["studio", "backdrop", "seamless"],
|
| 100 |
+
"outdoor": ["outdoor", "outside", "landscape", "nature"],
|
| 101 |
+
"urban": ["city", "street", "urban", "building"],
|
| 102 |
+
"coastal": ["beach", "ocean", "coast", "sea"],
|
| 103 |
+
"indoor": ["room", "interior", "inside", "home"]
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
for setting, keywords in settings.items():
|
| 107 |
+
if any(keyword in prompt_lower for keyword in keywords):
|
| 108 |
+
return setting
|
| 109 |
+
|
| 110 |
+
return "neutral environment"
|
| 111 |
+
|
| 112 |
+
def optimize_for_flux(self, base_prompt, style_preference="professional"):
|
| 113 |
+
"""Apply Flux-specific optimization rules"""
|
| 114 |
+
|
| 115 |
+
# Clean forbidden elements
|
| 116 |
+
cleaned_prompt = base_prompt
|
| 117 |
+
for forbidden in self.forbidden_elements:
|
| 118 |
+
cleaned_prompt = cleaned_prompt.replace(forbidden, "")
|
| 119 |
+
|
| 120 |
+
# Extract key elements
|
| 121 |
+
subject = self.extract_subject(base_prompt)
|
| 122 |
+
setting = self.detect_setting(base_prompt)
|
| 123 |
+
|
| 124 |
+
# Build structured prompt
|
| 125 |
+
components = []
|
| 126 |
+
|
| 127 |
+
# 1. Article
|
| 128 |
+
article = "A" if subject[0] not in 'aeiou' else "An"
|
| 129 |
+
components.append(article)
|
| 130 |
+
|
| 131 |
+
# 2. Descriptive adjectives (max 2-3)
|
| 132 |
+
adjectives = np.random.choice(self.quality_adjectives, size=2, replace=False)
|
| 133 |
+
components.extend(adjectives)
|
| 134 |
+
|
| 135 |
+
# 3. Main subject
|
| 136 |
+
components.append(subject)
|
| 137 |
+
|
| 138 |
+
# 4. Verb/Action (gerund form)
|
| 139 |
+
if "person" in subject or "man" in subject or "woman" in subject:
|
| 140 |
+
action = "standing"
|
| 141 |
+
else:
|
| 142 |
+
action = "positioned"
|
| 143 |
+
components.append(action)
|
| 144 |
+
|
| 145 |
+
# 5. Context/Location
|
| 146 |
+
context_map = {
|
| 147 |
+
"studio": "in a professional studio setting",
|
| 148 |
+
"outdoor": "in a natural outdoor environment",
|
| 149 |
+
"urban": "on an urban street",
|
| 150 |
+
"coastal": "along a dramatic coastline",
|
| 151 |
+
"indoor": "in an elegant interior space"
|
| 152 |
+
}
|
| 153 |
+
components.append(context_map.get(setting, "in a carefully composed scene"))
|
| 154 |
+
|
| 155 |
+
# 6. Environmental details
|
| 156 |
+
env_details = ["with subtle atmospheric effects", "surrounded by carefully balanced elements"]
|
| 157 |
+
components.append(np.random.choice(env_details))
|
| 158 |
+
|
| 159 |
+
# 7. Materials/Textures (if applicable)
|
| 160 |
+
if any(mat in base_prompt.lower() for mat in ["car", "vehicle", "metal"]):
|
| 161 |
+
material = np.random.choice(["with metallic surfaces", "featuring chrome details"])
|
| 162 |
+
components.append(material)
|
| 163 |
+
|
| 164 |
+
# 8. Lighting effects
|
| 165 |
+
lighting = np.random.choice(self.lighting_types)
|
| 166 |
+
components.append(f"illuminated by {lighting}")
|
| 167 |
+
|
| 168 |
+
# 9. Technical specs
|
| 169 |
+
tech_spec = np.random.choice(self.technical_specs)
|
| 170 |
+
components.append(tech_spec)
|
| 171 |
+
|
| 172 |
+
# 10. Quality/Style
|
| 173 |
+
if style_preference == "cinematic":
|
| 174 |
+
quality = "cinematic composition"
|
| 175 |
+
elif style_preference == "commercial":
|
| 176 |
+
quality = "commercial photography quality"
|
| 177 |
+
else:
|
| 178 |
+
quality = "professional photography"
|
| 179 |
+
|
| 180 |
+
components.append(quality)
|
| 181 |
+
|
| 182 |
+
# Join components with proper punctuation
|
| 183 |
+
prompt = ", ".join(components)
|
| 184 |
+
|
| 185 |
+
# Capitalize first letter
|
| 186 |
+
prompt = prompt[0].upper() + prompt[1:]
|
| 187 |
+
|
| 188 |
+
return prompt
|
| 189 |
+
|
| 190 |
+
def get_optimization_score(self, prompt):
|
| 191 |
+
"""Calculate optimization score for Flux compatibility"""
|
| 192 |
+
score = 0
|
| 193 |
+
max_score = 100
|
| 194 |
+
|
| 195 |
+
# Structure check (order compliance)
|
| 196 |
+
if prompt.startswith(("A", "An", "The")):
|
| 197 |
+
score += 15
|
| 198 |
+
|
| 199 |
+
# Adjective count (optimal 2-3)
|
| 200 |
+
adj_count = len([adj for adj in self.quality_adjectives if adj in prompt.lower()])
|
| 201 |
+
if 2 <= adj_count <= 3:
|
| 202 |
+
score += 15
|
| 203 |
+
elif adj_count == 1:
|
| 204 |
+
score += 10
|
| 205 |
+
|
| 206 |
+
# Technical specs presence
|
| 207 |
+
if any(spec in prompt for spec in self.technical_specs):
|
| 208 |
+
score += 20
|
| 209 |
+
|
| 210 |
+
# Lighting specification
|
| 211 |
+
if any(light in prompt.lower() for light in self.lighting_types):
|
| 212 |
+
score += 15
|
| 213 |
+
|
| 214 |
+
# No forbidden elements
|
| 215 |
+
if not any(forbidden in prompt for forbidden in self.forbidden_elements):
|
| 216 |
+
score += 15
|
| 217 |
+
|
| 218 |
+
# Proper punctuation and structure
|
| 219 |
+
if "," in prompt and prompt.endswith(("photography", "composition", "quality")):
|
| 220 |
+
score += 10
|
| 221 |
+
|
| 222 |
+
# Length optimization (Flux works best with detailed but not excessive prompts)
|
| 223 |
+
word_count = len(prompt.split())
|
| 224 |
+
if 15 <= word_count <= 35:
|
| 225 |
+
score += 10
|
| 226 |
+
elif 10 <= word_count <= 45:
|
| 227 |
+
score += 5
|
| 228 |
+
|
| 229 |
+
return min(score, max_score)
|
| 230 |
|
| 231 |
+
class FluxPromptOptimizer:
|
| 232 |
def __init__(self):
|
| 233 |
self.interrogator = None
|
| 234 |
+
self.flux_engine = FluxRulesEngine()
|
| 235 |
self.usage_count = 0
|
| 236 |
self.device = DEVICE
|
| 237 |
self.is_initialized = False
|
|
|
|
| 242 |
|
| 243 |
try:
|
| 244 |
if progress_callback:
|
| 245 |
+
progress_callback("Initializing CLIP model...")
|
| 246 |
|
| 247 |
config = Config(
|
| 248 |
clip_model_name="ViT-L-14/openai",
|
|
|
|
| 263 |
return True
|
| 264 |
|
| 265 |
except Exception as e:
|
| 266 |
+
logger.error(f"Initialization error: {e}")
|
| 267 |
return False
|
| 268 |
|
| 269 |
def optimize_image(self, image):
|
|
|
|
| 278 |
if image.mode != 'RGB':
|
| 279 |
image = image.convert('RGB')
|
| 280 |
|
| 281 |
+
# Optimize image size for processing
|
| 282 |
max_size = 768 if self.device != "cpu" else 512
|
| 283 |
if image.size[0] > max_size or image.size[1] > max_size:
|
| 284 |
image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
|
|
|
| 286 |
return image
|
| 287 |
|
| 288 |
@spaces.GPU
|
| 289 |
+
def generate_optimized_prompt(self, image, style_preference="professional", mode="best", progress_callback=None):
|
| 290 |
try:
|
| 291 |
if not self.is_initialized:
|
| 292 |
if not self.initialize_model(progress_callback):
|
| 293 |
+
return "❌ Model initialization failed.", "", 0
|
| 294 |
|
| 295 |
if image is None:
|
| 296 |
+
return "❌ Please upload an image.", "", 0
|
| 297 |
|
| 298 |
self.usage_count += 1
|
| 299 |
|
| 300 |
if progress_callback:
|
| 301 |
+
progress_callback("Analyzing image content...")
|
| 302 |
|
| 303 |
image = self.optimize_image(image)
|
| 304 |
if image is None:
|
| 305 |
+
return "❌ Image processing failed.", "", 0
|
| 306 |
|
| 307 |
if progress_callback:
|
| 308 |
+
progress_callback("Extracting visual features...")
|
| 309 |
|
| 310 |
start_time = datetime.now()
|
| 311 |
|
| 312 |
+
# Get base analysis from CLIP
|
| 313 |
try:
|
| 314 |
if mode == "fast":
|
| 315 |
+
base_prompt = self.interrogator.interrogate_fast(image)
|
| 316 |
elif mode == "classic":
|
| 317 |
+
base_prompt = self.interrogator.interrogate_classic(image)
|
| 318 |
else:
|
| 319 |
+
base_prompt = self.interrogator.interrogate(image)
|
|
|
|
| 320 |
except Exception as e:
|
| 321 |
+
base_prompt = self.interrogator.interrogate_fast(image)
|
| 322 |
+
|
| 323 |
+
if progress_callback:
|
| 324 |
+
progress_callback("Applying Flux optimization rules...")
|
| 325 |
+
|
| 326 |
+
# Apply Flux-specific optimization
|
| 327 |
+
optimized_prompt = self.flux_engine.optimize_for_flux(base_prompt, style_preference)
|
| 328 |
+
|
| 329 |
+
# Calculate optimization score
|
| 330 |
+
score = self.flux_engine.get_optimization_score(optimized_prompt)
|
| 331 |
|
| 332 |
end_time = datetime.now()
|
| 333 |
duration = (end_time - start_time).total_seconds()
|
| 334 |
|
| 335 |
+
# Memory cleanup
|
| 336 |
if self.device == "cpu":
|
| 337 |
gc.collect()
|
| 338 |
else:
|
| 339 |
torch.cuda.empty_cache()
|
| 340 |
|
| 341 |
+
# Generate analysis info
|
| 342 |
+
gpu_status = "⚡ ZeroGPU" if torch.cuda.is_available() else "💻 CPU"
|
| 343 |
|
| 344 |
+
analysis_info = f"""
|
| 345 |
+
**Analysis Complete**
|
| 346 |
|
| 347 |
+
**Processing:** {gpu_status} • {duration:.1f}s • {mode.title()} mode
|
| 348 |
+
**Style:** {style_preference.title()} photography
|
| 349 |
+
**Optimization Score:** {score}/100
|
| 350 |
+
**Generation:** #{self.usage_count}
|
| 351 |
|
| 352 |
+
**Base Analysis:** {base_prompt[:100]}...
|
| 353 |
+
**Enhancement:** Applied Flux-specific structure and terminology
|
| 354 |
"""
|
| 355 |
|
| 356 |
+
return optimized_prompt, analysis_info, score
|
| 357 |
|
| 358 |
except Exception as e:
|
| 359 |
+
return f"❌ Error: {str(e)}", "Please try with a different image or contact support.", 0
|
| 360 |
|
| 361 |
+
optimizer = FluxPromptOptimizer()
|
| 362 |
|
| 363 |
@spaces.GPU
|
| 364 |
+
def process_image_with_progress(image, style_preference, mode):
|
| 365 |
def progress_callback(message):
|
| 366 |
return message
|
| 367 |
|
| 368 |
+
yield "🚀 Initializing Flux Optimizer...", """
|
| 369 |
+
**Flux Prompt Optimizer**
|
| 370 |
|
| 371 |
+
Analyzing image with advanced computer vision
|
| 372 |
+
Applying research-based optimization rules
|
| 373 |
+
Generating Flux-compatible prompt structure
|
| 374 |
+
""", 0
|
| 375 |
|
| 376 |
+
prompt, info, score = optimizer.generate_optimized_prompt(image, style_preference, mode, progress_callback)
|
| 377 |
+
yield prompt, info, score
|
| 378 |
|
| 379 |
def clear_outputs():
|
| 380 |
gc.collect()
|
| 381 |
if torch.cuda.is_available():
|
| 382 |
torch.cuda.empty_cache()
|
| 383 |
+
return "", "", 0
|
| 384 |
|
| 385 |
def create_interface():
|
| 386 |
+
# Professional CSS with elegant typography
|
| 387 |
css = """
|
| 388 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
|
| 389 |
+
|
| 390 |
.gradio-container {
|
| 391 |
+
max-width: 1200px !important;
|
| 392 |
+
margin: 0 auto !important;
|
| 393 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
|
| 394 |
+
background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%) !important;
|
| 395 |
}
|
| 396 |
+
|
| 397 |
+
.main-header {
|
| 398 |
+
text-align: center;
|
| 399 |
+
padding: 2rem 0 3rem 0;
|
| 400 |
+
background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
|
| 401 |
+
color: white;
|
| 402 |
+
margin: -2rem -2rem 2rem -2rem;
|
| 403 |
+
border-radius: 0 0 24px 24px;
|
| 404 |
}
|
| 405 |
+
|
| 406 |
.main-title {
|
| 407 |
+
font-size: 2.5rem !important;
|
| 408 |
+
font-weight: 700 !important;
|
| 409 |
+
margin: 0 0 0.5rem 0 !important;
|
| 410 |
+
letter-spacing: -0.025em !important;
|
| 411 |
+
background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 100%);
|
| 412 |
-webkit-background-clip: text;
|
| 413 |
-webkit-text-fill-color: transparent;
|
| 414 |
+
background-clip: text;
|
|
|
|
|
|
|
| 415 |
}
|
| 416 |
+
|
| 417 |
.subtitle {
|
| 418 |
+
font-size: 1.125rem !important;
|
| 419 |
+
font-weight: 400 !important;
|
| 420 |
+
opacity: 0.8 !important;
|
| 421 |
+
margin: 0 !important;
|
| 422 |
+
}
|
| 423 |
+
|
| 424 |
+
.section-header {
|
| 425 |
+
font-size: 1.25rem !important;
|
| 426 |
+
font-weight: 600 !important;
|
| 427 |
+
color: #1e293b !important;
|
| 428 |
+
margin: 0 0 1rem 0 !important;
|
| 429 |
+
padding-bottom: 0.5rem !important;
|
| 430 |
+
border-bottom: 2px solid #e2e8f0 !important;
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
.prompt-output {
|
| 434 |
+
font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace !important;
|
| 435 |
+
font-size: 14px !important;
|
| 436 |
+
line-height: 1.6 !important;
|
| 437 |
+
background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%) !important;
|
| 438 |
+
border: 1px solid #e2e8f0 !important;
|
| 439 |
+
border-radius: 12px !important;
|
| 440 |
+
padding: 1.5rem !important;
|
| 441 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
.info-panel {
|
| 445 |
+
background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%) !important;
|
| 446 |
+
border: 1px solid #0ea5e9 !important;
|
| 447 |
+
border-radius: 12px !important;
|
| 448 |
+
padding: 1.25rem !important;
|
| 449 |
+
font-size: 0.875rem !important;
|
| 450 |
+
line-height: 1.5 !important;
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
.score-display {
|
| 454 |
+
text-align: center !important;
|
| 455 |
+
padding: 1rem !important;
|
| 456 |
+
background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%) !important;
|
| 457 |
+
border: 2px solid #22c55e !important;
|
| 458 |
+
border-radius: 12px !important;
|
| 459 |
+
margin: 1rem 0 !important;
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
.score-number {
|
| 463 |
+
font-size: 2rem !important;
|
| 464 |
+
font-weight: 700 !important;
|
| 465 |
+
color: #16a34a !important;
|
| 466 |
+
margin: 0 !important;
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
+
.score-label {
|
| 470 |
+
font-size: 0.875rem !important;
|
| 471 |
+
color: #15803d !important;
|
| 472 |
+
margin: 0 !important;
|
| 473 |
+
text-transform: uppercase !important;
|
| 474 |
+
letter-spacing: 0.05em !important;
|
| 475 |
}
|
| 476 |
"""
|
| 477 |
|
| 478 |
+
with gr.Blocks(
|
| 479 |
+
theme=gr.themes.Soft(),
|
| 480 |
+
title="Flux Prompt Optimizer",
|
| 481 |
+
css=css
|
| 482 |
+
) as interface:
|
| 483 |
|
| 484 |
gr.HTML("""
|
| 485 |
+
<div class="main-header">
|
| 486 |
+
<div class="main-title">⚡ Flux Prompt Optimizer</div>
|
| 487 |
+
<div class="subtitle">Advanced prompt generation for Flux models • Research-based optimization</div>
|
| 488 |
+
</div>
|
| 489 |
""")
|
| 490 |
|
|
|
|
|
|
|
| 491 |
with gr.Row():
|
| 492 |
with gr.Column(scale=1):
|
| 493 |
+
gr.Markdown("## 📷 Image Input", elem_classes=["section-header"])
|
| 494 |
+
|
| 495 |
image_input = gr.Image(
|
| 496 |
+
label="Upload your image",
|
| 497 |
type="pil",
|
| 498 |
+
height=320,
|
| 499 |
+
show_label=False
|
| 500 |
)
|
| 501 |
|
| 502 |
+
gr.Markdown("## ⚙️ Optimization Settings", elem_classes=["section-header"])
|
| 503 |
+
|
| 504 |
+
style_selector = gr.Dropdown(
|
| 505 |
+
choices=["professional", "cinematic", "commercial", "artistic"],
|
| 506 |
+
value="professional",
|
| 507 |
+
label="Photography Style",
|
| 508 |
+
info="Select the target style for prompt optimization"
|
| 509 |
)
|
| 510 |
|
| 511 |
mode_selector = gr.Dropdown(
|
| 512 |
+
choices=["fast", "classic", "best"],
|
| 513 |
+
value="best",
|
| 514 |
+
label="Analysis Mode",
|
| 515 |
+
info="Balance between speed and detail"
|
| 516 |
)
|
| 517 |
|
| 518 |
+
optimize_btn = gr.Button(
|
| 519 |
+
"🚀 Generate Optimized Prompt",
|
| 520 |
+
variant="primary",
|
| 521 |
+
size="lg"
|
| 522 |
+
)
|
| 523 |
+
|
| 524 |
+
gr.Markdown("""
|
| 525 |
+
### About Flux Optimization
|
| 526 |
+
|
| 527 |
+
This tool applies research-validated rules for Flux prompt generation:
|
| 528 |
+
|
| 529 |
+
• **Structured composition** following optimal element order
|
| 530 |
+
• **Technical specifications** for professional results
|
| 531 |
+
• **Lighting and material** terminology optimization
|
| 532 |
+
• **Quality markers** specific to Flux model architecture
|
| 533 |
+
""")
|
| 534 |
|
| 535 |
with gr.Column(scale=1):
|
| 536 |
+
gr.Markdown("## 📝 Optimized Prompt", elem_classes=["section-header"])
|
| 537 |
+
|
| 538 |
prompt_output = gr.Textbox(
|
| 539 |
+
label="Generated Prompt",
|
| 540 |
+
placeholder="Your optimized Flux prompt will appear here...",
|
| 541 |
+
lines=6,
|
| 542 |
+
max_lines=10,
|
| 543 |
elem_classes=["prompt-output"],
|
| 544 |
+
show_copy_button=True,
|
| 545 |
+
show_label=False
|
| 546 |
)
|
| 547 |
|
| 548 |
+
# Score display
|
| 549 |
+
score_output = gr.HTML(
|
| 550 |
+
value='<div class="score-display"><div class="score-number">--</div><div class="score-label">Optimization Score</div></div>'
|
| 551 |
+
)
|
| 552 |
+
|
| 553 |
+
info_output = gr.Markdown(
|
| 554 |
+
value="",
|
| 555 |
+
elem_classes=["info-panel"]
|
| 556 |
+
)
|
| 557 |
|
| 558 |
with gr.Row():
|
| 559 |
+
clear_btn = gr.Button("🗑️ Clear", size="sm")
|
| 560 |
+
copy_btn = gr.Button("📋 Copy Prompt", size="sm")
|
| 561 |
|
| 562 |
gr.Markdown("""
|
| 563 |
+
---
|
| 564 |
+
### 🔬 Research Foundation
|
| 565 |
+
|
| 566 |
+
Flux Prompt Optimizer implements validated prompt engineering research for optimal Flux model performance.
|
| 567 |
+
The optimization engine applies structured composition rules, technical terminology, and quality markers
|
| 568 |
+
specifically calibrated for Flux architecture.
|
| 569 |
+
|
| 570 |
+
**Developed by Pariente AI** • Advanced AI Research Laboratory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 571 |
""")
|
| 572 |
|
| 573 |
+
# Event handlers
|
| 574 |
+
def update_score_display(score):
|
| 575 |
+
color = "#22c55e" if score >= 80 else "#f59e0b" if score >= 60 else "#ef4444"
|
| 576 |
+
return f'''
|
| 577 |
+
<div class="score-display" style="border-color: {color};">
|
| 578 |
+
<div class="score-number" style="color: {color};">{score}</div>
|
| 579 |
+
<div class="score-label">Optimization Score</div>
|
| 580 |
+
</div>
|
| 581 |
+
'''
|
| 582 |
+
|
| 583 |
+
def copy_prompt_to_clipboard(prompt):
|
| 584 |
+
return prompt
|
| 585 |
+
|
| 586 |
+
optimize_btn.click(
|
| 587 |
+
fn=lambda img, style, mode: [
|
| 588 |
+
*process_image_with_progress(img, style, mode),
|
| 589 |
+
update_score_display(list(process_image_with_progress(img, style, mode))[-1][2])
|
| 590 |
+
],
|
| 591 |
+
inputs=[image_input, style_selector, mode_selector],
|
| 592 |
+
outputs=[prompt_output, info_output, score_output]
|
| 593 |
)
|
| 594 |
|
| 595 |
clear_btn.click(
|
| 596 |
fn=clear_outputs,
|
| 597 |
+
outputs=[prompt_output, info_output, score_output]
|
| 598 |
+
)
|
| 599 |
+
|
| 600 |
+
copy_btn.click(
|
| 601 |
+
fn=copy_prompt_to_clipboard,
|
| 602 |
+
inputs=[prompt_output],
|
| 603 |
+
outputs=[]
|
| 604 |
)
|
| 605 |
|
| 606 |
return interface
|
| 607 |
|
| 608 |
if __name__ == "__main__":
|
| 609 |
+
logger.info("🚀 Starting Flux Prompt Optimizer")
|
| 610 |
interface = create_interface()
|
| 611 |
interface.launch(
|
| 612 |
server_name="0.0.0.0",
|