Spaces:
Running on Zero
Running on Zero
File size: 11,582 Bytes
2f95512 f1ef7e2 2f95512 f1ef7e2 2f95512 f1ef7e2 2f95512 f1ef7e2 2f95512 f1ef7e2 2f95512 f1ef7e2 2f95512 | 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 | """
Sentiment timeline utilities for long audio analysis.
This module splits longer audio files into smaller segments, runs emotion
prediction on each segment, and calculates:
- Segment-level sentiment timeline
- Emotional volatility
- Audio sentiment shift
- Peak emotional timestamp
Why this matters:
CREMA-D contains short labelled clips, so the model learns emotion at the
speech-segment level. For call-center audio, we apply the same model to
short windows across the full call to understand how emotion changes over time.
"""
import tempfile
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Dict, List, Optional
import numpy as np
import soundfile as sf
from src.data.audio_dataset import (
DEFAULT_SAMPLE_RATE,
load_audio_file,
resolve_audio_path,
)
from src.sentiment_config import IntensityLevel, SentimentShift
from src.sentiment_schema import (
EmotionProbabilities,
PeakEmotion,
SentimentSegment,
infer_overall_sentiment,
seconds_to_timestamp,
)
@dataclass(frozen=True)
class TimelineConfig:
"""
Configuration for segmenting long audio into timeline windows.
"""
sample_rate: int = DEFAULT_SAMPLE_RATE
segment_duration_seconds: float = 5.0
min_segment_duration_seconds: float = 1.0
max_duration_seconds: Optional[float] = None
@dataclass(frozen=True)
class AudioSegmentWindow:
"""
Represents one audio segment window.
"""
segment_id: int
start_time_seconds: float
end_time_seconds: float
waveform: np.ndarray
def split_audio_into_segments(
audio_path: Path,
config: Optional[TimelineConfig] = None,
) -> List[AudioSegmentWindow]:
"""
Split an audio file into fixed-length segments.
Args:
audio_path:
Path to audio file. Can be relative to ml-services or absolute.
config:
Timeline segmentation configuration.
Returns:
List of AudioSegmentWindow objects.
"""
if config is None:
config = TimelineConfig()
resolved_path = resolve_audio_path(str(audio_path))
waveform, _ = load_audio_file(
audio_path=resolved_path,
target_sample_rate=config.sample_rate,
max_duration_seconds=config.max_duration_seconds,
)
total_samples = len(waveform)
segment_samples = int(config.segment_duration_seconds * config.sample_rate)
min_segment_samples = int(config.min_segment_duration_seconds * config.sample_rate)
if total_samples == 0:
raise ValueError(f"Audio file is empty: {audio_path}")
segments: List[AudioSegmentWindow] = []
segment_id = 0
for start_sample in range(0, total_samples, segment_samples):
end_sample = min(start_sample + segment_samples, total_samples)
segment_waveform = waveform[start_sample:end_sample]
if len(segment_waveform) < min_segment_samples:
continue
start_time = start_sample / config.sample_rate
end_time = end_sample / config.sample_rate
segments.append(
AudioSegmentWindow(
segment_id=segment_id,
start_time_seconds=float(start_time),
end_time_seconds=float(end_time),
waveform=segment_waveform,
)
)
segment_id += 1
if not segments:
segments.append(
AudioSegmentWindow(
segment_id=0,
start_time_seconds=0.0,
end_time_seconds=total_samples / config.sample_rate,
waveform=waveform,
)
)
return segments
def save_segment_to_temp_wav(
segment: AudioSegmentWindow,
sample_rate: int = DEFAULT_SAMPLE_RATE,
) -> Path:
"""
Save a segment waveform to a temporary WAV file.
We do this so the existing EmotionPredictor probability function can reuse
the same audio loading path safely.
"""
temp_file = tempfile.NamedTemporaryFile(
suffix=".wav",
delete=False,
)
temp_path = Path(temp_file.name)
temp_file.close()
sf.write(
file=str(temp_path),
data=segment.waveform,
samplerate=sample_rate,
)
return temp_path
def build_emotion_probabilities_schema(
probabilities: Dict[str, float],
) -> EmotionProbabilities:
"""
Convert raw model probabilities into EmotionProbabilities schema.
"""
return EmotionProbabilities(
anger=probabilities.get("anger", 0.0),
disgust=probabilities.get("disgust", 0.0),
fear=probabilities.get("fear", 0.0),
happy=probabilities.get("happy", 0.0),
neutral=probabilities.get("neutral", 0.0),
sadness=probabilities.get("sadness", 0.0),
)
def calculate_segment_risk_score(
probabilities: EmotionProbabilities,
) -> float:
"""
Calculate risk score for a segment using emotion probabilities.
This matches the first-version escalation logic used for full-clip inference.
"""
score = (
0.35 * probabilities.anger
+ 0.25 * probabilities.stress_probability()
+ 0.25 * probabilities.negative_probability()
+ 0.15 * probabilities.fear
)
return float(np.clip(score, 0.0, 1.0))
def build_sentiment_timeline(
audio_path: Path,
probability_predictor: Callable[[Path], Dict[str, float]],
config: Optional[TimelineConfig] = None,
single_window_probabilities: Optional[Dict[str, float]] = None,
) -> List[SentimentSegment]:
"""
Build a segment-level sentiment timeline for an audio file.
Args:
audio_path:
Path to the full audio file.
probability_predictor:
Function that accepts an audio path and returns emotion probabilities.
config:
Segmenting configuration.
Returns:
List of SentimentSegment objects.
"""
if config is None:
config = TimelineConfig()
audio_segments = split_audio_into_segments(audio_path, config)
timeline: List[SentimentSegment] = []
for segment in audio_segments:
if len(audio_segments) == 1 and single_window_probabilities is not None:
probabilities = build_emotion_probabilities_schema(
single_window_probabilities
)
timeline.append(
SentimentSegment(
segment_id=segment.segment_id,
start_time_seconds=round(segment.start_time_seconds, 3),
end_time_seconds=round(segment.end_time_seconds, 3),
dominant_emotion=probabilities.dominant_emotion(),
overall_audio_sentiment=infer_overall_sentiment(probabilities),
emotion_probabilities=probabilities,
risk_score=calculate_segment_risk_score(probabilities),
)
)
continue
temp_path = save_segment_to_temp_wav(
segment=segment,
sample_rate=config.sample_rate,
)
try:
raw_probabilities = probability_predictor(temp_path)
probabilities = build_emotion_probabilities_schema(raw_probabilities)
dominant_emotion = probabilities.dominant_emotion()
overall_sentiment = infer_overall_sentiment(probabilities)
risk_score = calculate_segment_risk_score(probabilities)
timeline.append(
SentimentSegment(
segment_id=segment.segment_id,
start_time_seconds=round(segment.start_time_seconds, 3),
end_time_seconds=round(segment.end_time_seconds, 3),
dominant_emotion=dominant_emotion,
overall_audio_sentiment=overall_sentiment,
emotion_probabilities=probabilities,
risk_score=risk_score,
)
)
finally:
try:
temp_path.unlink(missing_ok=True)
except Exception:
pass
return timeline
def calculate_emotional_volatility(
timeline: List[SentimentSegment],
) -> IntensityLevel:
"""
Calculate how much emotion changes across the call.
For one short CREMA-D clip, volatility will usually be Low. For long calls,
frequent emotion/risk changes can become Medium or High.
"""
if len(timeline) <= 1:
return IntensityLevel.LOW
emotion_changes = 0
risk_changes: List[float] = []
for previous_segment, current_segment in zip(timeline[:-1], timeline[1:]):
if previous_segment.dominant_emotion != current_segment.dominant_emotion:
emotion_changes += 1
risk_changes.append(
abs(current_segment.risk_score - previous_segment.risk_score)
)
emotion_change_rate = emotion_changes / max(len(timeline) - 1, 1)
average_risk_change = float(np.mean(risk_changes)) if risk_changes else 0.0
volatility_score = (0.60 * emotion_change_rate) + (0.40 * average_risk_change)
if volatility_score >= 0.55:
return IntensityLevel.HIGH
if volatility_score >= 0.25:
return IntensityLevel.MEDIUM
return IntensityLevel.LOW
def calculate_audio_sentiment_shift(
timeline: List[SentimentSegment],
) -> SentimentShift:
"""
Calculate whether emotion improved, worsened, stayed unchanged, or was mixed.
Uses risk score from the first and last meaningful segments.
"""
if len(timeline) <= 1:
return SentimentShift.UNCHANGED
first_risk = timeline[0].risk_score
last_risk = timeline[-1].risk_score
risk_delta = last_risk - first_risk
risk_values = [segment.risk_score for segment in timeline]
risk_range = max(risk_values) - min(risk_values)
if risk_range >= 0.45 and abs(risk_delta) < 0.20:
return SentimentShift.MIXED
if risk_delta <= -0.20:
return SentimentShift.IMPROVED
if risk_delta >= 0.20:
return SentimentShift.WORSENED
return SentimentShift.UNCHANGED
def find_peak_emotion(
timeline: List[SentimentSegment],
) -> PeakEmotion:
"""
Find the segment with the highest emotional risk score.
"""
if not timeline:
return PeakEmotion()
peak_segment = max(timeline, key=lambda segment: segment.risk_score)
peak_time = (
peak_segment.start_time_seconds + peak_segment.end_time_seconds
) / 2.0
return PeakEmotion(
time_seconds=round(float(peak_time), 3),
timestamp=seconds_to_timestamp(peak_time),
emotion=peak_segment.dominant_emotion,
score=peak_segment.risk_score,
)
def summarize_timeline_risk(
timeline: List[SentimentSegment],
) -> float:
"""
Calculate a call-level risk score from the timeline.
Uses both average risk and peak risk so that one very emotional segment is
not ignored.
"""
if not timeline:
return 0.0
risk_values = [segment.risk_score for segment in timeline]
average_risk = float(np.mean(risk_values))
peak_risk = float(np.max(risk_values))
call_level_score = (0.60 * average_risk) + (0.40 * peak_risk)
return float(np.clip(call_level_score, 0.0, 1.0))
|