File size: 41,296 Bytes
3512db3
 
 
 
 
 
 
 
0d78b31
 
 
3512db3
 
0d78b31
 
 
 
 
3512db3
 
0d78b31
 
 
 
 
 
 
 
3512db3
 
 
 
 
0d78b31
3512db3
 
 
 
0d78b31
 
 
 
 
 
 
3512db3
 
 
 
 
 
0d78b31
 
 
 
 
 
 
 
 
 
 
 
 
 
3512db3
 
0d78b31
3512db3
 
 
0d78b31
 
3512db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d78b31
3512db3
 
 
0d78b31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3512db3
0d78b31
3512db3
 
 
0d78b31
 
 
3512db3
0d78b31
3512db3
0d78b31
3512db3
0d78b31
3512db3
0d78b31
 
3512db3
0d78b31
 
3512db3
 
 
0d78b31
3512db3
0d78b31
3512db3
0d78b31
3512db3
0d78b31
 
3512db3
 
 
 
 
 
0d78b31
3512db3
0d78b31
3512db3
0d78b31
3512db3
0d78b31
3512db3
 
 
 
 
 
0d78b31
3512db3
 
0d78b31
3512db3
 
0d78b31
3512db3
 
0d78b31
3512db3
 
0d78b31
3512db3
 
 
 
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
0d78b31
3512db3
 
 
0d78b31
 
 
 
 
 
 
 
 
 
3512db3
 
0d78b31
 
 
 
3512db3
 
 
 
 
 
 
 
 
0d78b31
3512db3
 
 
0d78b31
3512db3
 
0d78b31
3512db3
 
 
 
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
0d78b31
3512db3
 
 
 
 
 
 
 
 
 
 
 
0d78b31
3512db3
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
 
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
0d78b31
3512db3
 
 
 
 
 
0d78b31
 
 
 
 
 
 
 
 
 
 
 
 
3512db3
0d78b31
3512db3
 
0d78b31
3512db3
 
 
 
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
 
 
 
 
0d78b31
3512db3
 
0d78b31
3512db3
 
 
 
 
 
 
 
0d78b31
3512db3
 
 
 
 
0d78b31
3512db3
 
 
 
 
 
 
0d78b31
3512db3
0d78b31
 
 
 
 
 
 
 
3512db3
0d78b31
3512db3
 
 
0d78b31
3512db3
 
 
 
0d78b31
3512db3
0d78b31
 
 
3512db3
 
 
 
0d78b31
3512db3
 
 
 
0d78b31
3512db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d78b31
3512db3
0d78b31
3512db3
0d78b31
3512db3
 
 
 
 
 
 
0d78b31
3512db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
0d78b31
3512db3
 
 
 
 
 
 
 
0d78b31
3512db3
 
 
 
 
 
 
0d78b31
3512db3
 
 
0d78b31
3512db3
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
0d78b31
3512db3
0d78b31
3512db3
0d78b31
 
 
 
 
 
 
3512db3
 
0d78b31
 
 
 
 
3512db3
0d78b31
3512db3
0d78b31
3512db3
 
0d78b31
 
 
 
 
3512db3
 
 
0d78b31
 
 
 
 
 
 
 
3512db3
0d78b31
 
 
3512db3
 
0d78b31
 
3512db3
0d78b31
3512db3
 
 
 
 
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
 
 
 
 
 
 
 
 
0d78b31
3512db3
 
 
 
 
 
 
 
 
 
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
 
 
 
 
 
0d78b31
3512db3
0d78b31
3512db3
 
0d78b31
 
3512db3
 
 
 
 
 
 
0d78b31
3512db3
 
0d78b31
3512db3
 
 
 
0d78b31
3512db3
 
0d78b31
3512db3
 
0d78b31
3512db3
0d78b31
3512db3
 
0d78b31
 
3512db3
 
0d78b31
3512db3
 
0d78b31
 
3512db3
 
 
0d78b31
 
 
3512db3
 
 
 
 
 
 
 
 
 
 
 
 
0d78b31
3512db3
0d78b31
3512db3
 
 
 
 
 
 
 
0d78b31
3512db3
 
 
0d78b31
3512db3
 
 
 
 
 
 
 
0d78b31
3512db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d78b31
3512db3
0d78b31
c141650
ec7fffe
 
c141650
 
0d78b31
 
c141650
 
0d78b31
 
 
 
3512db3
0d78b31
c141650
 
 
 
5b97582
36297a4
c141650
 
0d78b31
c141650
0d78b31
 
 
3512db3
0d78b31
 
 
 
 
 
3512db3
0d78b31
 
 
 
 
 
 
 
 
 
 
 
3512db3
0d78b31
 
 
 
 
 
 
 
 
 
 
 
3512db3
0d78b31
 
 
 
 
 
 
 
 
 
 
 
3512db3
0d78b31
 
 
 
 
 
 
 
 
 
3512db3
0d78b31
 
 
 
 
 
 
 
 
 
 
 
3512db3
0d78b31
 
 
 
 
 
 
 
 
 
3512db3
0d78b31
3512db3
 
 
 
0d78b31
3512db3
 
0d78b31
 
3512db3
 
0d78b31
 
 
3512db3
 
 
 
0d78b31
 
3512db3
0d78b31
3512db3
 
 
 
 
 
 
0d78b31
3512db3
0d78b31
 
3512db3
 
 
 
 
 
 
 
0d78b31
 
3512db3
 
0d78b31
 
3512db3
0d78b31
 
3512db3
0d78b31
 
 
 
 
 
 
 
 
3512db3
0d78b31
 
 
 
 
 
3512db3
0d78b31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3512db3
 
0d78b31
 
 
3512db3
0d78b31
 
 
 
36297a4
 
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
#!/usr/bin/env python3
"""
VoiceBridge.AI - Production Ready Universal Communication Platform
Supporting: Blind, Deaf, Non-Verbal, Deaf-Blind Users
"""

import os
import logging
import json
import tempfile
import time
from datetime import datetime
from pathlib import Path
from typing import List

import gradio as gr
import speech_recognition as sr
import pyttsx3
import torch
from transformers import pipeline

# Optional imports (may fail gracefully in some environments)
try:
    import cv2
    import numpy as np
except Exception:
    cv2 = None
    np = None

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)


class ProductionVoiceBridge:
    """
    Production-grade universal communication system for all disabilities
    """

    def __init__(self, allow_microphone: bool = False):
        """
        If allow_microphone is False (default) we will not force initialization
        of system microphone β€” avoids failures in headless/HF Spaces.
        """
        self.allow_microphone = allow_microphone
        self.setup_directories()
        self.load_config()
        self.current_mode = "universal"
        self.user_preferences = {}
        self.conversation_history = []
        self.emergency_contacts = []
        # initialize engines in try so failures are caught
        self._init_defaults()
        self.initialize_engines()

    def _init_defaults(self):
        # placeholders so other methods can rely on attributes
        self.tts_engine = None
        self.recognizer = None
        self.microphone = None
        self.speech_to_text_model = None
        self.image_caption_model = None
        self.emergency_mode = False
        self.last_emergency_check = time.time()

    def setup_directories(self):
        """Create necessary directories for production"""
        Path("data").mkdir(parents=True, exist_ok=True)
        Path("data/conversations").mkdir(parents=True, exist_ok=True)
        Path("data/emergency").mkdir(parents=True, exist_ok=True)
        Path("data/user_profiles").mkdir(parents=True, exist_ok=True)
        Path("data/feedback").mkdir(parents=True, exist_ok=True)

    def load_config(self):
        """Load production configuration"""
        self.config = {
            "api_timeout": 30,
            "max_audio_length": 60,
            "emergency_check_interval": 5,
            "backup_interval": 300,
            "supported_languages": ["en", "es", "fr", "de"],
            "haptic_patterns": {
                "emergency": [500, 200, 500],
                "notification": [200],
                "confirmation": [100, 100],
                "error": [100, 50, 100, 50, 100]
            }
        }

    def initialize_engines(self):
        """Initialize all AI engines and hardware interfaces"""
        try:
            # Text-to-Speech Engine (pyttsx3 is local and usually safe)
            try:
                self.tts_engine = pyttsx3.init()
                voices = self.tts_engine.getProperty('voices') or []
                if voices:
                    self.tts_engine.setProperty('voice', voices[0].id)
                self.tts_engine.setProperty('rate', 160)
                self.tts_engine.setProperty('volume', 0.8)
            except Exception as e:
                logger.warning(f"TTS engine init failed: {e}")
                self.tts_engine = None

            # Speech Recognition (recognizer only; microphone optional)
            try:
                self.recognizer = sr.Recognizer()
            except Exception as e:
                logger.warning(f"SpeechRecognition init failed: {e}")
                self.recognizer = None

            # Only attempt to initialize Microphone if explicitly allowed
            if self.allow_microphone:
                try:
                    self.microphone = sr.Microphone()
                    # adjust_for_ambient_noise can fail in headless; guard it
                    try:
                        with self.microphone as source:
                            self.recognizer.adjust_for_ambient_noise(source, duration=1)
                    except Exception as e:
                        logger.warning(f"Ambient noise adjust failed: {e}")
                except Exception as e:
                    logger.warning(f"Microphone not available: {e}")
                    self.microphone = None
            else:
                # keep microphone None in headless mode to avoid exceptions
                self.microphone = None

            # AI Models with error handling (transformers pipelines)
            self.load_ai_models()

            # Emergency system
            self.emergency_mode = False
            self.last_emergency_check = time.time()

            logger.info("All engines initialized (best-effort)")

        except Exception as e:
            # Log full traceback then re-raise to make failure obvious in dev mode
            logger.error(f"Engine initialization failed: {e}")
            logger.debug("Traceback:", exc_info=True)
            raise

    def load_ai_models(self):
        """Load AI models with fallbacks. These can be heavy; fail gracefully."""
        # Whisper (ASR) - optional; if not available will fall back to sr.Recognizer
        try:
            # device selection: if CUDA available, use it, else CPU
            device = 0 if torch.cuda.is_available() else -1
            self.speech_to_text_model = pipeline(
                "automatic-speech-recognition",
                model="openai/whisper-base",
                device=device
            )
            logger.info("Whisper ASR model loaded")
        except Exception as e:
            logger.warning(f"Whisper model failed to load: {e}")
            self.speech_to_text_model = None

        # Image captioning - optional
        try:
            self.image_caption_model = pipeline(
                "image-to-text",
                model="Salesforce/blip-image-captioning-base",
                device=-1
            )
            logger.info("Image caption model loaded")
        except Exception as e:
            logger.warning(f"Image caption model failed to load: {e}")
            self.image_caption_model = None

    # ==================== UNIVERSAL MODE ====================

    def universal_communication(self, input_data: dict) -> dict:
        """
        Universal communication handler that adapts to any input type
        """
        try:
            input_type = input_data.get('type', 'voice')

            if input_type == 'voice' and input_data.get('audio'):
                return self.handle_voice_input(input_data['audio'])

            elif input_type == 'text' and input_data.get('text'):
                return self.handle_text_input(input_data['text'])

            elif input_type == 'image' and input_data.get('image'):
                return self.handle_image_input(input_data['image'])

            elif input_type == 'command':
                return self.handle_system_command(input_data.get('command', ''))

            else:
                return self.create_response(
                    "Please provide voice, text, or image input",
                    "error"
                )

        except Exception as e:
            logger.error(f"Universal communication error: {e}", exc_info=True)
            return self.create_response(
                "System error. Please try again or use emergency mode.",
                "error"
            )

    def handle_voice_input(self, audio_path: str) -> dict:
        """Process voice input for deaf users and general transcription"""
        try:
            transcript = ""
            # If Hugging Face/transformers ASR available and audio path exists, try it
            if self.speech_to_text_model and audio_path:
                try:
                    # transformers pipelines accept file path
                    out = self.speech_to_text_model(audio_path)
                    transcript = out.get("text", "") if isinstance(out, dict) else str(out)
                except Exception as e:
                    logger.warning(f"HF ASR failed, falling back to sr: {e}")
                    transcript = self.fallback_speech_to_text(audio_path)
            else:
                transcript = self.fallback_speech_to_text(audio_path)

            if not transcript:
                transcript = ""

            # Check for emergency keywords
            if self.detect_emergency_keywords(transcript):
                emergency_result = self.trigger_emergency_mode("voice_triggered")
                return self.create_response(
                    f"EMERGENCY DETECTED: {transcript}\n{emergency_result['message']}",
                    "emergency",
                    audio=emergency_result.get('audio'),
                    visual_alert="πŸ”΄ EMERGENCY ACTIVATED"
                )

            # Check for system commands
            if self.is_system_command(transcript):
                return self.handle_system_command(transcript)

            # Regular communication
            self.add_to_conversation("User", transcript)

            return self.create_response(
                transcript,
                "transcription",
                visual_alert=f"πŸ’¬ New message: {transcript[:50]}..."
            )

        except Exception as e:
            logger.error(f"Voice input error: {e}", exc_info=True)
            return self.create_response(
                "Could not process audio. Please try again.",
                "error"
            )

    def handle_text_input(self, text: str) -> dict:
        """Process text input for non-verbal users"""
        try:
            # Check for emergency
            if self.detect_emergency_keywords(text):
                emergency_result = self.trigger_emergency_mode("text_triggered")
                return self.create_response(
                    f"EMERGENCY: {text}\n{emergency_result['message']}",
                    "emergency",
                    audio=emergency_result.get('audio'),
                    visual_alert="πŸ”΄ EMERGENCY"
                )

            # Convert to speech
            audio_path = self.text_to_speech(text)

            self.add_to_conversation("User", text, "spoken")

            return self.create_response(
                text,
                "communication",
                audio=audio_path,
                visual_alert=f"πŸ—£οΈ Speaking: {text[:30]}..."
            )

        except Exception as e:
            logger.error(f"Text input error: {e}", exc_info=True)
            return self.create_response(
                "Could not process text. Please try again.",
                "error"
            )

    def handle_image_input(self, image_path: str) -> dict:
        """Process image input for blind users"""
        try:
            if not self.image_caption_model:
                description = "I see an image but cannot describe it in detail right now."
            else:
                try:
                    caption_out = self.image_caption_model(image_path)
                    # pipeline returns list or dict depending on version
                    if isinstance(caption_out, list) and caption_out:
                        description = caption_out[0].get('generated_text', '')
                    elif isinstance(caption_out, dict):
                        description = caption_out.get('generated_text', '') or caption_out.get('text', '')
                    else:
                        description = str(caption_out)
                except Exception as e:
                    logger.warning(f"Image captioning failed: {e}")
                    description = "I see an image but cannot describe it in detail right now."

                description = self.enhance_scene_description(description)

            # Convert description to speech
            audio_path = self.text_to_speech(description)

            return self.create_response(
                description,
                "scene_description",
                audio=audio_path
            )

        except Exception as e:
            logger.error(f"Image input error: {e}", exc_info=True)
            return self.create_response(
                "Could not process image. Please try again.",
                "error"
            )

    # ==================== DISABILITY-SPECIFIC MODES ====================

    def blind_mode(self, command: str = None, image_path: str = None) -> dict:
        """Voice-first interface for blind users"""
        if not command and not image_path:
            welcome_msg = (
                "Blind mode activated. Say 'describe scene' to use camera, "
                "'read text' for text recognition, or 'help' for options."
            )
            return self.create_response(welcome_msg, "system", audio=self.text_to_speech(welcome_msg))

        if command:
            command = command.lower()

            if 'describe' in command or 'scene' in command or image_path:
                if image_path:
                    return self.handle_image_input(image_path)
                else:
                    return self.create_response(
                        "Please capture an image using the camera",
                        "instruction"
                    )

            elif 'read' in command or 'text' in command:
                return self.create_response(
                    "Please capture an image containing text",
                    "instruction"
                )

            elif 'navigate' in command or 'direction' in command:
                guidance = "Navigation assistance: Move forward carefully. Obstacle detection active."
                return self.create_response(
                    guidance,
                    "navigation",
                    audio=self.text_to_speech(guidance)
                )

            elif 'help' in command:
                help_text = (
                    "Blind Mode Commands:\n"
                    "β€’ \"Describe scene\" - Describe surroundings using camera\n"
                    "β€’ \"Read text\" - Read text from images\n"
                    "β€’ \"Navigate\" - Get walking directions\n"
                    "β€’ \"Emergency\" - Immediate assistance\n"
                    "β€’ \"Change mode\" - Switch accessibility mode\n"
                )
                return self.create_response(help_text, "help", audio=self.text_to_speech(help_text))

            else:
                response = "Command not recognized. Say 'help' for options."
                return self.create_response(response, "error", audio=self.text_to_speech(response))

    def deaf_mode(self, audio_input: str = None, continuous: bool = False) -> dict:
        """Visual interface for deaf users with real-time transcription"""
        if audio_input:
            result = self.handle_voice_input(audio_input)

            # Add visual enhancements for deaf users
            if result.get('type') == 'transcription':
                result['visual_alert'] = f"πŸ‘‚ TRANSCRIPTION: {result.get('text','')[:100]}..."

            # Check for important sounds
            if self.detect_important_sounds(audio_input):
                result['visual_alert'] = "πŸ”” IMPORTANT SOUND DETECTED! " + result.get('visual_alert', '')
                result['haptic_feedback'] = self.config['haptic_patterns']['notification']

            return result
        else:
            status = "Deaf mode active. Real-time transcription ready. Visual alerts enabled."
            return self.create_response(status, "system", visual_alert="πŸ‘‚ Deaf Mode Active")

    def non_verbal_mode(self, text: str = None, preset: str = None) -> dict:
        """Text-to-speech communication for non-verbal users"""
        if preset:
            phrases = {
                'greeting': "Hello, I use this device to communicate",
                'help': "I need assistance please",
                'medical': "I have a medical condition and may need help",
                'emergency': "This is an emergency! I need immediate assistance!",
                'thanks': "Thank you for your help",
                'yes': "Yes",
                'no': "No",
                'pain': "I am in pain and need medical help",
                'lost': "I am lost and need directions",
                'bathroom': "I need to find a bathroom"
            }
            text_to_speak = phrases.get(preset, preset)
        else:
            text_to_speak = text or "I need help"

        audio_path = self.text_to_speech(text_to_speak)

        self.add_to_conversation("User", text_to_speak, "spoken")

        return self.create_response(
            text_to_speak,
            "communication",
            audio=audio_path,
            visual_alert=f"πŸ—£οΈ Speaking: {text_to_speak}",
            haptic_feedback=self.config['haptic_patterns']['confirmation']
        )

    def deaf_blind_mode(self, input_text: str = None, output_format: str = "haptic") -> dict:
        """Tactile communication for deaf-blind users"""
        if input_text:
            if output_format == "haptic":
                vibration_pattern = self.text_to_vibration_pattern(input_text)
                return self.create_response(
                    f"Message converted to vibrations: {input_text}",
                    "tactile",
                    haptic_feedback=vibration_pattern,
                    braille=self.text_to_braille(input_text)
                )
            else:  # braille output
                braille_text = self.text_to_braille(input_text)
                return self.create_response(
                    f"Braille output: {input_text}",
                    "tactile",
                    braille=braille_text
                )
        else:
            status = "Deaf-blind mode active. Use text input with haptic or braille output."
            return self.create_response(status, "system")

    # ==================== EMERGENCY SYSTEM ====================

    def trigger_emergency_mode(self, trigger_source: str = "manual") -> dict:
        """Activate emergency response system"""
        self.emergency_mode = True
        timestamp = datetime.now().isoformat()

        emergency_data = {
            "status": "EMERGENCY_ACTIVATED",
            "timestamp": timestamp,
            "trigger_source": trigger_source,
            "message": "EMERGENCY! Assistance required immediately!",
            "actions_taken": [],
            "contacts_notified": []
        }

        # Notify emergency contacts
        for contact in self.emergency_contacts:
            try:
                self.notify_emergency_contact(contact, emergency_data)
                emergency_data["contacts_notified"].append(contact)
            except Exception as e:
                logger.error(f"Failed to notify {contact}: {e}")

        # Create emergency audio message
        emergency_audio = self.text_to_speech(emergency_data["message"])
        emergency_data["audio"] = emergency_audio

        # Log emergency
        self.log_emergency(emergency_data)

        return emergency_data

    def notify_emergency_contact(self, contact: str, emergency_data: dict):
        """Notify emergency contact (simplified - in production would use SMS/email)"""
        logger.info(f"EMERGENCY NOTIFICATION to {contact}: {emergency_data['message']}")
        # In production: send SMS, email, or push notification

    # ==================== CORE ENGINE METHODS ====================

    def text_to_speech(self, text: str) -> str:
        """Convert text to speech and return audio file path (best-effort)."""
        if not text:
            return None
        # If no TTS engine, return None gracefully
        if self.tts_engine is None:
            logger.warning("TTS engine not available; returning None for audio path.")
            return None
        try:
            with tempfile.NamedTemporaryFile(delete=False, suffix='.wav', dir='data/') as tmp_file:
                tmp_path = tmp_file.name
            # pyttsx3 uses save_to_file then runAndWait
            self.tts_engine.save_to_file(text, tmp_path)
            self.tts_engine.runAndWait()
            return tmp_path
        except Exception as e:
            logger.error(f"TTS error: {e}", exc_info=True)
            return None

    def fallback_speech_to_text(self, audio_path: str) -> str:
        """Fallback speech recognition using speech_recognition library"""
        if not audio_path:
            return ""
        if self.recognizer is None:
            logger.warning("Recognizer not available; cannot transcribe audio.")
            return ""
        try:
            with sr.AudioFile(audio_path) as source:
                audio = self.recognizer.record(source)
                # Use Google Web Speech API (requires internet)
                text = self.recognizer.recognize_google(audio)
                return text
        except sr.UnknownValueError:
            return ""
        except sr.RequestError as e:
            logger.error(f"Speech recognition RequestError: {e}")
            return ""
        except Exception as e:
            logger.error(f"Fallback STT error: {e}", exc_info=True)
            return ""

    def detect_emergency_keywords(self, text: str) -> bool:
        """Detect emergency keywords in text"""
        if not text:
            return False
        emergency_words = [
            'emergency', 'help', 'urgent', 'danger', 'dangerous',
            'accident', 'injured', 'hurt', 'pain', 'bleeding',
            'fire', 'police', 'ambulance', 'hospital', '911',
            'save me', 'help me', 'i need help'
        ]
        text_lower = text.lower()
        return any(word in text_lower for word in emergency_words)

    def detect_important_sounds(self, audio_path: str) -> bool:
        """Detect important environmental sounds (simplified heuristic)"""
        try:
            transcript = self.fallback_speech_to_text(audio_path)
            important_words = ['help', 'emergency', 'fire', 'watch out', 'danger']
            return any(word in transcript.lower() for word in important_words)
        except Exception:
            return False

    def text_to_vibration_pattern(self, text: str) -> List[int]:
        """Convert text to vibration pattern (simplified Morse code)"""
        morse_code = {
            'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
            'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
            'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
            'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
            'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..',
            '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....',
            '6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----',
            ' ': ' '
        }

        pattern = []
        for char in text.upper():
            if char in morse_code:
                morse = morse_code[char]
                for symbol in morse:
                    if symbol == '.':
                        pattern.extend([100])  # Short vibration
                    elif symbol == '-':
                        pattern.extend([300])  # Long vibration
                    pattern.extend([50])  # Gap between symbols
                pattern.extend([200])  # Gap between letters

        return pattern

    def text_to_braille(self, text: str) -> str:
        """Convert text to braille unicode characters"""
        braille_map = {
            'A': '⠁', 'B': 'β ƒ', 'C': 'β ‰', 'D': 'β ™', 'E': 'β ‘', 'F': 'β ‹', 'G': 'β ›', 'H': 'β “', 'I': '⠊', 'J': '⠚',
            'K': 'β …', 'L': 'β ‡', 'M': '⠍', 'N': '⠝', 'O': 'β •', 'P': '⠏', 'Q': '⠟', 'R': 'β —', 'S': '⠎', 'T': '⠞',
            'U': 'β ₯', 'V': 'β §', 'W': 'β Ί', 'X': 'β ­', 'Y': 'β ½', 'Z': 'β ΅',
            '1': '⠁', '2': 'β ƒ', '3': 'β ‰', '4': 'β ™', '5': 'β ‘', '6': 'β ‹', '7': 'β ›', '8': 'β “', '9': '⠊', '0': '⠚',
            ' ': ' ', '.': 'β ²', ',': 'β ‚', '!': 'β –', '?': 'β ¦'
        }

        return ''.join(braille_map.get(char.upper(), '?') for char in text)

    def enhance_scene_description(self, description: str) -> str:
        """Enhance AI-generated scene descriptions"""
        if not description:
            return description
        enhancements = {
            "indoor": "This appears to be an indoor setting. ",
            "outdoor": "This appears to be an outdoor area. ",
            "people": "There are people visible. ",
            "text": "There is text that could be read. ",
            "obstacle": "Be careful of potential obstacles. ",
        }

        enhanced = description
        desc_lower = description.lower()

        if any(word in desc_lower for word in ['room', 'indoor', 'inside', 'wall']):
            enhanced = enhancements["indoor"] + enhanced
        elif any(word in desc_lower for word in ['outdoor', 'outside', 'sky', 'tree']):
            enhanced = enhancements["outdoor"] + enhanced

        if any(word in desc_lower for word in ['person', 'people', 'man', 'woman']):
            enhanced = enhancements["people"] + enhanced

        if any(word in desc_lower for word in ['sign', 'text', 'letter', 'word']):
            enhanced = enhancements["text"] + enhanced

        return enhanced

    def is_system_command(self, text: str) -> bool:
        """Check if text contains system commands"""
        if not text:
            return False
        commands = ['mode', 'help', 'emergency', 'stop', 'cancel', 'reset']
        return any(command in text.lower() for command in commands)

    def handle_system_command(self, command: str) -> dict:
        """Handle system control commands"""
        command = (command or "").lower()

        if 'blind' in command:
            self.current_mode = "blind"
            response = "Blind mode activated. Voice navigation enabled."
        elif 'deaf blind' in command:
            self.current_mode = "deaf_blind"
            response = "Deaf-blind mode activated. Haptic feedback enabled."
        elif 'deaf' in command:
            self.current_mode = "deaf"
            response = "Deaf mode activated. Visual alerts enabled."
        elif 'non verbal' in command or 'mute' in command:
            self.current_mode = "non_verbal"
            response = "Non-verbal mode activated. Text-to-speech ready."
        elif 'universal' in command:
            self.current_mode = "universal"
            response = "Universal mode activated."
        elif 'emergency' in command:
            return self.trigger_emergency_mode("voice_command")
        else:
            response = f"Current mode: {self.current_mode}. Say 'help' for options."

        return self.create_response(response, "system", audio=self.text_to_speech(response))

    def add_to_conversation(self, speaker: str, text: str, message_type: str = "text"):
        """Add message to conversation history"""
        self.conversation_history.append({
            "timestamp": datetime.now().isoformat(),
            "speaker": speaker,
            "text": text,
            "type": message_type
        })

        # Keep only last 100 messages
        if len(self.conversation_history) > 100:
            self.conversation_history = self.conversation_history[-100:]

    def log_emergency(self, emergency_data: dict):
        """Log emergency event"""
        try:
            filename = f"data/emergency/emergency_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
            with open(filename, 'w') as f:
                json.dump(emergency_data, f, indent=2)
        except Exception as e:
            logger.error(f"Failed to log emergency: {e}")

    def create_response(self, text: str, response_type: str, **kwargs) -> dict:
        """Create standardized response object"""
        return {
            "text": text,
            "type": response_type,
            "timestamp": datetime.now().isoformat(),
            "mode": self.current_mode,
            "audio": kwargs.get('audio'),
            "visual_alert": kwargs.get('visual_alert'),
            "haptic_feedback": kwargs.get('haptic_feedback'),
            "braille": kwargs.get('braille')
        }


# ==================== GRADIO INTERFACE ====================

def create_production_interface(allow_microphone: bool = False):
    """Create production-ready Gradio interface"""

    # Load your external CSS (if used)
    custom_css = ""   # No external CSS file


    # Initialize the system; microphone allowed only if specified
    voice_bridge = ProductionVoiceBridge(allow_microphone=allow_microphone)

    # Additional accessibility CSS
    accessibility_css = """
    :root { --primary-color: #2563eb; --danger-color: #dc2626; }
    .accessible-btn { min-height:48px !important; padding:12px 18px !important; font-size:16px !important; }
    .emergency-btn { background: linear-gradient(45deg,#dc2626,#ef4444) !important; color:white !important; font-weight:bold !important; }
    .large-text { font-size:18px !important; }
    """

    # Combine both CSS blocks
    final_css = custom_css + "\n" + accessibility_css

    # Gradio 4.x β†’ NO css= allowed
    with gr.Blocks() as demo:

        # Inject CSS manually (correct for Gradio 4.x)
        gr.HTML(f"<style>{final_css}</style>")

        # Your header
        gr.Markdown("# 🎯 VoiceBridge AI - Universal Communication Platform")

        # Status row
        with gr.Row():
            system_status = gr.Textbox(label="System Status",
                                      value="βœ… System Ready - VoiceBridge AI Initialized",
                                      interactive=False)
            current_mode_display = gr.Textbox(label="Current Mode", value=voice_bridge.current_mode, interactive=False)

        # Emergency
        with gr.Row():
            emergency_btn = gr.Button("🚨 ACTIVATE EMERGENCY MODE", elem_classes=["accessible-btn", "emergency-btn"])
            emergency_contact_input = gr.Textbox(label="Emergency Contact (Email/Phone)",
                                                 placeholder="Enter emergency contact information...")

        # Mode selector
        mode_selector = gr.Radio(choices=[("Universal", "universal"), ("Blind", "blind"),
                                         ("Deaf", "deaf"), ("Non-Verbal", "non_verbal"),
                                         ("Deaf-Blind", "deaf_blind")],
                                 label="Accessibility Mode", value=voice_bridge.current_mode)

        # Universal Tab
        with gr.Tab("🌐 Universal Communication"):
            with gr.Row():
                with gr.Column():
                    universal_audio = gr.Audio(label="🎀 Speak (Voice Input)", type="filepath", sources=["microphone", "upload"])
                    universal_text = gr.Textbox(label="⌨️ Type to Speak", lines=3)
                    universal_image = gr.Image(label="πŸ“· Capture Scene", type="filepath", sources=["webcam", "upload"])
                    process_universal = gr.Button("Process Input", elem_classes="accessible-btn")
                with gr.Column():
                    universal_output = gr.Textbox(label="Output", lines=6)
                    universal_audio_output = gr.Audio(label="Audio Output", type="filepath", interactive=False)
                    universal_alert = gr.Textbox(label="Visual Alerts", visible=False)

        # Blind Tab
        with gr.Tab("πŸ‘οΈ Blind Assistance"):
            with gr.Row():
                with gr.Column():
                    blind_audio = gr.Audio(label="Voice Commands", type="filepath", sources=["microphone", "upload"])
                    blind_commands = gr.Radio(choices=["describe scene", "read text", "navigate", "help"],
                                              label="Quick Commands", value="describe scene")
                    blind_image = gr.Image(label="Camera Feed", type="filepath", sources=["webcam", "upload"])
                    process_blind = gr.Button("Execute Command", elem_classes="accessible-btn")
                with gr.Column():
                    blind_output = gr.Textbox(label="Scene Description", lines=5)
                    blind_audio_output = gr.Audio(label="Audio Description", type="filepath")

        # Deaf Tab
        with gr.Tab("πŸ‘‚ Deaf Assistance"):
            with gr.Row():
                with gr.Column():
                    deaf_audio = gr.Audio(label="Audio to Transcribe", type="filepath", sources=["microphone", "upload"])
                    continuous_listening = gr.Checkbox(label="Continuous Listening Mode", value=False)
                    process_deaf = gr.Button("Transcribe Audio", elem_classes="accessible-btn")
                with gr.Column():
                    deaf_output = gr.Textbox(label="Transcription", lines=6)
                    deaf_alerts = gr.Textbox(label="Sound Alerts", lines=2)

        # Non-verbal Tab
        with gr.Tab("🀐 Non-Verbal Communication"):
            with gr.Row():
                with gr.Column():
                    preset_phrases = gr.Radio(choices=["greeting", "help", "medical", "emergency", "thanks",
                                                      "yes", "no", "pain", "lost", "bathroom"],
                                              label="Quick Phrases", value="greeting")
                    custom_phrase = gr.Textbox(label="Custom Message", lines=2)
                    speak_btn = gr.Button("Speak Message", elem_classes="accessible-btn")
                with gr.Column():
                    spoken_text = gr.Textbox(label="Message", lines=3)
                    message_audio = gr.Audio(label="Spoken Audio", type="filepath")

        # Deaf-Blind Tab
        with gr.Tab("πŸ‘οΈπŸ‘‚ Deaf-Blind Communication"):
            with gr.Row():
                with gr.Column():
                    tactile_input = gr.Textbox(label="Message to Convert", lines=3)
                    output_format = gr.Radio(choices=["haptic", "braille"], label="Output Format", value="haptic")
                    convert_btn = gr.Button("Convert to Tactile", elem_classes="accessible-btn")
                with gr.Column():
                    braille_output = gr.Textbox(label="Braille Output", lines=3)
                    vibration_pattern = gr.Textbox(label="Vibration Pattern", lines=2)

        # Settings & Feedback
        with gr.Tab("βš™οΈ Settings & Feedback"):
            with gr.Row():
                with gr.Column():
                    high_contrast = gr.Checkbox(label="High Contrast Mode", value=False)
                    large_text = gr.Checkbox(label="Large Text Mode", value=False)
                    voice_navigation = gr.Checkbox(label="Voice Navigation", value=True)
                    feedback_email = gr.Textbox(label="Your Email (optional)")
                    feedback_message = gr.Textbox(label="Feedback & Suggestions", lines=4)
                    submit_feedback = gr.Button("Submit Feedback", elem_classes="accessible-btn")
                    feedback_status = gr.Textbox(label="Status", interactive=False)
                with gr.Column():
                    conversation_history = gr.Textbox(label="Recent Conversation", lines=8, max_lines=10)
                    clear_history = gr.Button("Clear History", elem_classes="accessible-btn")
                    export_data = gr.Button("Export Data", elem_classes="accessible-btn")

        # ---------------- Event handlers (single definitions, no duplicates) ----------------

        def handle_mode_change(mode):
            voice_bridge.current_mode = mode
            status_msg = f"Mode changed to: {mode}"
            voice_bridge.add_to_conversation("System", status_msg)
            return status_msg, status_msg

        def handle_universal_input(audio, text, image, mode):
            # prioritize audio > text > image
            if audio:
                input_data = {'type': 'voice', 'audio': audio}
            elif text:
                input_data = {'type': 'text', 'text': text}
            elif image:
                input_data = {'type': 'image', 'image': image}
            else:
                return "Please provide input", None, ""
            result = voice_bridge.universal_communication(input_data)
            return result.get('text', ''), result.get('audio', None), result.get('visual_alert', '')

        def handle_blind_assistance(audio, command, image):
            if audio:
                transcript = voice_bridge.fallback_speech_to_text(audio)
                result = voice_bridge.blind_mode(transcript, image)
            elif image:
                result = voice_bridge.blind_mode(command, image)
            else:
                result = voice_bridge.blind_mode(command)
            return result.get('text', ''), result.get('audio', None)

        def handle_deaf_assistance(audio, continuous):
            result = voice_bridge.deaf_mode(audio, continuous)
            return result.get('text', ''), result.get('visual_alert', 'No important sounds detected')

        def handle_non_verbal(preset, custom):
            # preset radio contains phrase key
            text_to_speak = custom if custom and custom.strip() else None
            result = voice_bridge.non_verbal_mode(text_to_speak, preset)
            return result.get('text', ''), result.get('audio', None)

        def handle_deaf_blind(input_text, out_format):
            result = voice_bridge.deaf_blind_mode(input_text, out_format)
            braille = result.get('braille', '') if result else ''
            pattern = result.get('haptic_feedback', []) if result else []
            # Return braille text and vibration pattern string
            return braille, str(pattern)

        def handle_feedback(email, message):
            if not (message and message.strip()):
                return "Please enter feedback before submitting."
            fb = {
                "timestamp": datetime.now().isoformat(),
                "email": email,
                "message": message
            }
            Path("data/feedback").mkdir(parents=True, exist_ok=True)
            fname = f"data/feedback/feedback_{int(time.time())}.json"
            with open(fname, "w") as f:
                json.dump(fb, f, indent=2)
            return "Thank you! Feedback submitted."

        def handle_clear_history():
            voice_bridge.conversation_history.clear()
            return "History cleared."

        def handle_export_data():
            export_path = "data/export_conversation.json"
            with open(export_path, "w") as f:
                json.dump(voice_bridge.conversation_history, f, indent=2)
            return f"Conversation exported to {export_path}"

        def handle_emergency(contact=None):
            if contact:
                voice_bridge.emergency_contacts.append(contact)
            result = voice_bridge.trigger_emergency_mode("manual_button")
            # return message and (if available) audio path
            return result.get("message", ""), result.get("audio", None)

        # ---------------- Connect components ----------------

        mode_selector.change(fn=handle_mode_change, inputs=mode_selector, outputs=[system_status, current_mode_display])

        process_universal.click(fn=handle_universal_input, inputs=[universal_audio, universal_text, universal_image, mode_selector],
                               outputs=[universal_output, universal_audio_output, universal_alert])

        process_blind.click(fn=handle_blind_assistance, inputs=[blind_audio, blind_commands, blind_image],
                            outputs=[blind_output, blind_audio_output])

        process_deaf.click(fn=handle_deaf_assistance, inputs=[deaf_audio, continuous_listening],
                           outputs=[deaf_output, deaf_alerts])

        speak_btn.click(fn=handle_non_verbal, inputs=[preset_phrases, custom_phrase],
                        outputs=[spoken_text, message_audio])

        convert_btn.click(fn=handle_deaf_blind, inputs=[tactile_input, output_format], outputs=[braille_output, vibration_pattern])

        submit_feedback.click(fn=handle_feedback, inputs=[feedback_email, feedback_message], outputs=feedback_status)

        clear_history.click(fn=handle_clear_history, outputs=conversation_history)

        export_data.click(fn=handle_export_data, outputs=feedback_status)

        emergency_btn.click(fn=lambda: handle_emergency(emergency_contact_input.value), inputs=None, outputs=[system_status])

        # initial load state
        demo.load(fn=lambda: ("System Ready - VoiceBridge AI Initialized", voice_bridge.current_mode),
                  outputs=[system_status, current_mode_display])

    return demo


# ==================== LAUNCH ====================

if __name__ == "__main__":
    # In most headless deployments (Hugging Face Spaces) you must NOT initialize the microphone.
    # Set allow_microphone=True only if running on a device with a microphone and you want live mic support.
    demo = create_production_interface(allow_microphone=False)
    demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)), share=False)