#!/usr/bin/env python3 """ Test script for generate_images_from_prompts function Run this to test the function and see the response """ import os import sys import django # Setup Django sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend')) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'igny8_core.settings') django.setup() from igny8_core.ai.functions.generate_images_from_prompts import GenerateImagesFromPromptsFunction from igny8_core.ai.engine import AIEngine from igny8_core.modules.writer.models import Images from igny8_core.auth.models import Account def test_generate_images_from_prompts(): """Test the generate_images_from_prompts function""" print("=" * 80) print("Testing generate_images_from_prompts function") print("=" * 80) # Get first account try: account = Account.objects.first() if not account: print("ERROR: No accounts found in database") return print(f"Using account: {account.name} (ID: {account.id})") except Exception as e: print(f"ERROR: Failed to get account: {e}") return # Get pending images with prompts try: images = Images.objects.filter( account=account, status='pending' ).exclude(prompt__isnull=True).exclude(prompt='')[:3] # Get first 3 if not images.exists(): print("ERROR: No pending images with prompts found") print("Please create some images with prompts first") return image_ids = list(images.values_list('id', flat=True)) print(f"\nFound {len(image_ids)} pending images: {image_ids}") # Show image details for img in images: content_title = "Unknown" if img.content: content_title = img.content.title or img.content.meta_title or "No title" elif img.task: content_title = img.task.title or "No title" print(f" - Image ID {img.id}: {img.image_type} for '{content_title}'") print(f" Prompt: {img.prompt[:100] if img.prompt else 'None'}...") except Exception as e: print(f"ERROR: Failed to get images: {e}") import traceback traceback.print_exc() return # Create function instance try: fn = GenerateImagesFromPromptsFunction() print(f"\nFunction created: {fn.get_name()}") except Exception as e: print(f"ERROR: Failed to create function: {e}") import traceback traceback.print_exc() return # Validate try: print("\n" + "=" * 80) print("Step 1: VALIDATE") print("=" * 80) validation = fn.validate({'ids': image_ids}, account=account) print(f"Validation result: {validation}") if not validation.get('valid'): print(f"ERROR: Validation failed: {validation.get('error')}") return except Exception as e: print(f"ERROR: Validation failed: {e}") import traceback traceback.print_exc() return # Prepare try: print("\n" + "=" * 80) print("Step 2: PREPARE") print("=" * 80) prepared = fn.prepare({'ids': image_ids}, account=account) print(f"Prepared data keys: {list(prepared.keys())}") print(f"Provider: {prepared.get('provider')}") print(f"Model: {prepared.get('model')}") print(f"API Key: {'***' + prepared.get('api_key', '')[-4:] if prepared.get('api_key') and len(prepared.get('api_key', '')) > 4 else 'NOT SET'}") print(f"Images count: {len(prepared.get('images', []))}") print(f"Has prompt template: {bool(prepared.get('image_prompt_template'))}") print(f"Has negative prompt: {bool(prepared.get('negative_prompt'))}") except Exception as e: print(f"ERROR: Prepare failed: {e}") import traceback traceback.print_exc() return # Build prompt (placeholder for this function) try: print("\n" + "=" * 80) print("Step 3: BUILD PROMPT") print("=" * 80) prompt = fn.build_prompt(prepared, account=account) print(f"Prompt (placeholder): {prompt}") except Exception as e: print(f"ERROR: Build prompt failed: {e}") import traceback traceback.print_exc() return # Execute via AIEngine (simulated, no actual Celery) try: print("\n" + "=" * 80) print("Step 4: EXECUTE (via AIEngine - TEST MODE)") print("=" * 80) print("Note: This will queue prompts but NOT send to AI (TEST MODE)") engine = AIEngine(account=account) result = engine.execute(fn, {'ids': image_ids}) print("\n" + "=" * 80) print("EXECUTION RESULT") print("=" * 80) print(f"Success: {result.get('success')}") print(f"Keys in result: {list(result.keys())}") if result.get('success'): print(f"\nCount: {result.get('count', 0)}") print(f"Images generated: {result.get('images_generated', 0)}") print(f"Images failed: {result.get('images_failed', 0)}") print(f"Total images: {result.get('total_images', 0)}") print(f"Test mode: {result.get('test_mode', False)}") print(f"Provider: {result.get('provider')}") print(f"Model: {result.get('model')}") queued_prompts = result.get('queued_prompts', []) if queued_prompts: print(f"\nQueued Prompts ({len(queued_prompts)}):") for idx, qp in enumerate(queued_prompts, 1): print(f"\n Prompt {idx}/{len(queued_prompts)}:") print(f" Image ID: {qp.get('image_id')}") print(f" Image Type: {qp.get('image_type')}") print(f" Content Title: {qp.get('content_title')}") print(f" Provider: {qp.get('provider')}") print(f" Model: {qp.get('model')}") print(f" Prompt Length: {qp.get('prompt_length')} chars") print(f" Full Prompt:") print(f" {qp.get('formatted_prompt', '')[:200]}...") if qp.get('negative_prompt'): print(f" Negative Prompt: {qp.get('negative_prompt')}") else: print(f"\nError: {result.get('error')}") print(f"Error type: {result.get('error_type')}") print("\n" + "=" * 80) print("FULL RESULT JSON (for frontend)") print("=" * 80) import json print(json.dumps(result, indent=2, default=str)) except Exception as e: print(f"ERROR: Execution failed: {e}") import traceback traceback.print_exc() return if __name__ == '__main__': test_generate_images_from_prompts()