#!/usr/bin/env python3 """ ENHANCED FALLBACK SYSTEM TEST: Verify multiple models with fallback support. ROBUST PRODUCTION SYSTEM TESTING. """ import os import sys import asyncio import base64 import time from io import BytesIO from PIL import Image, ImageDraw import logging from typing import Dict, Any, List # Add AI directory ai_dir = os.path.join(os.path.dirname(__file__), 'ai') sys.path.insert(0, ai_dir) # Configure logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) class EnhancedFallbackTester: """Test the enhanced fallback system.""" def __init__(self): self.test_results = {} def create_test_image(self) -> str: """Create test image for multimodal testing.""" print("๐ŸŽจ Creating test image...") img = Image.new('RGB', (224, 224), color='white') draw = ImageDraw.Draw(img) # Draw a simple scene draw.rectangle([0, 150, 224, 224], fill='lightgreen') # Ground draw.rectangle([50, 100, 100, 150], fill='brown') # House draw.polygon([30, 100, 75, 60, 120, 100], fill='red') # Roof draw.ellipse([160, 80, 190, 110], fill='yellow') # Sun buffer = BytesIO() img.save(buffer, format='PNG') img_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8') print("โœ… Test image created") return img_base64 async def test_model_loader_fallbacks(self) -> Dict[str, Any]: """Test model loader with fallback support.""" print("\n๐Ÿ”ง TESTING MODEL LOADER FALLBACKS") print("=" * 60) try: from ai.multimodal.enhanced_model_loader import get_enhanced_model_loader # Initialize enhanced loader loader = get_enhanced_model_loader(device="cpu") # Test each task task_results = {} for task in ["image_captioning", "vqa", "multimodal_chat", "text_classification"]: print(f"\n๐Ÿ“‹ Testing task: {task}") try: # Get available models for this task available_models = loader.get_available_models_for_task(task) print(f" ๐Ÿ“ฆ Available models: {len(available_models)}") for model_info in available_models: status = "โœ… LOADED" if model_info["loaded"] else "โณ NOT LOADED" print(f" {status} {model_info['name']} (priority: {model_info['priority']})") # Try to load model with fallback start_time = time.time() model_info = loader.load_model_with_fallback(task) load_time = time.time() - start_time if model_info: print(f" โœ… Successfully loaded: {model_info['model_name']}") print(f" ๐Ÿ“Š Parameters: {model_info['parameters']:,}") print(f" โฑ๏ธ Load time: {load_time:.2f}s") print(f" ๐Ÿ’พ Memory: {model_info['memory_gb']}GB") task_results[task] = { "success": True, "model_used": model_info["model_name"], "parameters": model_info["parameters"], "load_time": load_time, "available_models": len(available_models) } else: print(f" โŒ Failed to load any model for task: {task}") task_results[task] = { "success": False, "error": "No models loaded" } except Exception as e: print(f" โŒ Task {task} failed: {e}") task_results[task] = { "success": False, "error": str(e) } # Get system status system_status = loader.get_system_status() return { "task_results": task_results, "system_status": system_status, "overall_success": all(result.get("success", False) for result in task_results.values()) } except Exception as e: print(f"โŒ Model loader test failed: {e}") return {"success": False, "error": str(e)} async def test_multimodal_handler_fallbacks(self) -> Dict[str, Any]: """Test multimodal handler with fallback support.""" print("\n๐Ÿค– TESTING MULTIMODAL HANDLER FALLBACKS") print("=" * 60) try: from ai.multimodal.enhanced_multimodal_handler import create_enhanced_multimodal_handler from ai.multimodal.schemas import MultimodalEvaluationRequest, MultimodalInput # Initialize enhanced handler handler = create_enhanced_multimodal_handler(device="cpu", enable_fallback=True) # Create test image test_image = self.create_test_image() # Test different scenarios test_scenarios = [ { "name": "Image Captioning", "text": "Describe this image", "image": test_image, "expected_task": "image_captioning" }, { "name": "Visual Question Answering", "text": "What do you see in this image?", "image": test_image, "expected_task": "vqa" }, { "name": "Multimodal Chat", "text": "What can you tell me about this image?", "image": test_image, "expected_task": "multimodal_chat" }, { "name": "Text Classification", "text": "This is safe and educational content", "image": None, "expected_task": "text_classification" } ] scenario_results = [] for scenario in test_scenarios: print(f"\n๐Ÿ“ Testing scenario: {scenario['name']}") try: # Process input multimodal_input = handler.process_input( text=scenario["text"], image=scenario["image"] ) # Create request request = MultimodalEvaluationRequest( input=multimodal_input, target_model="auto", # Let handler choose with fallback evaluation_type="test" ) # Evaluate with fallback start_time = time.time() result = await handler.evaluate_multimodal(request) eval_time = time.time() - start_time if result.success: print(f" โœ… Success: {result.success}") print(f" ๐Ÿค– Model Used: {result.model_used}") print(f" ๐Ÿ”„ Fallback Used: {result.fallback_used}") print(f" โฑ๏ธ Processing Time: {result.processing_time_ms:.1f}ms") print(f" ๐Ÿ›ก๏ธ Safety Score: {result.safety_score:.3f}") if result.evaluation and "model_response" in result.evaluation: response = result.evaluation["model_response"] print(f" ๐Ÿค– Response: '{response[:100]}...'") scenario_results.append({ "scenario": scenario["name"], "success": True, "model_used": result.model_used, "fallback_used": result.fallback_used, "processing_time_ms": result.processing_time_ms, "safety_score": result.safety_score, "expected_task": scenario["expected_task"] }) else: print(f" โŒ Evaluation failed") scenario_results.append({ "scenario": scenario["name"], "success": False, "error": "Evaluation failed" }) except Exception as e: print(f" โŒ Scenario failed: {e}") scenario_results.append({ "scenario": scenario["name"], "success": False, "error": str(e) }) # Calculate overall success successful = sum(1 for r in scenario_results if r["success"]) total = len(scenario_results) success_rate = successful / total return { "scenario_results": scenario_results, "successful_scenarios": successful, "total_scenarios": total, "success_rate": success_rate, "overall_success": success_rate >= 0.75 } except Exception as e: print(f"โŒ Multimodal handler test failed: {e}") return {"success": False, "error": str(e)} async def test_fallback_robustness(self) -> Dict[str, Any]: """Test fallback robustness by simulating failures.""" print("\n๐Ÿ›ก๏ธ TESTING FALLBACK ROBUSTNESS") print("=" * 60) try: from ai.multimodal.enhanced_multimodal_handler import create_enhanced_multimodal_handler from ai.multimodal.schemas import MultimodalEvaluationRequest, MultimodalInput # Initialize handler handler = create_enhanced_multimodal_handler(device="cpu", enable_fallback=True) # Get system status status = handler.get_system_status() print(f"๐Ÿ“Š System Status:") print(f" ๐Ÿ“‹ Supported Tasks: {len(status['supported_tasks'])}") print(f" ๐Ÿค– Loaded Models: {status['loaded_models']}") # Test each task status for task, task_status in status["task_status"].items(): print(f"\n๐Ÿ“‹ Task: {task}") print(f" ๐Ÿ“ฆ Total Models: {task_status['total_models']}") print(f" โœ… Loaded Models: {task_status['loaded_models']}") print(f" ๐ŸŽฏ Primary Loaded: {task_status['primary_loaded']}") for model in task_status["available_models"]: status_icon = "โœ…" if model["loaded"] else "โณ" print(f" {status_icon} {model['name']} (priority: {model['priority']})") # Test fallback chain print(f"\n๐Ÿ”„ TESTING FALLBACK CHAIN:") # Create test image test_image = self.create_test_image() # Test image captioning (should try multiple models if needed) multimodal_input = handler.process_input( text="Describe this image", image=test_image ) request = MultimodalEvaluationRequest( input=multimodal_input, target_model="auto", evaluation_type="fallback_test" ) start_time = time.time() result = await handler.evaluate_multimodal(request) eval_time = time.time() - start_time if result.success: print(f" โœ… Fallback chain successful") print(f" ๐Ÿค– Final Model: {result.model_used}") print(f" ๐Ÿ”„ Fallback Used: {result.fallback_used}") print(f" โฑ๏ธ Time: {eval_time:.1f}ms") if result.evaluation and "models_tried" in result.evaluation: models_tried = result.evaluation["models_tried"] print(f" ๐Ÿ“‹ Models Tried: {models_tried}") return { "success": True, "final_model": result.model_used, "fallback_used": result.fallback_used, "models_tried": result.evaluation.get("models_tried", []), "processing_time_ms": eval_time } else: print(f" โŒ Fallback chain failed") return {"success": False, "error": "Fallback chain failed"} except Exception as e: print(f"โŒ Robustness test failed: {e}") return {"success": False, "error": str(e)} async def run_comprehensive_fallback_test(self) -> Dict[str, Any]: """Run comprehensive fallback system test.""" print("๐Ÿญ ENHANCED FALLBACK SYSTEM TEST") print("=" * 70) print("๐Ÿ”„ TESTING MULTIPLE MODELS WITH FALLBACK SUPPORT") print("๐Ÿ›ก๏ธ PRODUCTION ROBUSTNESS VALIDATION") print() # Run all tests test_results = {} # Test 1: Model loader fallbacks print("๐Ÿงช TEST 1: Model Loader Fallbacks") test_results["model_loader"] = await self.test_model_loader_fallbacks() # Test 2: Multimodal handler fallbacks print("\n๐Ÿงช TEST 2: Multimodal Handler Fallbacks") test_results["multimodal_handler"] = await self.test_multimodal_handler_fallbacks() # Test 3: Fallback robustness print("\n๐Ÿงช TEST 3: Fallback Robustness") test_results["robustness"] = await self.test_fallback_robustness() # Calculate overall results tests_passed = sum(1 for result in test_results.values() if result.get("success", result.get("overall_success", False))) total_tests = len(test_results) overall_success_rate = tests_passed / total_tests # Generate final report final_report = { "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), "total_tests": total_tests, "tests_passed": tests_passed, "overall_success_rate": overall_success_rate, "system_robust": overall_success_rate >= 0.75, "test_results": test_results, "summary": { "model_loader_fallbacks": test_results.get("model_loader", {}).get("overall_success", False), "multimodal_handler_fallbacks": test_results.get("multimodal_handler", {}).get("overall_success", False), "fallback_robustness": test_results.get("robustness", {}).get("success", False) } } return final_report def generate_fallback_report(self, report: Dict[str, Any]): """Generate comprehensive fallback system report.""" print("\n๐Ÿ“Š ENHANCED FALLBACK SYSTEM REPORT") print("=" * 70) print(f"\n๐ŸŽฏ OVERALL FALLBACK SYSTEM STATUS:") print(f" ๐Ÿ“… Timestamp: {report['timestamp']}") print(f" ๐Ÿงช Tests Run: {report['total_tests']}") print(f" โœ… Tests Passed: {report['tests_passed']}") print(f" ๐Ÿ“ˆ Success Rate: {report['overall_success_rate']:.1%}") print(f" ๐Ÿ›ก๏ธ System Robust: {'โœ… YES' if report['system_robust'] else 'โŒ NO'}") print(f"\n๐Ÿ“‹ COMPONENT STATUS:") summary = report.get("summary", {}) components = { "model_loader_fallbacks": "๐Ÿ”ง Model Loader Fallbacks", "multimodal_handler_fallbacks": "๐Ÿค– Multimodal Handler Fallbacks", "fallback_robustness": "๐Ÿ›ก๏ธ Fallback Robustness" } for key, name in components.items(): status = "โœ… PASS" if summary.get(key, False) else "โŒ FAIL" print(f" {status} {name}") # Detailed results if "test_results" in report: print(f"\n๐Ÿ” DETAILED RESULTS:") # Model loader results if "model_loader" in report["test_results"]: loader_result = report["test_results"]["model_loader"] print(f"\n๐Ÿ”ง MODEL LOADER:") if loader_result.get("overall_success"): print(f" โœ… All tasks loaded successfully") if "task_results" in loader_result: for task, result in loader_result["task_results"].items(): if result.get("success"): print(f" โœ… {task}: {result.get('model_used', 'Unknown')}") else: print(f" โŒ {task}: Failed") else: print(f" โŒ Some tasks failed to load") # Multimodal handler results if "multimodal_handler" in report["test_results"]: handler_result = report["test_results"]["multimodal_handler"] print(f"\n๐Ÿค– MULTIMODAL HANDLER:") if handler_result.get("overall_success"): print(f" โœ… Scenarios: {handler_result.get('successful_scenarios', 0)}/{handler_result.get('total_scenarios', 0)}") if "scenario_results" in handler_result: for result in handler_result["scenario_results"]: if result.get("success"): fallback_status = "๐Ÿ”„" if result.get("fallback_used") else "โœ…" print(f" {fallback_status} {result.get('scenario', 'Unknown')}: {result.get('model_used', 'Unknown')}") else: print(f" โŒ {result.get('scenario', 'Unknown')}: Failed") else: print(f" โŒ Some scenarios failed") # Robustness results if "robustness" in report["test_results"]: robust_result = report["test_results"]["robustness"] print(f"\n๐Ÿ›ก๏ธ FALLBACK ROBUSTNESS:") if robust_result.get("success"): print(f" โœ… Fallback chain working") print(f" ๐Ÿค– Final Model: {robust_result.get('final_model', 'Unknown')}") print(f" ๐Ÿ”„ Fallback Used: {robust_result.get('fallback_used', False)}") models_tried = robust_result.get("models_tried", []) if models_tried: print(f" ๐Ÿ“‹ Models Tried: {models_tried}") else: print(f" โŒ Fallback chain failed") # Production readiness assessment if report["system_robust"]: print(f"\n๐Ÿ† ENHANCED FALLBACK SYSTEM: PRODUCTION READY!") print(f" โœ… Multiple models with fallback support") print(f" โœ… Robust error handling") print(f" โœ… Automatic model switching") print(f" โœ… Production reliability confirmed") else: print(f"\nโš ๏ธ ENHANCED FALLBACK SYSTEM: NEEDS IMPROVEMENT") print(f" โŒ Some fallback mechanisms not working") print(f" ๐Ÿ”ง System needs optimization") return report async def main(): """Main test function.""" print("๐Ÿญ ENHANCED FALLBACK SYSTEM TEST") print("=" * 70) print("๐Ÿ”„ TESTING MULTIPLE MODELS WITH FALLBACK SUPPORT") print("๐Ÿ›ก๏ธ PRODUCTION ROBUSTNESS VALIDATION") print() # Create tester tester = EnhancedFallbackTester() # Run comprehensive test fallback_report = await tester.run_comprehensive_fallback_test() # Generate report tester.generate_fallback_report(fallback_report) # Return exit code return 0 if fallback_report.get("system_robust", False) else 1 if __name__ == "__main__": exit_code = asyncio.run(main()) exit(exit_code)