Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Enhanced Verification System Demo. | |
| Demonstrates the enhanced verification system with new display formats, | |
| provider summaries, and CSV export capabilities. | |
| Requirements: 8.2, 8.3, 8.4, 8.5 | |
| """ | |
| import tempfile | |
| import os | |
| from datetime import datetime | |
| from src.core.conversation_logger import ConversationLogger | |
| from src.core.conversation_verification import ( | |
| EnhancedConversationVerificationManager, | |
| VerificationFeedback | |
| ) | |
| from src.core.verification_exporter import EnhancedVerificationExporter | |
| from src.core.spiritual_state import SpiritualState, SpiritualAssessment | |
| from src.core.provider_summary_generator import ProviderSummary | |
| def create_sample_conversation(): | |
| """Create a sample conversation with various classification levels.""" | |
| logger = ConversationLogger(patient_name="Demo Patient") | |
| # Sample conversation exchanges with different classifications | |
| exchanges = [ | |
| { | |
| "user": "I've been struggling with the meaning of my illness lately", | |
| "assistant": "I understand this is a very difficult time for you. Many people in your situation experience these feelings.", | |
| "assessment": SpiritualAssessment( | |
| state=SpiritualState.RED, | |
| confidence=0.85, | |
| indicators=["loss of meaning", "spiritual distress", "existential questioning"], | |
| reasoning="Patient expressing significant spiritual concerns about meaning and purpose in context of illness" | |
| ) | |
| }, | |
| { | |
| "user": "Sometimes I wonder if there's any point to all this suffering", | |
| "assistant": "Your feelings are completely valid. It's natural to question these things when facing challenges.", | |
| "assessment": SpiritualAssessment( | |
| state=SpiritualState.RED, | |
| confidence=0.90, | |
| indicators=["questioning purpose", "suffering distress", "existential crisis"], | |
| reasoning="Patient expressing deep existential concerns about suffering and purpose" | |
| ) | |
| }, | |
| { | |
| "user": "I'm feeling a bit better today, but still uncertain", | |
| "assistant": "I'm glad to hear you're feeling better. Uncertainty is normal during recovery.", | |
| "assessment": SpiritualAssessment( | |
| state=SpiritualState.YELLOW, | |
| confidence=0.65, | |
| indicators=["uncertainty", "mild improvement"], | |
| reasoning="Patient showing some improvement but still experiencing uncertainty" | |
| ) | |
| }, | |
| { | |
| "user": "Thank you for listening. I feel more hopeful now", | |
| "assistant": "You're very welcome. I'm here whenever you need support.", | |
| "assessment": SpiritualAssessment( | |
| state=SpiritualState.GREEN, | |
| confidence=0.80, | |
| indicators=["gratitude", "hope", "positive outlook"], | |
| reasoning="Patient expressing gratitude and hope, indicating improved spiritual state" | |
| ) | |
| } | |
| ] | |
| # Log all exchanges | |
| for exchange in exchanges: | |
| logger.log_exchange( | |
| exchange["user"], | |
| exchange["assistant"], | |
| exchange["assessment"] | |
| ) | |
| return logger | |
| def demonstrate_enhanced_verification(): | |
| """Demonstrate the enhanced verification system.""" | |
| print("=" * 80) | |
| print("ENHANCED VERIFICATION SYSTEM DEMONSTRATION") | |
| print("=" * 80) | |
| print() | |
| # Create temporary directory for demo | |
| temp_dir = tempfile.mkdtemp() | |
| print(f"π Using temporary directory: {temp_dir}") | |
| print() | |
| try: | |
| # Step 1: Create sample conversation | |
| print("π£οΈ STEP 1: Creating sample conversation...") | |
| logger = create_sample_conversation() | |
| print(f" β Created conversation with {len(logger.entries)} exchanges") | |
| print() | |
| # Step 2: Create enhanced verification session | |
| print("π STEP 2: Creating enhanced verification session...") | |
| verification_manager = EnhancedConversationVerificationManager(temp_dir) | |
| session = verification_manager.create_verification_session( | |
| logger, | |
| verifier_name="Demo Verifier", | |
| enable_enhanced_formats=True | |
| ) | |
| print(f" β Created enhanced verification session: {session.session_id}") | |
| print(f" π Enhanced format enabled: {session.enhanced_format_enabled}") | |
| print(f" π Total records: {len(session.verification_records)}") | |
| print() | |
| # Step 3: Show enhanced display formats | |
| print("π¨ STEP 3: Demonstrating enhanced display formats...") | |
| for i, record in enumerate(session.verification_records[:2], 1): # Show first 2 | |
| print(f" π Record {i} Enhanced Display:") | |
| if record.enhanced_display_format: | |
| # Show first 200 chars of HTML | |
| preview = record.enhanced_display_format[:200].replace('\n', ' ') | |
| print(f" HTML Preview: {preview}...") | |
| if record.visual_sections: | |
| print(f" Visual Sections: {len(record.visual_sections)} sections") | |
| for section in record.visual_sections: | |
| print(f" - {section.get('type', 'unknown')}") | |
| print() | |
| # Step 4: Add provider summary to RED flag cases | |
| print("π STEP 4: Adding provider summaries to RED flag cases...") | |
| red_records = [r for r in session.verification_records if r.original_classification == "RED"] | |
| for i, record in enumerate(red_records): | |
| provider_summary = ProviderSummary( | |
| patient_name="Demo Patient", | |
| patient_phone="555-DEMO", | |
| classification="RED", | |
| confidence=record.original_confidence, | |
| indicators=record.original_indicators, | |
| reasoning=record.original_reasoning, | |
| urgency_level="IMMEDIATE" if record.original_confidence > 0.8 else "URGENT", | |
| severity_level="HIGH", | |
| situation_description=f"Patient expressing {', '.join(record.original_indicators[:2])} requiring immediate spiritual care attention" | |
| ) | |
| record.set_enhanced_formats(provider_summary=provider_summary) | |
| print(f" β Added provider summary to RED flag record {i+1}") | |
| print(f" π Provider summaries added to {len(red_records)} RED flag cases") | |
| print() | |
| # Step 5: Add verification feedback | |
| print("β STEP 5: Adding verification feedback...") | |
| feedbacks = [ | |
| {"correct": True, "notes": "Correctly identified spiritual distress"}, | |
| {"correct": True, "notes": "Appropriate RED classification"}, | |
| {"correct": False, "classification": "GREEN", "reason": "Patient showing improvement, should be GREEN"}, | |
| {"correct": True, "notes": "Correct GREEN classification"} | |
| ] | |
| for i, (record, feedback_data) in enumerate(zip(session.verification_records, feedbacks)): | |
| feedback = VerificationFeedback( | |
| exchange_id=record.exchange_id, | |
| is_correct=feedback_data["correct"], | |
| correct_classification=feedback_data.get("classification"), | |
| correction_reason=feedback_data.get("reason"), | |
| notes=feedback_data.get("notes") | |
| ) | |
| success = verification_manager.submit_exchange_verification( | |
| session.session_id, | |
| record.exchange_id, | |
| feedback | |
| ) | |
| if success: | |
| print(f" β Added feedback for exchange {i+1}") | |
| else: | |
| print(f" β Failed to add feedback for exchange {i+1}") | |
| print() | |
| # Step 6: Export enhanced CSV | |
| print("π STEP 6: Exporting enhanced CSV with new format data...") | |
| exporter = EnhancedVerificationExporter(temp_dir) | |
| csv_path = exporter.export_session_to_csv(session, include_enhanced_data=True) | |
| print(f" β Exported enhanced CSV: {os.path.basename(csv_path)}") | |
| # Show CSV preview | |
| with open(csv_path, 'r', encoding='utf-8') as f: | |
| lines = f.readlines() | |
| print(f" π CSV contains {len(lines)} lines") | |
| print(" π Enhanced columns included:") | |
| # Find header line | |
| for line in lines: | |
| if 'has_enhanced_display' in line: | |
| enhanced_columns = [col for col in line.split(',') if 'enhanced' in col.lower() or 'provider' in col.lower()] | |
| for col in enhanced_columns[:5]: # Show first 5 enhanced columns | |
| print(f" - {col.strip()}") | |
| break | |
| print() | |
| # Step 7: Generate enhanced summary report | |
| print("π STEP 7: Generating enhanced summary report...") | |
| report_path = exporter.export_enhanced_summary_report(session) | |
| print(f" β Generated summary report: {os.path.basename(report_path)}") | |
| # Show report preview | |
| with open(report_path, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| # Extract key statistics | |
| lines = content.split('\n') | |
| for line in lines: | |
| if 'Enhanced Display Coverage:' in line or 'Records with Enhanced Display:' in line: | |
| print(f" π {line.strip()}") | |
| print() | |
| # Step 8: Show session statistics | |
| print("π STEP 8: Enhanced session statistics...") | |
| stats = verification_manager.get_session_statistics(session.session_id) | |
| if stats: | |
| print(f" π Total Exchanges: {stats.get('total_exchanges', 0)}") | |
| print(f" β Verified Exchanges: {stats.get('verified_exchanges', 0)}") | |
| print(f" π― Overall Accuracy: {stats.get('progress', {}).get('accuracy_overall', 0):.1%}") | |
| print(f" π¨ Enhanced Format Enabled: {stats.get('enhanced_format_enabled', False)}") | |
| print(f" π Records with Enhanced Display: {stats.get('records_with_enhanced_display', 0)}") | |
| print(f" π Records with Provider Summary: {stats.get('records_with_provider_summary', 0)}") | |
| print() | |
| print("π DEMONSTRATION COMPLETE!") | |
| print("=" * 80) | |
| print() | |
| print("π Generated Files:") | |
| print(f" π Enhanced CSV: {csv_path}") | |
| print(f" π Summary Report: {report_path}") | |
| print(f" πΎ Session Data: {temp_dir}") | |
| print() | |
| print("β¨ Enhanced verification system successfully demonstrated!") | |
| print(" - Enhanced display formats with visual sections") | |
| print(" - Provider summaries for RED flag cases") | |
| print(" - Enhanced CSV export with new data fields") | |
| print(" - Comprehensive summary reports") | |
| print(" - Backward compatibility with existing system") | |
| except Exception as e: | |
| print(f"β Error during demonstration: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| finally: | |
| # Clean up (optional - comment out to keep files for inspection) | |
| # import shutil | |
| # shutil.rmtree(temp_dir, ignore_errors=True) | |
| # print(f"π§Ή Cleaned up temporary directory") | |
| pass | |
| if __name__ == "__main__": | |
| demonstrate_enhanced_verification() |