File size: 28,484 Bytes
98d76bd
9454822
ce580ca
98d76bd
 
 
 
 
 
 
 
 
 
dc409a3
98d76bd
 
9454822
98d76bd
ce580ca
 
 
98d76bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce580ca
 
98d76bd
ce580ca
98d76bd
 
 
 
 
 
 
 
 
 
 
f6923bc
98d76bd
 
 
 
f6923bc
ce580ca
98d76bd
ce580ca
da25227
ce580ca
 
 
 
da25227
 
 
98d76bd
ce580ca
 
 
da25227
98d76bd
e49dc64
 
 
 
1d35a0c
dc409a3
f6923bc
 
 
 
1d35a0c
 
 
 
 
 
 
 
 
f6923bc
1d35a0c
f6923bc
 
 
 
 
 
 
 
e49dc64
1d35a0c
 
 
e49dc64
 
f6923bc
 
ce580ca
f6923bc
 
e49dc64
f6923bc
 
944fcc4
f6923bc
 
1d35a0c
f6923bc
 
 
 
 
 
 
 
 
1d35a0c
f6923bc
 
 
 
 
 
 
 
 
 
 
e49dc64
f6923bc
e49dc64
ce580ca
e49dc64
 
 
 
 
 
ce580ca
e49dc64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce580ca
e49dc64
 
 
f6923bc
e49dc64
f6923bc
 
 
 
 
 
 
 
e49dc64
 
 
 
 
 
 
 
 
 
 
 
ce580ca
 
 
e49dc64
 
ce580ca
e49dc64
ce580ca
 
 
 
 
 
 
e49dc64
ce580ca
 
 
e49dc64
f6923bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e49dc64
 
 
 
f6923bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e49dc64
f6923bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9454822
1298db9
b33cb3c
73f83b1
f6923bc
 
4cabc00
f6923bc
 
73f83b1
944fcc4
 
 
f6923bc
 
 
 
944fcc4
 
 
 
 
 
f6923bc
944fcc4
d5166a8
944fcc4
73f83b1
 
f6923bc
944fcc4
 
f6923bc
 
f8fc8c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b33cb3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
944fcc4
 
 
 
f6923bc
 
 
 
944fcc4
 
 
 
 
 
 
 
b33cb3c
f6923bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b33cb3c
 
f8fc8c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b33cb3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
944fcc4
 
b33cb3c
 
944fcc4
 
b33cb3c
 
944fcc4
 
b33cb3c
 
944fcc4
 
b33cb3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56274ac
f6923bc
 
b33cb3c
 
 
 
 
 
 
 
 
 
f6923bc
b33cb3c
 
 
 
944fcc4
 
 
 
b33cb3c
f6923bc
 
a43a340
f6923bc
b33cb3c
944fcc4
f6923bc
 
b33cb3c
f6923bc
 
 
 
 
 
 
b33cb3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8fc8c8
f6e71bf
73f83b1
4cabc00
73f83b1
 
 
1298db9
4dac01b
f6923bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b33cb3c
4cabc00
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
# Standard Libraries
import os
import json
import time
import asyncio
import logging
import gc
import re
import traceback
from pathlib import Path
from datetime import datetime
from typing import List, Dict, Union, Tuple, Optional, Any
from dataclasses import dataclass, field
import zipfile

# Machine Learning and Deep Learning Libraries
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.cuda.amp import autocast
from torch.utils.data import DataLoader
import tensorflow as tf
import keras
import numpy as np

# Hugging Face and Transformers
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer
from sentence_transformers import SentenceTransformer
from datasets import load_dataset, Dataset, concatenate_datasets
from huggingface_hub import login

# FAISS and PEFT
import faiss
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training, TaskType, PeftModel

# LangChain - updated imports as per recent deprecations
from langchain_community.vectorstores import FAISS  # Updated import
from langchain_community.embeddings import HuggingFaceEmbeddings  # Updated import
from langchain_community.document_loaders import TextLoader  # Updated import
from langchain.text_splitter import RecursiveCharacterTextSplitter

# Data Science and Visualization Libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from sklearn.metrics import classification_report, confusion_matrix

# Development and Testing
import pytest
from unittest.mock import Mock, patch

# External Tools and APIs
import wandb
import requests
import gradio as gr
import IPython.display as display  # Required for IPython display functionality
from dotenv import load_dotenv
from tqdm.auto import tqdm

# Suppress Warnings
import warnings
warnings.filterwarnings('ignore')


# Ensure Hugging Face login
try:
    hf_token = os.getenv("HF_TOKEN")
    if hf_token:
        login(token=hf_token)
    print("Login successful!")
except Exception as e:
    print("Hugging Face Login failed:", e)


os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:64,garbage_collection_threshold:0.8,expandable_segments:True'
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'



# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)



class ModelManager:
    """Handles model loading and resource management"""
    
    @staticmethod
    def verify_and_extract_model(checkpoint_zip_path: str, extracted_model_dir: str) -> str:
        """Verify and extract the model if it's not already extracted"""
        if not os.path.exists(extracted_model_dir):
            # Unzip the model if it hasn’t been extracted yet
            with zipfile.ZipFile(checkpoint_zip_path, 'r') as zip_ref:
                zip_ref.extractall(extracted_model_dir)
            logger.info(f"Extracted model to: {extracted_model_dir}")
        else:
            logger.info(f"Model already extracted: {extracted_model_dir}")
            
        return extracted_model_dir
    
    @staticmethod
    def clear_gpu_memory():
        """Clear GPU memory cache"""
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
            gc.collect()

class PearlyBot:
    def __init__(self, model_zip_path: str = "./checkpoint-500.zip", model_dir: str = "./checkpoint-500"):
        self.model_dir = ModelManager.verify_and_extract_model(model_zip_path, model_dir)
        self.setup_model(self.model_dir)
        self.setup_rag()
        self.conversation_history = []
        self.last_interaction_time = time.time()
        self.interaction_cooldown = 1.0  # seconds
        
    def setup_model(self, model_path: str):
        """Initialize the model with proper error handling"""
        try:
            logger.info("Starting model initialization...")
            ModelManager.clear_gpu_memory()
            
            # Load tokenizer
            try:
                self.tokenizer = AutoTokenizer.from_pretrained(model_path)
                self.tokenizer.pad_token = self.tokenizer.eos_token
                logger.info("Tokenizer loaded successfully")
            except Exception as e:
                logger.error(f"Failed to load tokenizer: {str(e)}")
                raise
            
            # Load model
            try:
                self.model = AutoModelForCausalLM.from_pretrained(
                    model_path,
                    device_map="auto",
                    load_in_8bit=True,
                    torch_dtype=torch.float16,
                    low_cpu_mem_usage=True
                )
                self.model.eval()
                logger.info("Model loaded successfully")
            except Exception as e:
                logger.error(f"Failed to load model: {str(e)}")
                raise
                
        except Exception as e:
            logger.error(f"Error in model setup: {str(e)}")
            raise

    def setup_rag(self):
        try:
            logger.info("Setting up RAG system...")
            # Load your knowledge base content
            knowledge_base = {
                "triage_scenarios.txt": """Medical Triage Scenarios and Responses:

EMERGENCY (999) SCENARIOS:
1. Cardiovascular:
- Chest pain/pressure
- Heart attack symptoms
- Irregular heartbeat with dizziness
Response: Immediate 999 call, sit/lie down, chew aspirin if available

2. Respiratory:
- Severe breathing difficulty
- Choking
- Unable to speak full sentences
Response: 999, sitting position, clear airway

3. Neurological:
- Stroke symptoms (FAST)
- Seizures
- Unconsciousness
Response: 999, recovery position if unconscious

4. Trauma:
- Severe bleeding
- Head injuries with confusion
- Major burns
Response: 999, apply direct pressure to bleeding

URGENT CARE (111) SCENARIOS:
1. Moderate Symptoms:
- Persistent fever
- Non-severe infections
- Minor injuries
Response: 111 contact, monitor symptoms

2. Minor Emergencies:
- Small cuts needing stitches
- Sprains and strains
- Mild allergic reactions
Response: 111 or urgent care visit

GP APPOINTMENT SCENARIOS:
1. Routine Care:
- Chronic condition review
- Medication reviews
- Non-urgent symptoms
Response: Book routine GP appointment

2. Preventive Care:
- Vaccinations
- Health screenings
- Regular check-ups
Response: Schedule with GP reception""",
                "emergency_detection.txt": """Enhanced Emergency Detection Criteria:

IMMEDIATE LIFE THREATS:
1. Cardiac Symptoms:
- Chest pain/pressure/tightness
- Pain spreading to arms/jaw/neck
- Sweating with nausea
- Shortness of breath

2. Breathing Problems:
- Severe shortness of breath
- Blue lips or face
- Unable to complete sentences
- Choking/airway blockage

3. Neurological:
- FAST (Face, Arms, Speech, Time)
- Sudden confusion
- Severe headache
- Seizures
- Loss of consciousness

4. Severe Trauma:
- Heavy bleeding
- Deep wounds
- Head injury with confusion
- Severe burns
- Broken bones with deformity

5. Anaphylaxis:
- Sudden swelling
- Difficulty breathing
- Rapid onset rash
- Light-headedness

URGENT BUT NOT IMMEDIATE:
1. Moderate Symptoms:
- Persistent fever
- Dehydration
- Non-severe infections
- Minor injuries

2. Worsening Conditions:
- Increasing pain
- Progressive symptoms
- Medication reactions

RESPONSE PROTOCOLS:
1. For Life Threats:
- Immediate 999 call
- Clear first aid instructions
- Stay on line until help arrives

2. For Urgent Care:
- 111 contact
- Monitor for worsening
- Document symptoms""",
                "gp_booking.txt": """GP Appointment Booking Templates:

APPOINTMENT TYPES:
1. Routine Appointments:
Template: "I need to book a routine appointment for [condition]. My availability is [times/dates]. My GP is Dr. [name] if available."

2. Follow-up Appointments:
Template: "I need a follow-up appointment regarding [condition] discussed on [date]. My previous appointment was with Dr. [name]."

3. Medication Reviews:
Template: "I need a medication review for [medication]. My last review was [date]."

BOOKING INFORMATION NEEDED:
1. Patient Details:
- Full name
- Date of birth
- NHS number (if known)
- Registered GP practice

2. Appointment Details:
- Nature of appointment
- Preferred times/dates
- Urgency level
- Special requirements

3. Contact Information:
- Phone number
- Alternative contact
- Preferred contact method

BOOKING PROCESS:
1. Online Booking:
- NHS app instructions
- Practice website guidance
- System navigation help

2. Phone Booking:
- Best times to call
- Required information
- Queue management tips

3. Special Circumstances:
- Interpreter needs
- Accessibility requirements
- Transport arrangements""",
                "cultural_sensitivity.txt": """Cultural Sensitivity Guidelines:

CULTURAL AWARENESS:
1. Religious Considerations:
- Prayer times
- Religious observations
- Dietary restrictions
- Gender preferences for care
- Religious festivals/fasting periods

2. Language Support:
- Interpreter services
- Multi-language resources
- Clear communication methods
- Family involvement preferences

3. Cultural Beliefs:
- Traditional medicine practices
- Cultural health beliefs
- Family decision-making
- Privacy customs

COMMUNICATION APPROACHES:
1. Respectful Interaction:
- Use preferred names/titles
- Appropriate greetings
- Non-judgmental responses
- Active listening

2. Language Usage:
- Clear, simple terms
- Avoid medical jargon
- Confirm understanding
- Respect silence/pauses

3. Non-verbal Communication:
- Eye contact customs
- Personal space
- Body language awareness
- Gesture sensitivity

SPECIFIC CONSIDERATIONS:
1. South Asian Communities:
- Family involvement
- Gender sensitivity
- Traditional medicine
- Language diversity

2. Middle Eastern Communities:
- Gender-specific care
- Religious observations
- Family hierarchies
- Privacy concerns

3. African/Caribbean Communities:
- Traditional healers
- Community involvement
- Historical medical mistrust
- Cultural specific conditions

4. Eastern European Communities:
- Direct communication
- Family involvement
- Medical documentation
- Language support

INCLUSIVE PRACTICES:
1. Appointment Scheduling:
- Religious holidays
- Prayer times
- Family availability
- Interpreter needs

2. Treatment Planning:
- Cultural preferences
- Traditional practices
- Family involvement
- Dietary requirements

3. Support Services:
- Community resources
- Cultural organizations
- Language services
- Social support""",
                "service_boundaries.txt": """Service Limitations and Professional Boundaries:

CLEAR BOUNDARIES:
1. Medical Advice:
- No diagnoses
- No prescriptions
- No treatment recommendations
- No medical procedures
- No second opinions

2. Emergency Services:
- Clear referral criteria
- Documented responses
- Follow-up protocols
- Handover procedures

3. Information Sharing:
- Confidentiality limits
- Data protection
- Record keeping
- Information governance

PROFESSIONAL CONDUCT:
1. Communication:
- Professional language
- Emotional boundaries
- Personal distance
- Service scope

2. Service Delivery:
- No financial transactions
- No personal relationships
- Clear role definition
- Professional limits"""
            }
            
            os.makedirs("knowledge_base", exist_ok=True)
            
            # Create and process documents
            documents = []
            for filename, content in knowledge_base.items():
                with open(f"knowledge_base/{filename}", "w") as f:
                    f.write(content)
                documents.append(content)
            
            # Setup embeddings and vector store
            self.embeddings = HuggingFaceEmbeddings(
                model_name="sentence-transformers/all-MiniLM-L6-v2"
            )
            
            text_splitter = RecursiveCharacterTextSplitter(
                chunk_size=300,
                chunk_overlap=100
            )
            
            texts = text_splitter.split_text("\n\n".join(documents))
            self.vector_store = FAISS.from_texts(texts, self.embeddings)
            logger.info("RAG system setup complete")
            
        except Exception as e:
            logger.error(f"Error setting up RAG: {str(e)}")
            raise

    def get_relevant_context(self, query):
        try:
            docs = self.vector_store.similarity_search(query, k=3)
            return "\n".join(doc.page_content for doc in docs)
        except Exception as e:
            logger.error(f"Error retrieving context: {str(e)}")
            return ""

    @torch.inference_mode()
    def generate_response(self, message: str, history: list) -> str:
        """Generate response using both fine-tuned model and RAG"""
        try:
            # Rate limiting
            current_time = time.time()
            if current_time - self.last_interaction_time < self.interaction_cooldown:
                time.sleep(self.interaction_cooldown)
            
            # Clear GPU memory before generation
            ModelManager.clear_gpu_memory()
            
            # Get RAG context
            context = self.get_relevant_context(message)
            
            # Format conversation history
            conv_history = "\n".join([
                f"User: {user}\nAssistant: {assistant}"
                for user, assistant in history[-3:]  # Keep last 3 turns
            ])
            
            # Create prompt
            prompt = f"""<start_of_turn>system
Using these medical guidelines:

{context}

Previous conversation:
{conv_history}

Guidelines:
1. Assess symptoms and severity
2. Ask relevant follow-up questions
3. Direct to appropriate care (999, 111, or GP)
4. Show empathy and cultural sensitivity
5. Never diagnose or recommend treatments
<end_of_turn>
<start_of_turn>user
{message}
<end_of_turn>
<start_of_turn>assistant"""

            # Generate response
            try:
                inputs = self.tokenizer(
                    prompt,
                    return_tensors="pt",
                    truncation=True,
                    max_length=512
                ).to(self.model.device)

                outputs = self.model.generate(
                    **inputs,
                    max_new_tokens=256,
                    min_new_tokens=20,
                    do_sample=True,
                    temperature=0.7,
                    top_p=0.9,
                    repetition_penalty=1.2,
                    no_repeat_ngram_size=3
                )
                
                response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
                response = response.split("<start_of_turn>assistant")[-1].strip()
                if "<end_of_turn>" in response:
                    response = response.split("<end_of_turn>")[0].strip()
                
                self.last_interaction_time = time.time()
                return response
                
            except torch.cuda.OutOfMemoryError:
                ModelManager.clear_gpu_memory()
                logger.error("GPU out of memory, cleared cache and retrying...")
                return "I apologize, but I'm experiencing technical difficulties. Please try again."
                
        except Exception as e:
            logger.error(f"Error generating response: {str(e)}")
            return "I apologize, but I encountered an error. Please try again."
            
    def handle_feedback(self, message: str, response: str, feedback: int):
        """Handle user feedback for responses"""
        try:
            timestamp = datetime.now().isoformat()
            feedback_data = {
                "message": message,
                "response": response,
                "feedback": feedback,
                "timestamp": timestamp
            }
            
            # Log feedback
            logger.info(f"Feedback received: {feedback_data}")
            
            # Here you could:
            # 1. Store feedback in a database
            # 2. Send to monitoring system
            # 3. Use for model improvements
            
            return True
        except Exception as e:
            logger.error(f"Error handling feedback: {e}")
            return False

    def __del__(self):
        """Cleanup resources"""
        try:
            if hasattr(self, 'model'):
                del self.model
            ModelManager.clear_gpu_memory()
        except Exception as e:
            logger.error(f"Error in cleanup: {e}")

    def process_feedback(positive: bool, comment: str, history: List[Dict[str, str]]):
        try:
            if not history or len(history) < 2:
                return gr.update(value="")
                
            last_user_msg = history[-2]["content"] if isinstance(history[-2], dict) else history[-2][0]
            last_bot_msg = history[-1]["content"] if isinstance(history[-1], dict) else history[-1][1]
            
            bot.handle_feedback(
                message=last_user_msg,
                response=last_bot_msg,
                feedback=1 if positive else -1
            )
            
            return gr.update(value="")
            
        except Exception as e:
            logger.error(f"Error processing feedback: {e}")
            return gr.update(value="")

def create_demo():
    """Set up Gradio interface for the chatbot with enhanced styling and functionality."""
    try:
        # Initialize bot
        bot = PearlyBot()

        def chat(message: str, history: list):
            """Handle chat interactions"""
            try:
                if not message.strip():
                    return history
                
                # Generate response
                response = bot.generate_response(message, history)
                
                # Update history with proper formatting
                history.append({
                    "role": "user",
                    "content": message
                })
                history.append({
                    "role": "assistant",
                    "content": response
                })
                return history
                
            except Exception as e:
                logger.error(f"Chat error: {e}")
                return history + [{
                    "role": "assistant",
                    "content": "I apologize, but I'm experiencing technical difficulties. For emergencies, please call 999."
                }]

        def process_feedback(positive: bool, comment: str, history: list):
            try:
                if not history or len(history) < 2:
                    return gr.update(value="")
                
                last_user_msg = history[-2]["content"] if isinstance(history[-2], dict) else history[-2][0]
                last_bot_msg = history[-1]["content"] if isinstance(history[-1], dict) else history[-1][1]
                
                bot.handle_feedback(
                    message=last_user_msg,
                    response=last_bot_msg,
                    feedback=1 if positive else -1
                )
                
                return gr.update(value="")
            except Exception as e:
                logger.error(f"Error processing feedback: {e}")
                return gr.update(value="")
        

        # Create enhanced Gradio interface
        with gr.Blocks(theme=gr.themes.Soft(
            primary_hue="blue",
            secondary_hue="indigo",
            neutral_hue="slate",
            font=gr.themes.GoogleFont("Inter")
        )) as demo:
            # Custom CSS for enhanced styling
            gr.HTML("""
                <style>
                    .container { max-width: 900px; margin: auto; }
                    .header { text-align: center; padding: 20px; }
                    .emergency-banner {
                        background-color: #ff4444;
                        color: white;
                        padding: 10px;
                        text-align: center;
                        font-weight: bold;
                        margin-bottom: 20px;
                    }
                    .feature-card {
                        padding: 15px;
                        border-radius: 10px;
                        text-align: center;
                        transition: transform 0.2s;
                        color: white;
                        font-weight: bold;
                    }
                    .feature-card:nth-child(1) { background: linear-gradient(135deg, #2193b0, #6dd5ed); }
                    .feature-card:nth-child(2) { background: linear-gradient(135deg, #834d9b, #d04ed6); }
                    .feature-card:nth-child(3) { background: linear-gradient(135deg, #ff4b1f, #ff9068); }
                    .feature-card:nth-child(4) { background: linear-gradient(135deg, #38ef7d, #11998e); }
                    .feature-card:hover {
                        transform: translateY(-5px);
                        box-shadow: 0 5px 15px rgba(0,0,0,0.2);
                    }
                    .feature-card span.emoji {
                        font-size: 2em;
                        display: block;
                        margin-bottom: 10px;
                    }
                    .message-textbox textarea { resize: none; }
                    #thumb-up, #thumb-down {
                        min-width: 60px;
                        padding: 8px;
                        margin: 5px;
                    }
                    .chatbot-message {
                        padding: 12px;
                        margin: 8px 0;
                        border-radius: 8px;
                    }
                    .user-message { background-color: #e3f2fd; }
                    .assistant-message { background-color: #f5f5f5; }
                    .feedback-section {
                        margin-top: 20px;
                        padding: 15px;
                        border-radius: 8px;
                        background-color: #f8f9fa;
                    }
                </style>
            """)
            # Event Handlers - Moved inside the gr.Blocks context
            msg.submit(chat, [msg, chatbot], [chatbot]).then(
                lambda: gr.update(value=""), None, [msg]
            )
            
            submit.click(chat, [msg, chatbot], [chatbot]).then(
                lambda: gr.update(value=""), None, [msg]
            )
            
            # Feedback handlers
            feedback_positive.click(
                lambda h: process_feedback(True, feedback_text.value, h),
                inputs=[chatbot],
                outputs=[feedback_text]
            )
            
            feedback_negative.click(
                lambda h: process_feedback(False, feedback_text.value, h),
                inputs=[chatbot],
                outputs=[feedback_text]
            )
            
            # Clear chat
            clear.click(lambda: None, None, chatbot)

            # Add queue for handling multiple users
            demo.queue(concurrency_count=1, max_size=10)

            # Emergency Banner
            gr.HTML("""
                <div class="emergency-banner">
                    🚨 For medical emergencies, always call 999 immediately 🚨
                </div>
            """)

            # Header Section
            with gr.Row(elem_classes="header"):
                gr.Markdown("""
                    # GP Medical Triage Assistant - Pearly
                    Welcome to your personal medical triage assistant. I'm here to help assess your symptoms and guide you to appropriate care.
                """)

            # Main Features Grid
            gr.HTML("""
                <div class="features-grid">
                    <div class="feature-card">
                        <span class="emoji">πŸ₯</span>
                        <div>GP Appointments</div>
                    </div>
                    <div class="feature-card">
                        <span class="emoji">πŸ”</span>
                        <div>Symptom Assessment</div>
                    </div>
                    <div class="feature-card">
                        <span class="emoji">⚑</span>
                        <div>Urgent Care Guide</div>
                    </div>
                    <div class="feature-card">
                        <span class="emoji">πŸ’Š</span>
                        <div>Medical Advice</div>
                    </div>
                </div>
            """)

            # Chat Interface
            with gr.Row():
                with gr.Column(scale=4):
                    chatbot = gr.Chatbot(
                        value=[{
                            "role": "assistant",
                            "content": "Hello! I'm Pearly, your GP medical assistant. How can I help you today?"
                        }],
                        height=500,
                        elem_id="chatbot",
                        type="messages",
                        show_label=False
                    )
                    
                    with gr.Row():
                        msg = gr.Textbox(
                            label="Your message",
                            placeholder="Type your message here...",
                            lines=2,
                            scale=4,
                            autofocus=True,
                            submit_on_enter=True
                        )
                        submit = gr.Button("Send", variant="primary", scale=1)

                with gr.Column(scale=1):
                    # Quick Actions Panel
                    gr.Markdown("### Quick Actions")
                    emergency_btn = gr.Button("🚨 Emergency Info", variant="secondary")
                    nhs_111_btn = gr.Button("πŸ“ž NHS 111 Info", variant="secondary")
                    booking_btn = gr.Button("πŸ“… GP Booking", variant="secondary")
                    
                    # Controls and Feedback
                    gr.Markdown("### Controls")
                    clear = gr.Button("πŸ—‘οΈ Clear Chat")
                    
                    gr.Markdown("### Feedback")
                    with gr.Row():
                        feedback_positive = gr.Button("πŸ‘", elem_id="thumb-up")
                        feedback_negative = gr.Button("πŸ‘Ž", elem_id="thumb-down")
                    
                    feedback_text = gr.Textbox(
                        label="Additional comments",
                        placeholder="Tell us more...",
                        lines=2,
                        visible=True
                    )
                    feedback_submit = gr.Button("Submit Feedback", visible=True)

            # Examples and Information
            with gr.Accordion("Example Messages", open=False):
                gr.Examples([
                    ["I've been having severe headaches for the past week"],
                    ["I need to book a routine checkup"],
                    ["I'm feeling very anxious lately and need help"],
                    ["My child has had a fever for 2 days"],
                    ["I need information about COVID-19 testing"]
                ], inputs=msg)

            with gr.Accordion("NHS Services Guide", open=False):
                gr.Markdown("""
                    ### Emergency Services (999)
                    - Life-threatening emergencies
                    - Severe injuries
                    - Suspected heart attack or stroke
                    
                    ### NHS 111
                    - Urgent but non-emergency situations
                    - Medical advice needed
                    - Unsure where to go
                    
                    ### GP Services
                    - Routine check-ups
                    - Non-urgent medical issues
                    - Prescription renewals
                """)

        

        return demo

    except Exception as e:
        logger.error(f"Error creating demo: {e}")
        raise

if __name__ == "__main__":
    try:
        # Initialize logging
        logging.basicConfig(level=logging.INFO)
        
        # Load environment variables
        load_dotenv()
        
        # Create and launch demo
        demo = create_demo()
        demo.launch(
            server_name="0.0.0.0",
            server_port=7860,
            show_error=True
        )
        
    except Exception as e:
        logger.error(f"Application startup failed: {e}")
        raise