File size: 11,632 Bytes
10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 10eddac cad89d4 | 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | """
RAG Query Engine for Lab Report Decoder
Uses Hugging Face models - OPTIMIZED for speed
"""
from sentence_transformers import SentenceTransformer
from transformers import pipeline
import chromadb
from typing import List, Dict
from pdf_extractor import LabResult
import os
class LabReportRAG:
"""RAG system for explaining lab results - Fast and efficient"""
def __init__(self, db_path: str = "./chroma_db"):
"""Initialize the RAG system with fast models"""
print("π Loading models (optimized for speed)...")
# Fast embedding model
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
print("β
Embeddings loaded")
# Use FAST text generation model
print("π Loading text generation model...")
try:
# Use Flan-T5 - much faster than Phi-3
self.text_generator = pipeline(
"text2text-generation",
model="google/flan-t5-small", # Even smaller/faster
max_length=256,
device=-1 # Force CPU (HF Spaces default)
)
print("β
Text generation model loaded (Flan-T5-small)")
except Exception as e:
print(f"β οΈ Model loading error: {e}")
self.text_generator = None
# Load vector store
try:
self.client = chromadb.PersistentClient(path=db_path)
self.collection = self.client.get_collection("lab_reports")
print("β
Vector database loaded")
except Exception as e:
print(f"β οΈ Vector database not found: {e}")
self.collection = None
def _retrieve_context(self, query: str, k: int = 2) -> str:
"""Retrieve relevant context from vector database"""
if self.collection is None:
return "Limited medical information available."
try:
# Create query embedding
query_embedding = self.embedding_model.encode(query).tolist()
# Query the collection
results = self.collection.query(
query_embeddings=[query_embedding],
n_results=k
)
# Combine documents
if results and results['documents'] and len(results['documents'][0]) > 0:
context = "\n".join(results['documents'][0])
# Limit context length for speed
return context[:1000]
else:
return "No specific information found."
except Exception as e:
print(f"Retrieval error: {e}")
return "Error retrieving information."
def _generate_text(self, prompt: str) -> str:
"""Generate text - with fallback to template-based"""
if self.text_generator is None:
return "AI model not available. Using basic explanation."
try:
# Generate with timeout protection
result = self.text_generator(
prompt,
max_length=256,
do_sample=True,
temperature=0.7,
num_return_sequences=1
)
return result[0]['generated_text'].strip()
except Exception as e:
print(f"Generation error: {e}")
return "Unable to generate detailed explanation."
def explain_result(self, result: LabResult) -> str:
"""Generate explanation for a single lab result"""
print(f" Explaining: {result.test_name} ({result.status})...")
# Quick template-based explanation for speed
if result.status == 'normal':
return self._explain_normal(result)
elif result.status == 'high':
return self._explain_high(result)
elif result.status == 'low':
return self._explain_low(result)
else:
return self._explain_unknown(result)
def _explain_normal(self, result: LabResult) -> str:
"""Fast template for normal results"""
context = self._retrieve_context(f"{result.test_name} normal meaning", k=1)
explanation = f"""β
Your {result.test_name} level of {result.value} {result.unit} is within the normal range ({result.reference_range}).
This indicates healthy levels. """
if context and len(context) > 20:
# Add context if available
explanation += f"\n\n{context[:300]}"
return explanation
def _explain_high(self, result: LabResult) -> str:
"""Fast template for high results"""
context = self._retrieve_context(f"{result.test_name} high causes treatment", k=2)
explanation = f"""β οΈ Your {result.test_name} level of {result.value} {result.unit} is ABOVE the normal range ({result.reference_range}).
"""
if context and len(context) > 20:
explanation += f"{context[:400]}\n\n"
explanation += "π‘ Recommendation: Discuss these results with your healthcare provider for personalized advice."
return explanation
def _explain_low(self, result: LabResult) -> str:
"""Fast template for low results"""
context = self._retrieve_context(f"{result.test_name} low causes treatment", k=2)
explanation = f"""β οΈ Your {result.test_name} level of {result.value} {result.unit} is BELOW the normal range ({result.reference_range}).
"""
if context and len(context) > 20:
explanation += f"{context[:400]}\n\n"
explanation += "π‘ Recommendation: Consult with your healthcare provider about these results."
return explanation
def _explain_unknown(self, result: LabResult) -> str:
"""Template for unknown status"""
return f"""Your {result.test_name} result is {result.value} {result.unit}.
Reference range: {result.reference_range}
We couldn't automatically determine if this is within normal range. Please consult your healthcare provider to interpret this result."""
def explain_all_results(self, results: List[LabResult]) -> Dict[str, str]:
"""Generate explanations for all lab results - FAST"""
explanations = {}
print(f"π§ Generating explanations for {len(results)} results...")
for i, result in enumerate(results, 1):
print(f" [{i}/{len(results)}] {result.test_name}...")
try:
explanation = self.explain_result(result)
explanations[result.test_name] = explanation
except Exception as e:
print(f" Error: {e}")
explanations[result.test_name] = f"Unable to generate explanation for {result.test_name}."
print("β
All explanations generated")
return explanations
def answer_followup_question(self, question: str, lab_results: List[LabResult]) -> str:
"""Answer follow-up questions - FAST"""
print(f"π¬ Processing question: {question[:50]}...")
# Create context from lab results
results_summary = []
for r in lab_results[:10]: # Limit to first 10 for speed
results_summary.append(
f"{r.test_name}: {r.value} {r.unit} ({r.status})"
)
results_context = "\n".join(results_summary)
# Get relevant medical info
medical_context = self._retrieve_context(question, k=2)
# Simple template-based response for speed
if "food" in question.lower() or "eat" in question.lower() or "diet" in question.lower():
answer = f"""Based on your lab results:\n\n{results_context}\n\n"""
if medical_context and len(medical_context) > 20:
answer += f"{medical_context[:500]}"
else:
answer += "For dietary recommendations specific to your results, please consult with a healthcare provider or nutritionist."
elif "why" in question.lower() or "cause" in question.lower():
answer = f"""Regarding your question about your results:\n\n"""
if medical_context and len(medical_context) > 20:
answer += f"{medical_context[:500]}"
else:
answer += "There can be various causes for abnormal lab results. Your healthcare provider can help identify the specific cause in your case."
else:
# General question
if medical_context and len(medical_context) > 20:
answer = medical_context[:500]
else:
answer = f"""Based on your results:\n{results_context}\n\nFor specific medical advice about your results, please consult with your healthcare provider."""
print("β
Answer generated")
return answer
def generate_summary(self, results: List[LabResult]) -> str:
"""Generate overall summary - FAST"""
print("π Generating summary...")
abnormal = [r for r in results if r.status in ['high', 'low']]
normal = [r for r in results if r.status == 'normal']
if not abnormal:
return """β
Excellent news! All your lab results are within normal ranges.
This suggests that the tested parameters are functioning well. Continue maintaining your current health habits, and follow your healthcare provider's recommendations for routine monitoring."""
# Build summary
summary = f"""π Lab Results Summary
Total Tests: {len(results)}
β
Normal: {len(normal)}
β οΈ Abnormal: {len(abnormal)}
"""
if abnormal:
summary += "**Tests Outside Normal Range:**\n"
for r in abnormal[:5]: # Limit to first 5
status_emoji = "β" if r.status == "high" else "β"
summary += f"{status_emoji} {r.test_name}: {r.value} {r.unit} ({r.status})\n"
if len(abnormal) > 5:
summary += f"... and {len(abnormal) - 5} more\n"
summary += "\n"
# Get context for abnormal results
if abnormal:
abnormal_names = ", ".join([r.test_name for r in abnormal[:3]])
context = self._retrieve_context(f"{abnormal_names} interpretation", k=2)
if context and len(context) > 20:
summary += f"**Key Information:**\n{context[:400]}\n\n"
summary += """**Next Steps:**
1. Review these results with your healthcare provider
2. Discuss any concerns or symptoms you're experiencing
3. Follow recommended treatment or monitoring plans
Remember: These results are for educational purposes. Always consult your doctor for medical advice."""
print("β
Summary generated")
return summary
# Test if ran directly
if __name__ == "__main__":
print("Testing RAG system...")
try:
rag = LabReportRAG()
print("\nβ
RAG system initialized successfully!")
# Test with example
from pdf_extractor import LabResult
test_result = LabResult(
test_name="Hemoglobin",
value="10.5",
unit="g/dL",
reference_range="12.0-15.5",
status="low"
)
explanation = rag.explain_result(test_result)
print(f"\nTest Explanation:\n{explanation}")
except Exception as e:
print(f"\nβ Error: {e}") |