File size: 34,582 Bytes
afcfb2d
 
 
 
 
 
 
b0b6579
 
 
 
 
 
 
afcfb2d
 
 
 
b0b6579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afcfb2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0b6579
afcfb2d
 
 
b0b6579
afcfb2d
 
b0b6579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afcfb2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0b6579
afcfb2d
 
 
b0b6579
 
 
 
 
afcfb2d
b0b6579
 
afcfb2d
b0b6579
 
 
 
afcfb2d
 
b0b6579
 
 
 
 
afcfb2d
 
 
 
b0b6579
afcfb2d
b0b6579
7ff0052
afcfb2d
7ff0052
b0b6579
 
 
7ff0052
afcfb2d
 
7ff0052
b0b6579
afcfb2d
 
 
 
7ff0052
afcfb2d
7ff0052
afcfb2d
 
 
7ff0052
b0b6579
 
 
7ff0052
afcfb2d
 
7ff0052
afcfb2d
b0b6579
afcfb2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6df642
afcfb2d
 
 
4e63d78
afcfb2d
 
 
 
 
 
4e63d78
 
 
 
 
 
 
 
 
 
 
 
 
afcfb2d
7ff0052
afcfb2d
 
 
 
 
 
 
 
 
 
 
a69a776
afcfb2d
 
 
 
 
 
 
 
 
7ff0052
afcfb2d
 
7ff0052
afcfb2d
 
 
c473c30
afcfb2d
 
 
 
 
 
 
 
 
7ff0052
afcfb2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a69a776
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
import gradio as gr
import spacy
import pandas as pd
import numpy as np
import re
from datetime import datetime
import os
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
import torch
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import pickle
import warnings
warnings.filterwarnings('ignore')

# Initialize the chatbot class
class PediatricPulmonologyChatbot:
    def __init__(self):
        # Initialize ML models
        self.setup_ml_models()
        
        # Training data for intent classification
        self.training_data = {
            "asthma": [
                "my child is wheezing", "he has a tight chest and can't breathe", "child sounds breathless",
                "she's coughing and short of breath", "wheezing at night", "tight chest", "inhaler needed",
                "difficulty breathing", "whistling sound when breathing", "chest tightness during exercise",
                "chronic wheezing", "allergic asthma", "exercise induced asthma", "nocturnal coughing"
            ],
            "bronchiolitis": [
                "baby has stuffy nose and cough", "infant wheezing with fever", "rapid breathing in baby",
                "congested and breathing fast", "nasal flaring", "chest congestion in baby", "rsv symptoms",
                "baby struggling to feed", "fast shallow breathing", "infant with cold symptoms",
                "runny nose and wheezing baby", "difficulty feeding baby", "baby sounds congested"
            ],
            "pneumonia": [
                "child has chest pain and fever", "coughing up mucus", "high fever and fatigue",
                "breathing fast with chills", "chest crackling sounds", "very tired with fever",
                "productive cough", "bacterial infection lungs", "viral pneumonia", "chest x-ray abnormal",
                "difficulty breathing with fever", "shaking chills", "pleuritic chest pain"
            ],
            "chronic cough": [
                "persistent cough for weeks", "dry cough won't stop", "cough worsens at night",
                "ongoing cough after cold", "chronic dry cough", "cough for more than month",
                "post-infectious cough", "habitual cough", "psychogenic cough", "cough variant asthma",
                "lingering cough", "cough without other symptoms", "nighttime coughing fits"
            ],
            "paradoxical vocal fold movement": [
                "stridor when inhaling", "tight throat can't breathe in", "voice disappears suddenly",
                "throat closes during breathing", "panic with breathing", "exercise induced stridor",
                "vocal cord dysfunction", "throat tightness stress", "difficulty inhaling only",
                "feels like choking", "throat spasm", "can't get air in", "inspiratory stridor"
            ],
            "subglottic stenosis": [
                "high pitched breathing sound", "noisy breathing", "stridor at rest",
                "airway narrowing", "difficulty breathing lying down", "hoarse voice chronic",
                "breathing obstruction", "harsh breathing sounds", "airway surgery needed",
                "congenital stridor", "biphasic stridor", "progressive breathing difficulty"
            ],
            "acute respiratory distress syndrome": [
                "severe breathing difficulty", "needs ventilator", "lung inflammation severe",
                "icu admission breathing", "oxygen not helping", "rapid onset breathing failure",
                "bilateral lung infiltrates", "severe hypoxia", "mechanical ventilation needed",
                "acute lung injury", "sepsis related breathing", "shock lung"
            ],
            "hereditary hemorrhagic telangiectasia": [
                "frequent nosebleeds", "family history bleeding", "abnormal blood vessels",
                "chronic bleeding", "telangiectasia", "arteriovenous malformation",
                "genetic bleeding disorder", "recurrent epistaxis", "vascular malformation",
                "hereditary bleeding", "osler weber rendu", "pulmonary avm"
            ],
            "tracheoesophageal fistula": [
                "coughs when feeding", "milk comes through nose", "choking during feeding",
                "aspiration with feeds", "connection windpipe foodpipe", "surgical repair needed",
                "esophageal atresia", "feeding difficulties newborn", "recurrent pneumonia feeding",
                "congenital anomaly", "h-type fistula", "feeding intolerance"
            ],
            "laryngeal web": [
                "weak hoarse cry", "congenital voice problems", "stridor since birth",
                "membrane vocal cords", "difficulty intubating", "web larynx", "glottic web",
                "voice abnormality birth", "inspiratory stridor newborn", "cry abnormal",
                "congenital laryngeal anomaly", "airway obstruction birth"
            ],
            "primary ciliary dyskinesia": [
                "chronic wet cough", "sinus infections recurrent", "ear infections frequent",
                "immotile cilia", "kartagener syndrome", "bronchiectasis", "daily sputum",
                "chronic rhinosinusitis", "hearing loss chronic", "situs inversus",
                "chronic respiratory infections", "ciliary dysfunction", "genetic lung disease"
            ],
            "pulmonary arterial hypertension": [
                "fatigue with exertion", "shortness breath exercise", "fainting spells",
                "high lung pressure", "right heart failure", "blue lips exercise",
                "syncope exertion", "chest pain exertion", "elevated pulmonary pressure",
                "right heart strain", "eisenmenger syndrome", "pah diagnosis"
            ],
            "esophageal atresia": [
                "frothy saliva newborn", "cannot swallow", "choking feeding attempts",
                "milk regurgitation", "unable pass feeding tube", "congenital esophagus",
                "feeding tube won't pass", "excessive drooling baby", "aspiration feeding",
                "surgical correction needed", "type c ea", "blind pouch esophagus"
            ],
            "asbestosis": [
                "asbestos exposure history", "lung scarring", "occupational lung disease",
                "interstitial fibrosis", "progressive breathlessness", "chest imaging abnormal",
                "environmental exposure", "lung function decline", "pulmonary fibrosis",
                "occupational hazard", "mesothelioma risk", "pleural plaques"
            ]
        }
        
        # Setup TF-IDF vectorizer for similarity matching
        self.setup_similarity_model()
        # Knowledge base from your original code
        self.knowledge_base = {
            "asthma": {
                "definition": "Asthma is a chronic condition that causes inflammation and narrowing of the airways, leading to wheezing, breathlessness, and coughing.",
                "symptoms": [
                    "wheezing",
                    "shortness of breath", 
                    "coughing, especially at night or early morning",
                    "tightness in the chest"
                ],
                "red_flags": [
                    "severe difficulty breathing",
                    "lips turning blue",
                    "child unable to speak or cry",
                    "no improvement with inhaler"
                ],
                "advice": "Use a prescribed inhaler, keep the child in an upright position, avoid triggers like dust or pollen, and seek emergency care if symptoms worsen."
            },
            "bronchiolitis": {
                "definition": "Bronchiolitis is a common lung infection in infants and young children, usually caused by a virus, that leads to inflammation and congestion in the small airways.",
                "symptoms": [
                    "cough",
                    "runny nose", 
                    "wheezing",
                    "fast or shallow breathing",
                    "poor feeding"
                ],
                "red_flags": [
                    "grunting or flaring nostrils while breathing",
                    "difficulty feeding or drinking",
                    "chest retractions",
                    "cyanosis (bluish skin)"
                ],
                "advice": "Keep the child well hydrated, monitor for worsening symptoms, and seek medical attention if breathing becomes labored or feeding decreases."
            },
            "pneumonia": {
                "definition": "Pneumonia is an infection of the lungs that causes the air sacs to fill with fluid or pus, leading to cough, fever, and difficulty breathing.",
                "symptoms": [
                    "fever",
                    "cough with phlegm",
                    "chest pain", 
                    "rapid breathing",
                    "fatigue"
                ],
                "red_flags": [
                    "very high fever",
                    "confusion or lethargy",
                    "labored breathing",
                    "cyanosis"
                ],
                "advice": "Ensure the child rests, drinks plenty of fluids, and consult a doctor. Severe symptoms may require antibiotics or hospitalization."
            },
            "chronic cough": {
                "definition": "Chronic cough is a cough that lasts more than 4 weeks in children. It can be dry or productive and may indicate an underlying condition.",
                "symptoms": [
                    "persistent cough for more than 4 weeks",
                    "hoarseness",
                    "dry or wet cough",
                    "cough worsens at night or with exercise"
                ],
                "red_flags": [
                    "cough with blood",
                    "weight loss", 
                    "difficulty breathing",
                    "loss of appetite"
                ],
                "advice": "Avoid environmental irritants, keep the child hydrated, and seek medical evaluation to determine the underlying cause."
            },
            "paradoxical vocal fold movement": {
                "definition": "PVFM is a condition in which the vocal folds close when they should open during breathing, often triggered by stress or irritants.",
                "symptoms": [
                    "stridor",
                    "sudden shortness of breath",
                    "tightness in the throat", 
                    "difficulty inhaling"
                ],
                "red_flags": [
                    "sudden and total voice loss",
                    "stridor during both inhale and exhale",
                    "severe anxiety with breathing difficulty"
                ],
                "advice": "Encourage relaxed throat breathing, avoid triggers, and work with a speech-language pathologist for breathing retraining."
            },
            "subglottic stenosis": {
                "definition": "Subglottic stenosis is a narrowing of the airway just below the vocal cords, which can be congenital or acquired.",
                "symptoms": [
                    "noisy breathing (stridor)",
                    "difficulty breathing during activity",
                    "voice changes or hoarseness"
                ],
                "red_flags": [
                    "severe breathing difficulty",
                    "cyanosis (bluish skin or lips)",
                    "stridor at rest"
                ],
                "advice": "Avoid irritants, monitor breathing, and seek evaluation by an ENT specialist."
            },
            "acute respiratory distress syndrome": {
                "definition": "ARDS is a severe inflammatory reaction in the lungs causing fluid accumulation and difficulty in oxygen exchange.",
                "symptoms": [
                    "rapid breathing",
                    "shortness of breath", 
                    "low oxygen levels"
                ],
                "red_flags": [
                    "extreme difficulty breathing",
                    "requires mechanical ventilation",
                    "persistent hypoxia"
                ],
                "advice": "Requires ICU admission and oxygen support. Early recognition and treatment are crucial."
            },
            "hereditary hemorrhagic telangiectasia": {
                "definition": "HHT is a genetic disorder causing abnormal blood vessel formation, leading to bleeding in organs like lungs and brain.",
                "symptoms": [
                    "frequent nosebleeds",
                    "shortness of breath",
                    "unexplained anemia"
                ],
                "red_flags": [
                    "stroke-like symptoms",
                    "brain or lung hemorrhage", 
                    "significant hemoptysis (coughing blood)"
                ],
                "advice": "Genetic counseling, monitor for bleeding, and treat complications promptly."
            },
            "tracheoesophageal fistula": {
                "definition": "A TEF is an abnormal connection between the trachea and esophagus, often congenital.",
                "symptoms": [
                    "coughing or choking during feeding",
                    "recurrent respiratory infections",
                    "difficulty swallowing"
                ],
                "red_flags": [
                    "cyanosis while feeding",
                    "aspiration pneumonia",
                    "failure to thrive"
                ],
                "advice": "Requires surgical correction. Ensure safe feeding methods until repaired."
            },
            "laryngeal web": {
                "definition": "Laryngeal web is a congenital or acquired membrane that partially obstructs the vocal cords.",
                "symptoms": [
                    "weak or hoarse cry",
                    "stridor",
                    "breathing difficulty during exertion"
                ],
                "red_flags": [
                    "airway obstruction",
                    "progressive stridor",
                    "poor weight gain due to effort in breathing"
                ],
                "advice": "ENT evaluation for surgical intervention. Avoid airway irritants."
            },
            "primary ciliary dyskinesia": {
                "definition": "PCD is a rare genetic disorder where cilia in the lungs do not function properly, leading to mucus build-up and infections.",
                "symptoms": [
                    "chronic wet cough",
                    "nasal congestion",
                    "recurrent ear and sinus infections"
                ],
                "red_flags": [
                    "bronchiectasis",
                    "hearing loss",
                    "progressive lung damage"
                ],
                "advice": "Airway clearance therapies, regular monitoring, and genetic counseling."
            },
            "pulmonary arterial hypertension": {
                "definition": "PAH is increased blood pressure in the arteries of the lungs, making it harder for the heart to pump blood.",
                "symptoms": [
                    "fatigue",
                    "shortness of breath during exertion",
                    "fainting spells"
                ],
                "red_flags": [
                    "cyanosis",
                    "chest pain",
                    "syncope (fainting)"
                ],
                "advice": "Specialist care with medications to reduce pressure. Avoid strenuous activity."
            },
            "esophageal atresia": {
                "definition": "Esophageal atresia is a birth defect where the esophagus does not connect to the stomach.",
                "symptoms": [
                    "frothy saliva",
                    "difficulty feeding",
                    "choking or coughing when feeding"
                ],
                "red_flags": [
                    "aspiration pneumonia",
                    "cyanosis during feeding",
                    "inability to pass a feeding tube"
                ],
                "advice": "Requires urgent surgical correction. Supportive care until surgery."
            },
            "asbestosis": {
                "definition": "Asbestosis is a chronic lung disease caused by inhaling asbestos fibers, rare in children unless exposed.",
                "symptoms": [
                    "persistent dry cough",
                    "chest tightness",
                    "shortness of breath"
                ],
                "red_flags": [
                    "respiratory failure",
                    "clubbing of fingers",
                    "cor pulmonale"
                ],
                "advice": "Prevent exposure, monitor lung function, and seek pulmonary care."
            }
        }
        
        
        # Try to load the spaCy model - fallback to rule-based if not available
        self.nlp = None
        try:
            # You can add your trained spaCy model here later
            pass
        except:
            print("Using ML-enhanced classification")

    def setup_ml_models(self):
        """Initialize machine learning models"""
        try:
            # Use a medical/clinical BERT model for better understanding
            print("Loading ML models...")
            self.tokenizer = AutoTokenizer.from_pretrained("emilyalsentzer/Bio_ClinicalBERT")
            self.model = AutoModelForSequenceClassification.from_pretrained("emilyalsentzer/Bio_ClinicalBERT", num_labels=14)
            print("Bio-Clinical BERT loaded successfully")
        except Exception as e:
            print(f"Could not load Bio-Clinical BERT: {e}")
            try:
                # Fallback to general medical model
                self.classifier = pipeline(
                    "text-classification",
                    model="microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext",
                    return_all_scores=True
                )
                print("PubMed BERT loaded successfully")
            except Exception as e2:
                print(f"Could not load PubMed BERT: {e2}")
                # Final fallback to general model
                try:
                    self.classifier = pipeline(
                        "text-classification", 
                        model="distilbert-base-uncased",
                        return_all_scores=True
                    )
                    print("DistilBERT loaded as fallback")
                except:
                    print("Using rule-based classification only")
                    self.classifier = None

    def setup_similarity_model(self):
        """Setup TF-IDF similarity model for intent matching"""
        # Prepare all training texts and labels
        all_texts = []
        self.labels = []
        
        for condition, examples in self.training_data.items():
            for example in examples:
                all_texts.append(example.lower())
                self.labels.append(condition)
        
        # Create TF-IDF vectors
        self.vectorizer = TfidfVectorizer(
            max_features=1000,
            stop_words='english',
            ngram_range=(1, 2)  # Include bigrams
        )
        self.tfidf_matrix = self.vectorizer.fit_transform(all_texts)
        print(f"TF-IDF model trained on {len(all_texts)} examples")

    def predict_condition_ml(self, text):
        """Use ML models to predict condition"""
        text_lower = text.lower()
        
        # Method 1: TF-IDF Similarity
        try:
            query_vector = self.vectorizer.transform([text_lower])
            similarities = cosine_similarity(query_vector, self.tfidf_matrix).flatten()
            
            # Get top 3 most similar examples
            top_indices = similarities.argsort()[-3:][::-1]
            top_similarities = similarities[top_indices]
            
            if top_similarities[0] > 0.1:  # Minimum similarity threshold
                predicted_condition = self.labels[top_indices[0]]
                confidence = float(top_similarities[0])
                
                # Calculate condition-level confidence by averaging similar examples
                condition_scores = {}
                for idx in top_indices:
                    if similarities[idx] > 0.05:
                        condition = self.labels[idx]
                        if condition not in condition_scores:
                            condition_scores[condition] = []
                        condition_scores[condition].append(similarities[idx])
                
                # Average scores for each condition
                for condition in condition_scores:
                    condition_scores[condition] = np.mean(condition_scores[condition])
                
                best_condition = max(condition_scores, key=condition_scores.get)
                best_confidence = condition_scores[best_condition]
                
                return best_condition, float(best_confidence)
        except Exception as e:
            print(f"TF-IDF prediction error: {e}")
        
        # Method 2: Rule-based fallback with enhanced patterns
        return self.rule_based_classifier_enhanced(text_lower)

    def rule_based_classifier_enhanced(self, text):
        """Enhanced rule-based classification with better pattern matching"""
        text_lower = text.lower()
        
        # Create scoring system for multiple keywords
        condition_scores = {}
        
        # Enhanced keyword patterns
        patterns = {
            "asthma": [
                (["wheez", "wheezing"], 0.8),
                (["tight chest", "chest tight"], 0.7),
                (["shortness", "breath"], 0.6),
                (["inhaler"], 0.9),
                (["asthma"], 1.0),
                (["breathless"], 0.6),
                (["night", "cough"], 0.5),
                (["exercise", "breathing"], 0.4)
            ],
            "bronchiolitis": [
                (["baby", "infant"], 0.6),
                (["runny nose", "stuffy"], 0.5),
                (["fast breathing", "rapid"], 0.7),
                (["bronchiolitis"], 1.0),
                (["rsv"], 0.8),
                (["nasal flaring"], 0.8),
                (["feeding", "difficulty"], 0.6)
            ],
            "pneumonia": [
                (["fever", "high"], 0.6),
                (["chest pain"], 0.7),
                (["mucus", "phlegm"], 0.6),
                (["pneumonia"], 1.0),
                (["chills"], 0.5),
                (["fatigue", "tired"], 0.4),
                (["crackling"], 0.8)
            ],
            "chronic cough": [
                (["persistent", "chronic"], 0.7),
                (["weeks", "month"], 0.6),
                (["dry cough"], 0.6),
                (["night", "cough"], 0.5),
                (["4 weeks", "four weeks"], 0.8),
                (["ongoing"], 0.5)
            ],
            "paradoxical vocal fold movement": [
                (["stridor"], 0.9),
                (["throat", "tight"], 0.7),
                (["voice", "loss"], 0.6),
                (["inhaling", "difficult"], 0.7),
                (["vocal cord"], 0.8),
                (["choking", "feeling"], 0.6)
            ]
            # Add more patterns for other conditions...
        }
        
        # Score each condition
        for condition, pattern_list in patterns.items():
            score = 0
            for keywords, weight in pattern_list:
                if isinstance(keywords, list):
                    if all(keyword in text_lower for keyword in keywords):
                        score += weight
                else:
                    if keywords in text_lower:
                        score += weight
            
            if score > 0:
                condition_scores[condition] = score
        
        if condition_scores:
            best_condition = max(condition_scores, key=condition_scores.get)
            confidence = min(condition_scores[best_condition] / 2.0, 0.95)  # Normalize
            return best_condition, confidence
        
        return None, 0.0

    def rule_based_classifier(self, text):
        """Rule-based classification as fallback"""
        text_lower = text.lower()
        
        # Asthma indicators
        if any(word in text_lower for word in ["wheez", "tight chest", "shortness of breath", "inhaler", "asthma"]):
            return "asthma"
        
        # Bronchiolitis indicators  
        elif any(word in text_lower for word in ["baby", "infant", "runny nose", "fast breathing", "bronchiolitis"]):
            return "bronchiolitis"
            
        # Pneumonia indicators
        elif any(word in text_lower for word in ["fever", "chest pain", "mucus", "pneumonia", "chills"]):
            return "pneumonia"
            
        # Chronic cough indicators
        elif any(word in text_lower for word in ["chronic cough", "persistent cough", "4 weeks", "long cough"]):
            return "chronic cough"
            
        # PVFM indicators
        elif any(word in text_lower for word in ["stridor", "throat tight", "voice loss", "inhaling difficult"]):
            return "paradoxical vocal fold movement"
            
        # Add more conditions...
        elif any(word in text_lower for word in ["noisy breathing", "stenosis", "airway narrow"]):
            return "subglottic stenosis"
            
        elif any(word in text_lower for word in ["nosebleed", "bleeding", "telangiectasia"]):
            return "hereditary hemorrhagic telangiectasia"
            
        elif any(word in text_lower for word in ["choking feeding", "tracheoesophageal", "fistula"]):
            return "tracheoesophageal fistula"
            
        elif any(word in text_lower for word in ["hoarse cry", "laryngeal web", "weak voice"]):
            return "laryngeal web"
            
        elif any(word in text_lower for word in ["wet cough chronic", "ciliary", "sinus infection"]):
            return "primary ciliary dyskinesia"
            
        elif any(word in text_lower for word in ["pulmonary hypertension", "fainting", "blue lips"]):
            return "pulmonary arterial hypertension"
            
        elif any(word in text_lower for word in ["feeding difficult", "esophageal atresia", "choking milk"]):
            return "esophageal atresia"
            
        elif any(word in text_lower for word in ["asbestos", "lung scar", "occupational"]):
            return "asbestosis"
            
        return None

    def generate_response(self, user_input, history):
        """Generate response based on user input using ML models"""
        if not user_input or not user_input.strip():
            return "Please describe your child's symptoms so I can help you better."
        
        # Use ML model for classification
        condition, confidence = self.predict_condition_ml(user_input)
        
        # Set confidence threshold
        confidence_threshold = 0.15
        
        if not condition or confidence < confidence_threshold:
            return f"""I need more specific information to help you better. Please describe:

• **Specific symptoms** (e.g., "wheezing", "fever", "difficulty breathing")
• **How long** has your child had these symptoms?
• **Your child's age** (infant, toddler, school-age)
• **When symptoms occur** (night, during activity, after eating)
• **Any triggers** you've noticed

**Example:** "My 3-year-old has been wheezing at night and coughs when running"

**Remember:** This is for educational purposes only. Always consult a pediatrician for proper medical evaluation.

**Current analysis:** Based on your input "{user_input}", I detected some patterns but need more details for accurate information (confidence: {confidence:.2f})."""

        # Get information from knowledge base
        info = self.knowledge_base.get(condition)
        if not info:
            return f"I identified this might be related to **{condition.replace('_', ' ').title()}** (confidence: {confidence:.2f}), but I need more information to provide specific guidance. Please consult a pediatric pulmonologist."

        # Build comprehensive response with confidence indicator
        confidence_indicator = "High" if confidence > 0.6 else "Moderate" if confidence > 0.3 else "Low"
        
        response = f"## **Analysis Result**\n\n"
        response += f"**Possible Condition:** {condition.replace('_', ' ').title()}\n"
        response += f"**Confidence Level:** {confidence_indicator} ({confidence:.2f})\n\n"
        
        response += f"**Definition:**\n{info['definition']}\n\n"
        
        if info['symptoms']:
            response += "**Common Symptoms:**\n"
            for symptom in info['symptoms'][:4]:
                response += f"• {symptom}\n"
            response += "\n"
        
        if info['red_flags']:
            response += "**RED FLAGS - Seek URGENT Medical Care if you notice:**\n"
            for flag in info['red_flags']:
                response += f"•{flag}\n"
            response += "\n"
        
        if info['advice']:
            response += f"**General Advice:**\n{info['advice']}\n\n"
        
        # Add confidence-based disclaimer
        if confidence < 0.4:
            response += "**Low Confidence Notice:**\nThis prediction has lower confidence. Please provide more specific symptoms or consult a healthcare provider for accurate assessment.\n\n"
        
        response += """---
**IMPORTANT DISCLAIMER:**
- This is for educational purposes only
- Always consult a pediatrician for proper diagnosis  
- If symptoms worsen or red flags appear, seek immediate medical attention
- Call emergency services if your child has severe breathing difficulty"""

        return response

# Initialize chatbot
chatbot = PediatricPulmonologyChatbot()

def chat_function(message, history):
    """Main chat function for Gradio"""
    if not message or not message.strip():
        return "", history
    
    try:
        response = chatbot.generate_response(message, history)
    except Exception as e:
        response = f"I encountered an error processing your request. Please try rephrasing your question or consult a healthcare provider. Error: {str(e)}"
    
    history.append([message, response])
    return "", history

def clear_conversation():
    """Clear chat history"""
    return [], ""

# Create Gradio interface
with gr.Blocks(
    theme=gr.themes.Soft(),
    title="Pediatric Pulmonology Assistant",
    css="""
    .gradio-container {
        max-width: 900px;
        margin: 0 auto;
    }
    .warning-box {
        background-color: #fff3cd;
        border: 1px solid #ffeaa7;
        border-radius: 5px;
        padding: 10px;
        margin: 10px 0;
    }
    """
) as demo:
    
    # Header
    gr.Markdown("""
    # Pediatric Pulmonology Chatbot
    
    **AI-Powered Support for Understanding Childhood Respiratory Conditions**
    
    This AI assistant helps users understand a wide range of pediatric respiratory conditions affecting children. Developed as the final project by Team 3, it offers medically-informed guidance based on user input, making complex symptoms easier to interpret.
    
    ---
    """)
    
    # Warning Box
    gr.HTML("""
       <style>
        .warning-box {
            background-color: #f44336;  /* Bright red */
            color: white;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-family: Arial, sans-serif;
        }
        .warning-box h3 {
            margin-top: 0;
        }
    </style>
    <div class="warning-box">
        <h3> Medical Disclaimer</h3>
        <p><strong>This tool is for educational purposes only and is not a substitute for professional medical advice.</strong> 
        Always consult with a qualified pediatrician or healthcare provider for proper diagnosis and treatment. 
        In case of emergency or severe symptoms, call emergency services immediately.</p>
    </div>
    """)
    
    # Main Interface
    with gr.Row():
        with gr.Column():
            chatbot_interface = gr.Chatbot(
                height=500,
                show_label=False
            )
            
            with gr.Row():
                msg = gr.Textbox(
                    placeholder="Describe your child's symptoms (e.g., 'My 3-year-old has been wheezing and coughing at night')",
                    container=False,
                    scale=4,
                    lines=2
                )
                send_btn = gr.Button("Send", scale=1, variant="primary")
            
            with gr.Row():
                clear_btn = gr.Button("Clear Chat", variant="secondary")
                
    # Quick Examples
    gr.Markdown("""
    Example Questions You Can Ask:
    
    - "My 2-year-old has been wheezing and coughing, especially at night"
    - "Baby has runny nose, fast breathing, and won't eat well" 
    - "Child has persistent cough for 6 weeks after a cold"
    - "My child makes a high-pitched sound when breathing in"
    - "Toddler has fever, chest pain, and is breathing fast"
    """)
    
    # Conditions Covered
    with gr.Accordion("Conditions This Assistant Can Help With", open=False):
        gr.Markdown("""
        **Common Conditions:**
        - Asthma
        - Bronchiolitis  
        - Pneumonia
        - Chronic Cough
        
        **Specialized Conditions:**
        - Paradoxical Vocal Fold Movement (PVFM)
        - Subglottic Stenosis
        - Acute Respiratory Distress Syndrome (ARDS)
        - Tracheoesophageal Fistula
        - Laryngeal Web
        - Primary Ciliary Dyskinesia
        - Pulmonary Arterial Hypertension
        - Hereditary Hemorrhagic Telangiectasia
        - Esophageal Atresia
        - Asbestosis (rare in children)
        """)
    
    # Event handlers
    msg.submit(chat_function, inputs=[msg, chatbot_interface], outputs=[msg, chatbot_interface])
    send_btn.click(chat_function, inputs=[msg, chatbot_interface], outputs=[msg, chatbot_interface])
    clear_btn.click(clear_conversation, outputs=[chatbot_interface, msg])

# Launch the app
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True
    )