File size: 4,318 Bytes
542c765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Complete end-to-end test: Schema β†’ Text β†’ Chat Response
Shows the full document scanning conversion pipeline
"""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent))

from app.routers.doctor_upload import convert_analysis_to_text
from app.ml.openrouter import chat

# Sample schema from PDF analysis
sample_analysis = {
    "findings": [
        {"parameter": "Haemoglobin", "value": 9.2, "unit": "g/dL", "status": "LOW", "reference": "13.0 - 17.0"},
        {"parameter": "Iron", "value": 45, "unit": "Β΅g/dL", "status": "LOW", "reference": "60 - 170"},
        {"parameter": "Vitamin B12", "value": 180, "unit": "pg/mL", "status": "LOW", "reference": "200 - 900"},
        {"parameter": "RBC", "value": 3.8, "unit": "10^6/Β΅L", "status": "LOW", "reference": "4.5 - 5.5"},
        {"parameter": "WBC", "value": 7.2, "unit": "10^3/Β΅L", "status": "NORMAL", "reference": "4.5 - 11.0"}
    ],
    "severity_level": "NORMAL",
    "affected_organs": ["Blood", "Bone Marrow"],
    "dietary_flags": ["Low Iron Intake", "Insufficient B12"],
    "exercise_flags": ["Fatigue Limiting Activity"]
}

def print_section(title):
    print("\n" + "=" * 90)
    print(f"  {title}")
    print("=" * 90)

def test_full_pipeline():
    """Test the complete schema β†’ text β†’ chat response pipeline"""
    
    print_section("1️⃣  INPUT: Schema-Based PDF Analysis")
    print("\nFindings from PDF extraction:")
    for finding in sample_analysis["findings"]:
        status_color = "❌" if finding['status'] != "NORMAL" else "βœ“"
        print(f"  {status_color} {finding['parameter']}: {finding['value']} {finding['unit']} ({finding['status']})")
    
    print(f"\n  Severity: {sample_analysis['severity_level']}")
    print(f"  Affected: {', '.join(sample_analysis['affected_organs'])}")
    print(f"  Dietary: {', '.join(sample_analysis['dietary_flags'])}")
    print(f"  Exercise: {', '.join(sample_analysis['exercise_flags'])}")
    
    print_section("2️⃣  CONVERSION: Schema β†’ Natural Language Text")
    analysis_text = convert_analysis_to_text(sample_analysis)
    print(analysis_text)
    
    print_section("3️⃣  PROCESSING: Text β†’ Chat System")
    print("\nSending text through chat system with patient context...")
    
    # Build patient context
    patient_context = {
        "name": "Amit Kumar",
        "age": "28",
        "gender": "Male",
        "language": "EN",
        "latestReport": sample_analysis,
        "mentalWellness": {"stressLevel": 6, "sleepQuality": 5}
    }
    
    # Send through chat system
    print("Processing...\n")
    doctor_response = chat(
        message=analysis_text,
        history=[],
        guc=patient_context
    )
    
    print_section("4️⃣  OUTPUT: Doctor Response (Natural Language)")
    print(f"\nDr. Raahat:\n{doctor_response}")
    
    print_section("βœ… PIPELINE COMPLETE")
    print("""
Flow Summary:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  1. PDF Upload                                          β”‚
β”‚     ↓                                                   β”‚
β”‚  2. PDF Analysis (Schema-Based)                         β”‚
β”‚     ↓                                                   β”‚
β”‚  3. Schema β†’ Natural Language Text Conversion           β”‚
β”‚     ↓                                                   β”‚
β”‚  4. Send Text to Chat System                            β”‚
β”‚     ↓                                                   β”‚
β”‚  5. Chat System Processes & Returns Response            β”‚
β”‚     ↓                                                   β”‚
β”‚  6. Doctor's Human-Friendly Response                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Innovation:
- Schema analysis receives proper natural language processing
- Doctor responses are contextual to actual findings
- No raw JSON schema returned - only human dialogue
- Seamless user experience in `/upload_and_chat` endpoint
    """)

if __name__ == "__main__":
    test_full_pipeline()