removeing unneceary code

This commit is contained in:
Desktop
2025-11-12 04:20:43 +05:00
parent d1d2d768e5
commit 4bd158ce01
10 changed files with 130 additions and 1004 deletions

View File

@@ -6,7 +6,6 @@ from igny8_core.ai.functions.generate_ideas import GenerateIdeasFunction
from igny8_core.ai.functions.generate_content import GenerateContentFunction
from igny8_core.ai.functions.generate_images import GenerateImagesFunction, generate_images_core
from igny8_core.ai.functions.generate_image_prompts import GenerateImagePromptsFunction
from igny8_core.ai.functions.generate_images_from_prompts import GenerateImagesFromPromptsFunction
__all__ = [
'AutoClusterFunction',
@@ -15,5 +14,4 @@ __all__ = [
'GenerateImagesFunction',
'generate_images_core',
'GenerateImagePromptsFunction',
'GenerateImagesFromPromptsFunction',
]

View File

@@ -1,472 +0,0 @@
"""
Generate Images from Prompts AI Function
Generates actual images from existing image prompts using AI
"""
import logging
from typing import Dict, List, Any
from django.db import transaction
from igny8_core.ai.base import BaseAIFunction
from igny8_core.modules.writer.models import Images, Content
from igny8_core.ai.ai_core import AICore
from igny8_core.ai.validators import validate_ids
from igny8_core.ai.prompts import PromptRegistry
logger = logging.getLogger(__name__)
class GenerateImagesFromPromptsFunction(BaseAIFunction):
"""Generate actual images from image prompts using AI"""
def get_name(self) -> str:
return 'generate_images_from_prompts'
def get_metadata(self) -> Dict:
return {
'display_name': 'Generate Images from Prompts',
'description': 'Generate actual images from existing image prompts',
'phases': {
'INIT': 'Validating image prompts...',
'PREP': 'Preparing image generation queue...',
'AI_CALL': 'Generating images with AI...',
'PARSE': 'Processing image URLs...',
'SAVE': 'Saving image URLs...',
'DONE': 'Images generated!'
}
}
def get_max_items(self) -> int:
return 100 # Max images per batch
def validate(self, payload: dict, account=None) -> Dict:
"""Validate image IDs exist and have prompts"""
result = validate_ids(payload, max_items=self.get_max_items())
if not result['valid']:
return result
# Check images exist and have prompts
image_ids = payload.get('ids', [])
if image_ids:
queryset = Images.objects.filter(id__in=image_ids)
if account:
queryset = queryset.filter(account=account)
images = list(queryset.select_related('content', 'task'))
if not images:
return {
'valid': False,
'error': 'No images found with provided IDs'
}
# Check all images have prompts
images_without_prompts = [img.id for img in images if not img.prompt or not img.prompt.strip()]
if images_without_prompts:
return {
'valid': False,
'error': f'Images {images_without_prompts} do not have prompts'
}
# Check all images are pending
images_not_pending = [img.id for img in images if img.status != 'pending']
if images_not_pending:
return {
'valid': False,
'error': f'Images {images_not_pending} are not in pending status'
}
return {'valid': True}
def prepare(self, payload: dict, account=None) -> Dict:
"""Load images and image generation settings"""
image_ids = payload.get('ids', [])
queryset = Images.objects.filter(id__in=image_ids, status='pending')
if account:
queryset = queryset.filter(account=account)
images = list(queryset.select_related('content', 'task', 'account', 'site', 'sector'))
if not images:
raise ValueError("No pending images found with prompts")
# Get image generation settings - CHECK IF ENABLED
image_settings = {}
image_generation_enabled = False
if account:
try:
from igny8_core.modules.system.models import IntegrationSettings
integration = IntegrationSettings.objects.get(
account=account,
integration_type='image_generation'
)
image_generation_enabled = integration.is_active
image_settings = integration.config or {}
logger.info(f"[generate_images_from_prompts] Image generation settings: enabled={image_generation_enabled}, config_keys={list(image_settings.keys())}")
except IntegrationSettings.DoesNotExist:
logger.warning(f"[generate_images_from_prompts] Image generation integration not found for account {account.id}")
raise ValueError("Image generation integration not configured")
except Exception as e:
logger.error(f"[generate_images_from_prompts] Failed to load image generation settings: {e}")
raise ValueError(f"Failed to load image generation settings: {str(e)}")
if not image_generation_enabled:
raise ValueError("Image generation is not enabled in settings")
# Get provider from image_generation settings
provider = image_settings.get('provider') or image_settings.get('service', 'openai')
logger.info(f"[generate_images_from_prompts] Provider from settings: {provider}")
# Get provider-specific settings (OpenAI or Runware) - CHECK IF ENABLED
provider_api_key = None
provider_enabled = False
provider_model = None
if provider == 'openai':
try:
openai_settings = IntegrationSettings.objects.get(
account=account,
integration_type='openai'
)
provider_enabled = openai_settings.is_active
provider_api_key = openai_settings.config.get('apiKey') if openai_settings.config else None
provider_model = openai_settings.config.get('model') if openai_settings.config else None
logger.info(f"[generate_images_from_prompts] OpenAI settings: enabled={provider_enabled}, has_key={bool(provider_api_key)}, model={provider_model}")
except IntegrationSettings.DoesNotExist:
logger.error(f"[generate_images_from_prompts] OpenAI integration not found")
raise ValueError("OpenAI integration not configured")
except Exception as e:
logger.error(f"[generate_images_from_prompts] Error getting OpenAI settings: {e}")
raise ValueError(f"Failed to load OpenAI settings: {str(e)}")
elif provider == 'runware':
try:
runware_settings = IntegrationSettings.objects.get(
account=account,
integration_type='runware'
)
provider_enabled = runware_settings.is_active
provider_api_key = runware_settings.config.get('apiKey') if runware_settings.config else None
provider_model = runware_settings.config.get('model') if runware_settings.config else None
logger.info(f"[generate_images_from_prompts] Runware settings: enabled={provider_enabled}, has_key={bool(provider_api_key)}, model={provider_model}")
except IntegrationSettings.DoesNotExist:
logger.error(f"[generate_images_from_prompts] Runware integration not found")
raise ValueError("Runware integration not configured")
except Exception as e:
logger.error(f"[generate_images_from_prompts] Error getting Runware settings: {e}")
raise ValueError(f"Failed to load Runware settings: {str(e)}")
else:
raise ValueError(f"Invalid provider: {provider}")
# Validate provider is enabled and has API key
if not provider_enabled:
raise ValueError(f"{provider.capitalize()} integration is not enabled")
if not provider_api_key:
raise ValueError(f"{provider.capitalize()} API key not configured")
# Determine model: from provider settings, or image_generation settings, or default
if provider_model:
model = provider_model
elif provider == 'runware':
model = image_settings.get('model') or image_settings.get('runwareModel', 'runware:97@1')
else:
model = image_settings.get('model') or image_settings.get('imageModel', 'dall-e-3')
logger.info(f"[generate_images_from_prompts] Final settings: provider={provider}, model={model}, enabled={provider_enabled}, has_api_key={bool(provider_api_key)}")
# Get prompt templates
image_prompt_template = PromptRegistry.get_image_prompt_template(account)
negative_prompt = PromptRegistry.get_negative_prompt(account)
return {
'images': images,
'account': account,
'provider': provider,
'model': model,
'api_key': provider_api_key, # Include API key
'image_type': image_settings.get('image_type', 'realistic'),
'image_format': image_settings.get('image_format', 'webp'),
'image_prompt_template': image_prompt_template,
'negative_prompt': negative_prompt,
}
def build_prompt(self, data: Dict, account=None) -> str:
"""
Build prompt for AI_CALL phase.
For image generation, we return a placeholder since we process images in save_output.
"""
# Return placeholder - actual processing happens in save_output
return "Image generation queue prepared"
def parse_response(self, response: str, step_tracker=None) -> Dict:
"""
Parse response from AI_CALL.
For image generation, we process images directly in save_output, so this is a placeholder.
"""
return {'processed': True}
def save_output(
self,
parsed: Dict,
original_data: Dict,
account=None,
progress_tracker=None,
step_tracker=None,
console_tracker=None
) -> Dict:
"""
Process all images sequentially and generate them.
This method handles the loop and makes AI calls directly.
"""
function_name = self.get_name()
if console_tracker:
console_tracker.save(f"[{function_name}] Starting image generation queue")
images = original_data.get('images', [])
if not images:
error_msg = "[{function_name}] No images to process"
if console_tracker:
console_tracker.error('ValidationError', error_msg)
raise ValueError(error_msg)
provider = original_data.get('provider', 'openai')
model = original_data.get('model', 'dall-e-3')
api_key = original_data.get('api_key') # Get API key from prepare
image_type = original_data.get('image_type', 'realistic')
image_prompt_template = original_data.get('image_prompt_template', '')
negative_prompt = original_data.get('negative_prompt', '')
# Validate API key is present
if not api_key:
error_msg = f"[{function_name}] API key not found for provider {provider}"
if console_tracker:
console_tracker.error('ConfigurationError', error_msg)
raise ValueError(error_msg)
ai_core = AICore(account=account or original_data.get('account'))
total_images = len(images)
images_generated = 0
images_failed = 0
errors = []
if console_tracker:
console_tracker.prep(f"[{function_name}] Preparing {total_images} image{'s' if total_images != 1 else ''} for generation")
# Initialize image queue in meta for frontend
image_queue = []
for idx, img in enumerate(images, 1):
content_obj = img.content
if not content_obj:
if img.task:
content_title = img.task.title
else:
content_title = "Content"
else:
content_title = content_obj.title or content_obj.meta_title or "Content"
image_queue.append({
'image_id': img.id,
'index': idx,
'label': f"{img.image_type.replace('_', ' ').title()} Image",
'content_title': content_title,
'status': 'pending',
'progress': 0,
'image_url': None,
'error': None
})
# Send initial queue to frontend
if progress_tracker:
initial_meta = step_tracker.get_meta() if step_tracker else {}
initial_meta['image_queue'] = image_queue
progress_tracker.update("PREP", 10, f"Preparing to generate {total_images} image{'s' if total_images != 1 else ''}", meta=initial_meta)
if console_tracker:
console_tracker.prep(f"[{function_name}] Image queue initialized with {total_images} image{'s' if total_images != 1 else ''}")
console_tracker.prep(f"[{function_name}] Provider: {provider}, Model: {model}, API Key: {'***' + api_key[-4:] if api_key and len(api_key) > 4 else 'NOT SET'}")
# Queue all prompts first (TEST MODE - don't send to AI)
queued_prompts = []
# Process each image sequentially
for index, image in enumerate(images, 1):
queue_item = image_queue[index - 1]
queue_item['status'] = 'processing'
queue_item['progress'] = 0
try:
# Get content title
content = image.content
if not content:
# Fallback to task if no content
if image.task:
content_title = image.task.title
else:
content_title = "Content"
else:
content_title = content.title or content.meta_title or "Content"
if console_tracker:
console_tracker.prep(f"[{function_name}] Processing image {index}/{total_images}: {image.image_type} for '{content_title}'")
# Format prompt using template
if image_prompt_template:
try:
formatted_prompt = image_prompt_template.format(
post_title=content_title,
image_prompt=image.prompt,
image_type=image_type
)
if console_tracker:
console_tracker.prep(f"[{function_name}] Formatted prompt using template (length: {len(formatted_prompt)})")
except KeyError as e:
logger.warning(f"Template formatting error: {e}, using simple format")
formatted_prompt = f"Create a high-quality {image_type} image: {image.prompt}"
if console_tracker:
console_tracker.prep(f"[{function_name}] Template formatting error, using fallback prompt")
else:
# Fallback template
formatted_prompt = f"Create a high-quality {image_type} image: {image.prompt}"
if console_tracker:
console_tracker.prep(f"[{function_name}] Using fallback prompt template")
# Update progress: PREP phase for this image
if progress_tracker and step_tracker:
prep_msg = f"Generating image {index} of {total_images}: {image.image_type}"
step_tracker.add_request_step("PREP", "success", prep_msg)
queue_item['progress'] = 10
# Update queue in meta
meta = step_tracker.get_meta()
meta['image_queue'] = image_queue
progress_pct = 10 + int((index - 1) / total_images * 15) # 10-25% for PREP
progress_tracker.update("PREP", progress_pct, prep_msg, meta=meta)
# Generate image - update progress incrementally
if progress_tracker and step_tracker:
ai_msg = f"Generating {image.image_type} image {index} of {total_images} with AI"
step_tracker.add_response_step("AI_CALL", "success", ai_msg)
queue_item['progress'] = 25
meta = step_tracker.get_meta()
meta['image_queue'] = image_queue
progress_pct = 25 + int((index - 1) / total_images * 45) # 25-70% for AI_CALL
progress_tracker.update("AI_CALL", progress_pct, ai_msg, meta=meta)
# Update progress to 50% (simulating API call start)
queue_item['progress'] = 50
if progress_tracker and step_tracker:
meta = step_tracker.get_meta()
meta['image_queue'] = image_queue
progress_tracker.update("AI_CALL", progress_pct, ai_msg, meta=meta)
# Queue the complete prompt (TEST MODE - don't send to AI yet)
queued_prompts.append({
'image_id': image.id,
'index': index,
'image_type': image.image_type,
'content_title': content_title,
'provider': provider,
'model': model,
'formatted_prompt': formatted_prompt,
'negative_prompt': negative_prompt if provider == 'runware' else None,
'prompt_length': len(formatted_prompt)
})
if console_tracker:
console_tracker.ai_call(f"[{function_name}] [TEST MODE] Queued prompt {index}/{total_images}: {image.image_type} for '{content_title}'")
console_tracker.ai_call(f"[{function_name}] [TEST MODE] Provider: {provider}, Model: {model}")
console_tracker.ai_call(f"[{function_name}] [TEST MODE] Prompt length: {len(formatted_prompt)} chars")
console_tracker.ai_call(f"[{function_name}] [TEST MODE] Prompt preview: {formatted_prompt[:150]}...")
# TEMPORARY: Simulate result for testing (don't actually call AI)
result = {
'url': None,
'error': None,
'test_mode': True,
'queued': True
}
# ACTUAL AI CALL (COMMENTED OUT FOR TESTING)
# if console_tracker:
# console_tracker.ai_call(f"[{function_name}] Calling {provider}/{model} API for image {index}/{total_images}")
#
# result = ai_core.generate_image(
# prompt=formatted_prompt,
# provider=provider,
# model=model,
# size='1024x1024',
# n=1,
# api_key=api_key, # Pass API key explicitly
# negative_prompt=negative_prompt if provider == 'runware' else None,
# function_name='generate_images_from_prompts'
# )
# TEST MODE: Mark as queued (not actually generated)
queue_item['status'] = 'completed'
queue_item['progress'] = 100
queue_item['image_url'] = None # No URL in test mode
if console_tracker:
console_tracker.parse(f"[{function_name}] [TEST MODE] Prompt queued for image {index}/{total_images}")
console_tracker.save(f"[{function_name}] [TEST MODE] Queued image {index}/{total_images} (ID: {image.id})")
# Update progress: SAVE phase
if progress_tracker and step_tracker:
save_msg = f"Queued prompt {index} of {total_images} (TEST MODE)"
step_tracker.add_request_step("SAVE", "success", save_msg)
meta = step_tracker.get_meta()
meta['image_queue'] = image_queue
progress_pct = 85 + int((index - 1) / total_images * 13) # 85-98% for SAVE
progress_tracker.update("SAVE", progress_pct, save_msg, meta=meta)
except Exception as e:
# Mark as failed
queue_item['status'] = 'failed'
queue_item['progress'] = 100
queue_item['error'] = str(e)
with transaction.atomic():
image.status = 'failed'
image.save(update_fields=['status', 'updated_at'])
error_msg = f"[{function_name}] Image {index}/{total_images} exception: {str(e)}"
errors.append(error_msg)
images_failed += 1
logger.error(f"Exception generating image {image.id}: {str(e)}", exc_info=True)
if console_tracker:
console_tracker.error('Exception', error_msg)
continue
# Log all queued prompts (TEST MODE)
if console_tracker:
console_tracker.save(f"[{function_name}] [TEST MODE] All prompts queued. Total: {len(queued_prompts)} prompts")
console_tracker.save(f"[{function_name}] [TEST MODE] Provider: {provider}, Model: {model}")
for qp in queued_prompts:
console_tracker.save(f"[{function_name}] [TEST MODE] Image {qp['index']}: {qp['image_type']} - '{qp['content_title']}'")
console_tracker.save(f"[{function_name}] [TEST MODE] Prompt ({qp['prompt_length']} chars): {qp['formatted_prompt'][:100]}...")
# Final progress update
if progress_tracker and step_tracker:
final_msg = f"Queued {len(queued_prompts)} prompts (TEST MODE - not sent to AI)"
step_tracker.add_request_step("SAVE", "success", final_msg)
meta = step_tracker.get_meta()
meta['image_queue'] = image_queue
meta['queued_prompts'] = queued_prompts # Include queued prompts in meta
progress_tracker.update("SAVE", 98, final_msg, meta=meta)
if console_tracker:
console_tracker.done(f"[{function_name}] [TEST MODE] Queued {len(queued_prompts)}/{total_images} prompts successfully (NOT sent to AI)")
return {
'count': len(queued_prompts),
'images_generated': 0, # 0 because we're in test mode
'images_failed': 0,
'total_images': total_images,
'queued_prompts': queued_prompts, # Return queued prompts
'test_mode': True,
'provider': provider,
'model': model,
'errors': errors if errors else None
}