fhirflame / tests /test_gradio_interface.py
leksval
initial commit
a963d65
#!/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)