#!/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)