|
|
|
""" |
|
Test Main App Mistral Integration |
|
Test the actual workflow to see enhanced logging |
|
""" |
|
|
|
import asyncio |
|
import os |
|
import sys |
|
import base64 |
|
from PIL import Image, ImageDraw |
|
import io |
|
|
|
|
|
sys.path.insert(0, '/app') |
|
|
|
from src.workflow_orchestrator import WorkflowOrchestrator |
|
|
|
async def test_main_app_mistral(): |
|
"""Test the main app with a sample document to see Mistral API logs""" |
|
|
|
print("π§ͺ Testing Main App Mistral Integration") |
|
print("=" * 50) |
|
|
|
|
|
print("π Creating test medical document...") |
|
test_image = Image.new('RGB', (400, 300), color='white') |
|
draw = ImageDraw.Draw(test_image) |
|
|
|
|
|
draw.text((10, 10), "MEDICAL REPORT", fill='black') |
|
draw.text((10, 40), "Patient: Jane Smith", fill='black') |
|
draw.text((10, 70), "DOB: 02/15/1985", fill='black') |
|
draw.text((10, 100), "Diagnosis: Hypertension", fill='black') |
|
draw.text((10, 130), "Medication: Lisinopril 10mg", fill='black') |
|
draw.text((10, 160), "Blood Pressure: 140/90 mmHg", fill='black') |
|
draw.text((10, 190), "Provider: Dr. Johnson", fill='black') |
|
|
|
|
|
img_byte_arr = io.BytesIO() |
|
test_image.save(img_byte_arr, format='JPEG', quality=95) |
|
document_bytes = img_byte_arr.getvalue() |
|
|
|
print(f"π Document size: {len(document_bytes)} bytes") |
|
|
|
|
|
print("\nπ§ Initializing WorkflowOrchestrator...") |
|
orchestrator = WorkflowOrchestrator() |
|
|
|
|
|
print("\nπ Testing workflow with enhanced logging...") |
|
try: |
|
result = await orchestrator.process_complete_workflow( |
|
document_bytes=document_bytes, |
|
user_id="test_user", |
|
filename="test_medical_report.jpg", |
|
use_mistral_ocr=True |
|
) |
|
|
|
print("\nβ
Workflow completed successfully!") |
|
|
|
text_extraction = result.get('text_extraction', {}) |
|
medical_analysis = result.get('medical_analysis', {}) |
|
workflow_metadata = result.get('workflow_metadata', {}) |
|
|
|
print(f"π Extracted text length: {text_extraction.get('full_text_length', 0)}") |
|
print(f"π₯ Medical entities found: {medical_analysis.get('entities_found', 0)}") |
|
print(f"π FHIR bundle created: {'fhir_bundle' in result}") |
|
|
|
|
|
extracted_data_str = medical_analysis.get('extracted_data', '{}') |
|
try: |
|
import json |
|
entities = json.loads(extracted_data_str) |
|
except: |
|
entities = {} |
|
|
|
print(f"\nπ Medical Entities:") |
|
print(f" Patient: {entities.get('patient_name', 'N/A')}") |
|
print(f" DOB: {entities.get('date_of_birth', 'N/A')}") |
|
print(f" Provider: {entities.get('provider_name', 'N/A')}") |
|
print(f" Conditions: {entities.get('conditions', [])}") |
|
print(f" Medications: {entities.get('medications', [])}") |
|
|
|
|
|
print(f"\nπ OCR method used: {workflow_metadata.get('ocr_method', 'Unknown')}") |
|
|
|
|
|
extracted_text = text_extraction.get('extracted_text', '') |
|
if extracted_text: |
|
print(f"\nπ Extracted text preview: {extracted_text[:200]}...") |
|
|
|
except Exception as e: |
|
print(f"\nβ Workflow failed: {e}") |
|
import traceback |
|
print(f"π Traceback: {traceback.format_exc()}") |
|
|
|
if __name__ == "__main__": |
|
print("π Enhanced logging should show:") |
|
print(" - mistral_attempt_start") |
|
print(" - mistral_success_in_fallback OR mistral_fallback_failed") |
|
print(" - Detailed error traces if Mistral fails") |
|
print() |
|
|
|
asyncio.run(test_main_app_mistral()) |