File size: 22,043 Bytes
b103b07
4d42741
b103b07
 
 
 
4d42741
 
 
 
 
 
 
 
 
 
 
 
 
 
b103b07
 
4d42741
b103b07
 
 
 
 
4d42741
b103b07
4d42741
b103b07
 
 
 
 
4d42741
b103b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d42741
 
 
b103b07
 
 
 
4d42741
 
b103b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d42741
b103b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d42741
 
 
b103b07
 
 
 
4d42741
 
 
 
 
b103b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d42741
 
 
b103b07
 
 
 
 
 
 
 
4d42741
b103b07
4d42741
b103b07
 
4d42741
b103b07
4d42741
 
 
b103b07
4d42741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b103b07
4d42741
b103b07
4d42741
 
 
 
 
 
b103b07
4d42741
b103b07
4d42741
 
 
 
 
de52d28
4d42741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b103b07
 
 
4d42741
 
b103b07
 
 
 
 
 
4d42741
b103b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d42741
 
 
 
 
b103b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d42741
 
 
 
 
b103b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d42741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b103b07
 
4d42741
 
b103b07
 
 
4d42741
b103b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Vera - AI Coaching Dashboard
A real-time speech emotion analysis tool for coaching sessions.
"""

import os

# Set cache directory to something writable in your Space
os.environ["HF_HOME"] = "/app/cache"
os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
os.environ["XDG_CACHE_HOME"] = "/app/cache"

# Make sure it exists
os.makedirs("/app/cache", exist_ok=True)


from transformers import pipeline
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")


import io
import wave
import pyaudio
import threading
import time
import logging
from datetime import datetime
from collections import deque
from typing import Dict, Optional, List, Tuple
from dataclasses import dataclass
from contextlib import contextmanager

from dotenv import load_dotenv
from openai import OpenAI
import streamlit as st
from transformers import pipeline
import pandas as pd
import plotly.graph_objects as go

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

load_dotenv()


@dataclass
class SentimentResult:
    """Data class for sentiment analysis results."""
    label: str
    score: float
    
    def __post_init__(self):
        """Validate sentiment result."""
        if self.label not in ["POSITIVE", "NEGATIVE", "NEUTRAL"]:
            self.label = "NEUTRAL"
        self.score = max(0.0, min(1.0, self.score))


@dataclass
class TranscriptionEntry:
    """Data class for a single transcription entry."""
    text: str
    sentiment: SentimentResult
    timestamp: datetime


class AudioConfig:
    """Configuration for audio recording."""
    def __init__(
        self,
        chunk_duration: int = 3,
        sample_rate: int = 16000,
        channels: int = 1,
        chunk_size: int = 1024,
        format: int = pyaudio.paInt16
    ):
        self.chunk_duration = chunk_duration
        self.sample_rate = sample_rate
        self.channels = channels
        self.chunk_size = chunk_size
        self.format = format


class SentimentAnalyzer:
    """Handles sentiment analysis with enhanced neutral detection."""
    
    NEUTRAL_KEYWORDS = [
        'okay', 'ok', 'fine', 'alright', 'whatever', 'maybe', 'perhaps',
        'guess', 'not sure', "don't know", 'dunno', 'meh', 'so-so',
        'neither', 'middle', 'normal', 'average', 'moderate', 'fair'
    ]
    
    CONFIDENCE_THRESHOLD = 0.8
    MIN_WORD_COUNT = 3
    
    def __init__(self, model_name: str = "distilbert-base-uncased-finetuned-sst-2-english"):
        """Initialize sentiment analyzer with specified model."""
        self.model = pipeline("sentiment-analysis", model=model_name)
    
    def analyze(self, text: str) -> SentimentResult:
        """
        Analyze sentiment of text with enhanced neutral detection.
        
        Args:
            text: Input text to analyze
            
        Returns:
            SentimentResult with label and confidence score
        """
        if not text or not text.strip():
            return SentimentResult(label="NEUTRAL", score=0.5)
        
        try:
            # Get raw sentiment from model (truncate to avoid token limit)
            result = self.model(text[:512])[0]
            label = result["label"]
            score = result["score"]
            
            # Enhanced neutral detection
            if self._should_be_neutral(text, score):
                return SentimentResult(label="NEUTRAL", score=score)
            
            return SentimentResult(label=label, score=score)
            
        except Exception as e:
            logger.error(f"Sentiment analysis error: {e}")
            return SentimentResult(label="NEUTRAL", score=0.5)
    
    def _should_be_neutral(self, text: str, score: float) -> bool:
        """Determine if text should be classified as neutral."""
        text_lower = text.lower()
        word_count = len(text.split())
        
        has_neutral_keyword = any(
            keyword in text_lower for keyword in self.NEUTRAL_KEYWORDS
        )
        
        return (
            has_neutral_keyword or
            score < self.CONFIDENCE_THRESHOLD or
            word_count < self.MIN_WORD_COUNT
        )


@st.cache_resource
def get_sentiment_analyzer() -> SentimentAnalyzer:
    """Get cached sentiment analyzer instance."""
    return SentimentAnalyzer()


class AudioTranscriber:
    """Handles audio transcription using OpenAI Whisper."""
    
    def __init__(self, client: OpenAI, audio_config: AudioConfig):
        """
        Initialize transcriber.
        
        Args:
            client: OpenAI client instance
            audio_config: Audio configuration
        """
        self.client = client
        self.audio_config = audio_config
        self._audio = pyaudio.PyAudio()
    
    def transcribe(self, audio_data: bytes) -> Optional[str]:
        """
        Transcribe audio data to text.
        
        Args:
            audio_data: Raw audio bytes
            
        Returns:
            Transcribed text or None if transcription fails
        """
        try:
            wav_buffer = self._create_wav_buffer(audio_data)
            response = self.client.audio.transcriptions.create(
                model="whisper-1",
                file=("audio.wav", wav_buffer.read(), "audio/wav"),
                language="en",
            )
            return response.text.strip() if response.text else None
            
        except Exception as e:
            logger.error(f"Transcription error: {e}")
            return None
    
    def _create_wav_buffer(self, audio_data: bytes) -> io.BytesIO:
        """Create WAV format buffer from raw audio data."""
        wav_buffer = io.BytesIO()
        with wave.open(wav_buffer, "wb") as wav_file:
            wav_file.setnchannels(self.audio_config.channels)
            wav_file.setsampwidth(
                self._audio.get_sample_size(self.audio_config.format)
            )
            wav_file.setframerate(self.audio_config.sample_rate)
            wav_file.writeframes(audio_data)
        wav_buffer.seek(0)
        return wav_buffer
    
    def cleanup(self):
        """Clean up PyAudio resources."""
        if self._audio:
            self._audio.terminate()


class CoachingDashboard:
    """Main dashboard for real-time coaching emotion analysis."""
    
    def __init__(
        self,
        chunk_duration: int = 3,
        sample_rate: int = 16000,
        max_history: int = 50
    ):
        """
        Initialize coaching dashboard.
        
        Args:
            chunk_duration: Duration of each audio chunk in seconds
            sample_rate: Audio sample rate in Hz
            max_history: Maximum number of transcriptions to keep
        """
        self.audio_config = AudioConfig(
            chunk_duration=chunk_duration,
            sample_rate=sample_rate
        )
        self.max_history = max_history
        
        # Initialize API client
        try:
            api_key = st.secrets.get("OPENAI_API_KEY") or os.getenv("OPENAI_API_KEY")
        except Exception:
            api_key = os.getenv("OPENAI_API_KEY")
        
        if not api_key:
            raise ValueError("OPENAI_API_KEY not found in environment or secrets")
        
        self.client = OpenAI(api_key=api_key)
        
        # Initialize components
        self.transcriber = AudioTranscriber(self.client, self.audio_config)
        self.sentiment_analyzer = get_sentiment_analyzer()
        
        # Audio recording state
        self.stream: Optional[pyaudio.Stream] = None
        self.is_recording = False
        self.audio_buffer_lock = threading.Lock()
        self.audio_buffer: List[bytes] = []
        
        # Session data
        self.entries: deque[TranscriptionEntry] = deque(maxlen=max_history)
        self.current_sentiment = SentimentResult(label="NEUTRAL", score=0.5)
        self.session_start: Optional[datetime] = None
    
    def start_recording(self) -> bool:
        """
        Start audio recording session.
        
        Returns:
            True if recording started successfully, False otherwise
        """
        if self.is_recording:
            logger.warning("Recording already in progress")
            return False
        
        try:
            audio = pyaudio.PyAudio()
            self.stream = audio.open(
                format=self.audio_config.format,
                channels=self.audio_config.channels,
                rate=self.audio_config.sample_rate,
                input=True,
                frames_per_buffer=self.audio_config.chunk_size,
            )
            
            self.is_recording = True
            self.session_start = datetime.now()
            
            # Start background threads
            threading.Thread(target=self._record_audio, daemon=True).start()
            threading.Thread(target=self._process_transcription, daemon=True).start()
            
            logger.info("Recording started successfully")
            return True
            
        except Exception as e:
            logger.error(f"Failed to start recording: {e}")
            self.stop_recording()
            raise
    
    def stop_recording(self):
        """Stop audio recording session."""
        if not self.is_recording:
            return
        
        self.is_recording = False
        
        if self.stream:
            try:
                self.stream.stop_stream()
                self.stream.close()
            except Exception as e:
                logger.error(f"Error closing stream: {e}")
        
        logger.info("Recording stopped")
    
    def _record_audio(self):
        """Background thread for recording audio chunks."""
        frames = []
        frames_per_chunk = int(
            self.audio_config.sample_rate * self.audio_config.chunk_duration
        )
        
        while self.is_recording:
            try:
                if not self.stream:
                    break
                
                data = self.stream.read(
                    self.audio_config.chunk_size,
                    exception_on_overflow=False
                )
                frames.append(data)
                
                # When we have enough frames, add to buffer
                if len(frames) * self.audio_config.chunk_size >= frames_per_chunk:
                    audio_chunk = b"".join(frames)
                    with self.audio_buffer_lock:
                        self.audio_buffer.append(audio_chunk)
                    frames = []
                    
            except Exception as e:
                logger.error(f"Error recording audio: {e}")
                break
    
    def _process_transcription(self):
        """Background thread for processing transcriptions."""
        while self.is_recording:
            # Get audio chunk from buffer
            audio_data = None
            with self.audio_buffer_lock:
                if self.audio_buffer:
                    audio_data = self.audio_buffer.pop(0)
            
            if audio_data:
                self._process_audio_chunk(audio_data)
            else:
                time.sleep(0.1)
    
    def _process_audio_chunk(self, audio_data: bytes):
        """Process a single audio chunk through transcription and sentiment analysis."""
        try:
            # Transcribe
            text = self.transcriber.transcribe(audio_data)
            if not text:
                return
            
            # Analyze sentiment
            sentiment = self.sentiment_analyzer.analyze(text)
            
            # Store entry
            entry = TranscriptionEntry(
                text=text,
                sentiment=sentiment,
                timestamp=datetime.now()
            )
            self.entries.append(entry)
            self.current_sentiment = sentiment
            
            logger.info(f"Processed: {text[:50]}... ({sentiment.label})")
            
        except Exception as e:
            logger.error(f"Error processing audio chunk: {e}")
    
    def get_session_duration(self) -> int:
        """Get current session duration in seconds."""
        if not self.session_start:
            return 0
        return int((datetime.now() - self.session_start).total_seconds())
    
    def get_sentiment_stats(self) -> Dict[str, int]:
        """Get count of each sentiment type."""
        stats = {"POSITIVE": 0, "NEUTRAL": 0, "NEGATIVE": 0}
        for entry in self.entries:
            stats[entry.sentiment.label] += 1
        return stats
    
    def get_recent_entries(self, n: int = 5) -> List[TranscriptionEntry]:
        """Get the n most recent transcription entries."""
        return list(self.entries)[-n:]
    
    def cleanup(self):
        """Clean up all resources."""
        self.stop_recording()
        self.transcriber.cleanup()


class DashboardUI:
    """Handles the Streamlit UI for the coaching dashboard."""
    
    COLORS = {
        "POSITIVE": "#00C853",
        "NEUTRAL": "#FFC107",
        "NEGATIVE": "#FF1744"
    }
    
    EMOJIS = {
        "POSITIVE": {
            0.95: "๐Ÿฅณ",
            0.85: "๐Ÿ˜",
            0.70: "๐Ÿ˜Š",
            0.00: "๐Ÿ™‚"
        },
        "NEGATIVE": {
            0.95: "๐Ÿ˜ญ",
            0.85: "๐Ÿ˜ข",
            0.70: "๐Ÿ˜Ÿ",
            0.00: "๐Ÿ˜•"
        },
        "NEUTRAL": {
            0.60: "๐Ÿ˜",
            0.00: "๐Ÿคท"
        }
    }
    
    def __init__(self, dashboard: CoachingDashboard):
        """Initialize UI with dashboard instance."""
        self.dashboard = dashboard
    
    def render(self):
        """Render the complete dashboard UI."""
        st.set_page_config(page_title="Vera", layout="wide")
        self._inject_custom_css()
        st.title("๐ŸŽฏ Vera - Your Coaching Companion")
        
        self._render_sidebar()
        self._render_main_content()
        
        # Auto-refresh when recording
        if self.dashboard.is_recording:
            time.sleep(2)
            st.rerun()
    
    def _inject_custom_css(self):
        """Inject custom CSS styles."""
        st.markdown("""
        <style>
        .sentiment-box {
            padding: 30px;
            border-radius: 15px;
            text-align: center;
            font-size: 20px;
            font-weight: bold;
            margin: 20px 0;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .transcription-card {
            border-radius: 8px;
            padding: 15px;
            margin: 10px 0;
            transition: transform 0.2s;
        }
        .transcription-card:hover {
            transform: translateX(5px);
        }
        </style>
        """, unsafe_allow_html=True)
    
    def _render_sidebar(self):
        """Render sidebar with controls and stats."""
        with st.sidebar:
            st.header("๐ŸŽฎ Controls")
            
            col1, col2 = st.columns(2)
            with col1:
                if st.button("โ–ถ๏ธ Start", disabled=self.dashboard.is_recording, use_container_width=True):
                    try:
                        self.dashboard.start_recording()
                        st.rerun()
                    except Exception as e:
                        st.error(f"Failed to start: {e}")
            
            with col2:
                if st.button("โน๏ธ Stop", disabled=not self.dashboard.is_recording, use_container_width=True):
                    self.dashboard.stop_recording()
                    st.rerun()
            
            st.divider()
            
            # Recording status
            if self.dashboard.is_recording:
                st.success("๐Ÿ”ด Recording...")
                duration = self.dashboard.get_session_duration()
                st.metric("Duration", f"{duration // 60}m {duration % 60}s")
            else:
                st.info("โšช Stopped")
            
            st.divider()
            
            # Statistics
            st.header("๐Ÿ“Š Statistics")
            st.metric("Total Entries", len(self.dashboard.entries))
            
            if self.dashboard.entries:
                stats = self.dashboard.get_sentiment_stats()
                total = len(self.dashboard.entries)
                
                st.metric(
                    "๐Ÿ˜Š Positive",
                    f"{stats['POSITIVE']} ({stats['POSITIVE']/total*100:.0f}%)"
                )
                st.metric(
                    "๐Ÿ˜ Neutral",
                    f"{stats['NEUTRAL']} ({stats['NEUTRAL']/total*100:.0f}%)"
                )
                st.metric(
                    "๐Ÿ˜Ÿ Negative",
                    f"{stats['NEGATIVE']} ({stats['NEGATIVE']/total*100:.0f}%)"
                )
    
    def _render_main_content(self):
        """Render main content area."""
        col1, col2 = st.columns([2, 1])
        
        with col1:
            self._render_emotion_timeline()
        
        with col2:
            self._render_current_status()
        
        st.divider()
        self._render_recent_transcriptions()
    
    def _render_emotion_timeline(self):
        """Render emotion timeline chart."""
        st.subheader("๐Ÿ“ˆ Emotion Timeline")
        
        if not self.dashboard.entries:
            st.info("Start a session to see the emotion timeline")
            return
        
        # Prepare data
        timestamps = [entry.timestamp for entry in self.dashboard.entries]
        scores = [self._sentiment_to_score(entry.sentiment) for entry in self.dashboard.entries]
        labels = [entry.sentiment.label for entry in self.dashboard.entries]
        
        # Create chart
        fig = go.Figure()
        fig.add_trace(go.Scatter(
            x=timestamps,
            y=scores,
            mode='lines+markers',
            line=dict(width=3, color='#2196F3'),
            marker=dict(
                size=12,
                color=[self.COLORS[label] for label in labels],
                line=dict(width=2, color='white')
            ),
            hovertemplate='<b>%{text}</b><br>Score: %{y:.2f}<br>%{x}<extra></extra>',
            text=labels
        ))
        
        # Add reference zones
        fig.add_hline(y=0, line_dash="dash", line_color="gray", opacity=0.5)
        fig.add_hrect(y0=0.3, y1=1, fillcolor="green", opacity=0.1, line_width=0, annotation_text="Positive")
        fig.add_hrect(y0=-0.3, y1=0.3, fillcolor="yellow", opacity=0.1, line_width=0, annotation_text="Neutral")
        fig.add_hrect(y0=-1, y1=-0.3, fillcolor="red", opacity=0.1, line_width=0, annotation_text="Negative")
        
        fig.update_layout(
            height=400,
            xaxis_title="Time",
            yaxis_title="Emotional Valence",
            yaxis=dict(range=[-1.1, 1.1]),
            showlegend=False,
            hovermode='closest'
        )
        
        st.plotly_chart(fig, use_container_width=True)
    
    def _render_current_status(self):
        """Render current emotional status."""
        st.subheader("๐Ÿ’ญ Current Status")
        
        sentiment = self.dashboard.current_sentiment
        color = self.COLORS[sentiment.label]
        emoji = self._get_emoji(sentiment)
        
        st.markdown(f"""
        <div class="sentiment-box" style="background-color: {color}; color: white;">
            <div style="font-size: 48px;">{emoji}</div>
            <div style="margin: 10px 0;">{sentiment.label}</div>
            <div style="font-size: 16px; opacity: 0.9;">
                Confidence: {sentiment.score:.0%}
            </div>
        </div>
        """, unsafe_allow_html=True)
    
    def _render_recent_transcriptions(self):
        """Render recent transcription entries."""
        st.subheader("๐Ÿ’ฌ Recent Transcriptions")
        
        if not self.dashboard.entries:
            st.info("No transcriptions yet. Start recording to see results.")
            return
        
        recent = self.dashboard.get_recent_entries(5)
        
        for entry in reversed(recent):
            color = self.COLORS[entry.sentiment.label]
            time_str = entry.timestamp.strftime("%H:%M:%S")
            emoji = self._get_emoji(entry.sentiment)
            
            st.markdown(f"""
            <div class="transcription-card" style="
                background-color: {color}20;
                border-left: 5px solid {color};
            ">
                <div style="color: {color}; font-weight: bold; margin-bottom: 8px;">
                    {emoji} [{time_str}] {entry.sentiment.label} 
                    <span style="opacity: 0.8;">({entry.sentiment.score:.0%})</span>
                </div>
                <div style="font-size: 16px; color: #333;">
                    {entry.text}
                </div>
            </div>
            """, unsafe_allow_html=True)
    
    def _sentiment_to_score(self, sentiment: SentimentResult) -> float:
        """Convert sentiment to -1 to 1 scale for visualization."""
        if sentiment.label == "POSITIVE":
            return sentiment.score
        elif sentiment.label == "NEGATIVE":
            return -sentiment.score
        else:
            return 0
    
    def _get_emoji(self, sentiment: SentimentResult) -> str:
        """Get appropriate emoji for sentiment and confidence."""
        emoji_map = self.EMOJIS.get(sentiment.label, self.EMOJIS["NEUTRAL"])
        
        for threshold, emoji in sorted(emoji_map.items(), reverse=True):
            if sentiment.score >= threshold:
                return emoji
        
        return "๐Ÿ˜"


def main():
    """Main application entry point."""
    # Initialize dashboard in session state
    if 'dashboard' not in st.session_state:
        try:
            st.session_state.dashboard = CoachingDashboard(chunk_duration=3)
        except Exception as e:
            st.error(f"Failed to initialize dashboard: {e}")
            st.stop()
    
    dashboard = st.session_state.dashboard
    
    # Render UI
    ui = DashboardUI(dashboard)
    ui.render()


if __name__ == "__main__":
    main()