Spaces:
Running
on
Zero
Running
on
Zero
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -123,17 +123,17 @@ def apply_flux_rules(prompt: str, analysis_metadata: Optional[Dict[str, Any]] =
|
|
| 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 =
|
| 129 |
-
logger.info(f"Using BAGEL camera suggestion: {
|
| 130 |
else:
|
| 131 |
-
#
|
| 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
|
|
@@ -162,31 +162,78 @@ def _extract_description_only(prompt: str) -> str:
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
| 190 |
|
| 191 |
|
| 192 |
def _clean_prompt_formatting(prompt: str) -> str:
|
|
@@ -205,6 +252,9 @@ def _clean_prompt_formatting(prompt: str) -> str:
|
|
| 205 |
prompt = re.sub(r'^\s*,\s*', '', prompt) # Remove leading commas
|
| 206 |
prompt = re.sub(r'\s*,\s*$', '', prompt) # Remove trailing commas
|
| 207 |
|
|
|
|
|
|
|
|
|
|
| 208 |
return prompt.strip()
|
| 209 |
|
| 210 |
|
|
@@ -238,7 +288,7 @@ def calculate_prompt_score(prompt: str, analysis_data: Optional[Dict[str, Any]]
|
|
| 238 |
|
| 239 |
# Bonus points for BAGEL camera suggestions
|
| 240 |
if analysis_data and analysis_data.get("has_camera_suggestion"):
|
| 241 |
-
tech_score +=
|
| 242 |
|
| 243 |
breakdown["technical_details"] = min(25, tech_score)
|
| 244 |
|
|
@@ -250,14 +300,15 @@ def calculate_prompt_score(prompt: str, analysis_data: Optional[Dict[str, Any]]
|
|
| 250 |
# Flux optimization score (0-20 points)
|
| 251 |
flux_score = 0
|
| 252 |
|
| 253 |
-
# Check for camera configuration (
|
| 254 |
-
if
|
| 255 |
-
|
| 256 |
-
|
|
|
|
| 257 |
|
| 258 |
# Check for lighting configuration
|
| 259 |
if any(lighting in prompt for lighting in FLUX_RULES["lighting_enhancements"].values()):
|
| 260 |
-
flux_score +=
|
| 261 |
|
| 262 |
breakdown["flux_optimization"] = flux_score
|
| 263 |
|
|
@@ -313,7 +364,7 @@ def format_analysis_report(analysis_data: Dict[str, Any], processing_time: float
|
|
| 313 |
✅ Technical photography details
|
| 314 |
✅ Artistic enhancement keywords
|
| 315 |
|
| 316 |
-
**⚡ Powered by
|
| 317 |
|
| 318 |
return report
|
| 319 |
|
|
|
|
| 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 - clean and format it properly
|
| 127 |
bagel_camera = analysis_metadata["camera_setup"]
|
| 128 |
+
camera_config = _format_bagel_camera_suggestion(bagel_camera)
|
| 129 |
+
logger.info(f"Using BAGEL camera suggestion: {camera_config}")
|
| 130 |
else:
|
| 131 |
+
# Only use fallback if BAGEL didn't suggest anything
|
| 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 and not already covered by BAGEL
|
| 136 |
+
lighting_enhancement = _get_lighting_enhancement(description_part.lower(), camera_config)
|
| 137 |
|
| 138 |
# Build final prompt: Description + Camera + Lighting
|
| 139 |
final_prompt = description_part + camera_config + lighting_enhancement
|
|
|
|
| 162 |
elif description.startswith("1. DESCRIPTION:"):
|
| 163 |
description = description.replace("1. DESCRIPTION:", "").strip()
|
| 164 |
|
| 165 |
+
# Clean up any remaining camera recommendations from the description
|
| 166 |
+
description = re.sub(r'For this type of scene.*?shooting style would be.*?\.', '', description, flags=re.DOTALL)
|
| 167 |
+
description = re.sub(r'I would recommend.*?aperture.*?\.', '', description, flags=re.DOTALL)
|
| 168 |
+
|
| 169 |
return description.strip()
|
| 170 |
|
| 171 |
|
| 172 |
+
def _format_bagel_camera_suggestion(bagel_camera: str) -> str:
|
| 173 |
+
"""Format BAGEL's camera suggestion into clean FLUX format"""
|
| 174 |
+
try:
|
| 175 |
+
# Clean up the BAGEL suggestion
|
| 176 |
+
camera_text = bagel_camera.strip()
|
| 177 |
+
|
| 178 |
+
# Remove "CAMERA_SETUP:" if it's still there
|
| 179 |
+
camera_text = re.sub(r'^CAMERA_SETUP:\s*', '', camera_text)
|
| 180 |
+
|
| 181 |
+
# Extract key camera info using regex patterns
|
| 182 |
+
camera_patterns = {
|
| 183 |
+
'camera': r'(Canon EOS [^,]+|Sony A[^,]+|Leica [^,]+|Hasselblad [^,]+|Phase One [^,]+|Fujifilm [^,]+)',
|
| 184 |
+
'lens': r'(\d+mm[^,]*|[^,]*lens[^,]*)',
|
| 185 |
+
'aperture': r'(f/[\d.]+[^,]*)'
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
extracted_parts = []
|
| 189 |
+
|
| 190 |
+
for key, pattern in camera_patterns.items():
|
| 191 |
+
match = re.search(pattern, camera_text, re.IGNORECASE)
|
| 192 |
+
if match:
|
| 193 |
+
extracted_parts.append(match.group(1).strip())
|
| 194 |
+
|
| 195 |
+
if extracted_parts:
|
| 196 |
+
# Build clean camera config
|
| 197 |
+
camera_info = ', '.join(extracted_parts)
|
| 198 |
+
return f", Shot on {camera_info}, professional photography"
|
| 199 |
+
else:
|
| 200 |
+
# Fallback: use the first part of BAGEL's suggestion
|
| 201 |
+
first_sentence = camera_text.split('.')[0].strip()
|
| 202 |
+
if len(first_sentence) > 10:
|
| 203 |
+
return f", {first_sentence}, professional photography"
|
| 204 |
+
else:
|
| 205 |
+
return ", professional camera setup"
|
| 206 |
+
|
| 207 |
+
except Exception as e:
|
| 208 |
+
logger.warning(f"Failed to format BAGEL camera suggestion: {e}")
|
| 209 |
+
return ", professional camera setup"
|
| 210 |
+
|
| 211 |
+
|
| 212 |
def _get_fallback_camera_config(prompt_lower: str) -> str:
|
| 213 |
"""Get fallback camera configuration when BAGEL doesn't suggest one"""
|
| 214 |
+
# Improved detection logic
|
| 215 |
+
if any(word in prompt_lower for word in ['street', 'urban', 'city', 'documentary', 'crowd', 'protest']):
|
| 216 |
+
return FLUX_RULES["camera_configs"]["street"]
|
| 217 |
+
elif any(word in prompt_lower for word in ['portrait', 'person', 'man', 'woman', 'face']) and not any(word in prompt_lower for word in ['street', 'crowd', 'scene']):
|
| 218 |
return FLUX_RULES["camera_configs"]["portrait"]
|
| 219 |
elif any(word in prompt_lower for word in ['landscape', 'mountain', 'nature', 'outdoor']):
|
| 220 |
return FLUX_RULES["camera_configs"]["landscape"]
|
|
|
|
|
|
|
| 221 |
else:
|
| 222 |
return FLUX_RULES["camera_configs"]["default"]
|
| 223 |
|
| 224 |
|
| 225 |
+
def _get_lighting_enhancement(prompt_lower: str, camera_config: str) -> str:
|
| 226 |
"""Determine appropriate lighting enhancement"""
|
| 227 |
+
# Don't add lighting if already mentioned in prompt or camera config
|
| 228 |
+
if 'lighting' in prompt_lower or 'lighting' in camera_config.lower():
|
| 229 |
+
return ""
|
| 230 |
+
|
| 231 |
+
if 'dramatic' in prompt_lower or 'chaos' in prompt_lower or 'fire' in prompt_lower:
|
| 232 |
+
return FLUX_RULES["lighting_enhancements"]["dramatic"]
|
| 233 |
+
elif 'portrait' in camera_config.lower():
|
| 234 |
+
return FLUX_RULES["lighting_enhancements"]["portrait"]
|
| 235 |
+
else:
|
| 236 |
+
return FLUX_RULES["lighting_enhancements"]["default"]
|
| 237 |
|
| 238 |
|
| 239 |
def _clean_prompt_formatting(prompt: str) -> str:
|
|
|
|
| 252 |
prompt = re.sub(r'^\s*,\s*', '', prompt) # Remove leading commas
|
| 253 |
prompt = re.sub(r'\s*,\s*$', '', prompt) # Remove trailing commas
|
| 254 |
|
| 255 |
+
# Remove redundant periods
|
| 256 |
+
prompt = re.sub(r'\.+', '.', prompt)
|
| 257 |
+
|
| 258 |
return prompt.strip()
|
| 259 |
|
| 260 |
|
|
|
|
| 288 |
|
| 289 |
# Bonus points for BAGEL camera suggestions
|
| 290 |
if analysis_data and analysis_data.get("has_camera_suggestion"):
|
| 291 |
+
tech_score += 10 # Higher bonus for intelligent camera selection
|
| 292 |
|
| 293 |
breakdown["technical_details"] = min(25, tech_score)
|
| 294 |
|
|
|
|
| 300 |
# Flux optimization score (0-20 points)
|
| 301 |
flux_score = 0
|
| 302 |
|
| 303 |
+
# Check for camera configuration (prefer BAGEL over fallback)
|
| 304 |
+
if analysis_data and analysis_data.get("has_camera_suggestion"):
|
| 305 |
+
flux_score += 15 # Higher score for BAGEL suggestions
|
| 306 |
+
elif any(camera in prompt for camera in FLUX_RULES["camera_configs"].values()):
|
| 307 |
+
flux_score += 10 # Lower score for fallback
|
| 308 |
|
| 309 |
# Check for lighting configuration
|
| 310 |
if any(lighting in prompt for lighting in FLUX_RULES["lighting_enhancements"].values()):
|
| 311 |
+
flux_score += 5
|
| 312 |
|
| 313 |
breakdown["flux_optimization"] = flux_score
|
| 314 |
|
|
|
|
| 364 |
✅ Technical photography details
|
| 365 |
✅ Artistic enhancement keywords
|
| 366 |
|
| 367 |
+
**⚡ Powered by Frame 0 Laboratory for MIA**"""
|
| 368 |
|
| 369 |
return report
|
| 370 |
|