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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import torch
|
4 |
+
import warnings
|
5 |
+
import numpy as np
|
6 |
+
import gradio as gr
|
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
|
18 |
+
warnings.filterwarnings('ignore')
|
19 |
+
|
20 |
+
class BioGPTMedicalChatbot:
|
21 |
+
def __init__(self):
|
22 |
+
"""Initialize BioGPT chatbot for Gradio deployment"""
|
23 |
+
print("π₯ Initializing BioGPT Pediatric Pulmonology Chatbot...")
|
24 |
+
|
25 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
26 |
+
self.use_8bit = torch.cuda.is_available()
|
27 |
+
|
28 |
+
print(f"π₯οΈ Using device: {self.device}")
|
29 |
+
|
30 |
+
# Setup components
|
31 |
+
self.setup_embeddings()
|
32 |
+
self.setup_biogpt()
|
33 |
+
|
34 |
+
# Knowledge base and conversation tracking
|
35 |
+
self.knowledge_chunks = []
|
36 |
+
self.conversation_history = []
|
37 |
+
|
38 |
+
# Load default medical knowledge first
|
39 |
+
self.load_default_medical_knowledge()
|
40 |
+
|
41 |
+
# Try to load your specific pediatric pulmonology data
|
42 |
+
self.load_pediatric_pulmonology_data()
|
43 |
+
|
44 |
+
print("β
BioGPT Pediatric Pulmonology Chatbot ready!")
|
45 |
+
|
46 |
+
def load_pediatric_pulmonology_data(self):
|
47 |
+
"""Auto-load pediatric pulmonology data from uploaded file"""
|
48 |
+
pulmonology_files = [
|
49 |
+
'Pediatric_cleaned.txt',
|
50 |
+
'pediatric_cleaned.txt',
|
51 |
+
'Pediatric_Cleaned.txt',
|
52 |
+
'pediatric_pulmonology.txt',
|
53 |
+
'pulmonology_data.txt'
|
54 |
+
]
|
55 |
+
|
56 |
+
for filename in pulmonology_files:
|
57 |
+
if os.path.exists(filename):
|
58 |
+
print(f"π Found pediatric pulmonology data: {filename}")
|
59 |
+
try:
|
60 |
+
success = self.load_medical_data(filename)
|
61 |
+
if success:
|
62 |
+
print(f"β
Successfully loaded {filename} with pulmonology data!")
|
63 |
+
print(f"π Total knowledge chunks: {len(self.knowledge_chunks)}")
|
64 |
+
return True
|
65 |
+
except Exception as e:
|
66 |
+
print(f"β οΈ Failed to load {filename}: {e}")
|
67 |
+
continue
|
68 |
+
|
69 |
+
print("β οΈ No pediatric pulmonology data file found.")
|
70 |
+
print(" Expected files: Pediatric_cleaned.txt")
|
71 |
+
print(" Using default pediatric knowledge only.")
|
72 |
+
print(f"π Current knowledge chunks: {len(self.knowledge_chunks)}")
|
73 |
+
return False
|
74 |
+
|
75 |
+
def load_medical_data(self, file_path: str):
|
76 |
+
"""Load and process medical data from text file"""
|
77 |
+
print(f"π Loading medical data from {file_path}...")
|
78 |
+
|
79 |
+
try:
|
80 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
81 |
+
text = f.read()
|
82 |
+
print(f"π File loaded: {len(text):,} characters")
|
83 |
+
except FileNotFoundError:
|
84 |
+
print(f"β File {file_path} not found!")
|
85 |
+
return False
|
86 |
+
except Exception as e:
|
87 |
+
print(f"β Error reading file: {e}")
|
88 |
+
return False
|
89 |
+
|
90 |
+
# Create chunks optimized for medical content
|
91 |
+
print("π Creating pediatric pulmonology chunks...")
|
92 |
+
new_chunks = self.create_medical_chunks_from_text(text)
|
93 |
+
print(f"π Created {len(new_chunks)} new medical chunks from file")
|
94 |
+
|
95 |
+
# Add to existing knowledge chunks (don't replace, append)
|
96 |
+
starting_id = len(self.knowledge_chunks)
|
97 |
+
for i, chunk in enumerate(new_chunks):
|
98 |
+
chunk['id'] = starting_id + i
|
99 |
+
chunk['source'] = 'pediatric_pulmonology_file'
|
100 |
+
|
101 |
+
self.knowledge_chunks.extend(new_chunks)
|
102 |
+
|
103 |
+
print(f"β
Medical data loaded successfully!")
|
104 |
+
print(f"π Total knowledge chunks: {len(self.knowledge_chunks)}")
|
105 |
+
return True
|
106 |
+
|
107 |
+
def create_medical_chunks_from_text(self, text: str, chunk_size: int = 400) -> List[Dict]:
|
108 |
+
"""Create medically-optimized text chunks from uploaded file"""
|
109 |
+
chunks = []
|
110 |
+
|
111 |
+
# Split by medical sections first
|
112 |
+
medical_sections = self.split_by_medical_sections(text)
|
113 |
+
|
114 |
+
for section in medical_sections:
|
115 |
+
if len(section.split()) > chunk_size:
|
116 |
+
# Split large sections by sentences
|
117 |
+
sentences = re.split(r'[.!?]+', section)
|
118 |
+
current_chunk = ""
|
119 |
+
|
120 |
+
for sentence in sentences:
|
121 |
+
sentence = sentence.strip()
|
122 |
+
if not sentence:
|
123 |
+
continue
|
124 |
+
|
125 |
+
if len(current_chunk.split()) + len(sentence.split()) < chunk_size:
|
126 |
+
current_chunk += sentence + ". "
|
127 |
+
else:
|
128 |
+
if current_chunk.strip():
|
129 |
+
chunks.append({
|
130 |
+
'text': current_chunk.strip(),
|
131 |
+
'medical_focus': self.identify_medical_focus(current_chunk)
|
132 |
+
})
|
133 |
+
current_chunk = sentence + ". "
|
134 |
+
|
135 |
+
if current_chunk.strip():
|
136 |
+
chunks.append({
|
137 |
+
'text': current_chunk.strip(),
|
138 |
+
'medical_focus': self.identify_medical_focus(current_chunk)
|
139 |
+
})
|
140 |
+
else:
|
141 |
+
if section.strip():
|
142 |
+
chunks.append({
|
143 |
+
'text': section.strip(),
|
144 |
+
'medical_focus': self.identify_medical_focus(section)
|
145 |
+
})
|
146 |
+
|
147 |
+
return chunks
|
148 |
+
|
149 |
+
def split_by_medical_sections(self, text: str) -> List[str]:
|
150 |
+
"""Split text by medical sections"""
|
151 |
+
# Look for medical section headers
|
152 |
+
section_patterns = [
|
153 |
+
r'\n\s*(?:SYMPTOMS?|TREATMENT|DIAGNOSIS|CAUSES?|PREVENTION|MANAGEMENT).*?\n',
|
154 |
+
r'\n\s*\d+\.\s+', # Numbered sections
|
155 |
+
r'\n\n+' # Paragraph breaks
|
156 |
+
]
|
157 |
+
|
158 |
+
sections = [text]
|
159 |
+
for pattern in section_patterns:
|
160 |
+
new_sections = []
|
161 |
+
for section in sections:
|
162 |
+
splits = re.split(pattern, section, flags=re.IGNORECASE)
|
163 |
+
new_sections.extend([s.strip() for s in splits if len(s.strip()) > 100])
|
164 |
+
sections = new_sections
|
165 |
+
|
166 |
+
return sections
|
167 |
+
|
168 |
+
def identify_medical_focus(self, text: str) -> str:
|
169 |
+
"""Identify the medical focus of a text chunk with pulmonology emphasis"""
|
170 |
+
text_lower = text.lower()
|
171 |
+
|
172 |
+
# Enhanced medical categories with pulmonology focus
|
173 |
+
categories = {
|
174 |
+
'pediatric_pulmonology': [
|
175 |
+
'asthma', 'pneumonia', 'bronchiolitis', 'croup', 'respiratory', 'lung', 'airway',
|
176 |
+
'breathing', 'cough', 'wheeze', 'stridor', 'pneumothorax', 'pleural', 'ventilator',
|
177 |
+
'oxygen', 'respiratory distress', 'bronchitis', 'pulmonary', 'chest', 'inhaler'
|
178 |
+
],
|
179 |
+
'pediatric_symptoms': ['fever', 'rash', 'vomiting', 'diarrhea', 'pain'],
|
180 |
+
'treatments': ['treatment', 'therapy', 'medication', 'antibiotics', 'steroid'],
|
181 |
+
'diagnosis': ['diagnosis', 'diagnostic', 'symptoms', 'signs', 'test'],
|
182 |
+
'emergency': ['emergency', 'urgent', 'serious', 'hospital', 'icu'],
|
183 |
+
'prevention': ['prevention', 'vaccine', 'immunization', 'avoid']
|
184 |
+
}
|
185 |
+
|
186 |
+
for category, keywords in categories.items():
|
187 |
+
if any(keyword in text_lower for keyword in keywords):
|
188 |
+
return category
|
189 |
+
|
190 |
+
return 'general_medical'
|
191 |
+
|
192 |
+
def setup_embeddings(self):
|
193 |
+
"""Setup medical embeddings"""
|
194 |
+
try:
|
195 |
+
print("π§ Loading embeddings...")
|
196 |
+
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
197 |
+
self.use_embeddings = True
|
198 |
+
print("β
Embeddings loaded successfully")
|
199 |
+
except Exception as e:
|
200 |
+
print(f"β οΈ Embeddings failed: {e}")
|
201 |
+
self.embedding_model = None
|
202 |
+
self.use_embeddings = False
|
203 |
+
|
204 |
+
def setup_biogpt(self):
|
205 |
+
"""Setup BioGPT model with fallback"""
|
206 |
+
print("π§ Loading BioGPT model...")
|
207 |
+
|
208 |
+
# Try BioGPT first, then fallback to smaller model
|
209 |
+
models_to_try = [
|
210 |
+
"microsoft/BioGPT-Large",
|
211 |
+
"microsoft/BioGPT",
|
212 |
+
"microsoft/DialoGPT-medium"
|
213 |
+
]
|
214 |
+
|
215 |
+
for model_name in models_to_try:
|
216 |
+
try:
|
217 |
+
print(f" Trying {model_name}...")
|
218 |
+
|
219 |
+
# Setup quantization for memory efficiency
|
220 |
+
quantization_config = None
|
221 |
+
if self.use_8bit and "BioGPT" in model_name:
|
222 |
+
quantization_config = BitsAndBytesConfig(
|
223 |
+
load_in_8bit=True,
|
224 |
+
llm_int8_threshold=6.0,
|
225 |
+
)
|
226 |
+
|
227 |
+
# Load tokenizer
|
228 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
229 |
+
if self.tokenizer.pad_token is None:
|
230 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
231 |
+
|
232 |
+
# Load model
|
233 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
234 |
+
model_name,
|
235 |
+
quantization_config=quantization_config,
|
236 |
+
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
237 |
+
device_map="auto" if self.device == "cuda" and quantization_config else None,
|
238 |
+
trust_remote_code=True
|
239 |
+
)
|
240 |
+
|
241 |
+
if self.device == "cuda" and quantization_config is None:
|
242 |
+
self.model = self.model.to(self.device)
|
243 |
+
|
244 |
+
print(f"β
Successfully loaded {model_name}!")
|
245 |
+
self.model_name = model_name
|
246 |
+
return
|
247 |
+
|
248 |
+
except Exception as e:
|
249 |
+
print(f"β Failed to load {model_name}: {e}")
|
250 |
+
continue
|
251 |
+
|
252 |
+
# If all models fail
|
253 |
+
print("β All models failed to load")
|
254 |
+
self.model = None
|
255 |
+
self.tokenizer = None
|
256 |
+
self.model_name = "None"
|
257 |
+
|
258 |
+
def load_default_medical_knowledge(self):
|
259 |
+
"""Load comprehensive default medical knowledge base"""
|
260 |
+
default_knowledge = [
|
261 |
+
{
|
262 |
+
'id': 0,
|
263 |
+
'text': "Fever in children is commonly caused by viral infections (most common), bacterial infections, immunizations, teething in infants, or overdressing. Normal body temperature ranges from 97Β°F to 100.4Β°F (36.1Β°C to 38Β°C). A fever is generally considered when oral temperature exceeds 100.4Β°F (38Β°C). Most fevers are not dangerous and help the body fight infection. Treatment includes rest, fluids, and fever reducers like acetaminophen or ibuprofen for comfort.",
|
264 |
+
'medical_focus': 'pediatric_symptoms',
|
265 |
+
'source': 'default_knowledge'
|
266 |
+
},
|
267 |
+
{
|
268 |
+
'id': 1,
|
269 |
+
'text': "Dehydration in infants and children can occur rapidly, especially during illness with vomiting or diarrhea. Warning signs include: dry mouth and tongue, decreased urination, lethargy or irritability, sunken eyes, and in infants under 12 months, sunken fontanelle (soft spot). Mild dehydration can be treated with oral rehydration solutions or clear fluids. Severe dehydration requires immediate medical attention.",
|
270 |
+
'medical_focus': 'pediatric_symptoms',
|
271 |
+
'source': 'default_knowledge'
|
272 |
+
},
|
273 |
+
{
|
274 |
+
'id': 2,
|
275 |
+
'text': "Common cold symptoms in children include runny or stuffy nose, cough, low-grade fever, sneezing, and general fussiness. Most colds are viral and resolve within 7-10 days without specific treatment. Treatment focuses on comfort measures: rest, adequate fluids, humidified air, and saline nasal drops for congestion. Antibiotics are not effective against viral colds.",
|
276 |
+
'medical_focus': 'pediatric_symptoms',
|
277 |
+
'source': 'default_knowledge'
|
278 |
+
},
|
279 |
+
{
|
280 |
+
'id': 3,
|
281 |
+
'text': "Emergency warning signs in children requiring immediate medical attention include: severe difficulty breathing, persistent high fever over 104Β°F (40Β°C), signs of severe dehydration, persistent vomiting preventing fluid intake, severe headache with neck stiffness, altered consciousness or extreme lethargy, severe abdominal pain, or any concerning change in behavior. When in doubt, seek medical care.",
|
282 |
+
'medical_focus': 'emergency',
|
283 |
+
'source': 'default_knowledge'
|
284 |
+
},
|
285 |
+
{
|
286 |
+
'id': 4,
|
287 |
+
'text': "Childhood vaccination schedules protect against serious diseases including measles, mumps, rubella, polio, hepatitis B, Haemophilus influenzae, pneumococcal disease, and others. Vaccines are rigorously tested for safety and effectiveness. Side effects are typically mild, such as low-grade fever or soreness at injection site. Following recommended vaccination schedules protects individual children and the community.",
|
288 |
+
'medical_focus': 'prevention',
|
289 |
+
'source': 'default_knowledge'
|
290 |
+
},
|
291 |
+
{
|
292 |
+
'id': 5,
|
293 |
+
'text': "Persistent cough in children can be caused by viral upper respiratory infections, asthma, allergies, bacterial infections, or irritants. Most coughs from colds resolve within 2-3 weeks. Seek medical evaluation for coughs lasting more than 3 weeks, coughs with blood, difficulty breathing, or coughs severely interfering with sleep. Treatment depends on the underlying cause.",
|
294 |
+
'medical_focus': 'pediatric_symptoms',
|
295 |
+
'source': 'default_knowledge'
|
296 |
+
},
|
297 |
+
# Adding more default knowledge chunks for comprehensive coverage...
|
298 |
+
{
|
299 |
+
'id': 6,
|
300 |
+
'text': "Asthma in children is a chronic respiratory condition affecting the airways, causing them to become inflamed, narrow, and produce excess mucus. Common triggers include viral infections, allergens (dust mites, pollen, pet dander), irritants (smoke, strong odors), cold air, and exercise. Symptoms include wheezing, cough (especially at night), shortness of breath, and chest tightness. Management includes avoiding triggers, using prescribed inhalers, and having an asthma action plan.",
|
301 |
+
'medical_focus': 'pediatric_pulmonology',
|
302 |
+
'source': 'default_knowledge'
|
303 |
+
},
|
304 |
+
{
|
305 |
+
'id': 7,
|
306 |
+
'text': "Bronchiolitis is a common respiratory infection in infants and young children, typically caused by respiratory syncytial virus (RSV). It affects the small airways (bronchioles) in the lungs, causing inflammation and mucus buildup. Symptoms include runny nose, cough, low-grade fever, and difficulty breathing. Most cases are mild and resolve with supportive care, but severe cases may require hospitalization for oxygen support.",
|
307 |
+
'medical_focus': 'pediatric_pulmonology',
|
308 |
+
'source': 'default_knowledge'
|
309 |
+
}
|
310 |
+
]
|
311 |
+
|
312 |
+
self.knowledge_chunks = default_knowledge
|
313 |
+
print(f"π Loaded {len(default_knowledge)} default medical knowledge chunks")
|
314 |
+
|
315 |
+
def retrieve_medical_context(self, query: str, n_results: int = 3) -> List[str]:
|
316 |
+
"""Retrieve relevant medical context using improved keyword search with pulmonology priority"""
|
317 |
+
if not self.knowledge_chunks:
|
318 |
+
return []
|
319 |
+
|
320 |
+
query_lower = query.lower()
|
321 |
+
query_words = set(query_lower.split())
|
322 |
+
chunk_scores = []
|
323 |
+
|
324 |
+
# Enhanced medical keyword mapping with pulmonology emphasis
|
325 |
+
medical_keywords = {
|
326 |
+
'pulmonology': ['asthma', 'pneumonia', 'bronchiolitis', 'croup', 'respiratory', 'lung', 'airway', 'breathing', 'cough', 'wheeze', 'stridor'],
|
327 |
+
'fever': ['fever', 'temperature', 'hot', 'warm', 'burning'],
|
328 |
+
'stomach': ['stomach', 'abdominal', 'belly', 'tummy', 'pain', 'ache'],
|
329 |
+
'rash': ['rash', 'skin', 'red', 'spots', 'bumps', 'itchy'],
|
330 |
+
'vomiting': ['vomit', 'vomiting', 'throw up', 'sick', 'nausea'],
|
331 |
+
'diarrhea': ['diarrhea', 'loose', 'stool', 'bowel', 'poop'],
|
332 |
+
'dehydration': ['dehydration', 'dehydrated', 'fluids', 'water', 'thirsty'],
|
333 |
+
'emergency': ['emergency', 'urgent', 'serious', 'severe', 'hospital', 'doctor']
|
334 |
+
}
|
335 |
+
|
336 |
+
# Expand query with related medical terms
|
337 |
+
expanded_query_words = set(query_words)
|
338 |
+
for medical_term, synonyms in medical_keywords.items():
|
339 |
+
if any(word in query_lower for word in synonyms):
|
340 |
+
expanded_query_words.update(synonyms)
|
341 |
+
|
342 |
+
for chunk_info in self.knowledge_chunks:
|
343 |
+
chunk_text = chunk_info['text'].lower()
|
344 |
+
|
345 |
+
# Calculate relevance score with expanded terms
|
346 |
+
word_overlap = sum(1 for word in expanded_query_words if word in chunk_text)
|
347 |
+
base_score = word_overlap / len(expanded_query_words) if expanded_query_words else 0
|
348 |
+
|
349 |
+
# Strong boost for pulmonology content
|
350 |
+
medical_boost = 0
|
351 |
+
medical_focus = chunk_info.get('medical_focus', '')
|
352 |
+
source = chunk_info.get('source', '')
|
353 |
+
|
354 |
+
if medical_focus == 'pediatric_pulmonology':
|
355 |
+
medical_boost = 0.8 # Highest priority for pulmonology
|
356 |
+
elif source == 'pediatric_pulmonology_file':
|
357 |
+
medical_boost = 0.7 # High priority for your uploaded data
|
358 |
+
elif medical_focus == 'emergency':
|
359 |
+
medical_boost = 0.4
|
360 |
+
elif medical_focus in ['treatments', 'diagnosis']:
|
361 |
+
medical_boost = 0.3
|
362 |
+
elif medical_focus == 'pediatric_symptoms':
|
363 |
+
medical_boost = 0.5
|
364 |
+
|
365 |
+
final_score = base_score + medical_boost
|
366 |
+
|
367 |
+
if final_score > 0:
|
368 |
+
chunk_scores.append((final_score, chunk_info['text']))
|
369 |
+
|
370 |
+
# Return top matches - prioritize pulmonology content
|
371 |
+
chunk_scores.sort(reverse=True)
|
372 |
+
results = [chunk for _, chunk in chunk_scores[:n_results]]
|
373 |
+
|
374 |
+
# If no good matches, return some default medical chunks
|
375 |
+
if not results:
|
376 |
+
results = [chunk['text'] for chunk in self.knowledge_chunks[:2]]
|
377 |
+
|
378 |
+
return results
|
379 |
+
|
380 |
+
def generate_biogpt_response(self, context: str, query: str) -> str:
|
381 |
+
"""Generate medical response using BioGPT"""
|
382 |
+
if not self.model or not self.tokenizer:
|
383 |
+
return "Medical model not available. Please try again later."
|
384 |
+
|
385 |
+
try:
|
386 |
+
# Create medical prompt
|
387 |
+
prompt = f"""Medical Context: {context[:800]}
|
388 |
+
|
389 |
+
Question: {query}
|
390 |
+
|
391 |
+
Medical Answer:"""
|
392 |
+
|
393 |
+
# Tokenize input
|
394 |
+
inputs = self.tokenizer(
|
395 |
+
prompt,
|
396 |
+
return_tensors="pt",
|
397 |
+
truncation=True,
|
398 |
+
max_length=1024
|
399 |
+
)
|
400 |
+
|
401 |
+
# Move inputs to device
|
402 |
+
if self.device == "cuda":
|
403 |
+
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
404 |
+
|
405 |
+
# Generate response
|
406 |
+
with torch.no_grad():
|
407 |
+
outputs = self.model.generate(
|
408 |
+
**inputs,
|
409 |
+
max_new_tokens=150,
|
410 |
+
do_sample=True,
|
411 |
+
temperature=0.7,
|
412 |
+
top_p=0.9,
|
413 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
414 |
+
repetition_penalty=1.1
|
415 |
+
)
|
416 |
+
|
417 |
+
# Decode response
|
418 |
+
full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
419 |
+
|
420 |
+
# Extract generated part
|
421 |
+
if "Medical Answer:" in full_response:
|
422 |
+
generated_response = full_response.split("Medical Answer:")[-1].strip()
|
423 |
+
else:
|
424 |
+
generated_response = full_response[len(prompt):].strip()
|
425 |
+
|
426 |
+
return self.clean_medical_response(generated_response)
|
427 |
+
|
428 |
+
except Exception as e:
|
429 |
+
print(f"β οΈ BioGPT generation failed: {e}")
|
430 |
+
return self.fallback_response(context, query)
|
431 |
+
|
432 |
+
def clean_medical_response(self, response: str) -> str:
|
433 |
+
"""Clean medical response"""
|
434 |
+
sentences = re.split(r'[.!?]+', response)
|
435 |
+
clean_sentences = []
|
436 |
+
|
437 |
+
for sentence in sentences:
|
438 |
+
sentence = sentence.strip()
|
439 |
+
if len(sentence) > 10:
|
440 |
+
clean_sentences.append(sentence)
|
441 |
+
if len(clean_sentences) >= 3:
|
442 |
+
break
|
443 |
+
|
444 |
+
if clean_sentences:
|
445 |
+
cleaned = '. '.join(clean_sentences) + '.'
|
446 |
+
else:
|
447 |
+
cleaned = response[:200] + '...' if len(response) > 200 else response
|
448 |
+
|
449 |
+
return cleaned
|
450 |
+
|
451 |
+
def fallback_response(self, context: str, query: str) -> str:
|
452 |
+
"""Fallback response when model fails"""
|
453 |
+
sentences = [s.strip() for s in context.split('.') if len(s.strip()) > 20]
|
454 |
+
|
455 |
+
if sentences:
|
456 |
+
response = sentences[0] + '.'
|
457 |
+
if len(sentences) > 1:
|
458 |
+
response += ' ' + sentences[1] + '.'
|
459 |
+
else:
|
460 |
+
response = context[:300] + '...'
|
461 |
+
|
462 |
+
return response
|
463 |
+
|
464 |
+
def handle_conversational_interactions(self, query: str) -> Optional[str]:
|
465 |
+
"""Handle conversational interactions"""
|
466 |
+
query_lower = query.lower().strip()
|
467 |
+
|
468 |
+
# Greeting patterns
|
469 |
+
exact_greetings = [
|
470 |
+
'hello', 'hi', 'hey', 'good morning', 'good afternoon',
|
471 |
+
'good evening', 'how are you', 'how are you doing'
|
472 |
+
]
|
473 |
+
|
474 |
+
if query_lower in exact_greetings:
|
475 |
+
return "π Hello! I'm BioGPT, your AI medical assistant specialized in pediatric pulmonology. I provide evidence-based medical information about children's respiratory health. What can I help you with today?"
|
476 |
+
|
477 |
+
# Thanks patterns
|
478 |
+
thanks_only = ['thank you', 'thanks', 'thank you so much', 'thanks a lot']
|
479 |
+
if query_lower in thanks_only:
|
480 |
+
return "π You're welcome! I'm glad I could provide helpful pediatric pulmonology information. Remember to always consult healthcare providers for personalized advice. Feel free to ask more questions!"
|
481 |
+
|
482 |
+
# Help patterns
|
483 |
+
help_only = ['help', 'what can you do', 'what are you', 'who are you']
|
484 |
+
if query_lower in help_only:
|
485 |
+
return """π€ **About BioGPT Pediatric Pulmonology Assistant**
|
486 |
+
|
487 |
+
I'm an AI medical assistant powered by BioGPT, specialized in pediatric pulmonology and respiratory medicine. I can help with:
|
488 |
+
|
489 |
+
π« **Pediatric Pulmonology:**
|
490 |
+
β’ Asthma, bronchiolitis, pneumonia, croup
|
491 |
+
β’ Respiratory symptoms and breathing difficulties
|
492 |
+
β’ Treatment guidance and management
|
493 |
+
β’ When to seek medical care
|
494 |
+
|
495 |
+
β οΈ **Important:** I provide educational information only. Always consult healthcare professionals for medical decisions."""
|
496 |
+
|
497 |
+
return None
|
498 |
+
|
499 |
+
def chat_interface(self, message: str, history: List[List[str]]) -> str:
|
500 |
+
"""Main chat interface for Gradio"""
|
501 |
+
if not message.strip():
|
502 |
+
return "Hello! I'm BioGPT, your pediatric pulmonology AI assistant. How can I help you with children's respiratory health today?"
|
503 |
+
|
504 |
+
print(f"π Processing query: '{message}'")
|
505 |
+
|
506 |
+
# Handle conversational interactions
|
507 |
+
conversational_response = self.handle_conversational_interactions(message)
|
508 |
+
if conversational_response:
|
509 |
+
print(" Handled as conversational")
|
510 |
+
return conversational_response
|
511 |
+
|
512 |
+
print(" Processing as medical query")
|
513 |
+
|
514 |
+
# Process as medical query
|
515 |
+
context = self.retrieve_medical_context(message)
|
516 |
+
|
517 |
+
if not context:
|
518 |
+
return f"""π« **Pediatric Pulmonology Query:** {message}
|
519 |
+
|
520 |
+
β οΈ I don't have specific information about this topic in my current medical database. However, I recommend:
|
521 |
+
|
522 |
+
1. **Consult Healthcare Provider**: For personalized medical advice
|
523 |
+
2. **Emergency Signs**: If symptoms are severe, seek immediate care
|
524 |
+
3. **Pediatric Pulmonologist**: For specialized respiratory concerns
|
525 |
+
|
526 |
+
**For urgent respiratory concerns, contact your healthcare provider or emergency services.**
|
527 |
+
|
528 |
+
π‘ **Try asking about**: asthma, breathing difficulties, cough, pneumonia, or other respiratory symptoms."""
|
529 |
+
|
530 |
+
# Generate medical response
|
531 |
+
main_context = '\n\n'.join(context)
|
532 |
+
response = self.generate_biogpt_response(main_context, message)
|
533 |
+
|
534 |
+
# Format as medical response
|
535 |
+
final_response = f"π« **Pediatric Pulmonology Information:** {response}\n\nβ οΈ **Important:** This information is for educational purposes only. Always consult qualified healthcare professionals for medical diagnosis, treatment, and personalized advice."
|
536 |
+
|
537 |
+
return final_response
|
538 |
+
|
539 |
+
def get_knowledge_stats(self) -> Dict:
|
540 |
+
"""Get statistics about loaded knowledge"""
|
541 |
+
if not self.knowledge_chunks:
|
542 |
+
return {"total_chunks": 0}
|
543 |
+
|
544 |
+
stats = {
|
545 |
+
"total_chunks": len(self.knowledge_chunks),
|
546 |
+
"default_knowledge": len([c for c in self.knowledge_chunks if c.get('source') == 'default_knowledge']),
|
547 |
+
"pulmonology_file_data": len([c for c in self.knowledge_chunks if c.get('source') == 'pediatric_pulmonology_file']),
|
548 |
+
"pulmonology_focused": len([c for c in self.knowledge_chunks if c.get('medical_focus') == 'pediatric_pulmonology']),
|
549 |
+
"model_used": getattr(self, 'model_name', 'Unknown')
|
550 |
+
}
|
551 |
+
return stats
|
552 |
+
|
553 |
+
# Test function
|
554 |
+
def test_chatbot_responses():
|
555 |
+
"""Test the chatbot with various queries"""
|
556 |
+
print("\nπ§ͺ Testing BioGPT Pediatric Pulmonology Chatbot...")
|
557 |
+
print("=" * 50)
|
558 |
+
|
559 |
+
test_queries = [
|
560 |
+
"hello",
|
561 |
+
"what is asthma in children",
|
562 |
+
"my child has breathing difficulties",
|
563 |
+
"help",
|
564 |
+
"treatment for pediatric pneumonia",
|
565 |
+
"thank you"
|
566 |
+
]
|
567 |
+
|
568 |
+
for query in test_queries:
|
569 |
+
print(f"\nπ Query: '{query}'")
|
570 |
+
response = chatbot.chat_interface(query, [])
|
571 |
+
response_type = 'CONVERSATIONAL' if any(word in response for word in ['Hello!', 'welcome!', 'About BioGPT']) else 'MEDICAL'
|
572 |
+
print(f"π€ Response type: {response_type}")
|
573 |
+
print(f"π Response: {response[:100]}...")
|
574 |
+
print("-" * 30)
|
575 |
+
|
576 |
+
# Initialize the chatbot globally
|
577 |
+
print("π Initializing BioGPT Pediatric Pulmonology Chatbot...")
|
578 |
+
chatbot = BioGPTMedicalChatbot()
|
579 |
+
|
580 |
+
# Show knowledge statistics
|
581 |
+
print("\nπ Knowledge Base Statistics:")
|
582 |
+
stats = chatbot.get_knowledge_stats()
|
583 |
+
for key, value in stats.items():
|
584 |
+
print(f" {key}: {value}")
|
585 |
+
|
586 |
+
# Run tests
|
587 |
+
test_chatbot_responses()
|
588 |
+
|
589 |
+
def create_gradio_interface():
|
590 |
+
"""Create Gradio chat interface"""
|
591 |
+
|
592 |
+
# Get current knowledge stats for display
|
593 |
+
stats = chatbot.get_knowledge_stats()
|
594 |
+
|
595 |
+
# Custom CSS for medical theme
|
596 |
+
css = """
|
597 |
+
.gradio-container {
|
598 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
599 |
+
}
|
600 |
+
.chat-message {
|
601 |
+
background-color: #f8f9fa;
|
602 |
+
border-radius: 10px;
|
603 |
+
padding: 10px;
|
604 |
+
margin: 5px;
|
605 |
+
}
|
606 |
+
"""
|
607 |
+
|
608 |
+
with gr.Blocks(
|
609 |
+
css=css,
|
610 |
+
title="BioGPT Pediatric Pulmonology Assistant",
|
611 |
+
theme=gr.themes.Soft()
|
612 |
+
) as demo:
|
613 |
+
|
614 |
+
# Header
|
615 |
+
gr.HTML(f"""
|
616 |
+
<div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea, #764ba2); color: white; border-radius: 10px; margin-bottom: 20px;">
|
617 |
+
<h1>π« BioGPT Pediatric Pulmonology Assistant</h1>
|
618 |
+
<p>Specialized AI Medical Chatbot for Children's Respiratory Health</p>
|
619 |
+
<p><strong>Powered by BioGPT | {stats['total_chunks']} Medical Knowledge Chunks Loaded</strong></p>
|
620 |
+
<p><small>Model: {stats['model_used']} | Pulmonology Data: {stats['pulmonology_file_data']} chunks</small></p>
|
621 |
+
</div>
|
622 |
+
""")
|
623 |
+
|
624 |
+
# Important disclaimer
|
625 |
+
gr.HTML("""
|
626 |
+
<div style="background-color: #fff3cd; border: 1px solid #ffeaa7; border-radius: 8px; padding: 15px; margin-bottom: 20px;">
|
627 |
+
<h3 style="color: #856404; margin-top: 0;">β οΈ Medical Disclaimer</h3>
|
628 |
+
<p style="color: #856404; margin-bottom: 0;">
|
629 |
+
This AI provides educational pediatric pulmonology information only and is NOT a substitute for professional medical advice,
|
630 |
+
diagnosis, or treatment. Always consult qualified healthcare providers for medical decisions.
|
631 |
+
<strong>In case of respiratory emergency, call emergency services immediately.</strong>
|
632 |
+
</p>
|
633 |
+
</div>
|
634 |
+
""")
|
635 |
+
|
636 |
+
# Chat interface
|
637 |
+
chatbot_interface = gr.ChatInterface(
|
638 |
+
fn=chatbot.chat_interface,
|
639 |
+
title="π¬ Chat with BioGPT Pulmonology Assistant",
|
640 |
+
description="Ask me about pediatric respiratory health, asthma, breathing difficulties, and pulmonology treatments.",
|
641 |
+
examples=[
|
642 |
+
"What is asthma in children?",
|
643 |
+
"My child has a persistent cough, what should I do?",
|
644 |
+
"How is bronchiolitis treated in infants?",
|
645 |
+
"When should I be worried about my child's breathing?",
|
646 |
+
"What are the signs of pneumonia in children?",
|
647 |
+
"How can I prevent respiratory infections?"
|
648 |
+
],
|
649 |
+
retry_btn=None,
|
650 |
+
undo_btn=None,
|
651 |
+
clear_btn="ποΈ Clear Chat",
|
652 |
+
submit_btn="π« Ask BioGPT",
|
653 |
+
chatbot=gr.Chatbot(
|
654 |
+
height=500,
|
655 |
+
placeholder="<div style='text-align: center; color: #666;'>Start a conversation with BioGPT Pediatric Pulmonology Assistant</div>",
|
656 |
+
show_copy_button=True,
|
657 |
+
bubble_full_width=False
|
658 |
+
)
|
659 |
+
)
|
660 |
+
|
661 |
+
# Information tabs
|
662 |
+
with gr.Tabs():
|
663 |
+
with gr.Tab("βΉοΈ About"):
|
664 |
+
gr.Markdown(f"""
|
665 |
+
## About BioGPT Pediatric Pulmonology Assistant
|
666 |
+
|
667 |
+
This AI assistant is powered by **BioGPT**, specialized for pediatric pulmonology and respiratory medicine.
|
668 |
+
|
669 |
+
### π― Current Knowledge Base:
|
670 |
+
- **Total Chunks**: {stats['total_chunks']}
|
671 |
+
- **Default Medical Knowledge**: {stats['default_knowledge']} chunks
|
672 |
+
- **Pulmonology File Data**: {stats['pulmonology_file_data']} chunks
|
673 |
+
- **Pulmonology Focus**: {stats['pulmonology_focused']} chunks
|
674 |
+
- **Model**: {stats['model_used']}
|
675 |
+
|
676 |
+
### π« Specializations:
|
677 |
+
- **Pediatric Asthma**: Diagnosis, treatment, management
|
678 |
+
- **Respiratory Infections**: Pneumonia, bronchiolitis, croup
|
679 |
+
- **Breathing Difficulties**: Assessment and guidance
|
680 |
+
- **Chronic Respiratory Conditions**: Long-term management
|
681 |
+
- **Emergency Respiratory Care**: When to seek immediate help
|
682 |
+
|
683 |
+
### π§ Technical Features:
|
684 |
+
- **Model**: Microsoft BioGPT (Medical AI)
|
685 |
+
- **Auto-Loading**: Automatically loads your pulmonology data file
|
686 |
+
- **Smart Retrieval**: Prioritizes pulmonology content
|
687 |
+
- **Fallback System**: Ensures reliability across different environments
|
688 |
+
|
689 |
+
### π± How to Use:
|
690 |
+
1. Type your pediatric respiratory question
|
691 |
+
2. Be specific about symptoms or conditions
|
692 |
+
3. Ask about treatments, diagnosis, or management
|
693 |
+
4. Request guidance on when to seek care
|
694 |
+
""")
|
695 |
+
|
696 |
+
with gr.Tab("π« Pulmonology Topics"):
|
697 |
+
gr.Markdown("""
|
698 |
+
## Pediatric Pulmonology Coverage
|
699 |
+
|
700 |
+
### π΄ Common Respiratory Conditions:
|
701 |
+
- **Asthma**: Triggers, symptoms, management, action plans
|
702 |
+
- **Bronchiolitis**: RSV, treatment, when to hospitalize
|
703 |
+
- **Pneumonia**: Bacterial vs viral, antibiotics, recovery
|
704 |
+
- **Croup**: Barking cough, stridor, home treatment
|
705 |
+
- **Bronchitis**: Acute vs chronic, treatment approaches
|
706 |
+
|
707 |
+
### π‘ Respiratory Symptoms:
|
708 |
+
- **Cough**: Persistent, productive, dry, nocturnal
|
709 |
+
- **Wheezing**: Causes, assessment, treatment
|
710 |
+
- **Shortness of Breath**: Evaluation and management
|
711 |
+
- **Chest Pain**: When concerning in children
|
712 |
+
- **Stridor**: Upper airway obstruction signs
|
713 |
+
|
714 |
+
### π’ Diagnostic & Treatment:
|
715 |
+
- **Pulmonary Function Tests**: When appropriate
|
716 |
+
- **Imaging**: X-rays, CT scans for respiratory issues
|
717 |
+
- **Medications**: Bronchodilators, steroids, antibiotics
|
718 |
+
- **Oxygen Therapy**: Indications and monitoring
|
719 |
+
- **Respiratory Support**: CPAP, ventilation considerations
|
720 |
+
|
721 |
+
### π΅ Prevention & Management:
|
722 |
+
- **Trigger Avoidance**: Environmental controls
|
723 |
+
- **Vaccination**: Respiratory disease prevention
|
724 |
+
- **Exercise Guidelines**: For children with respiratory conditions
|
725 |
+
- **School Management**: Asthma action plans, inhaler use
|
726 |
+
""")
|
727 |
+
|
728 |
+
with gr.Tab("β οΈ Emergency & Safety"):
|
729 |
+
gr.Markdown("""
|
730 |
+
## Respiratory Emergency Guidance
|
731 |
+
|
732 |
+
### π¨ CALL EMERGENCY SERVICES IMMEDIATELY:
|
733 |
+
- **Severe Breathing Difficulty**: Cannot speak in full sentences
|
734 |
+
- **Blue Lips or Fingernails**: Cyanosis indicating oxygen deprivation
|
735 |
+
- **Severe Wheezing**: With significant distress
|
736 |
+
- **Stridor at Rest**: High-pitched breathing sound
|
737 |
+
- **Unconsciousness**: Related to breathing problems
|
738 |
+
- **Severe Chest Retractions**: Pulling in around ribs/sternum
|
739 |
+
|
740 |
+
### π₯ SEEK IMMEDIATE MEDICAL CARE:
|
741 |
+
- **Persistent High Fever**: >104Β°F (40Β°C) with respiratory symptoms
|
742 |
+
- **Worsening Symptoms**: Despite treatment
|
743 |
+
- **Dehydration Signs**: With respiratory illness
|
744 |
+
- **Significant Behavior Changes**: Extreme lethargy, irritability
|
745 |
+
- **Inhaler Not Helping**: Asthma symptoms not responding
|
746 |
+
|
747 |
+
### π CONTACT HEALTHCARE PROVIDER:
|
748 |
+
- **New Respiratory Symptoms**: Lasting more than a few days
|
749 |
+
- **Chronic Cough**: Persisting beyond 2-3 weeks
|
750 |
+
- **Asthma Questions**: About medications or management
|
751 |
+
- **Fever with Cough**: Especially if productive
|
752 |
+
- **Exercise Limitations**: Due to breathing difficulties
|
753 |
+
|
754 |
+
### π HOME MONITORING:
|
755 |
+
- **Respiratory Rate**: Normal ranges by age
|
756 |
+
- **Oxygen Saturation**: If pulse oximeter available
|
757 |
+
- **Peak Flow**: For children with asthma
|
758 |
+
- **Symptom Tracking**: Using asthma diaries or apps
|
759 |
+
""")
|
760 |
+
|
761 |
+
with gr.Tab("π Data Information"):
|
762 |
+
gr.Markdown(f"""
|
763 |
+
## Knowledge Base Status
|
764 |
+
|
765 |
+
### π Current Data Loaded:
|
766 |
+
- **Total Medical Chunks**: {stats['total_chunks']}
|
767 |
+
- **Default Knowledge**: {stats['default_knowledge']} chunks
|
768 |
+
- **Your Pulmonology File**: {stats['pulmonology_file_data']} chunks
|
769 |
+
- **Pulmonology Focused**: {stats['pulmonology_focused']} chunks
|
770 |
+
- **AI Model**: {stats['model_used']}
|
771 |
+
|
772 |
+
### π How Your Data Is Used:
|
773 |
+
1. **Auto-Detection**: System automatically looks for 'Pediatric_cleaned.txt'
|
774 |
+
2. **Smart Processing**: Breaks your file into medical chunks
|
775 |
+
3. **Priority Ranking**: Pulmonology content gets highest priority
|
776 |
+
4. **Context Retrieval**: Relevant chunks are used to answer questions
|
777 |
+
|
778 |
+
### π Expected File Formats:
|
779 |
+
- **Filename**: 'Pediatric_cleaned.txt' (case variations accepted)
|
780 |
+
- **Content**: Plain text with medical information
|
781 |
+
- **Structure**: Paragraphs, sections, or bullet points
|
782 |
+
- **Focus**: Pediatric pulmonology and respiratory medicine
|
783 |
+
|
784 |
+
### π Upload Instructions:
|
785 |
+
1. Go to your Hugging Face Space
|
786 |
+
2. Click "Files" tab
|
787 |
+
3. Upload your 'Pediatric_cleaned.txt' file
|
788 |
+
4. Restart the Space to reload data
|
789 |
+
|
790 |
+
{"β
**Status**: Pulmonology data file loaded successfully!" if stats['pulmonology_file_data'] > 0 else "β οΈ **Status**: No pulmonology data file detected. Upload 'Pediatric_cleaned.txt' to enhance responses."}
|
791 |
+
""")
|
792 |
+
|
793 |
+
# Footer
|
794 |
+
gr.HTML("""
|
795 |
+
<div style="text-align: center; padding: 20px; margin-top: 30px; border-top: 1px solid #ddd; color: #666;">
|
796 |
+
<p>π« <strong>BioGPT Pediatric Pulmonology Assistant</strong> | Powered by Microsoft BioGPT</p>
|
797 |
+
<p>Specialized in Children's Respiratory Health β’ Always consult healthcare professionals</p>
|
798 |
+
</div>
|
799 |
+
""")
|
800 |
+
|
801 |
+
return demo
|
802 |
+
|
803 |
+
# Create and launch the interface
|
804 |
+
demo = create_gradio_interface()
|
805 |
+
|
806 |
+
if __name__ == "__main__":
|
807 |
+
# Launch the app
|
808 |
+
demo.launch(
|
809 |
+
server_name="0.0.0.0",
|
810 |
+
server_port=7860,
|
811 |
+
share=False
|
812 |
+
)
|