|
|
|
""" |
|
π§ͺ Test Gradio Interface |
|
Quick test of the Gradio medical document processing interface |
|
""" |
|
|
|
import asyncio |
|
import sys |
|
import os |
|
from datetime import datetime |
|
|
|
|
|
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 |
|
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 |
|
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 |
|
|
|
|
|
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" |
|
|
|
|
|
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 |
|
|
|
|
|
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") |
|
|
|
|
|
|
|
parent_dir = os.path.dirname(os.path.dirname(__file__)) |
|
sys.path.insert(0, parent_dir) |
|
import app |
|
|
|
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: |
|
|
|
parent_dir = os.path.dirname(os.path.dirname(__file__)) |
|
sys.path.insert(0, parent_dir) |
|
import app |
|
|
|
|
|
assert hasattr(app, 'create_medical_ui'), "app.py should have create_medical_ui function" |
|
|
|
|
|
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() |
|
|
|
|
|
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 |
|
|
|
|
|
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) |