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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# azure_unified_assistant.py
"""
Azure OpenAI Unified Assistant with GPT-4o Realtime
Supports: Voice I/O, Streaming Text, Document Q&A, Vision
"""

import os
import json
import asyncio
import base64
import queue
import threading
import time
from datetime import datetime
from typing import Optional, Dict, Any, List, Tuple, AsyncGenerator
from dataclasses import dataclass, field
from enum import Enum

import gradio as gr
import numpy as np
import websockets
from openai import AzureOpenAI, AsyncAzureOpenAI
from dotenv import load_dotenv
import logging

# Load environment variables
load_dotenv()

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

# ========================= CONFIGURATION =========================

@dataclass
class UnifiedAssistantConfig:
    """Configuration for GPT-4o Realtime Unified Assistant"""
    # Azure Configuration
    azure_endpoint: str
    api_key: str
    api_version: str = "2024-10-01-preview"
    
    # Model Configuration
    model_name: str = "gpt-4o-realtime-preview-2024-10-01"
    deployment_name: str = "gpt-4o-realtime-preview"
    
    # Vector Storage
    vector_store_id: Optional[str] = None
    
    # Audio Configuration
    voice: str = "alloy"  # alloy, echo, fable, onyx, nova, shimmer
    audio_format: str = "pcm16"
    sample_rate: int = 24000
    
    # Model Parameters
    temperature: float = 0.7
    max_tokens: int = 4096
    top_p: float = 0.95
    
    # System Configuration
    enable_streaming: bool = True
    enable_voice: bool = True
    enable_vision: bool = True
    enable_functions: bool = True
    
    # WebSocket Configuration
    ws_timeout: int = 60
    ws_max_size: int = 10 * 1024 * 1024  # 10MB

class ResponseType(Enum):
    """Response types from the unified model"""
    TEXT = "text"
    AUDIO = "audio"
    TRANSCRIPT = "transcript"
    FUNCTION = "function"
    ERROR = "error"
    DONE = "done"

# ========================= UNIFIED ASSISTANT =========================

class GPT4oRealtimeAssistant:
    """
    Unified Assistant using GPT-4o Realtime Preview
    Single model for all capabilities
    """
    
    def __init__(self, config: UnifiedAssistantConfig):
        self.config = config
        self.websocket = None
        self.session_active = False
        self.conversation_id = None
        
        # Initialize clients
        self._initialize_clients()
        
        # Audio handling
        self.audio_queue = queue.Queue()
        self.is_recording = False
        
        logger.info(f"βœ… Initialized GPT-4o Realtime Assistant with model: {config.model_name}")
    
    def _initialize_clients(self):
        """Initialize both REST and WebSocket clients"""
        try:
            # REST client for standard operations
            self.rest_client = AzureOpenAI(
                azure_endpoint=self.config.azure_endpoint,
                api_key=self.config.api_key,
                api_version=self.config.api_version
            )
            
            # Async REST client for streaming
            self.async_client = AsyncAzureOpenAI(
                azure_endpoint=self.config.azure_endpoint,
                api_key=self.config.api_key,
                api_version=self.config.api_version
            )
            
            logger.info("βœ… REST clients initialized successfully")
            
        except Exception as e:
            logger.error(f"❌ Failed to initialize clients: {str(e)}")
            raise
    
    # ==================== REALTIME WEBSOCKET CONNECTION ====================
    
    async def connect_realtime(self):
        """Establish WebSocket connection for realtime interaction"""
        if self.session_active:
            logger.info("Session already active")
            return
        
        try:
            # Construct WebSocket URL
            ws_endpoint = self.config.azure_endpoint.replace("https://", "wss://")
            ws_url = f"{ws_endpoint}/openai/realtime?api-version={self.config.api_version}&deployment={self.config.deployment_name}"
            
            headers = {
                "api-key": self.config.api_key,
                "OpenAI-Beta": "realtime=v1"
            }
            
            # Connect with timeout
            self.websocket = await asyncio.wait_for(
                websockets.connect(
                    ws_url,
                    extra_headers=headers,
                    max_size=self.config.ws_max_size
                ),
                timeout=self.config.ws_timeout
            )
            
            # Configure session
            await self._configure_session()
            self.session_active = True
            self.conversation_id = f"conv_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
            
            logger.info(f"βœ… Realtime session connected: {self.conversation_id}")
            
        except asyncio.TimeoutError:
            logger.error("❌ WebSocket connection timeout")
            raise
        except Exception as e:
            logger.error(f"❌ Failed to connect: {str(e)}")
            raise
    
    async def _configure_session(self):
        """Configure the realtime session with all capabilities"""
        session_config = {
            "type": "session.update",
            "session": {
                "model": self.config.model_name,
                "modalities": ["text", "audio"],
                "voice": self.config.voice,
                "instructions": """You are a helpful AI assistant with access to:
                                1. Realtime voice conversations
                                2. Document search and analysis
                                3. Streaming text responses
                                4. Image understanding
                                5. Function calling capabilities
                                
                                Provide clear, accurate, and helpful responses.
                                For document questions, cite sources when available.
                                Keep voice responses concise and natural.""",
                
                # Audio Configuration
                "input_audio_format": self.config.audio_format,
                "output_audio_format": self.config.audio_format,
                "input_audio_transcription": {
                    "model": "whisper-1"
                },
                
                # Voice Activity Detection
                "turn_detection": {
                    "type": "server_vad",
                    "threshold": 0.5,
                    "prefix_padding_ms": 300,
                    "silence_duration_ms": 500
                },
                
                # Tools Configuration
                "tools": self._get_tools_config(),
                "tool_choice": "auto",
                
                # Model Parameters
                "temperature": self.config.temperature,
                "max_response_output_tokens": self.config.max_tokens
            }
        }
        
        await self.websocket.send(json.dumps(session_config))
        
        # Wait for session confirmation
        response = await self.websocket.recv()
        data = json.loads(response)
        
        if data.get("type") == "session.created":
            logger.info("βœ… Session configured successfully")
        else:
            logger.warning(f"⚠️ Unexpected session response: {data.get('type')}")
    
    def _get_tools_config(self) -> List[Dict]:
        """Get tools configuration based on settings"""
        tools = []
        
        if self.config.vector_store_id:
            tools.append({
                "type": "file_search",
                "file_search": {
                    "vector_store_ids": [self.config.vector_store_id]
                }
            })
        
        if self.config.enable_functions:
            tools.append({
                "type": "code_interpreter"
            })
        
        return tools
    
    # ==================== AUDIO HANDLING ====================
    
    async def send_audio(self, audio_data: bytes):
        """Send audio input to the model"""
        if not self.session_active:
            await self.connect_realtime()
        
        try:
            # Convert to base64
            audio_base64 = base64.b64encode(audio_data).decode('utf-8')
            
            message = {
                "type": "input_audio_buffer.append",
                "audio": audio_base64
            }
            
            await self.websocket.send(json.dumps(message))
            
            # Commit audio buffer to trigger processing
            await self.websocket.send(json.dumps({
                "type": "input_audio_buffer.commit"
            }))
            
        except Exception as e:
            logger.error(f"❌ Error sending audio: {str(e)}")
            raise
    
    async def process_audio_stream(self, audio_stream: AsyncGenerator[bytes, None]):
        """Process continuous audio stream"""
        if not self.session_active:
            await self.connect_realtime()
        
        try:
            async for audio_chunk in audio_stream:
                await self.send_audio(audio_chunk)
                
        except Exception as e:
            logger.error(f"❌ Error processing audio stream: {str(e)}")
            raise
    
    # ==================== TEXT HANDLING ====================
    
    async def send_text(self, text: str, use_realtime: bool = False):
        """Send text input - can use either realtime or REST API"""
        
        if use_realtime:
            # Use realtime WebSocket
            if not self.session_active:
                await self.connect_realtime()
            
            message = {
                "type": "conversation.item.create",
                "item": {
                    "type": "message",
                    "role": "user",
                    "content": [
                        {
                            "type": "input_text",
                            "text": text
                        }
                    ]
                }
            }
            
            await self.websocket.send(json.dumps(message))
            
            # Trigger response
            await self.websocket.send(json.dumps({
                "type": "response.create",
                "response": {
                    "modalities": ["text", "audio"] if self.config.enable_voice else ["text"]
                }
            }))
            
        else:
            # Use REST API for text-only
            return self.rest_client.chat.completions.create(
                model=self.config.deployment_name,
                messages=[
                    {"role": "user", "content": text}
                ],
                stream=self.config.enable_streaming,
                temperature=self.config.temperature,
                max_tokens=self.config.max_tokens
            )
    
    # ==================== STREAMING RESPONSES ====================
    
    async def stream_response(self) -> AsyncGenerator[Dict[str, Any], None]:
        """Stream responses from the realtime connection"""
        if not self.session_active:
            logger.error("❌ No active session")
            return
        
        try:
            while True:
                message = await asyncio.wait_for(
                    self.websocket.recv(),
                    timeout=self.config.ws_timeout
                )
                
                data = json.loads(message)
                response_type = data.get("type", "")
                
                # Handle different response types
                if response_type == "response.audio.delta":
                    yield {
                        "type": ResponseType.AUDIO,
                        "data": base64.b64decode(data.get("delta", ""))
                    }
                
                elif response_type == "response.text.delta":
                    yield {
                        "type": ResponseType.TEXT,
                        "data": data.get("delta", "")
                    }
                
                elif response_type == "response.audio_transcript.delta":
                    yield {
                        "type": ResponseType.TRANSCRIPT,
                        "data": data.get("delta", "")
                    }
                
                elif response_type == "response.function_call_arguments.delta":
                    yield {
                        "type": ResponseType.FUNCTION,
                        "data": json.loads(data.get("delta", "{}"))
                    }
                
                elif response_type == "response.done":
                    yield {
                        "type": ResponseType.DONE,
                        "data": None
                    }
                    break
                
                elif response_type == "error":
                    yield {
                        "type": ResponseType.ERROR,
                        "data": data.get("error", {})
                    }
                    break
                    
        except asyncio.TimeoutError:
            logger.error("❌ Response timeout")
            yield {
                "type": ResponseType.ERROR,
                "data": {"message": "Response timeout"}
            }
        except Exception as e:
            logger.error(f"❌ Stream error: {str(e)}")
            yield {
                "type": ResponseType.ERROR,
                "data": {"message": str(e)}
            }
    
    # ==================== DOCUMENT Q&A ====================
    
    async def query_documents(self, query: str, stream: bool = True):
        """Query documents using vector search"""
        
        if not self.config.vector_store_id:
            return "No vector store configured. Please set up a vector store ID."
        
        try:
            # Create assistant with vector search
            assistant = self.rest_client.beta.assistants.create(
                model=self.config.deployment_name,
                name="Document Q&A Assistant",
                instructions="""Answer questions based on the provided documents.
                             Cite sources when possible.
                             If information is not in the documents, say so clearly.""",
                tools=[{"type": "file_search"}],
                tool_resources={
                    "file_search": {
                        "vector_store_ids": [self.config.vector_store_id]
                    }
                },
                temperature=self.config.temperature
            )
            
            # Create thread
            thread = self.rest_client.beta.threads.create()
            
            # Add message
            self.rest_client.beta.threads.messages.create(
                thread_id=thread.id,
                role="user",
                content=query
            )
            
            # Run with streaming
            if stream:
                with self.rest_client.beta.threads.runs.create_and_stream(
                    thread_id=thread.id,
                    assistant_id=assistant.id,
                ) as stream:
                    for event in stream:
                        if event.event == 'thread.message.delta':
                            if hasattr(event.data, 'delta') and hasattr(event.data.delta, 'content'):
                                for content in event.data.delta.content:
                                    if content.type == 'text' and hasattr(content.text, 'value'):
                                        yield content.text.value
            else:
                # Non-streaming response
                run = self.rest_client.beta.threads.runs.create(
                    thread_id=thread.id,
                    assistant_id=assistant.id
                )
                
                # Wait for completion
                while run.status in ['queued', 'in_progress']:
                    time.sleep(1)
                    run = self.rest_client.beta.threads.runs.retrieve(
                        thread_id=thread.id,
                        run_id=run.id
                    )
                
                if run.status == 'completed':
                    messages = self.rest_client.beta.threads.messages.list(
                        thread_id=thread.id
                    )
                    if messages.data:
                        return messages.data[0].content[0].text.value
                else:
                    return f"Query failed: {run.status}"
                    
        except Exception as e:
            logger.error(f"❌ Document query error: {str(e)}")
            return f"Error querying documents: {str(e)}"
    
    # ==================== MULTIMODAL HANDLING ====================
    
    async def analyze_image(self, image_path: str, question: str):
        """Analyze image with text/voice response"""
        
        try:
            # Read and encode image
            with open(image_path, "rb") as image_file:
                image_data = base64.b64encode(image_file.read()).decode('utf-8')
            
            if self.session_active:
                # Use realtime connection for voice response
                message = {
                    "type": "conversation.item.create",
                    "item": {
                        "type": "message",
                        "role": "user",
                        "content": [
                            {
                                "type": "input_text",
                                "text": question
                            },
                            {
                                "type": "image_url",
                                "image_url": {
                                    "url": f"data:image/jpeg;base64,{image_data}",
                                    "detail": "high"
                                }
                            }
                        ]
                    }
                }
                
                await self.websocket.send(json.dumps(message))
                
                # Trigger response
                await self.websocket.send(json.dumps({
                    "type": "response.create"
                }))
                
                # Stream response
                async for response in self.stream_response():
                    yield response
                    
            else:
                # Use REST API
                response = self.rest_client.chat.completions.create(
                    model=self.config.deployment_name,
                    messages=[
                        {
                            "role": "user",
                            "content": [
                                {"type": "text", "text": question},
                                {
                                    "type": "image_url",
                                    "image_url": {
                                        "url": f"data:image/jpeg;base64,{image_data}",
                                        "detail": "high"
                                    }
                                }
                            ]
                        }
                    ],
                    max_tokens=self.config.max_tokens,
                    temperature=self.config.temperature
                )
                
                yield {
                    "type": ResponseType.TEXT,
                    "data": response.choices[0].message.content
                }
                
        except Exception as e:
            logger.error(f"❌ Image analysis error: {str(e)}")
            yield {
                "type": ResponseType.ERROR,
                "data": {"message": str(e)}
            }
    
    # ==================== CLEANUP ====================
    
    async def disconnect(self):
        """Disconnect realtime session"""
        if self.websocket:
            await self.websocket.close()
            self.session_active = False
            logger.info("βœ… Disconnected from realtime session")

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

class UnifiedGradioInterface:
    """Enhanced Gradio interface for the unified assistant"""
    
    def __init__(self, assistant: GPT4oRealtimeAssistant):
        self.assistant = assistant
        self.conversation_history = []
        self.audio_processor = AudioProcessor()
    
    def create_interface(self) -> gr.Blocks:
        """Create the complete Gradio interface"""
        
        with gr.Blocks(
            theme=gr.themes.Soft(),
            title="Azure GPT-4o Realtime Assistant",
            css=self._get_custom_css()
        ) as interface:
            
            # Header
            gr.Markdown(
                """
                # 🌟 Azure GPT-4o Realtime Assistant
                
                **Unified Model with All Capabilities:**
                - πŸŽ™οΈ **Voice:** Realtime voice conversations with ~300ms latency
                - ⚑ **Streaming:** Live text streaming responses
                - πŸ“š **Documents:** Vector search and Q&A on your documents
                - πŸ–ΌοΈ **Vision:** Image analysis and understanding
                - πŸ”§ **Functions:** Tool calling and code interpretation
                
                **Model:** `gpt-4o-realtime-preview-2024-10-01` | **Context:** 128K tokens
                """
            )
            
            # State management
            state = gr.State({
                "session_active": False,
                "conversation_id": None
            })
            
            with gr.Tabs() as tabs:
                
                # ============ TAB 1: STREAMING CHAT ============
                with gr.Tab("πŸ’¬ Streaming Chat", elem_id="chat-tab"):
                    with gr.Row():
                        with gr.Column(scale=4):
                            chatbot = gr.Chatbot(
                                height=500,
                                show_label=False,
                                elem_id="chatbot",
                                bubble_full_width=False
                            )
                            
                            with gr.Row():
                                msg = gr.Textbox(
                                    placeholder="Type your message here... (Press Enter to send)",
                                    label="Message",
                                    scale=4,
                                    lines=2
                                )
                                
                                with gr.Column(scale=1):
                                    send_btn = gr.Button("Send", variant="primary", size="lg")
                                    clear_btn = gr.Button("Clear", variant="secondary")
                        
                        with gr.Column(scale=1):
                            gr.Markdown("### Settings")
                            
                            streaming_mode = gr.Radio(
                                choices=["Stream", "Complete"],
                                value="Stream",
                                label="Response Mode"
                            )
                            
                            temperature = gr.Slider(
                                minimum=0,
                                maximum=1,
                                value=0.7,
                                step=0.1,
                                label="Temperature"
                            )
                            
                            gr.Markdown("### Status")
                            status_display = gr.Markdown("🟒 Ready")
                    
                    # Chat handlers
                    async def handle_chat(message, history, mode):
                        if not message:
                            return history
                        
                        history = history or []
                        history.append([message, ""])
                        
                        try:
                            if mode == "Stream":
                                response = ""
                                # Stream text response
                                await self.assistant.send_text(message, use_realtime=True)
                                
                                async for chunk in self.assistant.stream_response():
                                    if chunk["type"] == ResponseType.TEXT:
                                        response += chunk["data"]
                                        history[-1][1] = response
                                        yield history
                                    elif chunk["type"] == ResponseType.DONE:
                                        break
                            else:
                                # Complete response
                                result = await self.assistant.send_text(message, use_realtime=False)
                                history[-1][1] = result.choices[0].message.content
                                yield history
                                
                        except Exception as e:
                            history[-1][1] = f"❌ Error: {str(e)}"
                            yield history
                    
                    msg.submit(
                        handle_chat,
                        [msg, chatbot, streaming_mode],
                        chatbot
                    ).then(
                        lambda: "",
                        outputs=msg
                    )
                    
                    send_btn.click(
                        handle_chat,
                        [msg, chatbot, streaming_mode],
                        chatbot
                    ).then(
                        lambda: "",
                        outputs=msg
                    )
                    
                    clear_btn.click(
                        lambda: ([], ""),
                        outputs=[chatbot, msg]
                    )
                
                # ============ TAB 2: VOICE INTERACTION ============
                with gr.Tab("πŸŽ™οΈ Voice Chat", elem_id="voice-tab"):
                    gr.Markdown(
                        """
                        ### Realtime Voice Interaction
                        Talk naturally with the assistant using your microphone.
                        """
                    )
                    
                    with gr.Row():
                        with gr.Column():
                            # Connection control
                            with gr.Row():
                                connect_btn = gr.Button("πŸ”Œ Connect Voice", variant="primary")
                                disconnect_btn = gr.Button("πŸ”Œ Disconnect", variant="stop")
                            
                            connection_status = gr.Markdown("βšͺ Not connected")
                            
                            # Audio interface
                            audio_input = gr.Audio(
                                sources=["microphone"],
                                type="numpy",
                                streaming=True,
                                label="🎀 Speak to Assistant"
                            )
                            
                            audio_output = gr.Audio(
                                label="πŸ”Š Assistant Response",
                                type="numpy",
                                autoplay=True
                            )
                        
                        with gr.Column():
                            # Conversation display
                            transcript = gr.Textbox(
                                label="Conversation Transcript",
                                lines=20,
                                max_lines=30,
                                interactive=False
                            )
                            
                            # Voice settings
                            voice_select = gr.Dropdown(
                                choices=["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
                                value="alloy",
                                label="Voice Selection"
                            )
                    
                    # Voice handlers
                    async def connect_voice():
                        try:
                            await self.assistant.connect_realtime()
                            return "🟒 Connected to realtime voice"
                        except Exception as e:
                            return f"πŸ”΄ Connection failed: {str(e)}"
                    
                    async def disconnect_voice():
                        try:
                            await self.assistant.disconnect()
                            return "βšͺ Disconnected"
                        except Exception as e:
                            return f"πŸ”΄ Error: {str(e)}"
                    
                    async def process_voice(audio_data):
                        if audio_data is None:
                            return None, ""
                        
                        try:
                            # Convert audio format
                            sample_rate, audio = audio_data
                            audio_bytes = (audio * 32767).astype(np.int16).tobytes()
                            
                            # Send audio
                            await self.assistant.send_audio(audio_bytes)
                            
                            # Collect responses
                            transcript_text = ""
                            audio_chunks = []
                            
                            async for response in self.assistant.stream_response():
                                if response["type"] == ResponseType.AUDIO:
                                    audio_chunks.append(response["data"])
                                elif response["type"] == ResponseType.TRANSCRIPT:
                                    transcript_text += response["data"]
                                elif response["type"] == ResponseType.DONE:
                                    break
                            
                            # Process audio output
                            if audio_chunks:
                                combined = b''.join(audio_chunks)
                                audio_array = np.frombuffer(combined, dtype=np.int16) / 32767.0
                                return (24000, audio_array), transcript_text
                            
                            return None, transcript_text
                            
                        except Exception as e:
                            return None, f"Error: {str(e)}"
                    
                    connect_btn.click(
                        connect_voice,
                        outputs=connection_status
                    )
                    
                    disconnect_btn.click(
                        disconnect_voice,
                        outputs=connection_status
                    )
                    
                    audio_input.stream(
                        process_voice,
                        audio_input,
                        [audio_output, transcript]
                    )
                
                # ============ TAB 3: DOCUMENT Q&A ============
                with gr.Tab("πŸ“š Document Q&A", elem_id="doc-tab"):
                    gr.Markdown(
                        """
                        ### Query Your Documents
                        Ask questions about documents in your vector storage.
                        """
                    )
                    
                    with gr.Row():
                        with gr.Column():
                            doc_query = gr.Textbox(
                                placeholder="Ask a question about your documents...",
                                label="Query",
                                lines=3
                            )
                            
                            with gr.Row():
                                search_btn = gr.Button("πŸ” Search", variant="primary")
                                stream_toggle = gr.Checkbox(label="Stream Response", value=True)
                            
                            doc_response = gr.Textbox(
                                label="Response",
                                lines=15,
                                max_lines=25
                            )
                        
                        with gr.Column(scale=1):
                            gr.Markdown("### Vector Store Info")
                            
                            vector_status = gr.Markdown(
                                f"**Store ID:** `{self.assistant.config.vector_store_id or 'Not configured'}`"
                            )
                            
                            gr.Markdown(
                                """
                                ### Tips
                                - Be specific in your questions
                                - Reference document sections if known
                                - Ask for citations when needed
                                """
                            )
                    
                    # Document Q&A handler
                    async def search_documents(query, stream):
                        if not query:
                            return "Please enter a query."
                        
                        try:
                            if stream:
                                response = ""
                                async for chunk in self.assistant.query_documents(query, stream=True):
                                    response += chunk
                                    yield response
                            else:
                                response = await self.assistant.query_documents(query, stream=False)
                                yield response
                                
                        except Exception as e:
                            yield f"❌ Error: {str(e)}"
                    
                    search_btn.click(
                        search_documents,
                        [doc_query, stream_toggle],
                        doc_response
                    )
                    
                    doc_query.submit(
                        search_documents,
                        [doc_query, stream_toggle],
                        doc_response
                    )
                
                # ============ TAB 4: IMAGE ANALYSIS ============
                with gr.Tab("πŸ–ΌοΈ Vision", elem_id="vision-tab"):
                    gr.Markdown(
                        """
                        ### Image Analysis with Voice
                        Upload an image and ask questions about it.
                        """
                    )
                    
                    with gr.Row():
                        with gr.Column():
                            image_input = gr.Image(
                                type="filepath",
                                label="Upload Image"
                            )
                            
                            image_query = gr.Textbox(
                                placeholder="What would you like to know about this image?",
                                label="Question",
                                lines=2
                            )
                            
                            analyze_btn = gr.Button("πŸ” Analyze", variant="primary")
                        
                        with gr.Column():
                            image_response = gr.Textbox(
                                label="Analysis",
                                lines=10
                            )
                            
                            image_audio = gr.Audio(
                                label="Voice Response",
                                type="numpy"
                            )
                    
                    # Image analysis handler
                    async def analyze_image(image, query):
                        if not image or not query:
                            return "Please provide both an image and a question.", None
                        
                        try:
                            text_response = ""
                            audio_chunks = []
                            
                            async for response in self.assistant.analyze_image(image, query):
                                if response["type"] == ResponseType.TEXT:
                                    text_response += response["data"]
                                elif response["type"] == ResponseType.AUDIO:
                                    audio_chunks.append(response["data"])
                            
                            # Process audio if available
                            audio_output = None
                            if audio_chunks:
                                combined = b''.join(audio_chunks)
                                audio_array = np.frombuffer(combined, dtype=np.int16) / 32767.0
                                audio_output = (24000, audio_array)
                            
                            return text_response, audio_output
                            
                        except Exception as e:
                            return f"❌ Error: {str(e)}", None
                    
                    analyze_btn.click(
                        analyze_image,
                        [image_input, image_query],
                        [image_response, image_audio]
                    )
                
                # ============ TAB 5: SETTINGS ============
                with gr.Tab("βš™οΈ Settings", elem_id="settings-tab"):
                    gr.Markdown("### Model Configuration")
                    
                    with gr.Row():
                        with gr.Column():
                            gr.Markdown("#### Model Information")
                            model_info = gr.JSON(
                                value={
                                    "model": self.assistant.config.model_name,
                                    "deployment": self.assistant.config.deployment_name,
                                    "context_window": 128000,
                                    "capabilities": [
                                        "realtime_audio",
                                        "streaming_text",
                                        "document_qa",
                                        "vision",
                                        "function_calling"
                                    ],
                                    "audio_format": self.assistant.config.audio_format,
                                    "sample_rate": self.assistant.config.sample_rate
                                },
                                label="Current Configuration"
                            )
                        
                        with gr.Column():
                            gr.Markdown("#### Performance Metrics")
                            metrics = gr.JSON(
                                value={
                                    "voice_latency": "~300ms",
                                    "text_streaming": "~50ms/token",
                                    "document_search": "1-2 seconds",
                                    "image_analysis": "2-3 seconds"
                                },
                                label="Expected Performance"
                            )
            
            # Footer
            gr.Markdown(
                """
                ---
                <center>
                **Azure GPT-4o Realtime** | Single Model for All Tasks | 
                [Documentation](https://docs.microsoft.com/azure/cognitive-services/openai/)
                </center>
                """
            )
        
        return interface
    
    def _get_custom_css(self) -> str:
        """Get custom CSS for the interface"""
        return """
        #chatbot {
            height: 500px !important;
        }
        
        .gradio-container {
            max-width: 1400px !important;
            margin: auto !important;
        }
        
        #voice-tab .gradio-audio {
            height: 120px !important;
        }
        
        .markdown-text {
            font-size: 14px !important;
        }
        """

# ========================= AUDIO PROCESSOR =========================

class AudioProcessor:
    """Handle audio format conversions and processing"""
    
    @staticmethod
    def numpy_to_pcm16(audio_array: np.ndarray) -> bytes:
        """Convert numpy array to PCM16 bytes"""
        return (audio_array * 32767).astype(np.int16).tobytes()
    
    @staticmethod
    def pcm16_to_numpy(audio_bytes: bytes) -> np.ndarray:
        """Convert PCM16 bytes to numpy array"""
        return np.frombuffer(audio_bytes, dtype=np.int16) / 32767.0

# ========================= MAIN APPLICATION =========================

async def main():
    """Main application entry point"""
    
    # Load configuration from environment
    config = UnifiedAssistantConfig(
        azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
        api_key=os.getenv("AZURE_OPENAI_API_KEY"),
        api_version=os.getenv("AZURE_API_VERSION", "2024-10-01-preview"),
        deployment_name=os.getenv("AZURE_DEPLOYMENT_NAME", "gpt-4o-realtime-preview"),
        vector_store_id=os.getenv("AZURE_VECTOR_STORE_ID"),
        voice=os.getenv("AZURE_VOICE", "alloy"),
        temperature=float(os.getenv("TEMPERATURE", "0.7")),
        max_tokens=int(os.getenv("MAX_TOKENS", "4096"))
    )
    
    # Validate configuration
    if not config.azure_endpoint or not config.api_key:
        logger.error("❌ Missing required environment variables")
        raise ValueError(
            "Please set AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_API_KEY environment variables"
        )
    
    if not config.vector_store_id:
        logger.warning("⚠️ No vector store ID configured - document Q&A will be limited")
    
    try:
        # Initialize assistant
        logger.info("πŸš€ Initializing GPT-4o Realtime Unified Assistant...")
        assistant = GPT4oRealtimeAssistant(config)
        
        # Create Gradio interface
        logger.info("🎨 Creating Gradio interface...")
        interface_manager = UnifiedGradioInterface(assistant)
        interface = interface_manager.create_interface()
        
        # Launch application
        logger.info("βœ… Launching application...")
        interface.queue(max_size=100)  # Enable queuing for concurrent users
        interface.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=True,  # Create public URL
            show_error=True,
            quiet=False
        )
        
    except KeyboardInterrupt:
        logger.info("πŸ‘‹ Shutting down gracefully...")
        if assistant.session_active:
            await assistant.disconnect()
    except Exception as e:
        logger.error(f"❌ Fatal error: {str(e)}")
        raise

def run():
    """Synchronous entry point"""
    asyncio.run(main())

if __name__ == "__main__":
    run()