Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Error Handling Demo for UI Classification Improvements. | |
| Demonstrates error handling and recovery mechanisms in action. | |
| Requirements: 9.1, 9.2, 9.3, 9.4 | |
| """ | |
| import sys | |
| import os | |
| # Add src to path for imports | |
| current_dir = os.path.dirname(os.path.abspath(__file__)) | |
| parent_dir = os.path.dirname(current_dir) | |
| if parent_dir not in sys.path: | |
| sys.path.insert(0, parent_dir) | |
| from src.core.ui_error_handler import UIErrorHandler | |
| from src.core.provider_summary_generator import ProviderSummary, ProviderSummaryGenerator | |
| from src.interface.enhanced_results_display_manager import EnhancedResultsDisplayManager | |
| from src.core.improved_classification_prompt_manager import ImprovedClassificationPromptManager | |
| def demo_provider_summary_validation(): | |
| """Demonstrate provider summary validation and error recovery.""" | |
| print("=" * 60) | |
| print("PROVIDER SUMMARY VALIDATION DEMO") | |
| print("=" * 60) | |
| error_handler = UIErrorHandler() | |
| # Create invalid summary | |
| print("\n1. Creating invalid provider summary...") | |
| invalid_summary = ProviderSummary( | |
| patient_name="[Patient Name]", # Placeholder | |
| patient_phone="[Phone Number]", # Placeholder | |
| classification="RED", | |
| confidence=1.5, # Invalid confidence | |
| reasoning="", # Empty reasoning | |
| indicators=[], # No indicators | |
| severity_level="INVALID", # Invalid level | |
| urgency_level="INVALID" # Invalid level | |
| ) | |
| print(f" Patient Name: {invalid_summary.patient_name}") | |
| print(f" Patient Phone: {invalid_summary.patient_phone}") | |
| print(f" Confidence: {invalid_summary.confidence}") | |
| print(f" Reasoning: '{invalid_summary.reasoning}'") | |
| print(f" Indicators: {invalid_summary.indicators}") | |
| print(f" Severity: {invalid_summary.severity_level}") | |
| print(f" Urgency: {invalid_summary.urgency_level}") | |
| # Validate summary | |
| print("\n2. Validating summary structure...") | |
| validation_result = error_handler.validate_provider_summary_structure(invalid_summary) | |
| print(f" Valid: {validation_result.is_valid}") | |
| print(f" Errors: {len(validation_result.errors)}") | |
| print(f" Warnings: {len(validation_result.warnings)}") | |
| if validation_result.errors: | |
| print("\n Error Details:") | |
| for i, error in enumerate(validation_result.errors[:3], 1): # Show first 3 | |
| print(f" {i}. {error.message}") | |
| if error.suggestion: | |
| print(f" Suggestion: {error.suggestion}") | |
| # Apply fallback template | |
| print("\n3. Applying fallback template...") | |
| fixed_summary = error_handler.apply_fallback_template(invalid_summary, "general") | |
| print(f" Patient Name: {fixed_summary.patient_name}") | |
| print(f" Patient Phone: {fixed_summary.patient_phone}") | |
| print(f" Confidence: {fixed_summary.confidence}") | |
| print(f" Reasoning: '{fixed_summary.reasoning[:50]}...'") | |
| print(f" Indicators: {fixed_summary.indicators}") | |
| print(f" Severity: {fixed_summary.severity_level}") | |
| print(f" Urgency: {fixed_summary.urgency_level}") | |
| # Re-validate | |
| print("\n4. Re-validating fixed summary...") | |
| new_validation = error_handler.validate_provider_summary_structure(fixed_summary) | |
| print(f" Valid: {new_validation.is_valid}") | |
| print(f" Errors: {len(new_validation.errors)}") | |
| print(f" Warnings: {len(new_validation.warnings)}") | |
| def demo_display_error_handling(): | |
| """Demonstrate display error handling and degraded mode.""" | |
| print("\n" + "=" * 60) | |
| print("DISPLAY ERROR HANDLING DEMO") | |
| print("=" * 60) | |
| display_manager = EnhancedResultsDisplayManager() | |
| # Test with invalid data | |
| print("\n1. Testing display with invalid data...") | |
| try: | |
| result = display_manager.format_ai_analysis_section( | |
| classification="", # Empty classification | |
| indicators=[], # Empty indicators | |
| reasoning="", # Empty reasoning | |
| confidence=None | |
| ) | |
| print(" Display result generated successfully") | |
| print(f" Result length: {len(result)} characters") | |
| print(f" Contains 'AI Analysis': {'AI Analysis' in result}") | |
| print(f" Contains error handling: {'Display Error' in result or 'No indicators' in result}") | |
| except Exception as e: | |
| print(f" Error occurred: {e}") | |
| # Test degraded display | |
| print("\n2. Testing degraded display creation...") | |
| error_context = "Simulated formatting error" | |
| original_content = "<div>Original content that failed to format</div>" | |
| degraded_display = display_manager.error_handler.create_degraded_display( | |
| error_context, original_content | |
| ) | |
| print(" Degraded display created successfully") | |
| print(f" Contains error message: {'Display Error Detected' in degraded_display}") | |
| print(f" Contains recovery actions: {'Recovery Actions' in degraded_display}") | |
| print(f" Contains original content: {original_content in degraded_display}") | |
| def demo_classification_error_handling(): | |
| """Demonstrate classification error handling.""" | |
| print("\n" + "=" * 60) | |
| print("CLASSIFICATION ERROR HANDLING DEMO") | |
| print("=" * 60) | |
| error_handler = UIErrorHandler() | |
| # Simulate classification error | |
| print("\n1. Simulating classification error...") | |
| test_error = Exception("Simulated AI classification failure") | |
| input_data = { | |
| 'message': 'I feel hopeless and have lost all meaning in life', | |
| 'classification': 'red', | |
| 'confidence': 0.8 | |
| } | |
| fallback_result = error_handler.handle_classification_error(test_error, input_data) | |
| print(f" Fallback classification: {fallback_result.classification}") | |
| print(f" Fallback confidence: {fallback_result.confidence}") | |
| print(f" Fallback indicators: {fallback_result.indicators}") | |
| print(f" Fallback reasoning: {fallback_result.reasoning[:50]}...") | |
| print(f" Is valid: {fallback_result.is_valid}") | |
| # Test with critical keywords | |
| print("\n2. Testing with critical keywords...") | |
| critical_input = { | |
| 'message': 'I want to kill myself and end it all' | |
| } | |
| critical_result = error_handler.handle_classification_error(test_error, critical_input) | |
| print(f" Critical classification: {critical_result.classification}") | |
| print(f" Critical confidence: {critical_result.confidence}") | |
| print(f" Critical indicators: {critical_result.indicators}") | |
| def demo_end_to_end_recovery(): | |
| """Demonstrate end-to-end error recovery.""" | |
| print("\n" + "=" * 60) | |
| print("END-TO-END ERROR RECOVERY DEMO") | |
| print("=" * 60) | |
| # Initialize components | |
| summary_generator = ProviderSummaryGenerator() | |
| display_manager = EnhancedResultsDisplayManager() | |
| # Start with problematic data | |
| print("\n1. Starting with problematic data...") | |
| problematic_data = { | |
| 'classification': 'INVALID', | |
| 'confidence': -0.5, | |
| 'indicators': [], | |
| 'reasoning': '', | |
| 'patient_name': '', | |
| 'patient_phone': '' | |
| } | |
| print(f" Classification: {problematic_data['classification']}") | |
| print(f" Confidence: {problematic_data['confidence']}") | |
| print(f" Indicators: {problematic_data['indicators']}") | |
| print(f" Reasoning: '{problematic_data['reasoning']}'") | |
| print(f" Patient Name: '{problematic_data['patient_name']}'") | |
| print(f" Patient Phone: '{problematic_data['patient_phone']}'") | |
| # Generate summary with error recovery | |
| print("\n2. Generating summary with error recovery...") | |
| summary = summary_generator.generate_summary( | |
| indicators=problematic_data['indicators'], | |
| reasoning=problematic_data['reasoning'], | |
| confidence=problematic_data['confidence'], | |
| patient_name=problematic_data['patient_name'], | |
| patient_phone=problematic_data['patient_phone'] | |
| ) | |
| print(f" Fixed confidence: {summary.confidence}") | |
| print(f" Fixed reasoning length: {len(summary.reasoning)}") | |
| print(f" Fixed patient name: {summary.patient_name}") | |
| print(f" Fixed patient phone: {summary.patient_phone}") | |
| print(f" Recommended actions: {len(summary.recommended_actions)}") | |
| # Display with error handling | |
| print("\n3. Displaying with error handling...") | |
| display_result = display_manager.format_provider_summary_section(summary) | |
| print(f" Display result generated: {len(display_result)} characters") | |
| print(f" Contains provider summary: {'Provider Summary' in display_result}") | |
| print(f" Contains patient info: {summary.patient_name in display_result}") | |
| print("\n4. System successfully recovered from all errors!") | |
| def demo_error_statistics(): | |
| """Demonstrate error statistics collection.""" | |
| print("\n" + "=" * 60) | |
| print("ERROR STATISTICS DEMO") | |
| print("=" * 60) | |
| error_handler = UIErrorHandler() | |
| # Create multiple invalid summaries to collect statistics | |
| print("\n1. Creating multiple invalid summaries...") | |
| summaries = [ | |
| ProviderSummary(patient_name="", patient_phone="", confidence=1.5, reasoning=""), | |
| ProviderSummary(patient_name="[Patient Name]", patient_phone="invalid", confidence=-0.1, reasoning="short"), | |
| ProviderSummary(patient_name="Valid Name", patient_phone="555-123-4567", confidence=0.8, reasoning="", indicators=[]) | |
| ] | |
| all_errors = [] | |
| for i, summary in enumerate(summaries, 1): | |
| validation_result = error_handler.validate_provider_summary_structure(summary) | |
| all_errors.extend(validation_result.errors) | |
| print(f" Summary {i}: {len(validation_result.errors)} errors, {len(validation_result.warnings)} warnings") | |
| # Get statistics | |
| print(f"\n2. Collecting statistics from {len(all_errors)} total errors...") | |
| stats = error_handler.get_error_statistics(all_errors) | |
| print(f" Total errors: {stats['total']}") | |
| print(f" By category: {stats['by_category']}") | |
| print(f" By severity: {stats['by_severity']}") | |
| print(f" By component: {stats['by_component']}") | |
| def main(): | |
| """Run all error handling demos.""" | |
| print("UI ERROR HANDLING AND RECOVERY DEMONSTRATION") | |
| print("Requirements: 9.1, 9.2, 9.3, 9.4") | |
| try: | |
| demo_provider_summary_validation() | |
| demo_display_error_handling() | |
| demo_classification_error_handling() | |
| demo_end_to_end_recovery() | |
| demo_error_statistics() | |
| print("\n" + "=" * 60) | |
| print("ALL DEMOS COMPLETED SUCCESSFULLY!") | |
| print("Error handling and recovery mechanisms are working correctly.") | |
| print("=" * 60) | |
| except Exception as e: | |
| print(f"\nDemo failed with error: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| if __name__ == "__main__": | |
| main() |