Spaces:
Sleeping
Sleeping
Delete medical_chatbot.py
Browse files- medical_chatbot.py +0 -499
medical_chatbot.py
DELETED
|
@@ -1,499 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import re
|
| 3 |
-
import torch
|
| 4 |
-
import warnings
|
| 5 |
-
import numpy as np
|
| 6 |
-
import faiss
|
| 7 |
-
from transformers import (
|
| 8 |
-
AutoTokenizer,
|
| 9 |
-
AutoModelForCausalLM,
|
| 10 |
-
BitsAndBytesConfig
|
| 11 |
-
)
|
| 12 |
-
from sentence_transformers import SentenceTransformer
|
| 13 |
-
from typing import List, Dict, Optional
|
| 14 |
-
import time
|
| 15 |
-
from datetime import datetime
|
| 16 |
-
|
| 17 |
-
# Suppress warnings for cleaner output
|
| 18 |
-
warnings.filterwarnings('ignore')
|
| 19 |
-
|
| 20 |
-
class ColabBioGPTChatbot:
|
| 21 |
-
def __init__(self, use_gpu=True, use_8bit=True):
|
| 22 |
-
"""Initialize BioGPT chatbot optimized for Hugging Face Spaces"""
|
| 23 |
-
print("🏥 Initializing Medical Chatbot...")
|
| 24 |
-
self.use_gpu = use_gpu
|
| 25 |
-
self.use_8bit = use_8bit
|
| 26 |
-
self.device = "cuda" if torch.cuda.is_available() and use_gpu else "cpu"
|
| 27 |
-
print(f"🖥️ Using device: {self.device}")
|
| 28 |
-
|
| 29 |
-
self.tokenizer = None
|
| 30 |
-
self.model = None
|
| 31 |
-
self.knowledge_chunks = []
|
| 32 |
-
self.conversation_history = []
|
| 33 |
-
self.embedding_model = None
|
| 34 |
-
self.faiss_index = None
|
| 35 |
-
self.faiss_ready = False
|
| 36 |
-
self.use_embeddings = True
|
| 37 |
-
|
| 38 |
-
# Initialize components
|
| 39 |
-
self.setup_biogpt()
|
| 40 |
-
self.load_sentence_transformer()
|
| 41 |
-
|
| 42 |
-
def setup_biogpt(self):
|
| 43 |
-
"""Setup BioGPT model with fallback to base BioGPT if Large fails"""
|
| 44 |
-
print("🧠 Loading BioGPT model...")
|
| 45 |
-
|
| 46 |
-
try:
|
| 47 |
-
# Try BioGPT-Large first
|
| 48 |
-
model_name = "microsoft/BioGPT-Large"
|
| 49 |
-
print(f"Attempting to load {model_name}...")
|
| 50 |
-
|
| 51 |
-
if self.use_8bit and self.device == "cuda":
|
| 52 |
-
quantization_config = BitsAndBytesConfig(
|
| 53 |
-
load_in_8bit=True,
|
| 54 |
-
llm_int8_threshold=6.0,
|
| 55 |
-
llm_int8_has_fp16_weight=False,
|
| 56 |
-
)
|
| 57 |
-
else:
|
| 58 |
-
quantization_config = None
|
| 59 |
-
|
| 60 |
-
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 61 |
-
if self.tokenizer.pad_token is None:
|
| 62 |
-
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 63 |
-
|
| 64 |
-
self.model = AutoModelForCausalLM.from_pretrained(
|
| 65 |
-
model_name,
|
| 66 |
-
quantization_config=quantization_config,
|
| 67 |
-
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
| 68 |
-
device_map="auto" if self.device == "cuda" else None,
|
| 69 |
-
trust_remote_code=True,
|
| 70 |
-
low_cpu_mem_usage=True
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
if self.device == "cuda" and quantization_config is None:
|
| 74 |
-
self.model = self.model.to(self.device)
|
| 75 |
-
|
| 76 |
-
print("✅ BioGPT-Large loaded successfully!")
|
| 77 |
-
|
| 78 |
-
except Exception as e:
|
| 79 |
-
print(f"❌ BioGPT-Large loading failed: {e}")
|
| 80 |
-
print("🔁 Falling back to base BioGPT...")
|
| 81 |
-
self.setup_fallback_biogpt()
|
| 82 |
-
|
| 83 |
-
def setup_fallback_biogpt(self):
|
| 84 |
-
"""Fallback to microsoft/BioGPT if BioGPT-Large fails"""
|
| 85 |
-
try:
|
| 86 |
-
model_name = "microsoft/BioGPT"
|
| 87 |
-
print(f"Loading fallback model: {model_name}")
|
| 88 |
-
|
| 89 |
-
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 90 |
-
if self.tokenizer.pad_token is None:
|
| 91 |
-
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 92 |
-
|
| 93 |
-
self.model = AutoModelForCausalLM.from_pretrained(
|
| 94 |
-
model_name,
|
| 95 |
-
torch_dtype=torch.float32,
|
| 96 |
-
trust_remote_code=True,
|
| 97 |
-
low_cpu_mem_usage=True
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
if self.device == "cuda":
|
| 101 |
-
self.model = self.model.to(self.device)
|
| 102 |
-
|
| 103 |
-
print("✅ Base BioGPT model loaded successfully!")
|
| 104 |
-
|
| 105 |
-
except Exception as e:
|
| 106 |
-
print(f"❌ Failed to load fallback BioGPT: {e}")
|
| 107 |
-
self.model = None
|
| 108 |
-
self.tokenizer = None
|
| 109 |
-
|
| 110 |
-
def load_sentence_transformer(self):
|
| 111 |
-
"""Load sentence transformer for embeddings"""
|
| 112 |
-
try:
|
| 113 |
-
print("🔮 Loading sentence transformer...")
|
| 114 |
-
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 115 |
-
|
| 116 |
-
# Initialize FAISS index (will be populated when data is loaded)
|
| 117 |
-
embedding_dim = 384 # Dimension for all-MiniLM-L6-v2
|
| 118 |
-
self.faiss_index = faiss.IndexFlatL2(embedding_dim)
|
| 119 |
-
self.faiss_ready = True
|
| 120 |
-
print("✅ Sentence transformer and FAISS index ready!")
|
| 121 |
-
|
| 122 |
-
except Exception as e:
|
| 123 |
-
print(f"❌ Failed to load sentence transformer: {e}")
|
| 124 |
-
self.use_embeddings = False
|
| 125 |
-
self.faiss_ready = False
|
| 126 |
-
|
| 127 |
-
def load_medical_data(self, file_path):
|
| 128 |
-
"""Load and process medical data"""
|
| 129 |
-
print(f"📖 Loading medical data from {file_path}...")
|
| 130 |
-
|
| 131 |
-
try:
|
| 132 |
-
if not os.path.exists(file_path):
|
| 133 |
-
raise FileNotFoundError(f"File {file_path} not found")
|
| 134 |
-
|
| 135 |
-
with open(file_path, 'r', encoding='utf-8') as f:
|
| 136 |
-
text = f.read()
|
| 137 |
-
print(f"📄 File loaded: {len(text):,} characters")
|
| 138 |
-
|
| 139 |
-
except Exception as e:
|
| 140 |
-
print(f"❌ Error loading file: {e}")
|
| 141 |
-
raise ValueError(f"Failed to load medical data: {e}")
|
| 142 |
-
|
| 143 |
-
# Create chunks
|
| 144 |
-
print("📝 Creating medical chunks...")
|
| 145 |
-
chunks = self.create_medical_chunks(text)
|
| 146 |
-
print(f"📋 Created {len(chunks)} medical chunks")
|
| 147 |
-
|
| 148 |
-
self.knowledge_chunks = chunks
|
| 149 |
-
|
| 150 |
-
# Generate embeddings if available
|
| 151 |
-
if self.use_embeddings and self.embedding_model and self.faiss_ready:
|
| 152 |
-
try:
|
| 153 |
-
self.generate_embeddings_with_progress(chunks)
|
| 154 |
-
print("✅ Medical data loaded with embeddings!")
|
| 155 |
-
except Exception as e:
|
| 156 |
-
print(f"⚠️ Embedding generation failed: {e}")
|
| 157 |
-
print("✅ Medical data loaded (keyword search mode)")
|
| 158 |
-
else:
|
| 159 |
-
print("✅ Medical data loaded (keyword search mode)")
|
| 160 |
-
|
| 161 |
-
def create_medical_chunks(self, text: str, chunk_size: int = 400) -> List[Dict]:
|
| 162 |
-
"""Create medically-optimized text chunks"""
|
| 163 |
-
chunks = []
|
| 164 |
-
|
| 165 |
-
# Split by paragraphs first
|
| 166 |
-
paragraphs = [p.strip() for p in text.split('\n\n') if len(p.strip()) > 50]
|
| 167 |
-
|
| 168 |
-
chunk_id = 0
|
| 169 |
-
for paragraph in paragraphs:
|
| 170 |
-
if len(paragraph.split()) <= chunk_size:
|
| 171 |
-
chunks.append({
|
| 172 |
-
'id': chunk_id,
|
| 173 |
-
'text': paragraph,
|
| 174 |
-
'medical_focus': self.identify_medical_focus(paragraph)
|
| 175 |
-
})
|
| 176 |
-
chunk_id += 1
|
| 177 |
-
else:
|
| 178 |
-
# Split large paragraphs by sentences
|
| 179 |
-
sentences = re.split(r'[.!?]+', paragraph)
|
| 180 |
-
current_chunk = ""
|
| 181 |
-
|
| 182 |
-
for sentence in sentences:
|
| 183 |
-
sentence = sentence.strip()
|
| 184 |
-
if not sentence:
|
| 185 |
-
continue
|
| 186 |
-
|
| 187 |
-
if len(current_chunk.split()) + len(sentence.split()) <= chunk_size:
|
| 188 |
-
current_chunk += sentence + ". "
|
| 189 |
-
else:
|
| 190 |
-
if current_chunk.strip():
|
| 191 |
-
chunks.append({
|
| 192 |
-
'id': chunk_id,
|
| 193 |
-
'text': current_chunk.strip(),
|
| 194 |
-
'medical_focus': self.identify_medical_focus(current_chunk)
|
| 195 |
-
})
|
| 196 |
-
chunk_id += 1
|
| 197 |
-
current_chunk = sentence + ". "
|
| 198 |
-
|
| 199 |
-
if current_chunk.strip():
|
| 200 |
-
chunks.append({
|
| 201 |
-
'id': chunk_id,
|
| 202 |
-
'text': current_chunk.strip(),
|
| 203 |
-
'medical_focus': self.identify_medical_focus(current_chunk)
|
| 204 |
-
})
|
| 205 |
-
chunk_id += 1
|
| 206 |
-
|
| 207 |
-
return chunks
|
| 208 |
-
|
| 209 |
-
def identify_medical_focus(self, text: str) -> str:
|
| 210 |
-
"""Identify the medical focus of a text chunk"""
|
| 211 |
-
text_lower = text.lower()
|
| 212 |
-
|
| 213 |
-
categories = {
|
| 214 |
-
'pediatric_symptoms': ['fever', 'cough', 'rash', 'vomiting', 'diarrhea'],
|
| 215 |
-
'treatments': ['treatment', 'therapy', 'medication', 'antibiotics'],
|
| 216 |
-
'diagnosis': ['diagnosis', 'diagnostic', 'symptoms', 'signs'],
|
| 217 |
-
'emergency': ['emergency', 'urgent', 'serious', 'hospital'],
|
| 218 |
-
'prevention': ['prevention', 'vaccine', 'immunization', 'avoid']
|
| 219 |
-
}
|
| 220 |
-
|
| 221 |
-
for category, keywords in categories.items():
|
| 222 |
-
if any(keyword in text_lower for keyword in keywords):
|
| 223 |
-
return category
|
| 224 |
-
|
| 225 |
-
return 'general_medical'
|
| 226 |
-
|
| 227 |
-
def generate_embeddings_with_progress(self, chunks: List[Dict]):
|
| 228 |
-
"""Generate embeddings and add to FAISS index"""
|
| 229 |
-
print("🔮 Generating embeddings...")
|
| 230 |
-
|
| 231 |
-
try:
|
| 232 |
-
texts = [chunk['text'] for chunk in chunks]
|
| 233 |
-
|
| 234 |
-
# Generate embeddings in batches
|
| 235 |
-
batch_size = 32
|
| 236 |
-
all_embeddings = []
|
| 237 |
-
|
| 238 |
-
for i in range(0, len(texts), batch_size):
|
| 239 |
-
batch_texts = texts[i:i+batch_size]
|
| 240 |
-
batch_embeddings = self.embedding_model.encode(batch_texts, show_progress_bar=False)
|
| 241 |
-
all_embeddings.extend(batch_embeddings)
|
| 242 |
-
|
| 243 |
-
progress = min(i + batch_size, len(texts))
|
| 244 |
-
print(f" Progress: {progress}/{len(texts)} chunks processed", end='\r')
|
| 245 |
-
|
| 246 |
-
print(f"\n ✅ Generated embeddings for {len(texts)} chunks")
|
| 247 |
-
|
| 248 |
-
# Add to FAISS index
|
| 249 |
-
embeddings_array = np.array(all_embeddings).astype('float32')
|
| 250 |
-
self.faiss_index.add(embeddings_array)
|
| 251 |
-
print("✅ Embeddings added to FAISS index!")
|
| 252 |
-
|
| 253 |
-
except Exception as e:
|
| 254 |
-
print(f"❌ Embedding generation failed: {e}")
|
| 255 |
-
raise
|
| 256 |
-
|
| 257 |
-
def retrieve_medical_context(self, query: str, n_results: int = 3) -> List[str]:
|
| 258 |
-
"""Retrieve relevant medical context"""
|
| 259 |
-
if self.use_embeddings and self.embedding_model and self.faiss_ready and self.faiss_index.ntotal > 0:
|
| 260 |
-
try:
|
| 261 |
-
# Generate query embedding
|
| 262 |
-
query_embedding = self.embedding_model.encode([query])
|
| 263 |
-
|
| 264 |
-
# Search FAISS index
|
| 265 |
-
distances, indices = self.faiss_index.search(
|
| 266 |
-
np.array(query_embedding).astype('float32'),
|
| 267 |
-
min(n_results, self.faiss_index.ntotal)
|
| 268 |
-
)
|
| 269 |
-
|
| 270 |
-
# Get relevant chunks
|
| 271 |
-
context_chunks = []
|
| 272 |
-
for idx in indices[0]:
|
| 273 |
-
if idx != -1 and idx < len(self.knowledge_chunks):
|
| 274 |
-
context_chunks.append(self.knowledge_chunks[idx]['text'])
|
| 275 |
-
|
| 276 |
-
if context_chunks:
|
| 277 |
-
return context_chunks
|
| 278 |
-
|
| 279 |
-
except Exception as e:
|
| 280 |
-
print(f"⚠️ Embedding search failed: {e}")
|
| 281 |
-
|
| 282 |
-
# Fallback to keyword search
|
| 283 |
-
return self.keyword_search_medical(query, n_results)
|
| 284 |
-
|
| 285 |
-
def keyword_search_medical(self, query: str, n_results: int) -> List[str]:
|
| 286 |
-
"""Medical-focused keyword search"""
|
| 287 |
-
if not self.knowledge_chunks:
|
| 288 |
-
return []
|
| 289 |
-
|
| 290 |
-
query_words = set(query.lower().split())
|
| 291 |
-
chunk_scores = []
|
| 292 |
-
|
| 293 |
-
for chunk_info in self.knowledge_chunks:
|
| 294 |
-
chunk_text = chunk_info['text']
|
| 295 |
-
chunk_words = set(chunk_text.lower().split())
|
| 296 |
-
|
| 297 |
-
# Calculate relevance score
|
| 298 |
-
word_overlap = len(query_words.intersection(chunk_words))
|
| 299 |
-
base_score = word_overlap / len(query_words) if query_words else 0
|
| 300 |
-
|
| 301 |
-
# Boost medical content
|
| 302 |
-
medical_boost = 0
|
| 303 |
-
if chunk_info.get('medical_focus') in ['pediatric_symptoms', 'treatments', 'diagnosis']:
|
| 304 |
-
medical_boost = 0.3
|
| 305 |
-
|
| 306 |
-
final_score = base_score + medical_boost
|
| 307 |
-
|
| 308 |
-
if final_score > 0:
|
| 309 |
-
chunk_scores.append((final_score, chunk_text))
|
| 310 |
-
|
| 311 |
-
# Return top matches
|
| 312 |
-
chunk_scores.sort(reverse=True)
|
| 313 |
-
return [chunk for _, chunk in chunk_scores[:n_results]]
|
| 314 |
-
|
| 315 |
-
def generate_biogpt_response(self, context: str, query: str) -> str:
|
| 316 |
-
"""Generate medical response using BioGPT model"""
|
| 317 |
-
if not self.model or not self.tokenizer:
|
| 318 |
-
return self.create_context_based_response(context, query)
|
| 319 |
-
|
| 320 |
-
try:
|
| 321 |
-
# Create a medical prompt
|
| 322 |
-
prompt = f"Context: {context[:800]}\n\nQuestion: {query}\n\nMedical Answer:"
|
| 323 |
-
|
| 324 |
-
# Tokenize input
|
| 325 |
-
inputs = self.tokenizer(
|
| 326 |
-
prompt,
|
| 327 |
-
return_tensors="pt",
|
| 328 |
-
max_length=512,
|
| 329 |
-
truncation=True,
|
| 330 |
-
padding=True
|
| 331 |
-
)
|
| 332 |
-
|
| 333 |
-
if self.device == "cuda":
|
| 334 |
-
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
| 335 |
-
|
| 336 |
-
# Generate response
|
| 337 |
-
with torch.no_grad():
|
| 338 |
-
outputs = self.model.generate(
|
| 339 |
-
**inputs,
|
| 340 |
-
max_new_tokens=150,
|
| 341 |
-
temperature=0.7,
|
| 342 |
-
do_sample=True,
|
| 343 |
-
pad_token_id=self.tokenizer.eos_token_id,
|
| 344 |
-
no_repeat_ngram_size=3,
|
| 345 |
-
early_stopping=True
|
| 346 |
-
)
|
| 347 |
-
|
| 348 |
-
# Decode response
|
| 349 |
-
generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 350 |
-
|
| 351 |
-
# Extract the answer part
|
| 352 |
-
if "Medical Answer:" in generated_text:
|
| 353 |
-
response = generated_text.split("Medical Answer:")[-1].strip()
|
| 354 |
-
else:
|
| 355 |
-
response = generated_text[len(prompt):].strip()
|
| 356 |
-
|
| 357 |
-
# Clean the response
|
| 358 |
-
response = self.clean_medical_response(response)
|
| 359 |
-
|
| 360 |
-
# If response is too short or unclear, fallback to context-based response
|
| 361 |
-
if len(response) < 20 or not response:
|
| 362 |
-
return self.create_context_based_response(context, query)
|
| 363 |
-
|
| 364 |
-
return response
|
| 365 |
-
|
| 366 |
-
except Exception as e:
|
| 367 |
-
print(f"⚠️ BioGPT generation failed: {e}")
|
| 368 |
-
return self.create_context_based_response(context, query)
|
| 369 |
-
|
| 370 |
-
def create_context_based_response(self, context: str, query: str) -> str:
|
| 371 |
-
"""Create response directly from medical context"""
|
| 372 |
-
if not context:
|
| 373 |
-
return "I don't have specific information about this topic in my medical database."
|
| 374 |
-
|
| 375 |
-
# Split context into sentences
|
| 376 |
-
sentences = [s.strip() + '.' for s in context.split('.') if len(s.strip()) > 15]
|
| 377 |
-
|
| 378 |
-
# Find sentences most relevant to the query
|
| 379 |
-
query_words = set(query.lower().split())
|
| 380 |
-
scored_sentences = []
|
| 381 |
-
|
| 382 |
-
for sentence in sentences[:20]:
|
| 383 |
-
sentence_words = set(sentence.lower().split())
|
| 384 |
-
score = len(query_words.intersection(sentence_words))
|
| 385 |
-
if score > 0:
|
| 386 |
-
scored_sentences.append((score, sentence))
|
| 387 |
-
|
| 388 |
-
# Sort by relevance and take top sentences
|
| 389 |
-
scored_sentences.sort(reverse=True)
|
| 390 |
-
|
| 391 |
-
if scored_sentences:
|
| 392 |
-
# Take top 3-4 most relevant sentences
|
| 393 |
-
response_sentences = [sent for _, sent in scored_sentences[:4]]
|
| 394 |
-
response = ' '.join(response_sentences)
|
| 395 |
-
else:
|
| 396 |
-
# Fallback to first few sentences
|
| 397 |
-
response = ' '.join(sentences[:3])
|
| 398 |
-
|
| 399 |
-
# Clean up the response
|
| 400 |
-
response = re.sub(r'\s+', ' ', response).strip()
|
| 401 |
-
|
| 402 |
-
return response[:500] + '...' if len(response) > 500 else response
|
| 403 |
-
|
| 404 |
-
def clean_medical_response(self, response: str) -> str:
|
| 405 |
-
"""Clean and format medical response"""
|
| 406 |
-
# Remove training artifacts and unwanted symbols
|
| 407 |
-
response = re.sub(r'<[^>]*>', '', response) # Remove HTML-like tags
|
| 408 |
-
response = re.sub(r'▃+', '', response) # Remove block symbols
|
| 409 |
-
response = re.sub(r'FREETEXT|INTRO|/FREETEXT|/INTRO', '', response) # Remove training markers
|
| 410 |
-
response = re.sub(r'\s+', ' ', response) # Clean up whitespace
|
| 411 |
-
response = response.strip()
|
| 412 |
-
|
| 413 |
-
# Split into sentences and keep only complete, relevant ones
|
| 414 |
-
sentences = re.split(r'[.!?]+', response)
|
| 415 |
-
clean_sentences = []
|
| 416 |
-
|
| 417 |
-
for sentence in sentences:
|
| 418 |
-
sentence = sentence.strip()
|
| 419 |
-
# Skip very short sentences and those with artifacts
|
| 420 |
-
if len(sentence) > 15 and not any(artifact in sentence.lower() for artifact in ['▃', '<', '>', 'freetext']):
|
| 421 |
-
clean_sentences.append(sentence)
|
| 422 |
-
if len(clean_sentences) >= 3: # Limit to 3 good sentences
|
| 423 |
-
break
|
| 424 |
-
|
| 425 |
-
if clean_sentences:
|
| 426 |
-
cleaned = '. '.join(clean_sentences) + '.'
|
| 427 |
-
else:
|
| 428 |
-
# Fallback to first 150 characters if no good sentences found
|
| 429 |
-
cleaned = response[:150].strip()
|
| 430 |
-
if cleaned and not cleaned.endswith('.'):
|
| 431 |
-
cleaned += '.'
|
| 432 |
-
|
| 433 |
-
return cleaned
|
| 434 |
-
|
| 435 |
-
def handle_conversational_interactions(self, query: str) -> Optional[str]:
|
| 436 |
-
"""Handle conversational interactions"""
|
| 437 |
-
query_lower = query.lower().strip()
|
| 438 |
-
|
| 439 |
-
# Greeting patterns
|
| 440 |
-
greeting_patterns = [
|
| 441 |
-
r'^\s*(hello|hi|hey)\s*$',
|
| 442 |
-
r'^\s*(good morning|good afternoon|good evening)\s*$',
|
| 443 |
-
r'^\s*(hi there|hello there)\s*$'
|
| 444 |
-
]
|
| 445 |
-
|
| 446 |
-
for pattern in greeting_patterns:
|
| 447 |
-
if re.match(pattern, query_lower):
|
| 448 |
-
return "👋 Hello! I'm your pediatric medical AI assistant. How can I help you with medical questions today?"
|
| 449 |
-
|
| 450 |
-
# Thanks patterns
|
| 451 |
-
thanks_patterns = [
|
| 452 |
-
r'^\s*(thank you|thanks|thx)\s*$',
|
| 453 |
-
r'^\s*(thank you so much|thanks a lot)\s*$'
|
| 454 |
-
]
|
| 455 |
-
|
| 456 |
-
for pattern in thanks_patterns:
|
| 457 |
-
if re.match(pattern, query_lower):
|
| 458 |
-
return "🙏 You're welcome! I'm glad I could help. Remember to consult healthcare professionals for medical decisions. What else can I help you with?"
|
| 459 |
-
|
| 460 |
-
# Goodbye patterns
|
| 461 |
-
goodbye_patterns = [
|
| 462 |
-
r'^\s*(bye|goodbye)\s*$',
|
| 463 |
-
r'^\s*(see you later|see ya)\s*$',
|
| 464 |
-
r'^\s*(have a good day|take care)\s*$'
|
| 465 |
-
]
|
| 466 |
-
|
| 467 |
-
for pattern in goodbye_patterns:
|
| 468 |
-
if re.match(pattern, query_lower):
|
| 469 |
-
return "👋 Goodbye! Take care and remember to consult healthcare professionals for any medical concerns. Stay healthy!"
|
| 470 |
-
|
| 471 |
-
return None
|
| 472 |
-
|
| 473 |
-
def chat(self, query: str) -> str:
|
| 474 |
-
"""Main chat function"""
|
| 475 |
-
if not query.strip():
|
| 476 |
-
return "Hello! I'm your pediatric medical AI assistant. How can I help you today?"
|
| 477 |
-
|
| 478 |
-
# Handle conversational interactions
|
| 479 |
-
conversational_response = self.handle_conversational_interactions(query)
|
| 480 |
-
if conversational_response:
|
| 481 |
-
return conversational_response
|
| 482 |
-
|
| 483 |
-
if not self.knowledge_chunks:
|
| 484 |
-
return "Please load medical data first to access the medical knowledge base."
|
| 485 |
-
|
| 486 |
-
# Retrieve context
|
| 487 |
-
context = self.retrieve_medical_context(query)
|
| 488 |
-
|
| 489 |
-
if not context:
|
| 490 |
-
return "I don't have specific information about this topic in my medical database. Please consult with a healthcare professional for personalized medical advice."
|
| 491 |
-
|
| 492 |
-
# Generate response
|
| 493 |
-
main_context = '\n\n'.join(context)
|
| 494 |
-
response = self.generate_biogpt_response(main_context, query)
|
| 495 |
-
|
| 496 |
-
# Format final response
|
| 497 |
-
final_response = f"🩺 **Medical Information:** {response}\n\n⚠️ **Important:** This information is for educational purposes only. Always consult with qualified healthcare professionals for medical diagnosis, treatment, and personalized advice."
|
| 498 |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