File size: 7,029 Bytes
a963d65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3
"""
πŸ§ͺ Test Gradio Interface
Quick test of the Gradio medical document processing interface
"""

import asyncio
import sys
import os
from datetime import datetime

# Add src to path (from tests directory)
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))

def test_gradio_imports():
    """Test that all required imports work"""
    print("πŸ§ͺ Testing Gradio Interface Dependencies...")
    
    try:
        import gradio as gr
        print("βœ… Gradio imported successfully")
        assert True  # Gradio import successful
    except ImportError as e:
        print(f"❌ Gradio import failed: {e}")
        assert False, f"Gradio import failed: {e}"
    
    try:
        from src.file_processor import local_processor
        from src.codellama_processor import CodeLlamaProcessor
        from src.fhir_validator import FhirValidator
        from src.monitoring import monitor
        print("βœ… All FhirFlame modules imported successfully")
        assert True  # All modules imported successfully
    except ImportError as e:
        print(f"❌ FhirFlame module import failed: {e}")
        assert False, f"FhirFlame module import failed: {e}"

def test_basic_functionality():
    """Test basic processing functionality"""
    print("\nπŸ”¬ Testing Basic Processing Functionality...")
    
    try:
        from src.file_processor import local_processor
        from src.fhir_validator import FhirValidator
        
        # Test local processor
        sample_text = """
        Patient: John Doe
        Diagnosis: Hypertension
        Medications: Lisinopril 10mg daily
        """
        
        entities = local_processor._extract_medical_entities(sample_text)
        print(f"βœ… Entity extraction working: {len(entities)} entities found")
        assert len(entities) > 0, "Entity extraction should find at least one entity"
        
        # Test FHIR validator
        validator = FhirValidator()
        sample_bundle = {
            "resourceType": "Bundle",
            "id": "test-bundle",
            "type": "document",
            "entry": []
        }
        
        validation = validator.validate_fhir_bundle(sample_bundle)
        print(f"βœ… FHIR validation working: {validation['is_valid']}")
        assert validation['is_valid'], "FHIR validation should succeed for valid bundle"
        
    except Exception as e:
        print(f"❌ Basic functionality test failed: {e}")
        assert False, f"Basic functionality test failed: {e}"

def test_gradio_components():
    """Test Gradio interface components"""
    print("\n🎨 Testing Gradio Interface Components...")
    
    try:
        import gradio as gr
        
        # Test basic components creation
        with gr.Blocks() as test_interface:
            file_input = gr.File(label="Test File Input")
            text_input = gr.Textbox(label="Test Text Input")
            output_json = gr.JSON(label="Test JSON Output")
            
        print("βœ… Gradio components created successfully")
        
        # Test that interface can be created (without launching)
        # We need to import from the parent directory (app.py instead of gradio_app.py)
        parent_dir = os.path.dirname(os.path.dirname(__file__))
        sys.path.insert(0, parent_dir)
        import app
        # Test that the app module exists and has the necessary functions
        assert hasattr(app, 'create_medical_ui'), "app.py should have create_medical_ui function"
        interface = app.create_medical_ui()
        print("βœ… Medical UI interface created successfully")
        
    except Exception as e:
        print(f"❌ Gradio components test failed: {e}")
        assert False, f"Gradio components test failed: {e}"

def test_processing_pipeline():
    """Test the complete processing pipeline"""
    print("\nβš™οΈ Testing Complete Processing Pipeline...")
    
    try:
        # Import the processing function from parent directory (app.py instead of gradio_app.py)
        parent_dir = os.path.dirname(os.path.dirname(__file__))
        sys.path.insert(0, parent_dir)
        import app
        
        # Verify app has the necessary functions
        assert hasattr(app, 'create_medical_ui'), "app.py should have create_medical_ui function"
        
        # Create sample medical text
        sample_medical_text = """
        MEDICAL RECORD
        Patient: Jane Smith
        DOB: 1980-05-15
        
        Chief Complaint: Shortness of breath
        
        Assessment:
        - Asthma exacerbation
        - Hypertension
        
        Medications:
        - Albuterol inhaler PRN
        - Lisinopril 5mg daily
        - Prednisone 20mg daily x 5 days
        
        Plan: Follow up in 1 week
        """
        
        print("βœ… Sample medical text prepared")
        print(f"   Text length: {len(sample_medical_text)} characters")
        print("βœ… Processing pipeline test completed")
        
        assert len(sample_medical_text) > 0, "Sample text should not be empty"
        
    except Exception as e:
        print(f"❌ Processing pipeline test failed: {e}")
        assert False, f"Processing pipeline test failed: {e}"

def display_configuration():
    """Display current configuration"""
    print("\nπŸ”§ Current Configuration:")
    print(f"   USE_REAL_OLLAMA: {os.getenv('USE_REAL_OLLAMA', 'false')}")
    print(f"   USE_MISTRAL_FALLBACK: {os.getenv('USE_MISTRAL_FALLBACK', 'false')}")
    print(f"   LANGFUSE_SECRET_KEY: {'βœ… Set' if os.getenv('LANGFUSE_SECRET_KEY') else '❌ Missing'}")
    print(f"   MISTRAL_API_KEY: {'βœ… Set' if os.getenv('MISTRAL_API_KEY') else '❌ Missing'}")

def main():
    """Run all tests"""
    print("πŸ”₯ FhirFlame Gradio Interface Test Suite")
    print("=" * 50)
    print(f"πŸ• Starting at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    
    # Display configuration
    display_configuration()
    
    # Run tests
    tests = [
        ("Import Dependencies", test_gradio_imports),
        ("Basic Functionality", test_basic_functionality), 
        ("Gradio Components", test_gradio_components),
        ("Processing Pipeline", test_processing_pipeline)
    ]
    
    passed = 0
    total = len(tests)
    
    for test_name, test_func in tests:
        print(f"\nπŸ“‹ Running: {test_name}")
        if test_func():
            passed += 1
        
    # Summary
    print("\n" + "=" * 50)
    print(f"🎯 Test Results: {passed}/{total} tests passed")
    
    if passed == total:
        print("πŸŽ‰ All tests passed! Gradio interface is ready to launch.")
        print("\nπŸš€ To start the interface, run:")
        print("   python gradio_app.py")
        print("   or")
        print("   docker run --rm -v .:/app -w /app -p 7860:7860 fhirflame-complete python gradio_app.py")
        return 0
    else:
        print(f"⚠️  {total - passed} tests failed. Please check the errors above.")
        return 1

if __name__ == "__main__":
    exit_code = main()
    sys.exit(exit_code)