File size: 32,730 Bytes
e6541df
 
a8131ef
010f6d4
e6541df
 
 
 
 
 
 
010f6d4
e6541df
 
 
 
ca5dc36
 
90787b9
e6541df
ca5dc36
e6541df
 
 
ca5dc36
e6541df
 
ca5dc36
e6541df
 
 
 
 
 
 
 
 
ca5dc36
 
 
 
 
010f6d4
 
 
 
 
 
 
 
 
 
 
 
 
ca5dc36
 
e6541df
ca5dc36
e6541df
ca5dc36
e6541df
 
 
 
ca5dc36
e6541df
 
 
 
ca5dc36
e6541df
ca5dc36
e6541df
 
 
 
ca5dc36
e6541df
ca5dc36
e6541df
 
 
 
ca5dc36
e6541df
ca5dc36
 
 
 
 
 
 
 
e6541df
 
 
 
ca5dc36
e6541df
ca5dc36
e6541df
 
 
 
ca5dc36
e6541df
ca5dc36
e6541df
 
 
 
ca5dc36
e6541df
ca5dc36
 
e6541df
 
 
 
 
ca5dc36
 
e6541df
 
 
ca5dc36
e6541df
ca5dc36
 
a8131ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6541df
 
 
ca5dc36
e6541df
ca5dc36
 
 
e6541df
ca5dc36
 
e6541df
 
ca5dc36
 
 
e6541df
 
 
 
 
 
 
 
ca5dc36
e6541df
ca5dc36
 
e6541df
ca5dc36
e6541df
010f6d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca5dc36
e6541df
ca5dc36
e6541df
ca5dc36
 
010f6d4
ca5dc36
e6541df
 
 
 
 
ca5dc36
e6541df
 
ca5dc36
 
 
a8131ef
 
ca5dc36
e6541df
a8131ef
 
 
 
ca5dc36
e6541df
ca5dc36
 
 
 
 
e6541df
ca5dc36
 
 
 
 
a8131ef
ca5dc36
 
a8131ef
 
ca5dc36
a8131ef
ca5dc36
a8131ef
ca5dc36
 
 
 
a8131ef
 
ca5dc36
e6541df
ca5dc36
e6541df
010f6d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca5dc36
 
e6541df
010f6d4
 
 
 
 
 
 
 
 
 
 
ca5dc36
 
010f6d4
 
e6541df
ca5dc36
 
e6541df
a8131ef
ca5dc36
e6541df
a8131ef
ca5dc36
 
e6541df
a8131ef
ca5dc36
e6541df
ca5dc36
 
 
 
 
 
 
 
90787b9
 
ca5dc36
 
e6541df
 
ca5dc36
 
 
 
010f6d4
 
 
 
 
a8131ef
ca5dc36
 
 
a8131ef
ca5dc36
 
 
 
 
 
 
 
 
 
 
e6541df
ca5dc36
 
010f6d4
 
 
ca5dc36
 
 
a8131ef
 
ca5dc36
010f6d4
ca5dc36
010f6d4
 
ca5dc36
010f6d4
e6541df
a8131ef
ca5dc36
e6541df
a8131ef
ca5dc36
010f6d4
ca5dc36
010f6d4
 
 
ca5dc36
 
 
 
e6541df
90787b9
 
 
 
 
ef5e8b6
 
90787b9
ef5e8b6
90787b9
010f6d4
 
 
 
ef5e8b6
90787b9
010f6d4
 
 
 
 
 
 
 
 
90787b9
 
 
ef5e8b6
90787b9
ef5e8b6
90787b9
 
 
010f6d4
90787b9
010f6d4
 
 
90787b9
 
010f6d4
90787b9
 
 
e6541df
ca5dc36
010f6d4
e6541df
010f6d4
ca5dc36
fceb8a5
ca5dc36
a8131ef
fceb8a5
a8131ef
fceb8a5
 
 
010f6d4
ca5dc36
e6541df
010f6d4
 
 
 
 
 
 
 
 
 
 
90787b9
 
a8131ef
90787b9
fceb8a5
 
 
 
 
 
010f6d4
fceb8a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca5dc36
e6541df
010f6d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90787b9
a8131ef
fceb8a5
 
ca5dc36
 
a8131ef
fceb8a5
 
 
 
7ae48d4
 
90787b9
fceb8a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
010f6d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fceb8a5
 
 
 
 
 
 
010f6d4
 
 
 
 
 
 
 
 
 
 
ca5dc36
 
fceb8a5
90787b9
fceb8a5
a8131ef
 
fceb8a5
 
a8131ef
90787b9
fceb8a5
 
a8131ef
 
90787b9
fceb8a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
010f6d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fceb8a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
010f6d4
 
 
 
 
 
fceb8a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
010f6d4
 
 
 
 
 
 
 
 
 
 
 
fceb8a5
 
 
 
 
 
 
 
 
 
 
 
ca5dc36
010f6d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Hybrid AI Assistant - General Purpose + Healthcare Billing Expert
Enhanced with Emotional UI and Voice Input
"""

import os
import sys
import json
import logging
import re
from typing import Dict, Optional, Tuple, List, Any
from dataclasses import dataclass, field
from enum import Enum
import requests
import gradio as gr
from datetime import datetime
import random
import time

# Set up environment
os.environ['OPENROUTER_API_KEY'] = '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'

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

# ============= Data Classes =============

@dataclass
class CodeInfo:
    code: str
    description: str
    code_type: str
    additional_info: Optional[str] = None
    category: Optional[str] = None

@dataclass
class ConversationContext:
    messages: List[Dict[str, str]] = field(default_factory=list)
    detected_codes: List[str] = field(default_factory=list)
    last_topic: Optional[str] = None
    current_sentiment: str = "neutral"
    sentiment_history: List[str] = field(default_factory=list)

class SentimentType(Enum):
    VERY_POSITIVE = "very_positive"
    POSITIVE = "positive"
    NEUTRAL = "neutral"
    NEGATIVE = "negative"
    VERY_NEGATIVE = "very_negative"
    ANXIOUS = "anxious"
    FRUSTRATED = "frustrated"
    EXCITED = "excited"
    CONFUSED = "confused"

# ============= Healthcare Billing Database =============

class BillingCodesDB:
    def __init__(self):
        self.codes = {
            'A0429': CodeInfo(
                code='A0429',
                description='Ambulance service, basic life support, emergency transport (BLS-emergency)',
                code_type='HCPCS',
                additional_info='Ground ambulance emergency transport with BLS level care. Used for emergency situations requiring immediate medical transport.',
                category='Ambulance Services'
            ),
            'A0428': CodeInfo(
                code='A0428',
                description='Ambulance service, basic life support, non-emergency transport',
                code_type='HCPCS',
                additional_info='Scheduled or non-urgent medical transport with basic life support.',
                category='Ambulance Services'
            ),
            '99213': CodeInfo(
                code='99213',
                description='Office visit for established patient, low complexity',
                code_type='CPT',
                additional_info='Typically 20-29 minutes. For straightforward medical issues.',
                category='E&M Services'
            ),
            '99214': CodeInfo(
                code='99214',
                description='Office visit for established patient, moderate complexity',
                code_type='CPT',
                additional_info='Typically 30-39 minutes. For moderately complex medical issues.',
                category='E&M Services'
            ),
            '99215': CodeInfo(
                code='99215',
                description='Office visit for established patient, high complexity',
                code_type='CPT',
                additional_info='Typically 40-54 minutes. For complex medical decision making.',
                category='E&M Services'
            ),
            '93000': CodeInfo(
                code='93000',
                description='Electrocardiogram (ECG/EKG) with interpretation',
                code_type='CPT',
                additional_info='Complete 12-lead ECG including test, interpretation, and report.',
                category='Cardiovascular'
            ),
            'DRG470': CodeInfo(
                code='DRG470',
                description='Major hip and knee joint replacement without complications',
                code_type='DRG',
                additional_info='Medicare payment group for joint replacement surgeries.',
                category='Orthopedic'
            ),
            'Z79.899': CodeInfo(
                code='Z79.899',
                description='Other long term drug therapy',
                code_type='ICD-10',
                additional_info='Indicates patient is on long-term medication.',
                category='Diagnosis'
            ),
            'E1399': CodeInfo(
                code='E1399',
                description='Durable medical equipment, miscellaneous',
                code_type='HCPCS',
                additional_info='For DME not elsewhere classified.',
                category='Equipment'
            ),
            'J3420': CodeInfo(
                code='J3420',
                description='Vitamin B-12 injection',
                code_type='HCPCS',
                additional_info='Cyanocobalamin up to 1000 mcg.',
                category='Injections'
            ),
            '80053': CodeInfo(
                code='80053',
                description='Comprehensive metabolic panel',
                code_type='CPT',
                additional_info='14 blood tests including glucose, kidney, and liver function.',
                category='Laboratory'
            ),
            '70450': CodeInfo(
                code='70450',
                description='CT head/brain without contrast',
                code_type='CPT',
                additional_info='Computed tomography of head without contrast material.',
                category='Radiology'
            ),
            '90837': CodeInfo(
                code='90837',
                description='Psychotherapy, 60 minutes',
                code_type='CPT',
                additional_info='Individual psychotherapy session.',
                category='Mental Health'
            ),
            '36415': CodeInfo(
                code='36415',
                description='Venipuncture (blood draw)',
                code_type='CPT',
                additional_info='Collection of blood by needle.',
                category='Laboratory'
            ),
            '99282': CodeInfo(
                code='99282',
                description='Emergency department visit, low-moderate severity',
                code_type='CPT',
                additional_info='ED visit for problems of low to moderate severity.',
                category='Emergency'
            )
        }
    
    def lookup(self, code: str) -> Optional[CodeInfo]:
        code = code.strip().upper()
        if code in self.codes:
            return self.codes[code]
        if code.isdigit() and len(code) == 3:
            drg_code = f"DRG{code}"
            if drg_code in self.codes:
                return self.codes[drg_code]
        return None
    
    def search_codes(self, text: str) -> List[str]:
        """Extract potential billing codes from text"""
        found_codes = []
        patterns = [
            r'\b([A-V][0-9]{4})\b',  # HCPCS
            r'\b([0-9]{5})\b',  # CPT
            r'\bDRG\s*([0-9]{3})\b',  # DRG
            r'\b([A-Z][0-9]{2}\.?[0-9]{0,3})\b',  # ICD-10
        ]
        
        for pattern in patterns:
            matches = re.findall(pattern, text.upper())
            for match in matches:
                if self.lookup(match):
                    found_codes.append(match)
        
        return found_codes

# ============= Sentiment Analysis =============

class SentimentAnalyzer:
    def __init__(self):
        self.positive_words = ['great', 'awesome', 'excellent', 'fantastic', 'wonderful', 'amazing', 'perfect', 'love', 'happy', 'excited', 'thank', 'thanks', 'good', 'nice', 'brilliant', 'outstanding']
        self.negative_words = ['terrible', 'awful', 'horrible', 'bad', 'worst', 'hate', 'frustrated', 'angry', 'sad', 'disappointed', 'upset', 'confused', 'difficult', 'problem', 'issue', 'error', 'wrong']
        self.anxious_words = ['worried', 'concerned', 'nervous', 'anxious', 'scared', 'afraid', 'stress', 'panic', 'uncertain', 'unsure']
        self.excited_words = ['excited', 'thrilled', 'amazing', 'wow', 'incredible', 'fantastic', 'brilliant', 'awesome']
        
    def analyze_sentiment(self, text: str) -> SentimentType:
        text_lower = text.lower()
        
        positive_count = sum(1 for word in self.positive_words if word in text_lower)
        negative_count = sum(1 for word in self.negative_words if word in text_lower)
        anxious_count = sum(1 for word in self.anxious_words if word in text_lower)
        excited_count = sum(1 for word in self.excited_words if word in text_lower)
        
        # Check for question marks (confusion indicator)
        question_marks = text.count('?')
        exclamation_marks = text.count('!')
        
        # Determine sentiment
        if excited_count > 0 or exclamation_marks > 1:
            return SentimentType.EXCITED
        elif anxious_count > 0:
            return SentimentType.ANXIOUS
        elif question_marks > 1 and negative_count > 0:
            return SentimentType.CONFUSED
        elif negative_count > positive_count and negative_count > 1:
            return SentimentType.VERY_NEGATIVE if negative_count > 2 else SentimentType.NEGATIVE
        elif positive_count > negative_count and positive_count > 1:
            return SentimentType.VERY_POSITIVE if positive_count > 2 else SentimentType.POSITIVE
        elif 'frustrated' in text_lower or 'frustrating' in text_lower:
            return SentimentType.FRUSTRATED
        else:
            return SentimentType.NEUTRAL

# ============= AI Assistant Class =============

class HybridAIAssistant:
    def __init__(self):
        self.api_key = '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'
        self.billing_db = BillingCodesDB()
        self.sentiment_analyzer = SentimentAnalyzer()
        self.context = ConversationContext()
        
        self.headers = {
            'Authorization': f'Bearer {self.api_key}',
            'Content-Type': 'application/json',
            'HTTP-Referer': 'https://huggingface.co',
            'X-Title': 'Hybrid AI Assistant'
        }
    
    def detect_intent(self, message: str) -> Dict[str, Any]:
        """Detect if the message is about billing codes or general conversation"""
        lower_msg = message.lower()
        
        # Check for billing codes in the message
        codes = self.billing_db.search_codes(message)
        
        # Keywords that suggest billing/medical coding questions
        billing_keywords = ['code', 'cpt', 'hcpcs', 'icd', 'drg', 'billing', 'medical code', 
                          'healthcare code', 'diagnosis code', 'procedure code']
        
        is_billing = any(keyword in lower_msg for keyword in billing_keywords) or len(codes) > 0
        
        return {
            'is_billing': is_billing,
            'codes_found': codes,
            'message': message
        }
    
    def handle_billing_query(self, message: str, codes: List[str]) -> str:
        """Handle healthcare billing specific queries"""
        responses = []
        
        if codes:
            for code in codes[:3]:  # Limit to first 3 codes
                info = self.billing_db.lookup(code)
                if info:
                    response = f"**{info.code} ({info.code_type})**\n"
                    response += f"πŸ“‹ **Description:** {info.description}\n"
                    if info.additional_info:
                        response += f"ℹ️ **Details:** {info.additional_info}\n"
                    if info.category:
                        response += f"🏷️ **Category:** {info.category}\n"
                    responses.append(response)
        
        if responses:
            final_response = "I found information about the billing code(s) you mentioned:\n\n"
            final_response += "\n---\n".join(responses)
            final_response += "\n\nπŸ’‘ **Need more details?** Feel free to ask specific questions about these codes!"
            return final_response
        else:
            return self.get_general_response(message, billing_context=True)
    
    def get_empathetic_response_prefix(self, sentiment: SentimentType) -> str:
        """Generate empathetic response based on sentiment"""
        prefixes = {
            SentimentType.VERY_POSITIVE: "I'm so glad to hear your enthusiasm! 🌟 ",
            SentimentType.POSITIVE: "That's wonderful! 😊 ",
            SentimentType.EXCITED: "I can feel your excitement! πŸŽ‰ ",
            SentimentType.ANXIOUS: "I understand this might be causing some concern. Let me help ease your worries. πŸ€— ",
            SentimentType.FRUSTRATED: "I can sense your frustration, and I'm here to help make this easier for you. πŸ’™ ",
            SentimentType.CONFUSED: "No worries, I'm here to clear things up for you! 🧠 ",
            SentimentType.NEGATIVE: "I hear that you're having some difficulties. Let me help you with that. πŸ’š ",
            SentimentType.VERY_NEGATIVE: "I'm really sorry you're going through this. I'm here to support you. ❀️ ",
            SentimentType.NEUTRAL: ""
        }
        return prefixes.get(sentiment, "")
    
    def get_general_response(self, message: str, billing_context: bool = False) -> str:
        """Get response from OpenRouter API for general queries"""
        
        # Analyze sentiment
        sentiment = self.sentiment_analyzer.analyze_sentiment(message)
        self.context.current_sentiment = sentiment.value
        self.context.sentiment_history.append(sentiment.value)
        
        # Keep only last 10 sentiments
        if len(self.context.sentiment_history) > 10:
            self.context.sentiment_history = self.context.sentiment_history[-10:]
        
        # Prepare system prompt with empathy
        system_prompt = """You are a helpful, friendly, and empathetic AI assistant with expertise in healthcare billing codes. 
        You can assist with any topic - from casual conversation to complex questions. 
        When discussing medical billing codes, you provide accurate, detailed information.
        Be conversational, helpful, and engaging. Show empathy and understanding.
        Adapt your tone based on the user's emotional state - be more supportive if they seem frustrated or anxious."""
        
        if billing_context:
            system_prompt += "\nThe user is asking about medical billing. Provide helpful information even if you don't have specific code details."
        
        # Build conversation history for context
        messages = [{'role': 'system', 'content': system_prompt}]
        
        # Add recent conversation history (last 5 exchanges)
        for msg in self.context.messages[-10:]:
            messages.append(msg)
        
        # Add current message
        messages.append({'role': 'user', 'content': message})
        
        try:
            response = requests.post(
                'https://openrouter.ai/api/v1/chat/completions',
                headers=self.headers,
                json={
                    'model': 'openai/gpt-3.5-turbo',
                    'messages': messages,
                    'temperature': 0.7,
                    'max_tokens': 500,
                    'stream': False
                },
                timeout=30
            )
            
            if response.status_code == 200:
                result = response.json()
                ai_response = result['choices'][0]['message']['content']
                
                # Add empathetic prefix based on sentiment
                empathy_prefix = self.get_empathetic_response_prefix(sentiment)
                if empathy_prefix:
                    ai_response = empathy_prefix + ai_response
                
                # Update context
                self.context.messages.append({'role': 'user', 'content': message})
                self.context.messages.append({'role': 'assistant', 'content': ai_response})
                
                # Keep only last 20 messages in context
                if len(self.context.messages) > 20:
                    self.context.messages = self.context.messages[-20:]
                
                return ai_response
            else:
                logger.error(f"API error: {response.status_code}")
                return self.get_fallback_response(message)
                
        except Exception as e:
            logger.error(f"Request failed: {e}")
            return self.get_fallback_response(message)
    
    def get_fallback_response(self, message: str) -> str:
        """Fallback responses when API fails"""
        sentiment = self.sentiment_analyzer.analyze_sentiment(message)
        empathy_prefix = self.get_empathetic_response_prefix(sentiment)
        
        fallbacks = [
            "I'm having trouble connecting right now, but I'm still here to help! Could you rephrase your question?",
            "Let me think about that differently. What specific aspect would you like to know more about?",
            "That's an interesting question! While I process that, is there anything specific you'd like to explore?",
            "I'm here to help! Could you provide a bit more detail about what you're looking for?"
        ]
        return empathy_prefix + random.choice(fallbacks)
    
    def process_message(self, message: str) -> Tuple[str, str]:
        """Main method to process any message and return response with sentiment"""
        if not message.strip():
            return "Feel free to ask me anything! I can help with general questions or healthcare billing codes. 😊", "neutral"
        
        # Detect intent
        intent = self.detect_intent(message)
        
        # Route to appropriate handler
        if intent['is_billing'] and intent['codes_found']:
            response = self.handle_billing_query(message, intent['codes_found'])
        else:
            response = self.get_general_response(message, billing_context=intent['is_billing'])
        
        return response, self.context.current_sentiment
    
    def reset_context(self):
        """Reset conversation context"""
        self.context = ConversationContext()

# ============= Global Assistant Instance =============
assistant = HybridAIAssistant()

# ============= Chat Functions =============

def respond(message, history):
    """Response function for ChatInterface"""
    if not message.strip():
        return "Feel free to ask me anything! I can help with general questions or healthcare billing codes. 😊"
    
    # Process message and get sentiment
    response, sentiment = assistant.process_message(message)
    
    # Update UI based on sentiment (this will be handled by JavaScript)
    return response

def process_voice_input(audio):
    """Process voice input and return text"""
    if audio is None:
        return "No audio received. Please try again."
    
    # For now, return a placeholder message
    # In a real implementation, you'd use speech recognition here
    return "Voice input processed! (Speech recognition would be implemented here)"

def reset_chat():
    """Reset the conversation context"""
    assistant.reset_context()
    return []

# ============= Examples =============

examples = [
    "What is healthcare billing code A0429?",
    "Can you explain CPT code 99213 in detail?", 
    "Tell me about DRG 470",
    "I'm feeling frustrated with this billing issue",
    "This is confusing, can you help me understand?",
    "Thank you so much! This is exactly what I needed!",
    "How does artificial intelligence work?",
    "Give me a simple pasta recipe",
    "Write a short poem about nature"
]

# ============= Create Interface =============

def create_interface():
    """Create the Gradio ChatInterface with Emotional UI and Voice Input"""
    
    # Enhanced CSS with emotional UI and voice features
    custom_css = """
    /* Global Styles */
    .gradio-container {
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif !important;
        max-width: 1200px !important;
        margin: auto !important;
        background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%) !important;
        min-height: 100vh !important;
        padding: 1rem !important;
        transition: all 0.5s ease !important;
    }
    
    /* Emotional UI Color Schemes */
    .sentiment-positive { background: linear-gradient(135deg, #84fab0 0%, #8fd3f4 100%) !important; }
    .sentiment-very-positive { background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%) !important; }
    .sentiment-negative { background: linear-gradient(135deg, #d299c2 0%, #fef9d7 100%) !important; }
    .sentiment-very-negative { background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%) !important; }
    .sentiment-anxious { background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%) !important; }
    .sentiment-frustrated { background: linear-gradient(135deg, #ff8a80 0%, #ffad80 100%) !important; }
    .sentiment-excited { background: linear-gradient(135deg, #ffd89b 0%, #19547b 100%) !important; }
    .sentiment-confused { background: linear-gradient(135deg, #a8caba 0%, #5d4e75 100%) !important; }
    
    /* Enhanced Header with Mood Indicator */
    .header-text {
        text-align: center;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        padding: 3rem 2rem;
        border-radius: 20px;
        margin-bottom: 2rem;
        box-shadow: 0 20px 40px rgba(102, 126, 234, 0.3);
        position: relative;
        overflow: hidden;
        transition: all 0.5s ease;
    }
    
    .header-text::before {
        content: '';
        position: absolute;
        top: -50%;
        left: -50%;
        width: 200%;
        height: 200%;
        background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
        animation: pulse 4s ease-in-out infinite;
    }
    
    @keyframes pulse {
        0%, 100% { opacity: 0.3; }
        50% { opacity: 0.6; }
    }
    
    /* Mood Indicator */
    .mood-indicator {
        position: absolute;
        top: 1rem;
        right: 1rem;
        width: 60px;
        height: 60px;
        border-radius: 50%;
        background: rgba(255,255,255,0.2);
        backdrop-filter: blur(10px);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
        transition: all 0.5s ease;
        animation: breathe 3s ease-in-out infinite;
    }
    
    @keyframes breathe {
        0%, 100% { transform: scale(1); }
        50% { transform: scale(1.05); }
    }
    
    .mood-positive { background: rgba(132, 250, 176, 0.3) !important; }
    .mood-negative { background: rgba(255, 154, 158, 0.3) !important; }
    .mood-anxious { background: rgba(255, 236, 210, 0.3) !important; }
    .mood-excited { background: rgba(255, 216, 155, 0.3) !important; }
    
    .header-text h1 {
        margin: 0;
        font-size: 3rem;
        font-weight: 800;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 1rem;
        position: relative;
        z-index: 1;
        text-shadow: 0 2px 4px rgba(0,0,0,0.2);
    }
    
    .header-text p {
        margin: 1rem 0 0 0;
        font-size: 1.2rem;
        opacity: 0.95;
        position: relative;
        z-index: 1;
        font-weight: 300;
    }
    
    .badge {
        background: rgba(255,255,255,0.25) !important;
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255,255,255,0.3);
        animation: glow 2s ease-in-out infinite alternate;
    }
    
    @keyframes glow {
        from { box-shadow: 0 0 5px rgba(255,255,255,0.3); }
        to { box-shadow: 0 0 20px rgba(255,255,255,0.6); }
    }
    
    /* Voice Input Button */
    .voice-btn {
        background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%) !important;
        color: white !important;
        border: none !important;
        border-radius: 50% !important;
        width: 60px !important;
        height: 60px !important;
        font-size: 24px !important;
        margin: 0.5rem !important;
        transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) !important;
        box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3) !important;
        position: relative;
        overflow: hidden;
    }
    
    .voice-btn:hover {
        transform: scale(1.1) !important;
        box-shadow: 0 8px 25px rgba(255, 107, 107, 0.5) !important;
    }
    
    .voice-btn.recording {
        animation: recordPulse 1s ease-in-out infinite !important;
        background: linear-gradient(135deg, #ff3030 0%, #ff1010 100%) !important;
    }
    
    @keyframes recordPulse {
        0% { box-shadow: 0 0 0 0 rgba(255, 107, 107, 0.7); }
        70% { box-shadow: 0 0 0 20px rgba(255, 107, 107, 0); }
        100% { box-shadow: 0 0 0 0 rgba(255, 107, 107, 0); }
    }
    
    /* Chat Interface Styling with Emotional Feedback */
    .gradio-chatinterface {
        background: white !important;
        border-radius: 20px !important;
        box-shadow: 0 25px 50px rgba(0,0,0,0.15) !important;
        padding: 2rem !important;
        margin: 1rem 0 !important;
        backdrop-filter: blur(10px) !important;
        transition: all 0.5s ease !important;
    }
    
    .gradio-chatinterface.emotional-positive {
        border: 2px solid rgba(132, 250, 176, 0.5) !important;
        box-shadow: 0 25px 50px rgba(132, 250, 176, 0.2) !important;
    }
    
    .gradio-chatinterface.emotional-negative {
        border: 2px solid rgba(255, 154, 158, 0.5) !important;
        box-shadow: 0 25px 50px rgba(255, 154, 158, 0.2) !important;
    }
    
    /* Enhanced Buttons */
    .reset-btn {
        background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%) !important;
        color: white !important;
        border: none !important;
        border-radius: 12px !important;
        padding: 0.75rem 1.5rem !important;
        font-weight: 600 !important;
        margin: 0.5rem 0 !important;
        transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) !important;
        box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3) !important;
    }
    
    .reset-btn:hover {
        background: linear-gradient(135deg, #ff5252 0%, #d32f2f 100%) !important;
        transform: translateY(-2px) scale(1.02) !important;
        box-shadow: 0 8px 25px rgba(255, 107, 107, 0.4) !important;
    }
    
    /* Example Buttons Enhancement */
    .gradio-chatinterface .examples .example {
        background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%) !important;
        border: 2px solid #e2e8f0 !important;
        border-radius: 15px !important;
        padding: 0.75rem 1rem !important;
        margin: 0.5rem !important;
        transition: all 0.3s ease !important;
        box-shadow: 0 2px 8px rgba(0,0,0,0.05) !important;
    }
    
    .gradio-chatinterface .examples .example:hover {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
        color: white !important;
        border-color: #667eea !important;
        transform: translateY(-2px) !important;
        box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3) !important;
    }
    
    /* Enhanced Stats Cards */
    .stats-container {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 1.5rem;
        margin: 2rem 0;
    }
    
    .stat-card {
        background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
        border-radius: 16px;
        padding: 2rem;
        text-align: center;
        box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        border: 1px solid #e2e8f0;
        transition: all 0.3s ease;
        position: relative;
        overflow: hidden;
    }
    
    .stat-card::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 4px;
        background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
    }
    
    .stat-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 20px 40px rgba(0,0,0,0.15);
    }
    
    .stat-number {
        font-size: 2.5rem;
        font-weight: 800;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
        margin-bottom: 0.5rem;
    }
    
    .stat-label {
        color: #64748b;
        font-size: 0.9rem;
        font-weight: 500;
        text-transform: uppercase;
        letter-spacing: 1px;
    }
    
    /* Empathy Animations */
    @keyframes empathyPulse {
        0%, 100% { transform: scale(1); }
        50% { transform: scale(1.02); }
    }
    
    @keyframes supportGlow {
        0%, 100% { box-shadow: 0 0 10px rgba(102, 126, 234, 0.3); }
        50% { box-shadow: 0 0 20px rgba(102, 126, 234, 0.6); }
    }
    
    .empathy-support {
        animation: empathyPulse 2s ease-in-out infinite, supportGlow 3s ease-in-out infinite;
    }
    
    /* Voice Recognition Feedback */
    .voice-feedback {
        position: fixed;
        bottom: 2rem;
        right: 2rem;
        background: rgba(102, 126, 234, 0.9);
        color: white;
        padding: 1rem 1.5rem;
        border-radius: 50px;
        backdrop-filter: blur(10px);
        z-index: 1000;
        animation: slideInRight 0.3s ease-out;
    }
    
    @keyframes slideInRight {
        from { transform: translateX(100%); opacity: 0; }
        to { transform: translateX(0); opacity: 1; }
    }
    
    /* Enhanced Accordion */
    .gradio-accordion {
        background: rgba(255,255,255,0.9) !important;
        backdrop-filter: blur(10px) !important;
        border-radius: 16px !important;
        border: 1px solid rgba(255,255,255,0.2) !important;
        box-shadow: 0 8px 25px rgba(0,0,0,0.1) !important;
        margin: 1.5rem 0 !important;
    }
    
    /* Feature Cards */
    .feature-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
        gap: 1.5rem;
        margin: 2rem 0;
    }
    
    .feature-card {
        background: rgba(255,255,255,0.95);
        backdrop-filter: blur(10px);
        border-radius: 16px;
        padding: 1.5rem;
        border: 1px solid rgba(255,255,255,0.3);
        box-shadow: 0 8px 25px rgba(0,0,0,0.1);
        transition: all 0.3s ease;
    }
    
    .feature-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 15px 35px rgba(0,0,0,0.15);
    }
    
    .feature-icon {
        font-size: 2rem;
        margin-bottom: 1rem;
    }
    
    .feature-title {
        color: #2d3748;
        font-size: 1.25rem;
        font-weight: 600;
        margin-bottom: 0.5rem;
    }
    
    .feature-desc {
        color: #64748b;
        line-height: 1.6;
    }
    
    /* Responsive Design */
    @media (max-width: 768px) {
        .gradio-container {
            padding: 0.5rem !important;
        }
        
        .header-text h1 {
            font-size: 2rem;
            flex-direction: column;
            gap: 0.5rem;
        }
        
        .header-text {
            padding: 2rem 1rem;
        }
        
        .stats-container {
            grid-template-columns: repeat(2, 1fr);
            gap: 1rem;
        }
        
        .stat-card {
            padding: 1.5rem;
        }
        
        .feature-grid {
            grid-template-columns: 1fr;
            gap: 1rem;
        }
        
        .mood-indicator {
            width: 50px;
            height: 50px;
            font-size: 20px;
        }
        
        .voice-btn {
            width: 50px !important;
            height: 50px !important;
            font-size: 20px !important;
        }
    }
    
    /* Loading Animation */
    @keyframes shimmer {
        0% { background-position: -468px 0; }
        100% { background-position: 468px 0; }
    }
    
    .loading {
        animation: shimmer 1.5s ease-in-out infinite;
        background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
        background-size: 400% 100%;
    }
    
    /* Sentiment-based message styling */
    .message-positive {
        border-left: 4px solid #84fab0 !important;
        background: linear-gradient(135deg, rgba(132, 250, 176, 0.1) 0%, rgba(143, 211, 244, 0.1) 100%) !important;
    }
    
    .message-negative {
        border-left: 4px solid #ff9a9e !important;
        background: linear-gradient(135deg, rgba(255, 154, 158, 0.1) 0%, rgba(254, 207, 239, 0.1) 100%) !important;
    }
    
    .message-anxious {
        border-left: 4px solid #ffecd2 !important;
        background: linear-gradient(135deg, rgba(255, 236, 210, 0.1) 0%, rgba(252, 182, 159, 0.1) 100%) !important;
    }
    """