Spaces:
Runtime error
Runtime error
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -98,12 +98,13 @@ def clean_memory() -> None:
|
|
| 98 |
logger.warning(f"Memory cleanup failed: {e}")
|
| 99 |
|
| 100 |
|
| 101 |
-
def apply_flux_rules(prompt: str) -> str:
|
| 102 |
"""
|
| 103 |
Apply Flux optimization rules to a prompt
|
| 104 |
|
| 105 |
Args:
|
| 106 |
prompt: Raw prompt text
|
|
|
|
| 107 |
|
| 108 |
Returns:
|
| 109 |
Optimized prompt following Flux rules
|
|
@@ -116,30 +117,26 @@ def apply_flux_rules(prompt: str) -> str:
|
|
| 116 |
for pattern in FLUX_RULES["remove_patterns"]:
|
| 117 |
cleaned_prompt = re.sub(pattern, '', cleaned_prompt, flags=re.IGNORECASE)
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
-
camera_config = ""
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
camera_config =
|
|
|
|
| 129 |
else:
|
| 130 |
-
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# Add lighting enhancements if not present
|
| 133 |
-
|
| 134 |
-
if 'dramatic' in prompt_lower:
|
| 135 |
-
cleaned_prompt += FLUX_RULES["lighting_enhancements"]["dramatic"]
|
| 136 |
-
elif 'portrait' in prompt_lower:
|
| 137 |
-
cleaned_prompt += FLUX_RULES["lighting_enhancements"]["portrait"]
|
| 138 |
-
else:
|
| 139 |
-
cleaned_prompt += FLUX_RULES["lighting_enhancements"]["default"]
|
| 140 |
|
| 141 |
-
# Build final prompt
|
| 142 |
-
final_prompt =
|
| 143 |
|
| 144 |
# Clean up formatting
|
| 145 |
final_prompt = _clean_prompt_formatting(final_prompt)
|
|
@@ -147,6 +144,51 @@ def apply_flux_rules(prompt: str) -> str:
|
|
| 147 |
return final_prompt
|
| 148 |
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
def _clean_prompt_formatting(prompt: str) -> str:
|
| 151 |
"""Clean up prompt formatting"""
|
| 152 |
if not prompt:
|
|
@@ -187,9 +229,17 @@ def calculate_prompt_score(prompt: str, analysis_data: Optional[Dict[str, Any]]
|
|
| 187 |
detail_score = min(10, len(prompt.split(',')) * 2) # Reward detail
|
| 188 |
breakdown["prompt_quality"] = length_score + detail_score
|
| 189 |
|
| 190 |
-
# Technical details score (0-25 points)
|
|
|
|
| 191 |
tech_keywords = ['shot on', 'lens', 'photography', 'lighting', 'camera']
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
breakdown["technical_details"] = min(25, tech_score)
|
| 194 |
|
| 195 |
# Artistic value score (0-25 points)
|
|
@@ -199,10 +249,16 @@ def calculate_prompt_score(prompt: str, analysis_data: Optional[Dict[str, Any]]
|
|
| 199 |
|
| 200 |
# Flux optimization score (0-20 points)
|
| 201 |
flux_score = 0
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
| 203 |
flux_score += 10
|
|
|
|
|
|
|
| 204 |
if any(lighting in prompt for lighting in FLUX_RULES["lighting_enhancements"].values()):
|
| 205 |
flux_score += 10
|
|
|
|
| 206 |
breakdown["flux_optimization"] = flux_score
|
| 207 |
|
| 208 |
# Calculate total
|
|
|
|
| 98 |
logger.warning(f"Memory cleanup failed: {e}")
|
| 99 |
|
| 100 |
|
| 101 |
+
def apply_flux_rules(prompt: str, analysis_metadata: Optional[Dict[str, Any]] = None) -> str:
|
| 102 |
"""
|
| 103 |
Apply Flux optimization rules to a prompt
|
| 104 |
|
| 105 |
Args:
|
| 106 |
prompt: Raw prompt text
|
| 107 |
+
analysis_metadata: Optional metadata from image analysis including camera suggestions
|
| 108 |
|
| 109 |
Returns:
|
| 110 |
Optimized prompt following Flux rules
|
|
|
|
| 117 |
for pattern in FLUX_RULES["remove_patterns"]:
|
| 118 |
cleaned_prompt = re.sub(pattern, '', cleaned_prompt, flags=re.IGNORECASE)
|
| 119 |
|
| 120 |
+
# Extract description part only (remove CAMERA_SETUP section if present)
|
| 121 |
+
description_part = _extract_description_only(cleaned_prompt)
|
|
|
|
| 122 |
|
| 123 |
+
# Check if BAGEL provided intelligent camera setup
|
| 124 |
+
camera_config = ""
|
| 125 |
+
if analysis_metadata and analysis_metadata.get("has_camera_suggestion") and analysis_metadata.get("camera_setup"):
|
| 126 |
+
# Use BAGEL's intelligent camera suggestion
|
| 127 |
+
bagel_camera = analysis_metadata["camera_setup"]
|
| 128 |
+
camera_config = f", {bagel_camera}" if not bagel_camera.startswith(",") else bagel_camera
|
| 129 |
+
logger.info(f"Using BAGEL camera suggestion: {bagel_camera}")
|
| 130 |
else:
|
| 131 |
+
# Fallback to static rules if BAGEL didn't suggest camera
|
| 132 |
+
camera_config = _get_fallback_camera_config(description_part.lower())
|
| 133 |
+
logger.info("Using fallback camera configuration")
|
| 134 |
|
| 135 |
# Add lighting enhancements if not present
|
| 136 |
+
lighting_enhancement = _get_lighting_enhancement(description_part.lower())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
# Build final prompt: Description + Camera + Lighting
|
| 139 |
+
final_prompt = description_part + camera_config + lighting_enhancement
|
| 140 |
|
| 141 |
# Clean up formatting
|
| 142 |
final_prompt = _clean_prompt_formatting(final_prompt)
|
|
|
|
| 144 |
return final_prompt
|
| 145 |
|
| 146 |
|
| 147 |
+
def _extract_description_only(prompt: str) -> str:
|
| 148 |
+
"""Extract only the description part, removing camera setup sections"""
|
| 149 |
+
# Remove CAMERA_SETUP section if present
|
| 150 |
+
if "CAMERA_SETUP:" in prompt:
|
| 151 |
+
parts = prompt.split("CAMERA_SETUP:")
|
| 152 |
+
description = parts[0].strip()
|
| 153 |
+
elif "2. CAMERA_SETUP" in prompt:
|
| 154 |
+
parts = prompt.split("2. CAMERA_SETUP")
|
| 155 |
+
description = parts[0].strip()
|
| 156 |
+
else:
|
| 157 |
+
description = prompt
|
| 158 |
+
|
| 159 |
+
# Remove "DESCRIPTION:" label if present
|
| 160 |
+
if description.startswith("DESCRIPTION:"):
|
| 161 |
+
description = description.replace("DESCRIPTION:", "").strip()
|
| 162 |
+
elif description.startswith("1. DESCRIPTION:"):
|
| 163 |
+
description = description.replace("1. DESCRIPTION:", "").strip()
|
| 164 |
+
|
| 165 |
+
return description.strip()
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def _get_fallback_camera_config(prompt_lower: str) -> str:
|
| 169 |
+
"""Get fallback camera configuration when BAGEL doesn't suggest one"""
|
| 170 |
+
if any(word in prompt_lower for word in ['portrait', 'person', 'man', 'woman', 'face']):
|
| 171 |
+
return FLUX_RULES["camera_configs"]["portrait"]
|
| 172 |
+
elif any(word in prompt_lower for word in ['landscape', 'mountain', 'nature', 'outdoor']):
|
| 173 |
+
return FLUX_RULES["camera_configs"]["landscape"]
|
| 174 |
+
elif any(word in prompt_lower for word in ['street', 'urban', 'city']):
|
| 175 |
+
return FLUX_RULES["camera_configs"]["street"]
|
| 176 |
+
else:
|
| 177 |
+
return FLUX_RULES["camera_configs"]["default"]
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def _get_lighting_enhancement(prompt_lower: str) -> str:
|
| 181 |
+
"""Determine appropriate lighting enhancement"""
|
| 182 |
+
if 'lighting' not in prompt_lower:
|
| 183 |
+
if 'dramatic' in prompt_lower:
|
| 184 |
+
return FLUX_RULES["lighting_enhancements"]["dramatic"]
|
| 185 |
+
elif 'portrait' in prompt_lower:
|
| 186 |
+
return FLUX_RULES["lighting_enhancements"]["portrait"]
|
| 187 |
+
else:
|
| 188 |
+
return FLUX_RULES["lighting_enhancements"]["default"]
|
| 189 |
+
return "" # No enhancement needed if lighting already mentioned
|
| 190 |
+
|
| 191 |
+
|
| 192 |
def _clean_prompt_formatting(prompt: str) -> str:
|
| 193 |
"""Clean up prompt formatting"""
|
| 194 |
if not prompt:
|
|
|
|
| 229 |
detail_score = min(10, len(prompt.split(',')) * 2) # Reward detail
|
| 230 |
breakdown["prompt_quality"] = length_score + detail_score
|
| 231 |
|
| 232 |
+
# Technical details score (0-25 points) - Enhanced for BAGEL camera suggestions
|
| 233 |
+
tech_score = 0
|
| 234 |
tech_keywords = ['shot on', 'lens', 'photography', 'lighting', 'camera']
|
| 235 |
+
for keyword in tech_keywords:
|
| 236 |
+
if keyword in prompt.lower():
|
| 237 |
+
tech_score += 5
|
| 238 |
+
|
| 239 |
+
# Bonus points for BAGEL camera suggestions
|
| 240 |
+
if analysis_data and analysis_data.get("has_camera_suggestion"):
|
| 241 |
+
tech_score += 5 # Bonus for intelligent camera selection
|
| 242 |
+
|
| 243 |
breakdown["technical_details"] = min(25, tech_score)
|
| 244 |
|
| 245 |
# Artistic value score (0-25 points)
|
|
|
|
| 249 |
|
| 250 |
# Flux optimization score (0-20 points)
|
| 251 |
flux_score = 0
|
| 252 |
+
|
| 253 |
+
# Check for camera configuration (either BAGEL or fallback)
|
| 254 |
+
if any(camera in prompt for camera in FLUX_RULES["camera_configs"].values()) or \
|
| 255 |
+
(analysis_data and analysis_data.get("has_camera_suggestion")):
|
| 256 |
flux_score += 10
|
| 257 |
+
|
| 258 |
+
# Check for lighting configuration
|
| 259 |
if any(lighting in prompt for lighting in FLUX_RULES["lighting_enhancements"].values()):
|
| 260 |
flux_score += 10
|
| 261 |
+
|
| 262 |
breakdown["flux_optimization"] = flux_score
|
| 263 |
|
| 264 |
# Calculate total
|