Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,961 +0,0 @@
|
|
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 |
-
# Clean the text first - remove XML/HTML tags and formatting artifacts
|
112 |
-
cleaned_text = self.clean_medical_text(text)
|
113 |
-
|
114 |
-
# Split by medical sections first
|
115 |
-
medical_sections = self.split_by_medical_sections(cleaned_text)
|
116 |
-
|
117 |
-
for section in medical_sections:
|
118 |
-
if len(section.split()) > chunk_size:
|
119 |
-
# Split large sections by sentences
|
120 |
-
sentences = re.split(r'[.!?]+', section)
|
121 |
-
current_chunk = ""
|
122 |
-
|
123 |
-
for sentence in sentences:
|
124 |
-
sentence = sentence.strip()
|
125 |
-
if not sentence:
|
126 |
-
continue
|
127 |
-
|
128 |
-
if len(current_chunk.split()) + len(sentence.split()) < chunk_size:
|
129 |
-
current_chunk += sentence + ". "
|
130 |
-
else:
|
131 |
-
if current_chunk.strip():
|
132 |
-
chunks.append({
|
133 |
-
'text': current_chunk.strip(),
|
134 |
-
'medical_focus': self.identify_medical_focus(current_chunk)
|
135 |
-
})
|
136 |
-
current_chunk = sentence + ". "
|
137 |
-
|
138 |
-
if current_chunk.strip():
|
139 |
-
chunks.append({
|
140 |
-
'text': current_chunk.strip(),
|
141 |
-
'medical_focus': self.identify_medical_focus(current_chunk)
|
142 |
-
})
|
143 |
-
else:
|
144 |
-
if section.strip():
|
145 |
-
chunks.append({
|
146 |
-
'text': section.strip(),
|
147 |
-
'medical_focus': self.identify_medical_focus(section)
|
148 |
-
})
|
149 |
-
|
150 |
-
return chunks
|
151 |
-
|
152 |
-
def clean_medical_text(self, text: str) -> str:
|
153 |
-
"""Clean medical text from formatting artifacts and XML tags"""
|
154 |
-
# Remove XML/HTML tags like </FREETEXT>, </ABSTRACT>, <SECTION>, etc.
|
155 |
-
text = re.sub(r'<[^>]+>', '', text)
|
156 |
-
|
157 |
-
# Remove common document formatting artifacts
|
158 |
-
text = re.sub(r'</?\s*FREETEXT\s*>', '', text, flags=re.IGNORECASE)
|
159 |
-
text = re.sub(r'</?\s*ABSTRACT\s*>', '', text, flags=re.IGNORECASE)
|
160 |
-
text = re.sub(r'</?\s*SECTION\s*>', '', text, flags=re.IGNORECASE)
|
161 |
-
text = re.sub(r'</?\s*TITLE\s*>', '', text, flags=re.IGNORECASE)
|
162 |
-
|
163 |
-
# Remove excessive whitespace and newlines
|
164 |
-
text = re.sub(r'\n\s*\n\s*\n+', '\n\n', text)
|
165 |
-
text = re.sub(r'\s+', ' ', text)
|
166 |
-
|
167 |
-
# Remove special characters that might be formatting artifacts
|
168 |
-
text = re.sub(r'[^\w\s.,;:!?()\-\'/"]', ' ', text)
|
169 |
-
|
170 |
-
# Clean up multiple spaces
|
171 |
-
text = re.sub(r'\s+', ' ', text).strip()
|
172 |
-
|
173 |
-
return text
|
174 |
-
|
175 |
-
def split_by_medical_sections(self, text: str) -> List[str]:
|
176 |
-
"""Split text by medical sections"""
|
177 |
-
# Look for medical section headers
|
178 |
-
section_patterns = [
|
179 |
-
r'\n\s*(?:SYMPTOMS?|TREATMENT|DIAGNOSIS|CAUSES?|PREVENTION|MANAGEMENT).*?\n',
|
180 |
-
r'\n\s*\d+\.\s+', # Numbered sections
|
181 |
-
r'\n\n+' # Paragraph breaks
|
182 |
-
]
|
183 |
-
|
184 |
-
sections = [text]
|
185 |
-
for pattern in section_patterns:
|
186 |
-
new_sections = []
|
187 |
-
for section in sections:
|
188 |
-
splits = re.split(pattern, section, flags=re.IGNORECASE)
|
189 |
-
new_sections.extend([s.strip() for s in splits if len(s.strip()) > 100])
|
190 |
-
sections = new_sections
|
191 |
-
|
192 |
-
return sections
|
193 |
-
|
194 |
-
def identify_medical_focus(self, text: str) -> str:
|
195 |
-
"""Identify the medical focus of a text chunk with pulmonology emphasis"""
|
196 |
-
text_lower = text.lower()
|
197 |
-
|
198 |
-
# Enhanced medical categories with pulmonology focus
|
199 |
-
categories = {
|
200 |
-
'pediatric_pulmonology': [
|
201 |
-
'asthma', 'pneumonia', 'bronchiolitis', 'croup', 'respiratory', 'lung', 'airway',
|
202 |
-
'breathing', 'cough', 'wheeze', 'stridor', 'pneumothorax', 'pleural', 'ventilator',
|
203 |
-
'oxygen', 'respiratory distress', 'bronchitis', 'pulmonary', 'chest', 'inhaler'
|
204 |
-
],
|
205 |
-
'pediatric_symptoms': ['fever', 'rash', 'vomiting', 'diarrhea', 'pain'],
|
206 |
-
'treatments': ['treatment', 'therapy', 'medication', 'antibiotics', 'steroid'],
|
207 |
-
'diagnosis': ['diagnosis', 'diagnostic', 'symptoms', 'signs', 'test'],
|
208 |
-
'emergency': ['emergency', 'urgent', 'serious', 'hospital', 'icu'],
|
209 |
-
'prevention': ['prevention', 'vaccine', 'immunization', 'avoid']
|
210 |
-
}
|
211 |
-
|
212 |
-
for category, keywords in categories.items():
|
213 |
-
if any(keyword in text_lower for keyword in keywords):
|
214 |
-
return category
|
215 |
-
|
216 |
-
return 'general_medical'
|
217 |
-
|
218 |
-
def setup_embeddings(self):
|
219 |
-
"""Setup medical embeddings"""
|
220 |
-
try:
|
221 |
-
print("🔧 Loading embeddings...")
|
222 |
-
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
223 |
-
self.use_embeddings = True
|
224 |
-
print("✅ Embeddings loaded successfully")
|
225 |
-
except Exception as e:
|
226 |
-
print(f"⚠️ Embeddings failed: {e}")
|
227 |
-
self.embedding_model = None
|
228 |
-
self.use_embeddings = False
|
229 |
-
|
230 |
-
def setup_biogpt(self):
|
231 |
-
"""Setup BioGPT model with better fallback strategy"""
|
232 |
-
print("🧠 Loading BioGPT model...")
|
233 |
-
|
234 |
-
# Try more stable models first
|
235 |
-
models_to_try = [
|
236 |
-
"microsoft/DialoGPT-medium", # Most stable conversational model
|
237 |
-
"microsoft/DialoGPT-small", # Smaller backup
|
238 |
-
"gpt2-medium", # General GPT-2 backup
|
239 |
-
"microsoft/BioGPT" # BioGPT if available
|
240 |
-
]
|
241 |
-
|
242 |
-
for model_name in models_to_try:
|
243 |
-
try:
|
244 |
-
print(f" Trying {model_name}...")
|
245 |
-
|
246 |
-
# Load tokenizer first
|
247 |
-
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
248 |
-
if self.tokenizer.pad_token is None:
|
249 |
-
self.tokenizer.pad_token = self.tokenizer.eos_token
|
250 |
-
|
251 |
-
# Load model with conservative settings
|
252 |
-
self.model = AutoModelForCausalLM.from_pretrained(
|
253 |
-
model_name,
|
254 |
-
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
255 |
-
device_map="auto" if self.device == "cuda" else None,
|
256 |
-
trust_remote_code=True,
|
257 |
-
low_cpu_mem_usage=True
|
258 |
-
)
|
259 |
-
|
260 |
-
if self.device == "cuda":
|
261 |
-
self.model = self.model.to(self.device)
|
262 |
-
|
263 |
-
print(f"✅ Successfully loaded {model_name}!")
|
264 |
-
self.model_name = model_name
|
265 |
-
return
|
266 |
-
|
267 |
-
except Exception as e:
|
268 |
-
print(f"❌ Failed to load {model_name}: {e}")
|
269 |
-
continue
|
270 |
-
|
271 |
-
# If all models fail - use rule-based fallback
|
272 |
-
print("❌ All models failed to load - using rule-based responses")
|
273 |
-
self.model = None
|
274 |
-
self.tokenizer = None
|
275 |
-
self.model_name = "Rule-based fallback"
|
276 |
-
|
277 |
-
def load_default_medical_knowledge(self):
|
278 |
-
"""Load comprehensive default medical knowledge base"""
|
279 |
-
default_knowledge = [
|
280 |
-
{
|
281 |
-
'id': 0,
|
282 |
-
'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.",
|
283 |
-
'medical_focus': 'pediatric_symptoms',
|
284 |
-
'source': 'default_knowledge'
|
285 |
-
},
|
286 |
-
{
|
287 |
-
'id': 1,
|
288 |
-
'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.",
|
289 |
-
'medical_focus': 'pediatric_symptoms',
|
290 |
-
'source': 'default_knowledge'
|
291 |
-
},
|
292 |
-
{
|
293 |
-
'id': 2,
|
294 |
-
'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.",
|
295 |
-
'medical_focus': 'pediatric_symptoms',
|
296 |
-
'source': 'default_knowledge'
|
297 |
-
},
|
298 |
-
{
|
299 |
-
'id': 3,
|
300 |
-
'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.",
|
301 |
-
'medical_focus': 'emergency',
|
302 |
-
'source': 'default_knowledge'
|
303 |
-
},
|
304 |
-
{
|
305 |
-
'id': 4,
|
306 |
-
'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.",
|
307 |
-
'medical_focus': 'prevention',
|
308 |
-
'source': 'default_knowledge'
|
309 |
-
},
|
310 |
-
{
|
311 |
-
'id': 5,
|
312 |
-
'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.",
|
313 |
-
'medical_focus': 'pediatric_symptoms',
|
314 |
-
'source': 'default_knowledge'
|
315 |
-
},
|
316 |
-
{
|
317 |
-
'id': 6,
|
318 |
-
'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.",
|
319 |
-
'medical_focus': 'pediatric_pulmonology',
|
320 |
-
'source': 'default_knowledge'
|
321 |
-
},
|
322 |
-
{
|
323 |
-
'id': 7,
|
324 |
-
'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.",
|
325 |
-
'medical_focus': 'pediatric_pulmonology',
|
326 |
-
'source': 'default_knowledge'
|
327 |
-
},
|
328 |
-
{
|
329 |
-
'id': 8,
|
330 |
-
'text': "Pneumonia in children is an infection that inflames air sacs in one or both lungs, which may fill with fluid. It can be caused by bacteria, viruses, or fungi. Symptoms include cough with phlegm, fever, chills, and difficulty breathing. Bacterial pneumonia often requires antibiotic treatment, while viral pneumonia typically resolves with supportive care. Seek medical attention for persistent fever, difficulty breathing, or worsening symptoms.",
|
331 |
-
'medical_focus': 'pediatric_pulmonology',
|
332 |
-
'source': 'default_knowledge'
|
333 |
-
},
|
334 |
-
{
|
335 |
-
'id': 9,
|
336 |
-
'text': "Croup is a respiratory condition that causes swelling around the vocal cords. It's most common in children between 6 months and 6 years old. Symptoms include a distinctive barking cough, stridor (harsh sound when breathing in), hoarse voice, and difficulty breathing. Most cases are mild and can be treated at home with humidified air and staying calm. Severe cases with significant breathing difficulty require immediate medical attention.",
|
337 |
-
'medical_focus': 'pediatric_pulmonology',
|
338 |
-
'source': 'default_knowledge'
|
339 |
-
}
|
340 |
-
]
|
341 |
-
|
342 |
-
self.knowledge_chunks = default_knowledge
|
343 |
-
print(f"📚 Loaded {len(default_knowledge)} default medical knowledge chunks")
|
344 |
-
|
345 |
-
def retrieve_medical_context(self, query: str, n_results: int = 3) -> List[str]:
|
346 |
-
"""Retrieve relevant medical context using improved keyword search with pulmonology priority"""
|
347 |
-
if not self.knowledge_chunks:
|
348 |
-
return []
|
349 |
-
|
350 |
-
query_lower = query.lower()
|
351 |
-
query_words = set(query_lower.split())
|
352 |
-
chunk_scores = []
|
353 |
-
|
354 |
-
# Enhanced medical keyword mapping with pulmonology emphasis
|
355 |
-
medical_keywords = {
|
356 |
-
'pulmonology': ['asthma', 'pneumonia', 'bronchiolitis', 'croup', 'respiratory', 'lung', 'airway', 'breathing', 'cough', 'wheeze', 'stridor'],
|
357 |
-
'fever': ['fever', 'temperature', 'hot', 'warm', 'burning'],
|
358 |
-
'stomach': ['stomach', 'abdominal', 'belly', 'tummy', 'pain', 'ache'],
|
359 |
-
'rash': ['rash', 'skin', 'red', 'spots', 'bumps', 'itchy'],
|
360 |
-
'vomiting': ['vomit', 'vomiting', 'throw up', 'sick', 'nausea'],
|
361 |
-
'diarrhea': ['diarrhea', 'loose', 'stool', 'bowel', 'poop'],
|
362 |
-
'dehydration': ['dehydration', 'dehydrated', 'fluids', 'water', 'thirsty'],
|
363 |
-
'emergency': ['emergency', 'urgent', 'serious', 'severe', 'hospital', 'doctor']
|
364 |
-
}
|
365 |
-
|
366 |
-
# Expand query with related medical terms
|
367 |
-
expanded_query_words = set(query_words)
|
368 |
-
for medical_term, synonyms in medical_keywords.items():
|
369 |
-
if any(word in query_lower for word in synonyms):
|
370 |
-
expanded_query_words.update(synonyms)
|
371 |
-
|
372 |
-
for chunk_info in self.knowledge_chunks:
|
373 |
-
chunk_text = chunk_info['text'].lower()
|
374 |
-
|
375 |
-
# Calculate relevance score with expanded terms
|
376 |
-
word_overlap = sum(1 for word in expanded_query_words if word in chunk_text)
|
377 |
-
base_score = word_overlap / len(expanded_query_words) if expanded_query_words else 0
|
378 |
-
|
379 |
-
# Strong boost for pulmonology content
|
380 |
-
medical_boost = 0
|
381 |
-
medical_focus = chunk_info.get('medical_focus', '')
|
382 |
-
source = chunk_info.get('source', '')
|
383 |
-
|
384 |
-
if medical_focus == 'pediatric_pulmonology':
|
385 |
-
medical_boost = 0.8 # Highest priority for pulmonology
|
386 |
-
elif source == 'pediatric_pulmonology_file':
|
387 |
-
medical_boost = 0.7 # High priority for your uploaded data
|
388 |
-
elif medical_focus == 'emergency':
|
389 |
-
medical_boost = 0.4
|
390 |
-
elif medical_focus in ['treatments', 'diagnosis']:
|
391 |
-
medical_boost = 0.3
|
392 |
-
elif medical_focus == 'pediatric_symptoms':
|
393 |
-
medical_boost = 0.5
|
394 |
-
|
395 |
-
final_score = base_score + medical_boost
|
396 |
-
|
397 |
-
if final_score > 0:
|
398 |
-
chunk_scores.append((final_score, chunk_info['text']))
|
399 |
-
|
400 |
-
# Return top matches - prioritize pulmonology content
|
401 |
-
chunk_scores.sort(reverse=True)
|
402 |
-
results = [chunk for _, chunk in chunk_scores[:n_results]]
|
403 |
-
|
404 |
-
# If no good matches, return some default medical chunks
|
405 |
-
if not results:
|
406 |
-
results = [chunk['text'] for chunk in self.knowledge_chunks[:2]]
|
407 |
-
|
408 |
-
return results
|
409 |
-
|
410 |
-
def generate_biogpt_response(self, context: str, query: str) -> str:
|
411 |
-
"""Generate medical response - prioritizing rule-based for reliability"""
|
412 |
-
|
413 |
-
# First try rule-based response using context - this is more reliable
|
414 |
-
rule_based_response = self.generate_rule_based_response(context, query)
|
415 |
-
|
416 |
-
# If rule-based gives a good response, use it
|
417 |
-
if len(rule_based_response) > 50 and not "consult with a qualified healthcare provider" in rule_based_response:
|
418 |
-
print("✅ Using rule-based response (reliable)")
|
419 |
-
return rule_based_response
|
420 |
-
|
421 |
-
# Only try AI model if rule-based didn't work and model is available
|
422 |
-
if self.model and self.tokenizer:
|
423 |
-
try:
|
424 |
-
print("🤖 Trying AI model generation...")
|
425 |
-
|
426 |
-
# Create medical prompt
|
427 |
-
prompt = f"Medical Question: {query}\n\nMedical Information: {context[:300]}\n\nAnswer:"
|
428 |
-
|
429 |
-
# Tokenize with conservative limits
|
430 |
-
inputs = self.tokenizer(
|
431 |
-
prompt,
|
432 |
-
return_tensors="pt",
|
433 |
-
truncation=True,
|
434 |
-
max_length=400,
|
435 |
-
padding=True
|
436 |
-
)
|
437 |
-
|
438 |
-
# Move to device
|
439 |
-
if self.device == "cuda":
|
440 |
-
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
441 |
-
|
442 |
-
# Generate with conservative settings
|
443 |
-
with torch.no_grad():
|
444 |
-
outputs = self.model.generate(
|
445 |
-
**inputs,
|
446 |
-
max_new_tokens=80,
|
447 |
-
do_sample=True,
|
448 |
-
temperature=0.2, # Very low temperature
|
449 |
-
top_p=0.7,
|
450 |
-
top_k=20,
|
451 |
-
pad_token_id=self.tokenizer.eos_token_id,
|
452 |
-
eos_token_id=self.tokenizer.eos_token_id,
|
453 |
-
repetition_penalty=1.2,
|
454 |
-
early_stopping=True
|
455 |
-
)
|
456 |
-
|
457 |
-
# Decode response
|
458 |
-
full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
459 |
-
|
460 |
-
# Extract generated part
|
461 |
-
if "Answer:" in full_response:
|
462 |
-
generated_response = full_response.split("Answer:")[-1].strip()
|
463 |
-
else:
|
464 |
-
generated_response = full_response[len(prompt):].strip()
|
465 |
-
|
466 |
-
# Simple cleaning
|
467 |
-
cleaned_response = self.simple_clean_response(generated_response)
|
468 |
-
|
469 |
-
# Validate AI response quality
|
470 |
-
if (len(cleaned_response.strip()) > 30 and
|
471 |
-
self.validate_medical_response(cleaned_response, query)):
|
472 |
-
print("✅ Using AI model response")
|
473 |
-
return cleaned_response
|
474 |
-
else:
|
475 |
-
print("⚠️ AI response quality check failed, using rule-based")
|
476 |
-
return rule_based_response
|
477 |
-
|
478 |
-
except Exception as e:
|
479 |
-
print(f"⚠️ Model generation failed: {e}")
|
480 |
-
|
481 |
-
# Final fallback
|
482 |
-
print("📋 Using rule-based fallback")
|
483 |
-
return rule_based_response
|
484 |
-
|
485 |
-
def simple_clean_response(self, response: str) -> str:
|
486 |
-
"""Simple, less aggressive cleaning"""
|
487 |
-
# Remove obvious artifacts
|
488 |
-
response = re.sub(r'<[^>]*>', '', response)
|
489 |
-
response = re.sub(r'[▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟]', '', response)
|
490 |
-
|
491 |
-
# Clean whitespace
|
492 |
-
response = re.sub(r'\s+', ' ', response).strip()
|
493 |
-
|
494 |
-
# Take first reasonable sentences
|
495 |
-
sentences = re.split(r'[.!?]+', response)
|
496 |
-
good_sentences = []
|
497 |
-
|
498 |
-
for sentence in sentences:
|
499 |
-
sentence = sentence.strip()
|
500 |
-
if len(sentence) > 5 and len(sentence.split()) >= 2:
|
501 |
-
good_sentences.append(sentence)
|
502 |
-
if len(good_sentences) >= 2: # Max 2 sentences
|
503 |
-
break
|
504 |
-
|
505 |
-
if good_sentences:
|
506 |
-
result = '. '.join(good_sentences)
|
507 |
-
if not result.endswith('.'):
|
508 |
-
result += '.'
|
509 |
-
return result
|
510 |
-
|
511 |
-
return response[:200] if response else ""
|
512 |
-
|
513 |
-
def validate_medical_response(self, response: str, query: str) -> bool:
|
514 |
-
"""Validate that AI response is relevant to the query"""
|
515 |
-
response_lower = response.lower()
|
516 |
-
query_lower = query.lower()
|
517 |
-
|
518 |
-
# Extract key medical terms from query
|
519 |
-
query_words = set(query_lower.split())
|
520 |
-
medical_terms = ['asthma', 'pneumonia', 'cough', 'fever', 'breathing', 'bronchiolitis', 'croup']
|
521 |
-
|
522 |
-
query_medical_terms = [term for term in medical_terms if term in query_lower]
|
523 |
-
|
524 |
-
# Check if response mentions the same medical condition
|
525 |
-
if query_medical_terms:
|
526 |
-
for term in query_medical_terms:
|
527 |
-
if term in response_lower:
|
528 |
-
return True
|
529 |
-
return False # Response doesn't mention the queried condition
|
530 |
-
|
531 |
-
# For general queries, check for reasonable medical content
|
532 |
-
medical_keywords = ['symptoms', 'treatment', 'children', 'medical', 'doctor', 'healthcare']
|
533 |
-
return any(keyword in response_lower for keyword in medical_keywords)
|
534 |
-
|
535 |
-
def generate_rule_based_response(self, context: str, query: str) -> str:
|
536 |
-
"""Generate rule-based medical response when model fails or for reliability"""
|
537 |
-
query_lower = query.lower()
|
538 |
-
|
539 |
-
# Priority 1: Use context directly if it's highly relevant
|
540 |
-
if context:
|
541 |
-
context_sentences = [s.strip() for s in context.split('.') if len(s.strip()) > 15]
|
542 |
-
|
543 |
-
# Find sentences that match the query topic
|
544 |
-
relevant_sentences = []
|
545 |
-
query_words = set(query_lower.split())
|
546 |
-
|
547 |
-
for sentence in context_sentences:
|
548 |
-
sentence_words = set(sentence.lower().split())
|
549 |
-
overlap = len(query_words.intersection(sentence_words))
|
550 |
-
if overlap > 0:
|
551 |
-
relevant_sentences.append((overlap, sentence))
|
552 |
-
|
553 |
-
# Sort by relevance and take top sentences
|
554 |
-
relevant_sentences.sort(reverse=True, key=lambda x: x[0])
|
555 |
-
|
556 |
-
if relevant_sentences:
|
557 |
-
# Use top 2 most relevant sentences
|
558 |
-
response_sentences = [sent[1] for sent in relevant_sentences[:2]]
|
559 |
-
response = '. '.join(response_sentences)
|
560 |
-
if not response.endswith('.'):
|
561 |
-
response += '.'
|
562 |
-
|
563 |
-
# Add appropriate medical advice
|
564 |
-
if any(word in query_lower for word in ['emergency', 'urgent', 'severe', 'serious']):
|
565 |
-
response += " If symptoms are severe or concerning, seek immediate medical attention."
|
566 |
-
elif any(word in query_lower for word in ['treatment', 'medicine', 'medication']):
|
567 |
-
response += " Always follow your healthcare provider's treatment recommendations."
|
568 |
-
else:
|
569 |
-
response += " Consult with a healthcare provider for personalized medical advice."
|
570 |
-
|
571 |
-
return response
|
572 |
-
|
573 |
-
# Priority 2: Specific keyword-based responses (enhanced)
|
574 |
-
keyword_responses = {
|
575 |
-
'asthma': "Asthma in children is a chronic respiratory condition that causes the airways to become inflamed, narrow, and produce excess mucus. Common symptoms include wheezing, cough (especially at night), shortness of breath, and chest tightness. Triggers can include viral infections, allergens like dust mites and pollen, irritants such as smoke, cold air, and exercise. Management typically involves avoiding known triggers, using prescribed medications like rescue inhalers for acute symptoms and controller medications for long-term management, and having an asthma action plan. Regular follow-up with healthcare providers is essential for optimal asthma control.",
|
576 |
-
|
577 |
-
'pneumonia': "Pneumonia is a lung infection that causes inflammation in the air sacs of one or both lungs, which may fill with fluid or pus. In children, it can be caused by bacteria, viruses, or other organisms. Common symptoms include cough with phlegm, fever, chills, difficulty breathing, chest pain, and fatigue. Bacterial pneumonia typically requires antibiotic treatment, while viral pneumonia is managed with supportive care including rest, fluids, and fever management. Seek medical evaluation promptly for persistent fever, difficulty breathing, or worsening symptoms.",
|
578 |
-
|
579 |
-
'bronchiolitis': "Bronchiolitis is a common respiratory infection in infants and young children under 2 years old, usually caused by viruses like respiratory syncytial virus (RSV). It affects the small airways (bronchioles) in the lungs, causing inflammation and mucus buildup. Symptoms typically start like a cold with runny nose and cough, then may progress to difficulty breathing, wheezing, and feeding problems. Most cases are mild and resolve with supportive care at home, but some children may need hospitalization for breathing support and monitoring.",
|
580 |
-
|
581 |
-
'croup': "Croup is a respiratory condition that causes swelling around the vocal cords and windpipe. It's most common in children between 6 months and 6 years old. The hallmark symptom is a distinctive barking cough that sounds like a seal, along with stridor (harsh sound when breathing in), hoarse voice, and sometimes difficulty breathing. Most cases are mild and can be managed at home with humidified air and keeping the child calm. However, seek immediate medical care if breathing becomes severely difficult or if stridor is present at rest.",
|
582 |
-
|
583 |
-
'cough': "Cough in children can have many different causes including viral upper respiratory infections (most common), asthma, allergies, bacterial infections, or environmental irritants. Most coughs from common colds resolve within 2-3 weeks without specific treatment. However, seek medical evaluation for coughs that persist longer than 3 weeks, are accompanied by high fever, produce blood, cause significant difficulty breathing, or severely interfere with sleep and daily activities. Treatment depends on identifying and addressing the underlying cause.",
|
584 |
-
|
585 |
-
'fever': "Fever in children is usually a sign that the body is fighting an infection, most commonly viral. Normal body temperature ranges from 97°F to 100.4°F (36.1°C to 38°C), with fever generally defined as a temperature above 100.4°F (38°C). Most fevers are not dangerous and actually help the immune system fight infection. Treatment focuses on comfort measures including adequate rest, increased fluid intake, and appropriate fever reducers like acetaminophen or ibuprofen if needed. Seek medical care for very high fevers over 104°F (40°C), fevers in infants under 3 months old, or if the child appears very ill.",
|
586 |
-
|
587 |
-
'breathing': "Breathing difficulties in children can range from mild to severe and have various causes including asthma, respiratory infections, allergies, or airway obstruction. Signs of respiratory distress include rapid breathing, difficulty speaking in full sentences, use of extra muscles to breathe (retractions), wheezing, or blue coloring around the lips or fingernails. Mild breathing difficulties may be managed with prescribed medications like inhalers, but severe breathing problems require immediate medical attention. Always seek emergency care if a child cannot breathe comfortably or appears to be struggling significantly.",
|
588 |
-
|
589 |
-
'wheeze': "Wheezing is a high-pitched whistling sound that occurs when breathing, usually more noticeable when exhaling. In children, it's commonly caused by asthma, but can also result from respiratory infections, allergies, or airway inflammation. Wheezing indicates narrowed or partially blocked airways. Treatment depends on the underlying cause and may include bronchodilator medications (rescue inhalers), anti-inflammatory medications, or treatment of underlying infections. Persistent or severe wheezing should be evaluated by a healthcare provider."
|
590 |
-
}
|
591 |
-
|
592 |
-
# Check for keyword matches with partial matching
|
593 |
-
for keyword, response in keyword_responses.items():
|
594 |
-
if keyword in query_lower or any(keyword in word for word in query_lower.split()):
|
595 |
-
return response
|
596 |
-
|
597 |
-
# Priority 3: General topic-based responses
|
598 |
-
if any(word in query_lower for word in ['child', 'children', 'pediatric', 'baby', 'infant']):
|
599 |
-
if any(word in query_lower for word in ['sick', 'illness', 'disease', 'condition']):
|
600 |
-
return "When children become ill, it's important to monitor their symptoms carefully and provide appropriate care. Common childhood illnesses include respiratory infections, gastrointestinal issues, and fever-related conditions. Most childhood illnesses are mild and resolve with supportive care, but some may require medical evaluation. Always consult with a pediatric healthcare provider when you're concerned about your child's health, especially for persistent symptoms, high fevers, or any signs of serious illness."
|
601 |
-
|
602 |
-
# Priority 4: Generic medical response
|
603 |
-
return "For specific medical concerns about your child's health, it's important to consult with a qualified pediatric healthcare provider. They can properly evaluate your child's individual situation, provide accurate diagnosis, and recommend appropriate treatment based on their specific symptoms, medical history, and current condition. If you're dealing with urgent symptoms or have immediate concerns about your child's breathing, fever, or overall condition, don't hesitate to seek prompt medical attention."
|
604 |
-
|
605 |
-
def handle_conversational_interactions(self, query: str) -> Optional[str]:
|
606 |
-
"""Handle conversational interactions"""
|
607 |
-
query_lower = query.lower().strip()
|
608 |
-
|
609 |
-
# Greeting patterns
|
610 |
-
exact_greetings = [
|
611 |
-
'hello', 'hi', 'hey', 'good morning', 'good afternoon',
|
612 |
-
'good evening', 'how are you', 'how are you doing'
|
613 |
-
]
|
614 |
-
|
615 |
-
if query_lower in exact_greetings:
|
616 |
-
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?"
|
617 |
-
|
618 |
-
# Thanks patterns
|
619 |
-
thanks_only = ['thank you', 'thanks', 'thank you so much', 'thanks a lot']
|
620 |
-
if query_lower in thanks_only:
|
621 |
-
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!"
|
622 |
-
|
623 |
-
# Help patterns
|
624 |
-
help_only = ['help', 'what can you do', 'what are you', 'who are you']
|
625 |
-
if query_lower in help_only:
|
626 |
-
return """🤖 **About BioGPT Pediatric Pulmonology Assistant**
|
627 |
-
|
628 |
-
I'm an AI medical assistant powered by BioGPT, specialized in pediatric pulmonology and respiratory medicine. I can help with:
|
629 |
-
|
630 |
-
🫁 **Pediatric Pulmonology:**
|
631 |
-
• Asthma, bronchiolitis, pneumonia, croup
|
632 |
-
• Respiratory symptoms and breathing difficulties
|
633 |
-
• Treatment guidance and management
|
634 |
-
• When to seek medical care
|
635 |
-
|
636 |
-
⚠️ **Important:** I provide educational information only. Always consult healthcare professionals for medical decisions."""
|
637 |
-
|
638 |
-
return None
|
639 |
-
|
640 |
-
def chat_interface(self, message: str, history: List[List[str]]) -> str:
|
641 |
-
"""Main chat interface for Gradio"""
|
642 |
-
if not message.strip():
|
643 |
-
return "Hello! I'm BioGPT, your pediatric pulmonology AI assistant. How can I help you with children's respiratory health today?"
|
644 |
-
|
645 |
-
print(f"🔍 Processing query: '{message}'")
|
646 |
-
|
647 |
-
# Handle conversational interactions
|
648 |
-
conversational_response = self.handle_conversational_interactions(message)
|
649 |
-
if conversational_response:
|
650 |
-
print(" Handled as conversational")
|
651 |
-
return conversational_response
|
652 |
-
|
653 |
-
print(" Processing as medical query")
|
654 |
-
|
655 |
-
# Process as medical query
|
656 |
-
context = self.retrieve_medical_context(message)
|
657 |
-
|
658 |
-
if not context:
|
659 |
-
return f"""🩺 **Medical Query:** {message}
|
660 |
-
|
661 |
-
⚠️ I don't have specific information about this topic in my current medical database. However, I recommend:
|
662 |
-
|
663 |
-
1. **Consult Healthcare Provider**: For personalized medical advice
|
664 |
-
2. **Emergency Signs**: If symptoms are severe, seek immediate care
|
665 |
-
3. **Pediatric Specialist**: For specialized concerns
|
666 |
-
|
667 |
-
**For urgent medical concerns, contact your healthcare provider or emergency services.**
|
668 |
-
|
669 |
-
💡 **Try asking about**: asthma, breathing difficulties, cough, pneumonia, or other respiratory symptoms."""
|
670 |
-
|
671 |
-
# Generate medical response
|
672 |
-
main_context = '\n\n'.join(context)
|
673 |
-
response = self.generate_biogpt_response(main_context, message)
|
674 |
-
|
675 |
-
# Format as medical response
|
676 |
-
final_response = f"🩺 **BioGPT Medical Assistant:** {response}\n\n⚠️ **Important:** This information is for educational purposes only. Always consult qualified healthcare professionals for medical diagnosis, treatment, and personalized advice."
|
677 |
-
|
678 |
-
return final_response
|
679 |
-
|
680 |
-
def get_knowledge_stats(self) -> Dict:
|
681 |
-
"""Get statistics about loaded knowledge"""
|
682 |
-
if not self.knowledge_chunks:
|
683 |
-
return {"total_chunks": 0}
|
684 |
-
|
685 |
-
stats = {
|
686 |
-
"total_chunks": len(self.knowledge_chunks),
|
687 |
-
"default_knowledge": len([c for c in self.knowledge_chunks if c.get('source') == 'default_knowledge']),
|
688 |
-
"pulmonology_file_data": len([c for c in self.knowledge_chunks if c.get('source') == 'pediatric_pulmonology_file']),
|
689 |
-
"pulmonology_focused": len([c for c in self.knowledge_chunks if c.get('medical_focus') == 'pediatric_pulmonology']),
|
690 |
-
"model_used": getattr(self, 'model_name', 'Unknown')
|
691 |
-
}
|
692 |
-
return stats
|
693 |
-
|
694 |
-
# Test function
|
695 |
-
def test_chatbot_responses():
|
696 |
-
"""Test the chatbot with various queries"""
|
697 |
-
print("\n🧪 Testing BioGPT Pediatric Pulmonology Chatbot...")
|
698 |
-
print("=" * 50)
|
699 |
-
|
700 |
-
test_queries = [
|
701 |
-
"hello",
|
702 |
-
"what is asthma in children",
|
703 |
-
"my child has breathing difficulties",
|
704 |
-
"help",
|
705 |
-
"treatment for pediatric pneumonia",
|
706 |
-
"thank you"
|
707 |
-
]
|
708 |
-
|
709 |
-
for query in test_queries:
|
710 |
-
print(f"\n🔍 Query: '{query}'")
|
711 |
-
response = chatbot.chat_interface(query, [])
|
712 |
-
response_type = 'CONVERSATIONAL' if any(word in response for word in ['Hello!', 'welcome!', 'About BioGPT']) else 'MEDICAL'
|
713 |
-
print(f"🤖 Response type: {response_type}")
|
714 |
-
print(f"📝 Response: {response[:100]}...")
|
715 |
-
print("-" * 30)
|
716 |
-
|
717 |
-
# Initialize the chatbot globally
|
718 |
-
print("🚀 Initializing BioGPT Pediatric Pulmonology Chatbot...")
|
719 |
-
chatbot = BioGPTMedicalChatbot()
|
720 |
-
|
721 |
-
# Show knowledge statistics
|
722 |
-
print("\n📊 Knowledge Base Statistics:")
|
723 |
-
stats = chatbot.get_knowledge_stats()
|
724 |
-
for key, value in stats.items():
|
725 |
-
print(f" {key}: {value}")
|
726 |
-
|
727 |
-
# Run tests
|
728 |
-
test_chatbot_responses()
|
729 |
-
|
730 |
-
def create_gradio_interface():
|
731 |
-
"""Create Gradio chat interface"""
|
732 |
-
|
733 |
-
# Get current knowledge stats for display
|
734 |
-
stats = chatbot.get_knowledge_stats()
|
735 |
-
|
736 |
-
# Custom CSS for medical theme
|
737 |
-
css = """
|
738 |
-
.gradio-container {
|
739 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
740 |
-
}
|
741 |
-
.chat-message {
|
742 |
-
background-color: #f8f9fa;
|
743 |
-
border-radius: 10px;
|
744 |
-
padding: 10px;
|
745 |
-
margin: 5px;
|
746 |
-
}
|
747 |
-
"""
|
748 |
-
|
749 |
-
with gr.Blocks(
|
750 |
-
css=css,
|
751 |
-
title="BioGPT Pediatric Pulmonology Assistant",
|
752 |
-
theme=gr.themes.Soft()
|
753 |
-
) as demo:
|
754 |
-
|
755 |
-
# Header
|
756 |
-
gr.HTML(f"""
|
757 |
-
<div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea, #764ba2); color: white; border-radius: 10px; margin-bottom: 20px;">
|
758 |
-
<h1>🫁 BioGPT Pediatric Pulmonology Assistant</h1>
|
759 |
-
<p>Specialized AI Medical Chatbot for Children's Respiratory Health</p>
|
760 |
-
<p><strong>Powered by BioGPT | {stats['total_chunks']} Medical Knowledge Chunks Loaded</strong></p>
|
761 |
-
<p><small>Model: {stats['model_used']} | Pulmonology Data: {stats['pulmonology_file_data']} chunks</small></p>
|
762 |
-
</div>
|
763 |
-
""")
|
764 |
-
|
765 |
-
# Important disclaimer
|
766 |
-
gr.HTML("""
|
767 |
-
<div style="background-color: #fff3cd; border: 1px solid #ffeaa7; border-radius: 8px; padding: 15px; margin-bottom: 20px;">
|
768 |
-
<h3 style="color: #856404; margin-top: 0;">⚠️ Medical Disclaimer</h3>
|
769 |
-
<p style="color: #856404; margin-bottom: 0;">
|
770 |
-
This AI provides educational pediatric pulmonology information only and is NOT a substitute for professional medical advice,
|
771 |
-
diagnosis, or treatment. Always consult qualified healthcare providers for medical decisions.
|
772 |
-
<strong>In case of respiratory emergency, call emergency services immediately.</strong>
|
773 |
-
</p>
|
774 |
-
</div>
|
775 |
-
""")
|
776 |
-
|
777 |
-
# Chat interface
|
778 |
-
chatbot_interface = gr.ChatInterface(
|
779 |
-
fn=chatbot.chat_interface,
|
780 |
-
title="💬 Chat with BioGPT Pulmonology Assistant",
|
781 |
-
description="Ask me about pediatric respiratory health, asthma, breathing difficulties, and pulmonology treatments.",
|
782 |
-
examples=[
|
783 |
-
"What is asthma in children?",
|
784 |
-
"My child has a persistent cough, what should I do?",
|
785 |
-
"How is bronchiolitis treated in infants?",
|
786 |
-
"When should I be worried about my child's breathing?",
|
787 |
-
"What are the signs of pneumonia in children?",
|
788 |
-
"How can I prevent respiratory infections?"
|
789 |
-
],
|
790 |
-
retry_btn=None,
|
791 |
-
undo_btn=None,
|
792 |
-
clear_btn="🗑️ Clear Chat",
|
793 |
-
submit_btn="🫁 Ask BioGPT",
|
794 |
-
chatbot=gr.Chatbot(
|
795 |
-
height=500,
|
796 |
-
placeholder="<div style='text-align: center; color: #666;'>Start a conversation with BioGPT Pediatric Pulmonology Assistant</div>",
|
797 |
-
show_copy_button=True,
|
798 |
-
bubble_full_width=False
|
799 |
-
)
|
800 |
-
)
|
801 |
-
|
802 |
-
# Information tabs
|
803 |
-
with gr.Tabs():
|
804 |
-
with gr.Tab("ℹ️ About"):
|
805 |
-
gr.Markdown(f"""
|
806 |
-
## About BioGPT Pediatric Pulmonology Assistant
|
807 |
-
|
808 |
-
This AI assistant is powered by **BioGPT**, specialized for pediatric pulmonology and respiratory medicine.
|
809 |
-
|
810 |
-
### 🎯 Current Knowledge Base:
|
811 |
-
- **Total Chunks**: {stats['total_chunks']}
|
812 |
-
- **Default Medical Knowledge**: {stats['default_knowledge']} chunks
|
813 |
-
- **Pulmonology File Data**: {stats['pulmonology_file_data']} chunks
|
814 |
-
- **Pulmonology Focus**: {stats['pulmonology_focused']} chunks
|
815 |
-
- **Model**: {stats['model_used']}
|
816 |
-
|
817 |
-
### 🫁 Specializations:
|
818 |
-
- **Pediatric Asthma**: Diagnosis, treatment, management
|
819 |
-
- **Respiratory Infections**: Pneumonia, bronchiolitis, croup
|
820 |
-
- **Breathing Difficulties**: Assessment and guidance
|
821 |
-
- **Chronic Respiratory Conditions**: Long-term management
|
822 |
-
- **Emergency Respiratory Care**: When to seek immediate help
|
823 |
-
|
824 |
-
### 🔧 Technical Features:
|
825 |
-
- **Model**: Microsoft BioGPT (Medical AI) with fallback systems
|
826 |
-
- **Auto-Loading**: Automatically loads your pulmonology data file
|
827 |
-
- **Smart Retrieval**: Prioritizes pulmonology content
|
828 |
-
- **Rule-Based Fallback**: Ensures reliable responses even if AI model fails
|
829 |
-
|
830 |
-
### 📱 How to Use:
|
831 |
-
1. Type your pediatric respiratory question
|
832 |
-
2. Be specific about symptoms or conditions
|
833 |
-
3. Ask about treatments, diagnosis, or management
|
834 |
-
4. Request guidance on when to seek care
|
835 |
-
""")
|
836 |
-
|
837 |
-
with gr.Tab("🫁 Pulmonology Topics"):
|
838 |
-
gr.Markdown("""
|
839 |
-
## Pediatric Pulmonology Coverage
|
840 |
-
|
841 |
-
### 🔴 Common Respiratory Conditions:
|
842 |
-
- **Asthma**: Triggers, symptoms, management, action plans
|
843 |
-
- **Bronchiolitis**: RSV, treatment, when to hospitalize
|
844 |
-
- **Pneumonia**: Bacterial vs viral, antibiotics, recovery
|
845 |
-
- **Croup**: Barking cough, stridor, home treatment
|
846 |
-
- **Bronchitis**: Acute vs chronic, treatment approaches
|
847 |
-
|
848 |
-
### 🟡 Respiratory Symptoms:
|
849 |
-
- **Cough**: Persistent, productive, dry, nocturnal
|
850 |
-
- **Wheezing**: Causes, assessment, treatment
|
851 |
-
- **Shortness of Breath**: Evaluation and management
|
852 |
-
- **Chest Pain**: When concerning in children
|
853 |
-
- **Stridor**: Upper airway obstruction signs
|
854 |
-
|
855 |
-
### 🟢 Diagnostic & Treatment:
|
856 |
-
- **Pulmonary Function Tests**: When appropriate
|
857 |
-
- **Imaging**: X-rays, CT scans for respiratory issues
|
858 |
-
- **Medications**: Bronchodilators, steroids, antibiotics
|
859 |
-
- **Oxygen Therapy**: Indications and monitoring
|
860 |
-
- **Respiratory Support**: CPAP, ventilation considerations
|
861 |
-
|
862 |
-
### 🔵 Prevention & Management:
|
863 |
-
- **Trigger Avoidance**: Environmental controls
|
864 |
-
- **Vaccination**: Respiratory disease prevention
|
865 |
-
- **Exercise Guidelines**: For children with respiratory conditions
|
866 |
-
- **School Management**: Asthma action plans, inhaler use
|
867 |
-
""")
|
868 |
-
|
869 |
-
with gr.Tab("⚠️ Emergency & Safety"):
|
870 |
-
gr.Markdown("""
|
871 |
-
## Respiratory Emergency Guidance
|
872 |
-
|
873 |
-
### 🚨 CALL EMERGENCY SERVICES IMMEDIATELY:
|
874 |
-
- **Severe Breathing Difficulty**: Cannot speak in full sentences
|
875 |
-
- **Blue Lips or Fingernails**: Cyanosis indicating oxygen deprivation
|
876 |
-
- **Severe Wheezing**: With significant distress
|
877 |
-
- **Stridor at Rest**: High-pitched breathing sound
|
878 |
-
- **Unconsciousness**: Related to breathing problems
|
879 |
-
- **Severe Chest Retractions**: Pulling in around ribs/sternum
|
880 |
-
|
881 |
-
### 🏥 SEEK IMMEDIATE MEDICAL CARE:
|
882 |
-
- **Persistent High Fever**: >104°F (40°C) with respiratory symptoms
|
883 |
-
- **Worsening Symptoms**: Despite treatment
|
884 |
-
- **Dehydration Signs**: With respiratory illness
|
885 |
-
- **Significant Behavior Changes**: Extreme lethargy, irritability
|
886 |
-
- **Inhaler Not Helping**: Asthma symptoms not responding
|
887 |
-
|
888 |
-
### 📞 CONTACT HEALTHCARE PROVIDER:
|
889 |
-
- **New Respiratory Symptoms**: Lasting more than a few days
|
890 |
-
- **Chronic Cough**: Persisting beyond 2-3 weeks
|
891 |
-
- **Asthma Questions**: About medications or management
|
892 |
-
- **Fever with Cough**: Especially if productive
|
893 |
-
- **Exercise Limitations**: Due to breathing difficulties
|
894 |
-
|
895 |
-
### 🏠 HOME MONITORING:
|
896 |
-
- **Respiratory Rate**: Normal ranges by age
|
897 |
-
- **Oxygen Saturation**: If pulse oximeter available
|
898 |
-
- **Peak Flow**: For children with asthma
|
899 |
-
- **Symptom Tracking**: Using asthma diaries or apps
|
900 |
-
""")
|
901 |
-
|
902 |
-
with gr.Tab("📁 Data Information"):
|
903 |
-
gr.Markdown(f"""
|
904 |
-
## Knowledge Base Status
|
905 |
-
|
906 |
-
### 📊 Current Data Loaded:
|
907 |
-
- **Total Medical Chunks**: {stats['total_chunks']}
|
908 |
-
- **Default Knowledge**: {stats['default_knowledge']} chunks
|
909 |
-
- **Your Pulmonology File**: {stats['pulmonology_file_data']} chunks
|
910 |
-
- **Pulmonology Focused**: {stats['pulmonology_focused']} chunks
|
911 |
-
- **AI Model**: {stats['model_used']}
|
912 |
-
|
913 |
-
### 📝 How Your Data Is Used:
|
914 |
-
1. **Auto-Detection**: System automatically looks for 'Pediatric_cleaned.txt'
|
915 |
-
2. **Smart Processing**: Breaks your file into medical chunks
|
916 |
-
3. **Priority Ranking**: Pulmonology content gets highest priority
|
917 |
-
4. **Context Retrieval**: Relevant chunks are used to answer questions
|
918 |
-
5. **Fallback Systems**: Multiple layers ensure reliable responses
|
919 |
-
|
920 |
-
### 📂 Expected File Formats:
|
921 |
-
- **Filename**: 'Pediatric_cleaned.txt' (case variations accepted)
|
922 |
-
- **Content**: Plain text with medical information
|
923 |
-
- **Structure**: Paragraphs, sections, or bullet points
|
924 |
-
- **Focus**: Pediatric pulmonology and respiratory medicine
|
925 |
-
|
926 |
-
### 🔄 Upload Instructions:
|
927 |
-
1. Go to your Hugging Face Space
|
928 |
-
2. Click "Files" tab
|
929 |
-
3. Upload your 'Pediatric_cleaned.txt' file
|
930 |
-
4. Restart the Space to reload data
|
931 |
-
|
932 |
-
{"✅ **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."}
|
933 |
-
|
934 |
-
### 🛡️ Reliability Features:
|
935 |
-
- **Multiple Model Support**: Tries BioGPT, DialoGPT, GPT-2 in order
|
936 |
-
- **Rule-Based Fallback**: If all AI models fail, uses knowledge-based responses
|
937 |
-
- **Conservative Generation**: Prevents problematic outputs
|
938 |
-
- **Medical Context Priority**: Always uses your medical knowledge first
|
939 |
-
""")
|
940 |
-
|
941 |
-
# Footer
|
942 |
-
gr.HTML("""
|
943 |
-
<div style="text-align: center; padding: 20px; margin-top: 30px; border-top: 1px solid #ddd; color: #666;">
|
944 |
-
<p>🫁 <strong>BioGPT Pediatric Pulmonology Assistant</strong> | Powered by Microsoft BioGPT</p>
|
945 |
-
<p>Specialized in Children's Respiratory Health • Always consult healthcare professionals</p>
|
946 |
-
</div>
|
947 |
-
""")
|
948 |
-
|
949 |
-
return demo
|
950 |
-
|
951 |
-
# Create and launch the interface
|
952 |
-
demo = create_gradio_interface()
|
953 |
-
|
954 |
-
if __name__ == "__main__":
|
955 |
-
# Launch the app
|
956 |
-
demo.launch(
|
957 |
-
server_name="0.0.0.0",
|
958 |
-
server_port=7860,
|
959 |
-
share=False
|
960 |
-
)
|
961 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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