Blaiseboy commited on
Commit
fd4e71f
·
verified ·
1 Parent(s): d79c4a9

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -883
app.py DELETED
@@ -1,883 +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 fallback"""
232
- print("🧠 Loading BioGPT model...")
233
-
234
- # Try BioGPT first, then fallback to smaller model
235
- models_to_try = [
236
- "microsoft/BioGPT-Large",
237
- "microsoft/BioGPT",
238
- "microsoft/DialoGPT-medium"
239
- ]
240
-
241
- for model_name in models_to_try:
242
- try:
243
- print(f" Trying {model_name}...")
244
-
245
- # Setup quantization for memory efficiency
246
- quantization_config = None
247
- if self.use_8bit and "BioGPT" in model_name:
248
- quantization_config = BitsAndBytesConfig(
249
- load_in_8bit=True,
250
- llm_int8_threshold=6.0,
251
- )
252
-
253
- # Load tokenizer
254
- self.tokenizer = AutoTokenizer.from_pretrained(model_name)
255
- if self.tokenizer.pad_token is None:
256
- self.tokenizer.pad_token = self.tokenizer.eos_token
257
-
258
- # Load model
259
- self.model = AutoModelForCausalLM.from_pretrained(
260
- model_name,
261
- quantization_config=quantization_config,
262
- torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
263
- device_map="auto" if self.device == "cuda" and quantization_config else None,
264
- trust_remote_code=True
265
- )
266
-
267
- if self.device == "cuda" and quantization_config is None:
268
- self.model = self.model.to(self.device)
269
-
270
- print(f"✅ Successfully loaded {model_name}!")
271
- self.model_name = model_name
272
- return
273
-
274
- except Exception as e:
275
- print(f"❌ Failed to load {model_name}: {e}")
276
- continue
277
-
278
- # If all models fail
279
- print("❌ All models failed to load")
280
- self.model = None
281
- self.tokenizer = None
282
- self.model_name = "None"
283
-
284
- def load_default_medical_knowledge(self):
285
- """Load comprehensive default medical knowledge base"""
286
- default_knowledge = [
287
- {
288
- 'id': 0,
289
- '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.",
290
- 'medical_focus': 'pediatric_symptoms',
291
- 'source': 'default_knowledge'
292
- },
293
- {
294
- 'id': 1,
295
- '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.",
296
- 'medical_focus': 'pediatric_symptoms',
297
- 'source': 'default_knowledge'
298
- },
299
- {
300
- 'id': 2,
301
- '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.",
302
- 'medical_focus': 'pediatric_symptoms',
303
- 'source': 'default_knowledge'
304
- },
305
- {
306
- 'id': 3,
307
- '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.",
308
- 'medical_focus': 'emergency',
309
- 'source': 'default_knowledge'
310
- },
311
- {
312
- 'id': 4,
313
- '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.",
314
- 'medical_focus': 'prevention',
315
- 'source': 'default_knowledge'
316
- },
317
- {
318
- 'id': 5,
319
- '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.",
320
- 'medical_focus': 'pediatric_symptoms',
321
- 'source': 'default_knowledge'
322
- },
323
- # Adding more default knowledge chunks for comprehensive coverage...
324
- {
325
- 'id': 6,
326
- '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.",
327
- 'medical_focus': 'pediatric_pulmonology',
328
- 'source': 'default_knowledge'
329
- },
330
- {
331
- 'id': 7,
332
- '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.",
333
- 'medical_focus': 'pediatric_pulmonology',
334
- 'source': 'default_knowledge'
335
- }
336
- ]
337
-
338
- self.knowledge_chunks = default_knowledge
339
- print(f"📚 Loaded {len(default_knowledge)} default medical knowledge chunks")
340
-
341
- def retrieve_medical_context(self, query: str, n_results: int = 3) -> List[str]:
342
- """Retrieve relevant medical context using improved keyword search with pulmonology priority"""
343
- if not self.knowledge_chunks:
344
- return []
345
-
346
- query_lower = query.lower()
347
- query_words = set(query_lower.split())
348
- chunk_scores = []
349
-
350
- # Enhanced medical keyword mapping with pulmonology emphasis
351
- medical_keywords = {
352
- 'pulmonology': ['asthma', 'pneumonia', 'bronchiolitis', 'croup', 'respiratory', 'lung', 'airway', 'breathing', 'cough', 'wheeze', 'stridor'],
353
- 'fever': ['fever', 'temperature', 'hot', 'warm', 'burning'],
354
- 'stomach': ['stomach', 'abdominal', 'belly', 'tummy', 'pain', 'ache'],
355
- 'rash': ['rash', 'skin', 'red', 'spots', 'bumps', 'itchy'],
356
- 'vomiting': ['vomit', 'vomiting', 'throw up', 'sick', 'nausea'],
357
- 'diarrhea': ['diarrhea', 'loose', 'stool', 'bowel', 'poop'],
358
- 'dehydration': ['dehydration', 'dehydrated', 'fluids', 'water', 'thirsty'],
359
- 'emergency': ['emergency', 'urgent', 'serious', 'severe', 'hospital', 'doctor']
360
- }
361
-
362
- # Expand query with related medical terms
363
- expanded_query_words = set(query_words)
364
- for medical_term, synonyms in medical_keywords.items():
365
- if any(word in query_lower for word in synonyms):
366
- expanded_query_words.update(synonyms)
367
-
368
- for chunk_info in self.knowledge_chunks:
369
- chunk_text = chunk_info['text'].lower()
370
-
371
- # Calculate relevance score with expanded terms
372
- word_overlap = sum(1 for word in expanded_query_words if word in chunk_text)
373
- base_score = word_overlap / len(expanded_query_words) if expanded_query_words else 0
374
-
375
- # Strong boost for pulmonology content
376
- medical_boost = 0
377
- medical_focus = chunk_info.get('medical_focus', '')
378
- source = chunk_info.get('source', '')
379
-
380
- if medical_focus == 'pediatric_pulmonology':
381
- medical_boost = 0.8 # Highest priority for pulmonology
382
- elif source == 'pediatric_pulmonology_file':
383
- medical_boost = 0.7 # High priority for your uploaded data
384
- elif medical_focus == 'emergency':
385
- medical_boost = 0.4
386
- elif medical_focus in ['treatments', 'diagnosis']:
387
- medical_boost = 0.3
388
- elif medical_focus == 'pediatric_symptoms':
389
- medical_boost = 0.5
390
-
391
- final_score = base_score + medical_boost
392
-
393
- if final_score > 0:
394
- chunk_scores.append((final_score, chunk_info['text']))
395
-
396
- # Return top matches - prioritize pulmonology content
397
- chunk_scores.sort(reverse=True)
398
- results = [chunk for _, chunk in chunk_scores[:n_results]]
399
-
400
- # If no good matches, return some default medical chunks
401
- if not results:
402
- results = [chunk['text'] for chunk in self.knowledge_chunks[:2]]
403
-
404
- return results
405
-
406
- def generate_biogpt_response(self, context: str, query: str) -> str:
407
- """Generate medical response using BioGPT with improved prompting"""
408
- if not self.model or not self.tokenizer:
409
- return "Medical model not available. Please try again later."
410
-
411
- try:
412
- # Create a more focused medical prompt for better responses
413
- prompt = f"""You are a pediatric medical expert. Based on the following medical information, provide a clear, helpful answer to the patient's question.
414
-
415
- Medical Context: {context[:600]}
416
-
417
- Patient Question: {query}
418
-
419
- Your response should be a direct medical answer. Do not ask follow-up questions. Just provide the best possible explanation or advice based on the context."""
420
-
421
- # Tokenize input
422
- inputs = self.tokenizer(
423
- prompt,
424
- return_tensors="pt",
425
- truncation=True,
426
- max_length=900 # Reduced to leave more room for generation
427
- )
428
-
429
- # Move inputs to device
430
- if self.device == "cuda":
431
- inputs = {k: v.to(self.device) for k, v in inputs.items()}
432
-
433
- # Generate response with improved parameters
434
- with torch.no_grad():
435
- outputs = self.model.generate(
436
- **inputs,
437
- max_new_tokens=200, # Increased for more detailed responses
438
- do_sample=True,
439
- temperature=0.6, # Reduced for more focused responses
440
- top_p=0.85, # Slightly reduced for better quality
441
- top_k=40, # Added top-k for better control
442
- pad_token_id=self.tokenizer.eos_token_id,
443
- repetition_penalty=1.15, # Increased to reduce repetition
444
- no_repeat_ngram_size=3 # Prevent 3-gram repetition
445
- )
446
-
447
- # Decode response
448
- full_response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
449
-
450
- # Extract generated part more carefully
451
- if "Medical Response" in full_response:
452
- generated_response = full_response.split("Medical Response")[-1].strip()
453
- # Remove any remaining prompt artifacts
454
- generated_response = re.sub(r'^\W*\([^)]*\)\W*', '', generated_response)
455
- generated_response = generated_response.lstrip(':').strip()
456
- else:
457
- generated_response = full_response[len(prompt):].strip()
458
-
459
- # Clean and improve response
460
- cleaned_response = self.clean_and_improve_medical_response(generated_response)
461
-
462
- return cleaned_response
463
-
464
- except Exception as e:
465
- print(f"⚠️ BioGPT generation failed: {e}")
466
- return self.fallback_response(context, query)
467
-
468
- def clean_and_improve_medical_response(self, response: str) -> str:
469
- """Enhanced cleaning and improvement of medical response"""
470
- # Remove any remaining XML/formatting artifacts
471
- response = re.sub(r'<[^>]+>', '', response)
472
- response = re.sub(r'</?\s*\w+\s*>', '', response)
473
-
474
- # Remove incomplete or malformed sentences at the end
475
- sentences = re.split(r'[.!?]+', response)
476
- clean_sentences = []
477
-
478
- for sentence in sentences:
479
- sentence = sentence.strip()
480
- # Keep sentences that are substantial and don't end with incomplete words
481
- if (len(sentence) > 15 and
482
- not sentence.endswith(('and', 'or', 'but', 'however', 'the', 'a', 'an', 'in', 'on', 'at')) and
483
- not re.search(r'\b\w{1,2}$', sentence)):
484
- clean_sentences.append(sentence)
485
- if len(clean_sentences) >= 4: # Limit to 4 good sentences
486
- break
487
-
488
- if clean_sentences:
489
- cleaned = '. '.join(clean_sentences) + '.'
490
- # Ensure it starts with a capital letter
491
- if cleaned and cleaned[0].islower():
492
- cleaned = cleaned[0].upper() + cleaned[1:]
493
- else:
494
- # If no good sentences, try to extract the first meaningful part
495
- cleaned = response[:300].strip()
496
- if not cleaned.endswith('.'):
497
- cleaned += '.'
498
-
499
- # Final cleanup
500
- cleaned = re.sub(r'\s+', ' ', cleaned).strip()
501
-
502
- return cleaned
503
- def clean_medical_response(self, response: str) -> str:
504
- """Clean medical response"""
505
- sentences = re.split(r'[.!?]+', response)
506
- clean_sentences = []
507
-
508
- for sentence in sentences:
509
- sentence = sentence.strip()
510
- if len(sentence) > 10:
511
- clean_sentences.append(sentence)
512
- if len(clean_sentences) >= 3:
513
- break
514
-
515
- if clean_sentences:
516
- cleaned = '. '.join(clean_sentences) + '.'
517
- else:
518
- cleaned = response[:200] + '...' if len(response) > 200 else response
519
-
520
- return cleaned
521
-
522
- def fallback_response(self, context: str, query: str) -> str:
523
- """Fallback response when model fails"""
524
- sentences = [s.strip() for s in context.split('.') if len(s.strip()) > 20]
525
-
526
- if sentences:
527
- response = sentences[0] + '.'
528
- if len(sentences) > 1:
529
- response += ' ' + sentences[1] + '.'
530
- else:
531
- response = context[:300] + '...'
532
-
533
- return response
534
-
535
- def handle_conversational_interactions(self, query: str) -> Optional[str]:
536
- """Handle conversational interactions"""
537
- query_lower = query.lower().strip()
538
-
539
- # Greeting patterns
540
- exact_greetings = [
541
- 'hello', 'hi', 'hey', 'good morning', 'good afternoon',
542
- 'good evening', 'how are you', 'how are you doing'
543
- ]
544
-
545
- if query_lower in exact_greetings:
546
- 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?"
547
-
548
- # Thanks patterns
549
- thanks_only = ['thank you', 'thanks', 'thank you so much', 'thanks a lot']
550
- if query_lower in thanks_only:
551
- 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!"
552
-
553
- # Help patterns
554
- help_only = ['help', 'what can you do', 'what are you', 'who are you']
555
- if query_lower in help_only:
556
- return """🤖 **About BioGPT Pediatric Pulmonology Assistant**
557
-
558
- I'm an AI medical assistant powered by BioGPT, specialized in pediatric pulmonology and respiratory medicine. I can help with:
559
-
560
- 🫁 **Pediatric Pulmonology:**
561
- • Asthma, bronchiolitis, pneumonia, croup
562
- • Respiratory symptoms and breathing difficulties
563
- • Treatment guidance and management
564
- • When to seek medical care
565
-
566
- ⚠️ **Important:** I provide educational information only. Always consult healthcare professionals for medical decisions."""
567
-
568
- return None
569
-
570
- def chat_interface(self, message: str, history: List[List[str]]) -> str:
571
- """Main chat interface for Gradio"""
572
- if not message.strip():
573
- return "Hello! I'm BioGPT, your pediatric pulmonology AI assistant. How can I help you with children's respiratory health today?"
574
-
575
- print(f"🔍 Processing query: '{message}'")
576
-
577
- # Handle conversational interactions
578
- conversational_response = self.handle_conversational_interactions(message)
579
- if conversational_response:
580
- print(" Handled as conversational")
581
- return conversational_response
582
-
583
- print(" Processing as medical query")
584
-
585
- # Process as medical query
586
- context = self.retrieve_medical_context(message)
587
-
588
- if not context:
589
- return f"""🩺 **Medical Query:** {message}
590
-
591
- ⚠️ I don't have specific information about this topic in my current medical database. However, I recommend:
592
-
593
- 1. **Consult Healthcare Provider**: For personalized medical advice
594
- 2. **Emergency Signs**: If symptoms are severe, seek immediate care
595
- 3. **Pediatric Specialist**: For specialized concerns
596
-
597
- **For urgent medical concerns, contact your healthcare provider or emergency services.**
598
-
599
- 💡 **Try asking about**: asthma, breathing difficulties, cough, pneumonia, or other respiratory symptoms."""
600
-
601
- # Generate medical response
602
- main_context = '\n\n'.join(context)
603
- response = self.generate_biogpt_response(main_context, message)
604
-
605
- # Format as medical response
606
- 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."
607
-
608
- return final_response
609
-
610
- def get_knowledge_stats(self) -> Dict:
611
- """Get statistics about loaded knowledge"""
612
- if not self.knowledge_chunks:
613
- return {"total_chunks": 0}
614
-
615
- stats = {
616
- "total_chunks": len(self.knowledge_chunks),
617
- "default_knowledge": len([c for c in self.knowledge_chunks if c.get('source') == 'default_knowledge']),
618
- "pulmonology_file_data": len([c for c in self.knowledge_chunks if c.get('source') == 'pediatric_pulmonology_file']),
619
- "pulmonology_focused": len([c for c in self.knowledge_chunks if c.get('medical_focus') == 'pediatric_pulmonology']),
620
- "model_used": getattr(self, 'model_name', 'Unknown')
621
- }
622
- return stats
623
-
624
- # Test function
625
- def test_chatbot_responses():
626
- """Test the chatbot with various queries"""
627
- print("\n🧪 Testing BioGPT Pediatric Pulmonology Chatbot...")
628
- print("=" * 50)
629
-
630
- test_queries = [
631
- "hello",
632
- "what is asthma in children",
633
- "my child has breathing difficulties",
634
- "help",
635
- "treatment for pediatric pneumonia",
636
- "thank you"
637
- ]
638
-
639
- for query in test_queries:
640
- print(f"\n🔍 Query: '{query}'")
641
- response = chatbot.chat_interface(query, [])
642
- response_type = 'CONVERSATIONAL' if any(word in response for word in ['Hello!', 'welcome!', 'About BioGPT']) else 'MEDICAL'
643
- print(f"🤖 Response type: {response_type}")
644
- print(f"📝 Response: {response[:100]}...")
645
- print("-" * 30)
646
-
647
- # Initialize the chatbot globally
648
- print("🚀 Initializing BioGPT Pediatric Pulmonology Chatbot...")
649
- chatbot = BioGPTMedicalChatbot()
650
-
651
- # Show knowledge statistics
652
- print("\n📊 Knowledge Base Statistics:")
653
- stats = chatbot.get_knowledge_stats()
654
- for key, value in stats.items():
655
- print(f" {key}: {value}")
656
-
657
- # Run tests
658
- test_chatbot_responses()
659
-
660
- def create_gradio_interface():
661
- """Create Gradio chat interface"""
662
-
663
- # Get current knowledge stats for display
664
- stats = chatbot.get_knowledge_stats()
665
-
666
- # Custom CSS for medical theme
667
- css = """
668
- .gradio-container {
669
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
670
- }
671
- .chat-message {
672
- background-color: #f8f9fa;
673
- border-radius: 10px;
674
- padding: 10px;
675
- margin: 5px;
676
- }
677
- """
678
-
679
- with gr.Blocks(
680
- css=css,
681
- title="BioGPT Pediatric Pulmonology Assistant",
682
- theme=gr.themes.Soft()
683
- ) as demo:
684
-
685
- # Header
686
- gr.HTML(f"""
687
- <div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea, #764ba2); color: white; border-radius: 10px; margin-bottom: 20px;">
688
- <h1>🫁 BioGPT Pediatric Pulmonology Assistant</h1>
689
- <p>Specialized AI Medical Chatbot for Children's Respiratory Health</p>
690
- <p><strong>Powered by BioGPT | {stats['total_chunks']} Medical Knowledge Chunks Loaded</strong></p>
691
- <p><small>Model: {stats['model_used']} | Pulmonology Data: {stats['pulmonology_file_data']} chunks</small></p>
692
- </div>
693
- """)
694
-
695
- # Important disclaimer
696
- gr.HTML("""
697
- <div style="background-color: #fff3cd; border: 1px solid #ffeaa7; border-radius: 8px; padding: 15px; margin-bottom: 20px;">
698
- <h3 style="color: #856404; margin-top: 0;">⚠️ Medical Disclaimer</h3>
699
- <p style="color: #856404; margin-bottom: 0;">
700
- This AI provides educational pediatric pulmonology information only and is NOT a substitute for professional medical advice,
701
- diagnosis, or treatment. Always consult qualified healthcare providers for medical decisions.
702
- <strong>In case of respiratory emergency, call emergency services immediately.</strong>
703
- </p>
704
- </div>
705
- """)
706
-
707
- # Chat interface
708
- chatbot_interface = gr.ChatInterface(
709
- fn=chatbot.chat_interface,
710
- title="💬 Chat with BioGPT Pulmonology Assistant",
711
- description="Ask me about pediatric respiratory health, asthma, breathing difficulties, and pulmonology treatments.",
712
- examples=[
713
- "What is asthma in children?",
714
- "My child has a persistent cough, what should I do?",
715
- "How is bronchiolitis treated in infants?",
716
- "When should I be worried about my child's breathing?",
717
- "What are the signs of pneumonia in children?",
718
- "How can I prevent respiratory infections?"
719
- ],
720
- retry_btn=None,
721
- undo_btn=None,
722
- clear_btn="🗑️ Clear Chat",
723
- submit_btn="🫁 Ask BioGPT",
724
- chatbot=gr.Chatbot(
725
- height=500,
726
- placeholder="<div style='text-align: center; color: #666;'>Start a conversation with BioGPT Pediatric Pulmonology Assistant</div>",
727
- show_copy_button=True,
728
- bubble_full_width=False
729
- )
730
- )
731
-
732
- # Information tabs
733
- with gr.Tabs():
734
- with gr.Tab("ℹ️ About"):
735
- gr.Markdown(f"""
736
- ## About BioGPT Pediatric Pulmonology Assistant
737
-
738
- This AI assistant is powered by **BioGPT**, specialized for pediatric pulmonology and respiratory medicine.
739
-
740
- ### 🎯 Current Knowledge Base:
741
- - **Total Chunks**: {stats['total_chunks']}
742
- - **Default Medical Knowledge**: {stats['default_knowledge']} chunks
743
- - **Pulmonology File Data**: {stats['pulmonology_file_data']} chunks
744
- - **Pulmonology Focus**: {stats['pulmonology_focused']} chunks
745
- - **Model**: {stats['model_used']}
746
-
747
- ### 🫁 Specializations:
748
- - **Pediatric Asthma**: Diagnosis, treatment, management
749
- - **Respiratory Infections**: Pneumonia, bronchiolitis, croup
750
- - **Breathing Difficulties**: Assessment and guidance
751
- - **Chronic Respiratory Conditions**: Long-term management
752
- - **Emergency Respiratory Care**: When to seek immediate help
753
-
754
- ### 🔧 Technical Features:
755
- - **Model**: Microsoft BioGPT (Medical AI)
756
- - **Auto-Loading**: Automatically loads your pulmonology data file
757
- - **Smart Retrieval**: Prioritizes pulmonology content
758
- - **Fallback System**: Ensures reliability across different environments
759
-
760
- ### 📱 How to Use:
761
- 1. Type your pediatric respiratory question
762
- 2. Be specific about symptoms or conditions
763
- 3. Ask about treatments, diagnosis, or management
764
- 4. Request guidance on when to seek care
765
- """)
766
-
767
- with gr.Tab("🫁 Pulmonology Topics"):
768
- gr.Markdown("""
769
- ## Pediatric Pulmonology Coverage
770
-
771
- ### 🔴 Common Respiratory Conditions:
772
- - **Asthma**: Triggers, symptoms, management, action plans
773
- - **Bronchiolitis**: RSV, treatment, when to hospitalize
774
- - **Pneumonia**: Bacterial vs viral, antibiotics, recovery
775
- - **Croup**: Barking cough, stridor, home treatment
776
- - **Bronchitis**: Acute vs chronic, treatment approaches
777
-
778
- ### 🟡 Respiratory Symptoms:
779
- - **Cough**: Persistent, productive, dry, nocturnal
780
- - **Wheezing**: Causes, assessment, treatment
781
- - **Shortness of Breath**: Evaluation and management
782
- - **Chest Pain**: When concerning in children
783
- - **Stridor**: Upper airway obstruction signs
784
-
785
- ### 🟢 Diagnostic & Treatment:
786
- - **Pulmonary Function Tests**: When appropriate
787
- - **Imaging**: X-rays, CT scans for respiratory issues
788
- - **Medications**: Bronchodilators, steroids, antibiotics
789
- - **Oxygen Therapy**: Indications and monitoring
790
- - **Respiratory Support**: CPAP, ventilation considerations
791
-
792
- ### 🔵 Prevention & Management:
793
- - **Trigger Avoidance**: Environmental controls
794
- - **Vaccination**: Respiratory disease prevention
795
- - **Exercise Guidelines**: For children with respiratory conditions
796
- - **School Management**: Asthma action plans, inhaler use
797
- """)
798
-
799
- with gr.Tab("⚠️ Emergency & Safety"):
800
- gr.Markdown("""
801
- ## Respiratory Emergency Guidance
802
-
803
- ### 🚨 CALL EMERGENCY SERVICES IMMEDIATELY:
804
- - **Severe Breathing Difficulty**: Cannot speak in full sentences
805
- - **Blue Lips or Fingernails**: Cyanosis indicating oxygen deprivation
806
- - **Severe Wheezing**: With significant distress
807
- - **Stridor at Rest**: High-pitched breathing sound
808
- - **Unconsciousness**: Related to breathing problems
809
- - **Severe Chest Retractions**: Pulling in around ribs/sternum
810
-
811
- ### 🏥 SEEK IMMEDIATE MEDICAL CARE:
812
- - **Persistent High Fever**: >104°F (40°C) with respiratory symptoms
813
- - **Worsening Symptoms**: Despite treatment
814
- - **Dehydration Signs**: With respiratory illness
815
- - **Significant Behavior Changes**: Extreme lethargy, irritability
816
- - **Inhaler Not Helping**: Asthma symptoms not responding
817
-
818
- ### 📞 CONTACT HEALTHCARE PROVIDER:
819
- - **New Respiratory Symptoms**: Lasting more than a few days
820
- - **Chronic Cough**: Persisting beyond 2-3 weeks
821
- - **Asthma Questions**: About medications or management
822
- - **Fever with Cough**: Especially if productive
823
- - **Exercise Limitations**: Due to breathing difficulties
824
-
825
- ### 🏠 HOME MONITORING:
826
- - **Respiratory Rate**: Normal ranges by age
827
- - **Oxygen Saturation**: If pulse oximeter available
828
- - **Peak Flow**: For children with asthma
829
- - **Symptom Tracking**: Using asthma diaries or apps
830
- """)
831
-
832
- with gr.Tab("📁 Data Information"):
833
- gr.Markdown(f"""
834
- ## Knowledge Base Status
835
-
836
- ### 📊 Current Data Loaded:
837
- - **Total Medical Chunks**: {stats['total_chunks']}
838
- - **Default Knowledge**: {stats['default_knowledge']} chunks
839
- - **Your Pulmonology File**: {stats['pulmonology_file_data']} chunks
840
- - **Pulmonology Focused**: {stats['pulmonology_focused']} chunks
841
- - **AI Model**: {stats['model_used']}
842
-
843
- ### 📝 How Your Data Is Used:
844
- 1. **Auto-Detection**: System automatically looks for 'Pediatric_cleaned.txt'
845
- 2. **Smart Processing**: Breaks your file into medical chunks
846
- 3. **Priority Ranking**: Pulmonology content gets highest priority
847
- 4. **Context Retrieval**: Relevant chunks are used to answer questions
848
-
849
- ### 📂 Expected File Formats:
850
- - **Filename**: 'Pediatric_cleaned.txt' (case variations accepted)
851
- - **Content**: Plain text with medical information
852
- - **Structure**: Paragraphs, sections, or bullet points
853
- - **Focus**: Pediatric pulmonology and respiratory medicine
854
-
855
- ### 🔄 Upload Instructions:
856
- 1. Go to your Hugging Face Space
857
- 2. Click "Files" tab
858
- 3. Upload your 'Pediatric_cleaned.txt' file
859
- 4. Restart the Space to reload data
860
-
861
- {"✅ **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."}
862
- """)
863
-
864
- # Footer
865
- gr.HTML("""
866
- <div style="text-align: center; padding: 20px; margin-top: 30px; border-top: 1px solid #ddd; color: #666;">
867
- <p>🫁 <strong>BioGPT Pediatric Pulmonology Assistant</strong> | Powered by Microsoft BioGPT</p>
868
- <p>Specialized in Children's Respiratory Health • Always consult healthcare professionals</p>
869
- </div>
870
- """)
871
-
872
- return demo
873
-
874
- # Create and launch the interface
875
- demo = create_gradio_interface()
876
-
877
- if __name__ == "__main__":
878
- # Launch the app
879
- demo.launch(
880
- server_name="0.0.0.0",
881
- server_port=7860,
882
- share=False
883
- )