Migrations

This commit is contained in:
Desktop
2025-11-12 05:39:45 +05:00
parent 86b5e48bae
commit 2371479636
4 changed files with 137 additions and 64 deletions

View File

@@ -317,32 +317,44 @@ def process_image_generation_queue(self, image_ids: list, account_id: int = None
continue
# Format template with image prompt from database
# For DALL-E 2: Use image prompt directly (no template)
# For DALL-E 3 and others: Use template with placeholders
# CRITICAL: OpenAI has strict 1000 character limit for prompts
# For DALL-E 2: Use image prompt directly (no template), 1000 char limit
# For DALL-E 3: Use template with placeholders, 4000 char limit
# CRITICAL: DALL-E 2 has 1000 char limit, DALL-E 3 has 4000 char limit
image_prompt = image.prompt or ""
# Determine character limit based on model
if model == 'dall-e-2':
max_prompt_length = 1000
elif model == 'dall-e-3':
max_prompt_length = 4000
else:
# Default to 1000 for safety
max_prompt_length = 1000
logger.warning(f"Unknown model '{model}', using 1000 char limit")
logger.info(f"[process_image_generation_queue] Model: {model}, Max prompt length: {max_prompt_length} chars")
if model == 'dall-e-2':
# DALL-E 2: Use image prompt directly, no template
logger.info(f"[process_image_generation_queue] Using DALL-E 2 - skipping template, using image prompt directly")
formatted_prompt = image_prompt
# Truncate to 1000 chars if needed
if len(formatted_prompt) > 1000:
logger.warning(f"DALL-E 2 prompt too long ({len(formatted_prompt)} chars), truncating to 1000")
truncated = formatted_prompt[:997]
if len(formatted_prompt) > max_prompt_length:
logger.warning(f"DALL-E 2 prompt too long ({len(formatted_prompt)} chars), truncating to {max_prompt_length}")
truncated = formatted_prompt[:max_prompt_length - 3]
last_space = truncated.rfind(' ')
if last_space > 900:
if last_space > max_prompt_length * 0.9:
formatted_prompt = truncated[:last_space] + "..."
else:
formatted_prompt = formatted_prompt[:1000]
formatted_prompt = formatted_prompt[:max_prompt_length]
else:
# DALL-E 3 and others: Use template
try:
# Truncate post_title aggressively (max 80 chars to leave more room for image_prompt)
# Truncate post_title (max 200 chars for DALL-E 3 to leave room for image_prompt)
post_title = content.title or content.meta_title or f"Content #{content.id}"
if len(post_title) > 80:
post_title = post_title[:77] + "..."
if len(post_title) > 200:
post_title = post_title[:197] + "..."
# Calculate actual template length with placeholders filled
# Format template with dummy values to measure actual length
@@ -353,11 +365,11 @@ def process_image_generation_queue(self, image_ids: list, account_id: int = None
)
template_overhead = len(template_with_dummies)
# Calculate max image_prompt length: 1000 - template_overhead - safety margin (20)
max_image_prompt_length = 1000 - template_overhead - 20
if max_image_prompt_length < 50:
# If template is too long, use minimum 50 chars for image_prompt
max_image_prompt_length = 50
# Calculate max image_prompt length: max_prompt_length - template_overhead - safety margin (50)
max_image_prompt_length = max_prompt_length - template_overhead - 50
if max_image_prompt_length < 100:
# If template is too long, use minimum 100 chars for image_prompt
max_image_prompt_length = 100
logger.warning(f"Template is very long ({template_overhead} chars), limiting image_prompt to {max_image_prompt_length}")
logger.info(f"[process_image_generation_queue] Template overhead: {template_overhead} chars, max image_prompt: {max_image_prompt_length} chars")
@@ -379,47 +391,48 @@ def process_image_generation_queue(self, image_ids: list, account_id: int = None
image_prompt=image_prompt
)
# CRITICAL: Final safety check - ALWAYS truncate to 1000 chars max
if len(formatted_prompt) > 1000:
logger.warning(f"Formatted prompt too long ({len(formatted_prompt)} chars), truncating to 1000")
# CRITICAL: Final safety check - truncate to model-specific limit
if len(formatted_prompt) > max_prompt_length:
logger.warning(f"Formatted prompt too long ({len(formatted_prompt)} chars), truncating to {max_prompt_length} for {model}")
# Try word-aware truncation
truncated = formatted_prompt[:997]
truncated = formatted_prompt[:max_prompt_length - 3]
last_space = truncated.rfind(' ')
if last_space > 900: # Only use word-aware if we have a reasonable space
if last_space > max_prompt_length * 0.9: # Only use word-aware if we have a reasonable space
formatted_prompt = truncated[:last_space] + "..."
else:
formatted_prompt = formatted_prompt[:1000] # Hard truncate
formatted_prompt = formatted_prompt[:max_prompt_length] # Hard truncate
# Double-check after truncation - MUST be <= 1000
if len(formatted_prompt) > 1000:
logger.error(f"Prompt still too long after truncation ({len(formatted_prompt)} chars), forcing hard truncate")
formatted_prompt = formatted_prompt[:1000]
# Double-check after truncation - MUST be <= max_prompt_length
if len(formatted_prompt) > max_prompt_length:
logger.error(f"Prompt still too long after truncation ({len(formatted_prompt)} chars), forcing hard truncate to {max_prompt_length}")
formatted_prompt = formatted_prompt[:max_prompt_length]
except Exception as e:
# Fallback if template formatting fails
logger.warning(f"Prompt template formatting failed: {e}, using image prompt directly")
formatted_prompt = image_prompt
# CRITICAL: Truncate to 1000 chars even in fallback
if len(formatted_prompt) > 1000:
logger.warning(f"Fallback prompt too long ({len(formatted_prompt)} chars), truncating to 1000")
# CRITICAL: Truncate to model-specific limit even in fallback
if len(formatted_prompt) > max_prompt_length:
logger.warning(f"Fallback prompt too long ({len(formatted_prompt)} chars), truncating to {max_prompt_length} for {model}")
# Try word-aware truncation
truncated = formatted_prompt[:997]
truncated = formatted_prompt[:max_prompt_length - 3]
last_space = truncated.rfind(' ')
if last_space > 900:
if last_space > max_prompt_length * 0.9:
formatted_prompt = truncated[:last_space] + "..."
else:
formatted_prompt = formatted_prompt[:1000] # Hard truncate
# Final hard truncate if still too long - MUST be <= 1000
if len(formatted_prompt) > 1000:
formatted_prompt = formatted_prompt[:1000]
formatted_prompt = formatted_prompt[:max_prompt_length] # Hard truncate
# Final hard truncate if still too long - MUST be <= max_prompt_length
if len(formatted_prompt) > max_prompt_length:
logger.error(f"Fallback prompt still too long ({len(formatted_prompt)} chars), forcing hard truncate to {max_prompt_length}")
formatted_prompt = formatted_prompt[:max_prompt_length]
# Generate image (using same approach as test image generation)
logger.info(f"[process_image_generation_queue] Generating image {index}/{total_images} (ID: {image_id})")
logger.info(f"[process_image_generation_queue] Provider: {provider}, Model: {model}")
logger.info(f"[process_image_generation_queue] Prompt length: {len(formatted_prompt)} (MUST be <= 1000)")
if len(formatted_prompt) > 1000:
logger.error(f"[process_image_generation_queue] ERROR: Prompt is {len(formatted_prompt)} chars, truncating NOW!")
formatted_prompt = formatted_prompt[:1000]
logger.info(f"[process_image_generation_queue] Prompt length: {len(formatted_prompt)} (MUST be <= {max_prompt_length} for {model})")
if len(formatted_prompt) > max_prompt_length:
logger.error(f"[process_image_generation_queue] ERROR: Prompt is {len(formatted_prompt)} chars, truncating NOW to {max_prompt_length}!")
formatted_prompt = formatted_prompt[:max_prompt_length]
logger.info(f"[process_image_generation_queue] Final prompt length: {len(formatted_prompt)}")
logger.info(f"[process_image_generation_queue] Image type: {image_type}")