--- title: Medical Assistant with Spiritual Support v.2.3.2 emoji: 🩺 colorFrom: blue colorTo: green sdk: gradio sdk_version: 6.0.2 app_file: src/interface/simplified_gradio_app.py pinned: false --- # Medical Assistant with Spiritual Support A comprehensive medical chat application with **automatic background monitoring for spiritual distress** and **advanced prompt optimization system**. This system provides seamless medical assistance while intelligently detecting and addressing spiritual care needs through a sophisticated AI-powered classification and triage system. ## ⚑ Quick Start ### 🎯 Two Interface Options The project now offers **two interfaces** to suit different needs: #### 1. **Simplified Interface** (Recommended for Testing Team) - βœ… Provider Summary as **final exchange** in verification - βœ… **Auto-save** reports with single button - βœ… Streamlined UI focused on testing ```bash python run_simplified_interface.py # http://localhost:7861 ``` πŸ“– **Quick Guide:** See `QUICK_START_SIMPLIFIED.md` or `QUICK_START_SIMPLIFIED_EN.md` #### 2. **Full Interface** (For Development & Configuration) Complete feature set with all advanced options: ```bash python app.py # http://localhost:7860 ``` ### Local Setup **πŸ₯ Medical Assistant + πŸ•ŠοΈ Spiritual Support + πŸ”§ Prompt Optimization** ```bash # 1. Configure API Keys (first time) cat > .env << EOF GEMINI_API_KEY=your_gemini_api_key_here ANTHROPIC_API_KEY=your_anthropic_api_key_here EOF # 2. Install Dependencies python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt # 3. Run Application # Simplified Interface (for testing): python run_simplified_interface.py # OR Full Interface (for development): python app.py ``` ### Interface Comparison | Feature | Simplified | Full | |---------|-----------|------| | Chat & Verification | βœ… | βœ… | | Provider Summary as Final Exchange | βœ… | ❌ | | Auto-Save Reports | βœ… | ❌ | | Model Settings | ❌ | βœ… | | Edit Prompts | ❌ | βœ… | | Patient Profiles | ❌ | βœ… | πŸ“Š **Full Comparison:** See `COMPARISON.md` **Main Interface Tabs (Full):** - πŸ’¬ **Chat** β€” Primary medical conversation with automatic spiritual monitoring - 🧾 **Conversation Verification** β€” Review and export chat-derived verification sessions - πŸ” **Enhanced Verification** β€” Manual input and file upload workflows for structured testing - βš™οΈ **Model Settings** β€” Configure AI models for different tasks (session-scoped) - πŸ”§ **Edit Prompts** β€” Real-time prompt editing with session-level overrides - πŸ‘₯ **Patient Profiles** β€” Predefined patient scenarios for testing - πŸ“– **Help** β€” Comprehensive user guide **Main Interface Tabs (Simplified):** - πŸ’¬ **Chat** β€” Test conversations with medical assistant - 🧾 **Conversation Verification** β€” Review exchanges with Provider Summary as final step - πŸ“– **Help** β€” User guide and documentation --- ## 🎯 System Architecture ### Intelligent Spiritual Monitoring The system operates as a **Medical Assistant** while continuously monitoring for spiritual distress: ``` Patient: "I'm feeling stressed about my treatment" ↓ [Spiritual Monitor] β†’ YELLOW (Potential distress detected) ↓ [Soft Spiritual Triage] β†’ Asks 2-3 gentle clarifying questions ↓ [Triage Response Evaluator] β†’ Evaluates responses ↓ Result: GREEN (Coping well) or RED (Needs referral) ``` ### Three-Tier Classification System (Enhanced v2.2) **🟒 GREEN (No Spiritual Distress)** - Medical symptoms only - Routine health questions - Standard wellness topics - **Clear positive statements** like "I am fine" (now correctly classified) - No emotional or spiritual concerns **🟑 YELLOW (Potential Spiritual Distress - Needs Clarification)** - **Simple sadness without spiritual context** (improved classification) - Stress, anxiety, sleep issues - Grief and loss (without expressed distress) - Existential questions - Spiritual disconnection - Feelings of isolation - Loss of interest in activities - **Ambiguous situations** requiring further investigation **πŸ”΄ RED (Active Spiritual Distress - Immediate Attention)** **Updated Definition:** Patient's situation appears to be **caused by or actively causing** emotional or spiritual distress (broader than crisis-only) **Explicit RED Indicators (Always RED):** - **Complex grief** - **Loss of a loved one** (when patient expresses emotional distress) - **Doubt about meaning of life** (e.g., "Life has no meaning anymore") - **Doubt about meaning of suffering** - **Doubt about personal dignity** - **Loss of meaning and purpose** - Suicidal ideation - Severe hopelessness - Spiritual crisis - Anger at God/higher power - Moral injury --- ## πŸš€ Advanced Prompt Optimization System ### Centralized Prompt Management - **PromptController**: Orchestrates all prompt operations with shared components - **Shared Catalogs**: Centralized storage for indicators, rules, templates, and categories - **Session Isolation**: Test prompt changes without affecting production - **Three-tier Priority**: Session Overrides β†’ Centralized Files β†’ Default Fallbacks ### Session-Level Prompt Overrides - **Real-time Testing**: Edit prompts and test immediately - **Session Isolation**: Changes apply only to your current session - **Promote to File**: Tested changes can be promoted to permanent files - **Automatic Backups**: Original files backed up before promotion ### Enhanced Edit Prompts Interface - **Visual Indicators**: Clear display of prompt sources (session vs centralized) - **Real-time Validation**: Immediate feedback on prompt structure and syntax - **CSS-Optimized Display**: No UI overflow issues with validation messages - **Promote Workflow**: Easy promotion of tested changes to permanent files --- ## πŸ“¦ Core Components ### 1. πŸ₯ Simplified Medical App Main application logic with integrated spiritual monitoring. **File:** `src/core/simplified_medical_app.py` ### 2. πŸ” Spiritual Monitor Classifies patient messages into GREEN/YELLOW/RED categories. **File:** `src/core/spiritual_monitor.py` ### 3. 🟑 Soft Triage Manager Conducts gentle spiritual triage questioning for YELLOW states. **File:** `src/core/soft_triage_manager.py` ### 4. οΏ½ Provider Summary Generator Generates structured summaries and compassionate LLM-based messages for spiritual care team. **File:** `src/core/provider_summary_generator.py` ### 5. οΏ½πŸ”§ Prompt Management System Centralized prompt optimization with session-level overrides. **Files:** `src/config/prompt_management/` ### 6. 🎨 Enhanced Gradio Interface Comprehensive web interface with all features integrated. **File:** `src/interface/gradio_app.py` --- ## οΏ½ ProjecΠ½t Structure ``` . β”œβ”€β”€ src/ β”‚ β”œβ”€β”€ core/ # Core application logic β”‚ β”‚ β”œβ”€β”€ simplified_medical_app.py # Main application β”‚ β”‚ β”œβ”€β”€ spiritual_monitor.py # Distress classifier β”‚ β”‚ β”œβ”€β”€ soft_triage_manager.py # Gentle triage questioning β”‚ β”‚ β”œβ”€β”€ spiritual_state.py # State management β”‚ β”‚ └── ai_client.py # AI provider interface β”‚ β”œβ”€β”€ config/ β”‚ β”‚ β”œβ”€β”€ prompt_management/ # πŸ†• Prompt optimization system β”‚ β”‚ β”‚ β”œβ”€β”€ prompt_controller.py # Central orchestrator β”‚ β”‚ β”‚ β”œβ”€β”€ shared_components.py # Shared catalogs β”‚ β”‚ β”‚ β”œβ”€β”€ data_models.py # Data structures β”‚ β”‚ β”‚ └── data/ # JSON storage β”‚ β”‚ β”œβ”€β”€ prompts/ # Prompt files β”‚ β”‚ └── ai_providers_config.py # Model configurations β”‚ └── interface/ β”‚ β”œβ”€β”€ gradio_app.py # Main web interface β”‚ └── enhanced_prompt_editor.py # πŸ†• Prompt editing UI β”‚ β”œβ”€β”€ tests/ # πŸ†• Organized test structure β”‚ β”œβ”€β”€ prompt_optimization/ # Prompt system tests β”‚ β”œβ”€β”€ integration/ # Integration tests β”‚ β”œβ”€β”€ unit/ # Unit tests β”‚ β”œβ”€β”€ verification/ # Verification tests β”‚ └── chaplain_feedback/ # Chaplain feedback tests β”‚ β”œβ”€β”€ scripts/ # πŸ†• Utility scripts β”‚ β”œβ”€β”€ cleanup_test_data.py β”‚ β”œβ”€β”€ reorganize_files.py β”‚ └── run_tests.py β”‚ β”œβ”€β”€ review/ # πŸ†• Medical professional feedback β”‚ β”œβ”€β”€ README.md # Comprehensive review documentation (Ukrainian) β”‚ β”œβ”€β”€ Or_2.txt # Medical professional recommendations β”‚ └── *.json # Verified conversation examples β”œβ”€β”€ docs/ # Documentation β”œβ”€β”€ .verification_data/ # Test data and sessions β”œβ”€β”€ requirements.txt # Dependencies β”œβ”€β”€ .env # API keys (not in git) └── README.md # This file ``` --- ## 🎯 Key Features ### πŸ₯ Medical Assistant with Spiritual Support #### Intelligent Background Monitoring - πŸ” Automatic spiritual distress detection - 🚦 Three-tier classification system (🟒 🟑 πŸ”΄) - πŸ“ Provider summary generation for RED cases - πŸ’¬ LLM-generated compassionate messages for spiritual care team - ❓ Gentle triage questioning for YELLOW cases - 🀝 Consent-based referral process #### Advanced AI Model Selection - πŸ€– Choose between Claude and Gemini models - βš™οΈ Task-specific model configuration (6 distinct agents) - πŸ”„ Dynamic model switching - πŸ’Ύ Session-scoped settings #### Comprehensive Prompt Management - πŸ”§ Edit 6 system prompts in real-time (including Spiritual Care Message) - πŸ“ Session-level prompt overrides - βœ… Real-time validation and syntax checking - πŸ“€ Promote tested changes to permanent files - πŸ”„ Reset to defaults anytime #### Verification & Export Capabilities - 🧾 **Conversation Verification**: Review chat exchanges and export results - πŸ” **Enhanced Verification**: Manual input and file upload for batch testing - πŸ“Š **Multiple Export Formats**: CSV and JSON with comprehensive metadata - πŸ“ˆ **Analytics**: Detailed statistics and performance metrics ### πŸ§ͺ Comprehensive Testing System #### 65+ Test Suite - βœ… All tests passing (65/65) - πŸ”¬ Property-based testing with Hypothesis - 🎯 9 correctness properties validated - πŸ“Š Complete coverage of all scenarios - πŸš€ Automated test organization and execution --- ## πŸ› οΈ Technology Stack - **Backend:** Python 3.14+ - **AI Models:** Google Gemini 2.5 Flash, Anthropic Claude 3.5 Sonnet - **UI Framework:** Gradio 6.0.2 - **Testing:** Pytest + Hypothesis (property-based testing) - **Storage:** JSON-based with automatic validation - **Architecture:** Modular, scalable, and maintainable --- ## οΏ½ Implementation Status ### βœ… Core Medical Assistant (v2.2) - βœ… Background spiritual monitoring with **enhanced classification logic** - βœ… **Improved three-tier classification system** (GREEN/YELLOW/RED) based on medical feedback - βœ… **Fixed critical classification bugs** (JSON parsing, specific case handling) - βœ… **Medical Brain Compatible Summary** as primary provider handoff format - βœ… Gentle triage questioning - βœ… Consent-based referral process - βœ… **Streamlined provider summary generation** (single format) - βœ… **Enhanced RED flag definition** (broader than crisis-only) - βœ… **Explicit RED indicators** for consistent classification - βœ… Patient medical context integration in messages - βœ… Multiple AI model support (6 distinct agents) ### βœ… Prompt Optimization System (v1.1) - βœ… Centralized prompt management with PromptController - βœ… Session-level prompt overrides with isolation - βœ… Enhanced Edit Prompts UI with validation - βœ… Shared component architecture (indicators, rules, templates) - βœ… Promote to File workflow with automatic backups - βœ… Real-time validation and syntax checking - βœ… Spiritual Care Message prompt editing support ### βœ… Testing & Quality Assurance (v2.2) - βœ… 65+ comprehensive tests (all passing) - βœ… **Enhanced classification accuracy** based on medical professional feedback - βœ… **Critical bug fixes validated** (JSON parsing, classification logic) - βœ… Property-based testing with 9 correctness properties - βœ… Organized test structure with clear categorization - βœ… Automated test execution and reporting - βœ… Complete integration and end-to-end testing - βœ… **Medical professional review integration** with verified conversation examples ### βœ… Enhanced User Experience - βœ… Comprehensive Help documentation - βœ… Patient profile management - βœ… Conversation verification and export - βœ… Enhanced verification with file upload - βœ… Real-time model and prompt configuration --- ## πŸ§ͺ Testing ### Run All Tests ```bash python run_tests.py ``` **Current Status:** βœ… 65/65 tests passing ### Test Categories ```bash # Prompt Optimization Tests python -m pytest tests/prompt_optimization/ -v # Integration Tests python -m pytest tests/integration/ -v # Unit Tests python -m pytest tests/unit/ -v # Verification Tests python -m pytest tests/verification/ -v # Chaplain Feedback Tests python -m pytest tests/chaplain_feedback/ -v ``` ### Property-Based Testing The system includes 9 correctness properties validated through property-based testing: 1. **Component Consistency Enforcement** 2. **Scenario-Targeted Question Generation** 3. **Structured Feedback Data Capture** 4. **Consent-Based Language Compliance** 5. **Shared Component Update Propagation** 6. **Context-Aware Classification Logic** 7. **Complete Provider Summary Generation** 8. **Comprehensive Performance Monitoring** 9. **Session-Level Prompt Override Preservation** --- ## πŸ”’ Security & Privacy - ❌ **No PHI Storage**: Protected Health Information is not stored - πŸ” **Secure API Keys**: Stored in .env file (not in version control) - πŸ›‘οΈ **Conservative Classification**: Errs on the side of caution - πŸ“ **Audit Logging**: All interactions logged for review - 🀝 **Consent-Based**: Referrals only with explicit patient consent - πŸ”’ **Session Isolation**: User sessions are completely isolated --- ## πŸ“š Documentation ### Core Documentation - `PROMPT_OPTIMIZATION_IMPLEMENTATION_REPORT.md` β€” Comprehensive implementation details - `PROJECT_STRUCTURE.md` β€” Detailed project organization - `docs/Spiritual Distress Testing Tool.md` β€” Customer specification - `docs/Spiritual Distress Definition, Defining Characteristics, and Descriptions.md` β€” Clinical reference - **`review/README.md`** β€” πŸ†• Medical professional feedback and classification improvements (Ukrainian) - **`review/Or_2.txt`** β€” πŸ†• Medical professional recommendations that guided v2.2 improvements ### User Guides - **Help Tab** β€” Built-in comprehensive user guide - **Interface Documentation** β€” Embedded in each tab - **Testing Guides** β€” Step-by-step verification workflows --- ## πŸš€ Getting Started ### Prerequisites - Python 3.14+ - API keys for Gemini and/or Claude - Virtual environment (recommended) ### Installation 1. **Clone and Setup:** ```bash git clone cd python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt ``` 2. **Configure API Keys:** ```bash cp .env.example .env # Edit .env with your API keys ``` 3. **Run Tests (Optional):** ```bash python run_tests.py ``` 4. **Start Application:** ```bash python src/interface/gradio_app.py ``` 5. **Access Interface:** Open http://localhost:7860 in your browser --- ## πŸ“ž Support & Troubleshooting ### Common Issues 1. **Check Logs:** ```bash tail -f ai_interactions.log ``` 2. **Verify Tests:** ```bash python run_tests.py ``` 3. **Reset Configuration:** - Use "Reset to Defaults" in Edit Prompts tab - Clear browser cache if needed ### Documentation Resources - **Help Tab**: Comprehensive user guide in the application - **Implementation Report**: `PROMPT_OPTIMIZATION_IMPLEMENTATION_REPORT.md` - **Project Structure**: `PROJECT_STRUCTURE.md` --- ## πŸŽ‰ Ready for Production The Medical Assistant with Spiritual Support system is **fully functional and production-ready** with: - βœ… **Complete Implementation**: All requirements satisfied - βœ… **Comprehensive Testing**: 65+ tests with 100% pass rate - βœ… **Advanced Features**: Prompt optimization, session management, verification workflows - βœ… **User-Friendly Interface**: Intuitive design with built-in help - βœ… **Robust Architecture**: Scalable, maintainable, and secure - βœ… **Quality Assurance**: Property-based testing and continuous validation --- **Version:** 2.3 **Last Updated:** January 7, 2026 **Status:** βœ… Production Ready with Enhanced Provider Summary **Test Coverage:** 65/65 tests passing ### πŸ†• Latest Updates (v2.3) - January 2026 #### Provider Summary Improvements Based on medical professional feedback from `review/Or_3.txt`: **βœ… Enhanced Provider Summary Content:** - **Removed phone numbers** from provider summaries (team has direct access in application) - **Include all patient inputs** throughout interaction instead of just final message - **Comprehensive conversation context** for better spiritual care team understanding **βœ… Automatic Summary Generation:** - **Instant provider summary** when RED flag triggers and patient consents - **No manual intervention required** - summary appears automatically in Provider Summary panel - **Immediate review capability** for spiritual care team to comment on accuracy **βœ… Improved User Experience:** - **Fixed HTML badge rendering** issues in provider summary status - **Clean text formatting** with proper Markdown compatibility - **Enhanced debug logging** for better troubleshooting #### Previous Updates (v2.2) - January 2026 #### Critical Classification Improvements Based on medical professional feedback from `review/Or_2.txt`: **βœ… Fixed Critical JSON Parsing Bug:** - **Issue:** LLM returned `"classification"` field but parser expected `"state"` field - **Solution:** Updated parser to handle both field formats - **Impact:** Eliminated classification errors due to parsing failures **βœ… Enhanced RED Flag Definition:** - **Previous:** Limited to "severe distress or crisis" - **Current:** Broader definition includes "active spiritual distress" - **Benefit:** More accurate detection of patients needing spiritual care **βœ… Added Explicit RED Indicators:** - Complex grief β†’ Always RED - Loss of loved one (with distress) β†’ Always RED - Doubt about meaning of life β†’ Always RED - Doubt about meaning of suffering β†’ Always RED - Doubt about personal dignity β†’ Always RED **βœ… Improved Specific Case Handling:** - **"I am fine"** now correctly classified as 🟒 GREEN (was incorrectly YELLOW) - **"I feel sad"** now correctly classified as 🟑 YELLOW (simple sadness without context) - **"Life has no meaning"** now correctly classified as πŸ”΄ RED (explicit meaning loss) #### Medical Brain Integration Optimization **βœ… Streamlined Provider Summary Format:** - Removed redundant "Message for Spiritual Care Team" functionality - **Medical Brain Compatible Summary** is now the primary and only format - Single coherent paragraph format for seamless Medical Brain integration - Optimized prompt structure for Medical Brain compatibility #### Documentation and Review **βœ… Comprehensive Review Documentation:** - Added detailed `review/README.md` (Ukrainian) with medical professional feedback - Analyzed verified conversation examples with outcomes - Documented all implemented improvements and validation results #### Validation Results ``` βœ… "I am fine" β†’ 🟒 GREEN (confidence: 1.0) βœ… "I feel sad" β†’ 🟑 YELLOW (confidence: 0.9) βœ… "Life has no meaning anymore" β†’ πŸ”΄ RED (confidence: 1.0) ``` All test cases now pass according to medical professional recommendations. --- ## πŸ“ Project Organization ### Test Files - **`tests/`** - Automated test suite (65+ tests) - **`manual_tests/`** - Manual validation scripts with detailed README ### Documentation - **`docs/reports/`** - Implementation reports and feature documentation - **`README.md`** - Main project documentation (this file) - **`PROJECT_STRUCTURE.md`** - Detailed architecture overview