Spaces:
Running
Running
File size: 4,782 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #!/usr/bin/env python3
"""
Test script: RAG-enhanced chat with HF dataset + FAISS index
Run this to see the improvement in chat quality
"""
import sys
import json
from pathlib import Path
# Add backend to path
sys.path.insert(0, str(Path(__file__).parent))
from app.ml.enhanced_chat import get_enhanced_mock_response, rag_retriever
def create_sample_guc():
"""Create sample Global User Context with medical data."""
return {
"name": "Amit",
"age": "28",
"gender": "Male",
"language": "EN",
"location": "Mumbai, India",
"latestReport": {
"overall_summary_english": "Lab report shows signs of anemia with low iron and B12",
"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"
}
],
"affected_organs": ["Blood", "Bone Marrow"],
"severity_level": "NORMAL",
"dietary_flags": ["Low Iron Intake", "Insufficient B12"],
"exercise_flags": ["Fatigue Limiting Activity"]
},
"medicationsActive": [],
"allergyFlags": [],
"mentalWellness": {"stressLevel": 6, "sleepQuality": 5}
}
def print_separator(title=""):
"""Print formatted separator."""
print("\n" + "=" * 80)
if title:
print(f" {title}")
print("=" * 80)
def test_chat():
"""Test enhanced chat with various queries."""
print_separator("π₯ Enhanced Chat System - RAG Integration Test")
guc = create_sample_guc()
# Check RAG status
if rag_retriever:
status = "β
LOADED" if rag_retriever.loaded else "β οΈ FAILED TO LOAD"
doc_count = len(rag_retriever.documents) if rag_retriever.loaded else 0
print(f"\nπ RAG Status: {status}")
print(f" Documents available: {doc_count}")
else:
print("\nβ οΈ RAG not initialized - using mock responses only")
# Test conversations
test_queries = [
{
"question": "I'm feeling very tired and weak. What should I do?",
"category": "Fatigue & Symptoms"
},
{
"question": "What foods should I eat to improve my condition?",
"category": "Nutrition"
},
{
"question": "What medicines do I need to take?",
"category": "Medications"
},
{
"question": "Can I exercise? I feel exhausted.",
"category": "Physical Activity"
},
{
"question": "When should I follow up with my doctor?",
"category": "Follow-up Care"
},
{
"question": "I'm worried this is serious. Should I panic?",
"category": "Reassurance"
}
]
print_separator("Chat Interaction Tests")
for i, test in enumerate(test_queries, 1):
print_separator(f"Test {i}: {test['category']}")
print(f"\nπ€ Patient: {test['question']}")
print("\nπ₯ Dr. Raahat:")
response = get_enhanced_mock_response(test["question"], guc)
print(response)
if rag_retriever and rag_retriever.loaded:
print("\nπ [Sources: Retrieved from medical database]")
else:
print("\nπ [Note: Using contextual mock responses. RAG sources not available.]")
print_separator("Test Complete")
print("""
β
Enhanced Chat Features:
β Context-aware responses based on actual findings
β Personalized health advice
β Step-by-step action plans
β RAG integration ready for HF dataset
β Clear formatting and readability
β Empathetic doctor persona
π Next Steps:
1. Deploy FAISS index from HF
2. Load medical documents
3. Enable actual document retrieval
4. Remove mock responses from production
5. Add response grounding/sources
""")
if __name__ == "__main__":
test_chat()
|