File size: 27,990 Bytes
1813edc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Streamlit Frontend - Voice RAG Bot
Interactive UI for audio input, processing, and response playback
"""

import streamlit as st
import requests
import json
import os
import time
import base64
from pathlib import Path
from datetime import datetime
from typing import Optional, Dict, Any

# Page configuration
st.set_page_config(
    page_title="Voice RAG Bot",
    page_icon="πŸ€–",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Styling
st.markdown("""
<style>
    .main {
        padding: 0rem 1rem;
    }
    .stTabs [data-baseweb="tab-list"] button [data-testid="stMarkdownContainer"] p {
        font-size: 1.1rem;
        font-weight: 500;
    }
    .success-box {
        padding: 1rem;
        border-radius: 0.5rem;
        background-color: #d4edda;
        border: 1px solid #c3e6cb;
        color: #155724;
    }
    .error-box {
        padding: 1rem;
        border-radius: 0.5rem;
        background-color: #f8d7da;
        border: 1px solid #f5c6cb;
        color: #721c24;
    }
    .info-box {
        padding: 1rem;
        border-radius: 0.5rem;
        background-color: #d1ecf1;
        border: 1px solid #bee5eb;
        color: #0c5460;
    }
</style>
""", unsafe_allow_html=True)

# ============================================================================
# CONFIGURATION
# ============================================================================
BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000")
DATA_DIR = Path("data/audio_output")
DATA_DIR.mkdir(parents=True, exist_ok=True)

# Session state initialization
if "customer_id" not in st.session_state:
    st.session_state.customer_id = "CUST_001"
if "processing" not in st.session_state:
    st.session_state.processing = False
if "last_response" not in st.session_state:
    st.session_state.last_response = None
if "history" not in st.session_state:
    st.session_state.history = []
if "voice_bot_mode" not in st.session_state:
    st.session_state.voice_bot_mode = False
if "voice_bot_session" not in st.session_state:
    st.session_state.voice_bot_session = None
if "voice_bot_active" not in st.session_state:
    st.session_state.voice_bot_active = False
if "voice_bot_messages" not in st.session_state:
    st.session_state.voice_bot_messages = []
if "pending_audio" not in st.session_state:
    st.session_state.pending_audio = None
if "processing_audio" not in st.session_state:
    st.session_state.processing_audio = False
if "last_processed_audio_id" not in st.session_state:
    st.session_state.last_processed_audio_id = None

# ============================================================================
# UTILITY FUNCTIONS
# ============================================================================

def check_backend_health() -> bool:
    """Check if FastAPI backend is running"""
    try:
        response = requests.get(f"{BACKEND_URL}/health", timeout=5)
        return response.status_code == 200
    except requests.exceptions.ConnectionError:
        return False
    except requests.exceptions.Timeout:
        return False
    except Exception as e:
        return False


def process_audio_file(audio_bytes: bytes, customer_id: str) -> Optional[Dict[str, Any]]:
    """Send audio to backend for processing"""
    try:
        from io import BytesIO
        
        # Send audio bytes directly as file to backend
        with st.spinner("Processing audio... (may take 30-60 seconds)"):
            # Create file-like object from bytes
            audio_file = BytesIO(audio_bytes)
            audio_file.name = f"audio_{datetime.now().strftime('%Y%m%d_%H%M%S')}.wav"
            
            files = {"file": (audio_file.name, audio_file, "audio/wav")}
            response = requests.post(
                f"{BACKEND_URL}/process-audio",
                files=files,
                params={"customer_id": customer_id},
                timeout=120
            )
        
        if response.status_code == 200:
            result = response.json()
            return result
        else:
            st.error(f"Backend error: {response.status_code}")
            st.error(response.text)
            return None
            
    except requests.exceptions.Timeout:
        st.error("Request timeout. Processing took too long.")
        return None
    except Exception as e:
        st.error(f"Error processing audio: {str(e)}")
        import traceback
        st.error(traceback.format_exc())
        return None


def process_text_input(user_input: str, customer_id: str) -> Optional[Dict[str, Any]]:
    """Send text to backend for processing"""
    try:
        with st.spinner("Processing text... (may take 20-30 seconds)"):
            response = requests.post(
                f"{BACKEND_URL}/process-text",
                params={
                    "user_input": user_input,
                    "customer_id": customer_id
                },
                timeout=120
            )
        
        if response.status_code == 200:
            return response.json()
        else:
            st.error(f"Backend error: {response.status_code}")
            st.error(response.text)
            return None
            
    except requests.exceptions.Timeout:
        st.error("Request timeout. Processing took too long.")
        return None
    except Exception as e:
        st.error(f"Error processing text: {str(e)}")
        return None




def voice_bot_start(customer_id: str) -> Optional[Dict[str, Any]]:
    """Start voice bot session"""
    try:
        response = requests.post(
            f"{BACKEND_URL}/voice-bot/start",
            params={"customer_id": customer_id},
            timeout=60
        )
        
        if response.status_code == 200:
            return response.json()
        else:
            st.error(f"Error starting voice bot: {response.status_code}")
            return None
    except Exception as e:
        st.error(f"Error starting voice bot: {str(e)}")
        return None


def voice_bot_process_message(user_message: str) -> Optional[Dict[str, Any]]:
    """Process message in voice bot session"""
    try:
        response = requests.post(
            f"{BACKEND_URL}/voice-bot/message",
            params={"user_message": user_message},
            timeout=120
        )
        
        if response.status_code == 200:
            return response.json()
        else:
            st.error(f"Backend error {response.status_code}: {response.text}")
            return None
    except Exception as e:
        st.error(f"Backend connection error: {str(e)}")
        return None


def voice_bot_end() -> Optional[Dict[str, Any]]:
    """End voice bot session"""
    try:
        response = requests.post(
            f"{BACKEND_URL}/voice-bot/end",
            timeout=10
        )
        
        if response.status_code == 200:
            return response.json()
        else:
            return None
    except Exception as e:
        return None


def display_response_results(response: Dict[str, Any]):
    """Display formatted response from backend"""
    
    # Display latency metrics first if available
    latency_metrics = response.get("latency_metrics")
    if latency_metrics:
        st.markdown("### ⏱️ Performance Metrics")
        
        total_time = latency_metrics.get("total_time_ms", 0)
        modules = latency_metrics.get("modules", {})
        breakdown = latency_metrics.get("breakdown_percent", {})
        
        # Display total time prominently
        col1, col2, col3 = st.columns(3)
        with col1:
            st.metric("Total Processing Time", f"{total_time:.0f} ms", f"{total_time/1000:.2f}s")
        with col2:
            fastest = min(modules.items(), key=lambda x: x[1]) if modules else ("N/A", 0)
            st.metric("Fastest Module", fastest[0].replace("_", " ").title(), f"{fastest[1]:.0f} ms")
        with col3:
            slowest = max(modules.items(), key=lambda x: x[1]) if modules else ("N/A", 0)
            st.metric("Slowest Module", slowest[0].replace("_", " ").title(), f"{slowest[1]:.0f} ms")
        
        # Module breakdown with progress bars
        with st.expander("πŸ“Š Detailed Module Breakdown", expanded=True):
            st.markdown("#### Time per Module")
            
            # Sort modules by time
            sorted_modules = sorted(modules.items(), key=lambda x: x[1], reverse=True)
            
            for module_name, time_ms in sorted_modules:
                percent = breakdown.get(module_name, 0)
                display_name = module_name.replace("_", " ").title()
                
                col1, col2, col3 = st.columns([3, 1, 1])
                with col1:
                    st.write(f"**{display_name}**")
                with col2:
                    st.write(f"{time_ms:.2f} ms")
                with col3:
                    st.write(f"{percent:.1f}%")
                
                # Progress bar
                st.progress(percent / 100)
        
        st.markdown("---")
    
    # Create tabs for different result sections
    tabs = st.tabs([
        "πŸ“ Response",
        "🎯 Intent",
        "😊 Sentiment",
        "🏷️ Entities",
        "πŸ“š Knowledge Base",
        "πŸ“œ History",
        "πŸ”Š Audio"
    ])
    
    # Tab 1: Main Response
    with tabs[0]:
        st.markdown("### Generated Response")
        st.info(response.get("response_text", "No response generated"))
        
        # Save to history
        st.session_state.history.append({
            "timestamp": datetime.now().isoformat(),
            "customer_id": st.session_state.customer_id,
            "response": response.get("response_text", ""),
            "intent": response.get("intent", {}).get("intent", ""),
            "sentiment": response.get("sentiment", {}).get("label", "")
        })
    
    # Tab 2: Intent Detection
    with tabs[1]:
        intent_data = response.get("intent", {})
        col1, col2 = st.columns(2)
        
        with col1:
            st.metric("Detected Intent", intent_data.get("intent", "N/A"))
        with col2:
            confidence = intent_data.get("confidence", 0)
            st.metric("Confidence", f"{confidence:.1%}")
        
        # Intent explanation
        intent_types = {
            "refund_request": "Customer wants to return/refund a product",
            "order_status": "Customer inquiring about order tracking",
            "product_inquiry": "Customer asking product details",
            "billing_issue": "Customer has billing/payment problems",
            "warranty_claim": "Customer filing warranty claim",
            "account_management": "Account settings/updates",
            "general_support": "General support request",
            "complaint": "Customer complaint",
            "other": "Other inquiry"
        }
        
        intent = intent_data.get("intent", "")
        if intent in intent_types:
            st.write(f"**Category**: {intent_types[intent]}")
    
    # Tab 3: Sentiment Analysis
    with tabs[2]:
        sentiment_data = response.get("sentiment", {})
        label = sentiment_data.get("label", "NEUTRAL")
        score = sentiment_data.get("score", 0)
        
        # Color-coded sentiment display
        if label == "POSITIVE":
            color = "🟒"
            tone = "Positive"
        elif label == "NEGATIVE":
            color = "πŸ”΄"
            tone = "Negative"
        else:
            color = "🟑"
            tone = "Neutral"
        
        col1, col2 = st.columns(2)
        with col1:
            st.metric("Sentiment", f"{color} {tone}")
        with col2:
            st.metric("Confidence", f"{score:.1%}")
        
        st.write(f"**Interpretation**: Response was generated with {tone.lower()}-{tone.lower()} tone")
    
    # Tab 4: Entities
    with tabs[3]:
        entities = response.get("entities", {})
        if entities:
            for entity_type, values in entities.items():
                if values:
                    st.write(f"**{entity_type.upper()}**")
                    for entity in values:
                        st.write(f"  β€’ {entity}")
        else:
            st.info("No entities extracted from input")
    
    # Tab 5: Knowledge Base Context
    with tabs[4]:
        kb_context = response.get("kb_context", "")
        if kb_context and isinstance(kb_context, str) and kb_context.strip() != "No relevant policies found.":
            st.write("**Retrieved Documents:**")
            st.write(kb_context)
        else:
            st.info("No KB documents retrieved")
    
    # Tab 6: Customer History
    with tabs[5]:
        history_context = response.get("history_context", "")
        if history_context and isinstance(history_context, str) and history_context.strip() != "No customer history available.":
            st.write("**Customer History:**")
            st.write(history_context)
        else:
            st.info("No customer history found")
    
    # Tab 7: Audio Output
    with tabs[6]:
        audio_path = response.get("audio_path", "")
        if audio_path and audio_path.strip():
            try:
                # Normalize path
                audio_file_path = Path(audio_path.replace("\\", "/"))
                if not audio_file_path.is_absolute():
                    project_root = Path(__file__).parent.parent
                    audio_file_path = project_root / audio_file_path
                
                if audio_file_path.exists():
                    st.write(f"**Audio file**: {audio_path}")
                    with open(audio_file_path, "rb") as audio_file:
                        st.audio(audio_file, format="audio/mp3")
                else:
                    st.warning(f"Audio file not found: {audio_file_path}")
            except Exception as e:
                st.error(f"Could not load audio file: {str(e)}")
        else:
            st.warning("No audio file generated")


# ============================================================================
# MAIN UI LAYOUT
# ============================================================================

# Header
st.title("πŸ€– Voice RAG Bot")
st.markdown("AI Customer Support with Voice Recognition and Retrieval-Augmented Generation")

# Sidebar
with st.sidebar:
    st.header("βš™οΈ Configuration")
    
    # Backend status with refresh
    col1, col2 = st.columns([3, 1])
    with col1:
        st.write("**Backend Status**")
    with col2:
        if st.button("πŸ”„", help="Refresh status", key="refresh_health"):
            st.rerun()
    
    backend_healthy = check_backend_health()
    if backend_healthy:
        st.success("βœ… Backend Connected")
        st.caption(f"URL: {BACKEND_URL}")
    else:
        st.error("❌ Backend Not Connected")
        st.error(f"Cannot reach {BACKEND_URL}")
        st.info("**To fix:**")
        st.code("python -m uvicorn backend.main:app --reload --port 8000", language="bash")
        st.info("**Or use startup script:**")
        st.code(".\\START_SYSTEM.ps1", language="bash")
    
    # Customer ID input
    st.subheader("Customer Information")
    customer_id = st.text_input(
        "Customer ID",
        value=st.session_state.customer_id,
        help="Unique identifier for customer (used for history)"
    )
    st.session_state.customer_id = customer_id
    
    st.divider()
    
    # Model information
    st.subheader("System Components")
    st.write("**LLM**: Groq (gpt-oss-20b)")
    st.write("**STT**: Faster Whisper (base)")
    st.write("**Vector DB**: Qdrant")
    st.write("**Embeddings**: BGE-M3 (1024-dim)")
    st.write("**Sentiment**: DistilBERT")
    st.write("**NER**: BERT-base-NER")

# Main content
st.divider()

# Voice Bot Mode Toggle
col1, col2, col3 = st.columns([1, 3, 1])
with col1:
    voice_bot_enabled = st.toggle("πŸ€– Voice Bot Mode", value=st.session_state.voice_bot_mode, key="voice_bot_toggle")
    st.session_state.voice_bot_mode = voice_bot_enabled

if voice_bot_enabled:
    # Voice Bot Interface
    st.markdown("### πŸŽ™οΈ Voice Bot Assistant")
    
    if not st.session_state.voice_bot_active:
        # Start button
        col1, col2, col3 = st.columns([1, 2, 1])
        with col2:
            if st.button("πŸŽ™οΈ Start Conversation", use_container_width=True, key="start_voice_bot"):
                with st.spinner("Starting voice bot..."):
                    result = voice_bot_start(st.session_state.customer_id)
                    if result:
                        st.session_state.voice_bot_session = result.get("session_id")
                        st.session_state.voice_bot_active = True
                        greeting_audio = result.get("audio_path", "")
                        st.session_state.voice_bot_messages = [
                            {
                                "role": "assistant",
                                "content": result.get("greeting"),
                                "audio_path": greeting_audio
                            }
                        ]
                        st.rerun()
    
    else:
        # Conversation display
        st.markdown("#### Conversation")
        
        # Display conversation history
        for msg in st.session_state.voice_bot_messages:
            if msg["role"] == "assistant":
                with st.chat_message("assistant", avatar="πŸ€–"):
                    st.write(msg["content"])
                    # Play audio if available
                    audio_path = msg.get("audio_path", "")
                    if audio_path and audio_path.strip():
                        try:
                            # Normalize path and check in project root
                            audio_file_path = Path(audio_path.replace("\\", "/"))
                            if not audio_file_path.is_absolute():
                                project_root = Path(__file__).parent.parent
                                audio_file_path = project_root / audio_file_path
                            
                            if audio_file_path.exists():
                                with open(audio_file_path, "rb") as audio_file:
                                    audio_bytes = audio_file.read()
                                    audio_b64 = base64.b64encode(audio_bytes).decode()
                                    st.markdown(f"""
                                    <audio autoplay controls style="width: 100%;">
                                        <source src="data:audio/mpeg;base64,{audio_b64}" type="audio/mpeg">
                                    </audio>
                                    """, unsafe_allow_html=True)
                            else:
                                st.caption(f"⚠️ Audio file not found: {audio_file_path}")
                        except Exception as e:
                            st.caption(f"⚠️ Error loading audio: {str(e)}")
            else:
                with st.chat_message("user", avatar="πŸ‘€"):
                    st.write(msg["content"])
        
        # Voice conversation section
        st.markdown("---")
        st.markdown("#### 🎀 Record your message:")
        
        # Voice input - Store audio in session state
        audio_bytes = st.audio_input(
            "Record your message",
            label_visibility="collapsed",
            key="voice_bot_audio_input"
        )
        
        # If new audio recorded, store it with unique ID
        if audio_bytes:
            audio_id = id(audio_bytes)
            if audio_id != st.session_state.last_processed_audio_id:
                st.session_state.pending_audio = audio_bytes
                st.session_state.last_processed_audio_id = audio_id
                st.session_state.processing_audio = True
        
        # Process pending audio (happens on next render after audio is saved)
        if st.session_state.pending_audio and st.session_state.processing_audio:
            # Immediately mark as processing to prevent duplicate processing
            st.session_state.processing_audio = False
            
            st.info("🎀 Processing audio...")
            try:
                from io import BytesIO
                from faster_whisper import WhisperModel
                
                # Convert UploadedFile to bytes if needed
                audio_data = st.session_state.pending_audio
                if hasattr(audio_data, 'read'):
                    audio_data = audio_data.read()
                
                st.info("Loading Whisper model...")
                @st.cache_resource
                def load_whisper():
                    return WhisperModel("base", device="cpu", compute_type="int8")
                
                whisper = load_whisper()
                st.success("βœ… Whisper model loaded")
                
                st.info("Transcribing audio...")
                audio_file = BytesIO(audio_data)
                segments, info = whisper.transcribe(audio_file, language="en")
                transcribed_text = " ".join([segment.text for segment in segments])
                
                if transcribed_text.strip():
                    st.success(f"βœ… Transcribed: {transcribed_text}")
                    
                    # Add user message
                    st.session_state.voice_bot_messages.append({
                        "role": "user",
                        "content": f"🎀 {transcribed_text}"
                    })
                    
                    st.info("πŸ€– Sending to bot...")
                    result = voice_bot_process_message(transcribed_text)
                    
                    if result:
                        response = result.get("response", "")
                        audio_path = result.get("audio_path", "")
                        
                        if response:
                            st.success("βœ… Bot responded")
                            # Add ONLY ONE bot response
                            st.session_state.voice_bot_messages.append({
                                "role": "assistant",
                                "content": response,
                                "audio_path": audio_path
                            })
                            
                            # Clear pending audio immediately
                            st.session_state.pending_audio = None
                            st.session_state.processing_audio = False
                        else:
                            st.error("❌ Bot response is empty")
                            st.session_state.pending_audio = None
                            st.session_state.processing_audio = False
                    else:
                        st.error("❌ Backend returned None")
                        st.session_state.pending_audio = None
                        st.session_state.processing_audio = False
                else:
                    st.warning("⚠️ No speech detected in audio")
                    st.session_state.pending_audio = None
                    st.session_state.processing_audio = False
                    
            except Exception as e:
                st.error(f"❌ Error: {str(e)}")
                st.session_state.pending_audio = None
                st.session_state.processing_audio = False
                import traceback
                st.write(traceback.format_exc())
        
        # End conversation button
        st.markdown("---")
        if st.button("πŸ›‘ End Conversation", use_container_width=True, key="end_voice_bot"):
            with st.spinner("Ending session..."):
                result = voice_bot_end()
                st.session_state.voice_bot_active = False
                st.session_state.voice_bot_messages = []
                st.success("βœ… Session ended. Thank you!")
                st.rerun()

else:
    # Regular Input Tabs
    st.markdown("### πŸ’¬ Manual Input Mode")
    
    # Tabs for input methods
    input_tab1, input_tab2 = st.tabs(["🎀 Audio Input", "πŸ“ Text Input"])

    with input_tab1:
        st.subheader("Upload or Record Audio")
        
        col1, col2 = st.columns(2)
        
        with col1:
            st.write("**Option 1: Record Audio**")
            audio_data = st.audio_input(
                "Record your message",
                label_visibility="collapsed",
                key="audio_input"
            )
            
            if audio_data:
                st.success("Audio recorded successfully!")
                if st.button("πŸ”„ Process Audio", key="process_audio_btn"):
                    response = process_audio_file(audio_data.getvalue(), st.session_state.customer_id)
                    if response:
                        st.session_state.last_response = response
                        st.success("βœ… Processing complete!")
                        st.rerun()
        
        with col2:
            st.write("**Option 2: Upload Audio File**")
            uploaded_file = st.file_uploader(
                "Upload an MP3 or WAV file",
                type=["mp3", "wav"],
                label_visibility="collapsed"
            )
            
            if uploaded_file:
                st.success(f"File uploaded: {uploaded_file.name}")
                if st.button("πŸ”„ Process Uploaded Audio", key="process_uploaded_btn"):
                    response = process_audio_file(uploaded_file.getvalue(), st.session_state.customer_id)
                    if response:
                        st.session_state.last_response = response
                        st.success("βœ… Processing complete!")
                        st.rerun()

    with input_tab2:
        st.subheader("Enter Text Directly")
        
        # Text area for input
        user_input = st.text_area(
            "Enter your message",
            placeholder="E.g., 'I want to return my defective laptop purchased last week'",
            height=100,
            label_visibility="collapsed"
        )
        
        if user_input:
            col1, col2, col3 = st.columns([1, 1, 2])
            
            with col1:
                if st.button("πŸš€ Process Text", use_container_width=True):
                    response = process_text_input(user_input, st.session_state.customer_id)
                    if response:
                        st.session_state.last_response = response
                        st.success("βœ… Processing complete!")
                        st.rerun()
            
            with col2:
                if st.button("πŸ”„ Clear", use_container_width=True):
                    st.rerun()
            
            with col3:
                st.caption("ℹ️ Processing may take 20-30 seconds")

# Display last response if available
st.divider()

if st.session_state.last_response:
    st.subheader("πŸ“Š Latest Results")
    display_response_results(st.session_state.last_response)

# Conversation history
st.divider()

with st.expander("πŸ“œ Conversation History"):
    if st.session_state.history:
        for i, record in enumerate(st.session_state.history, 1):
            with st.container(border=True):
                col1, col2, col3, col4 = st.columns(4)
                with col1:
                    st.caption(f"Time: {record['timestamp'][:16]}")
                with col2:
                    st.caption(f"Customer: {record['customer_id']}")
                with col3:
                    st.caption(f"Intent: {record['intent']}")
                with col4:
                    st.caption(f"Sentiment: {record['sentiment']}")
                st.write(record['response'][:150] + "..." if len(record['response']) > 150 else record['response'])
    else:
        st.info("No conversation history yet")

# Footer
st.divider()
st.markdown("""
---
**Voice RAG Bot** | Powered by Groq LLM, Qdrant Vector DB, and LangGraph Orchestration  
For technical support, refer to the backend logs at `backend/main.py`
""")