Spaces:
Running
Running
| #!/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() | |