Spaces:
Sleeping
Sleeping
| 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 <repository> | |
| cd <project-directory> | |
| 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 |