File size: 24,713 Bytes
0bdbf4d | 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 | """Audio / ASR tools (Phase-A: Mock implementation, opt-in real Whisper).
Provides a thin :class:`ASRAdapter` interface and a deterministic
:class:`MockASR` that returns an empty transcript with neutral metadata
so the agent main loop can be developed without a real STT service.
When the env var ``STREAMO_ASR_BACKEND=whisper`` is set, ``DEFAULT_ASR``
is swapped for :class:`WhisperASR`, which:
* Locates the source mp4 via ``$STREAMO_VIDEO_DIR/<sample_id>.mp4``
(default ``data/OVO-Bench/chunked_videos_asi_full``).
* Lazily extracts the audio track once per ``sample_id`` to a temp wav.
* Lazily loads ``whisper.load_model(STREAMO_WHISPER_MODEL or 'base')``.
* For ``transcribe``: runs whisper on the [t_start, t_end] sub-clip.
* ``speaker`` / ``event`` are not natively supported by Whisper and
return neutral fallbacks (the model ignores empty results).
If anything fails (no audio track, ffmpeg missing, ckpt missing, ...)
each call returns the same shape as :class:`MockASR` so the agent loop
stays robust.
``STREAMO_ASR_BACKEND=sidecar`` selects :class:`SidecarASR`, a read-only
adapter for precomputed fixed-window Whisper transcripts. It validates the
sample ID, question time, and source-media fingerprint before exposing only
windows whose end time is already visible to the streaming agent.
"""
from __future__ import annotations
import os
import shutil
import subprocess
import tempfile
import threading
import hashlib
import json
from dataclasses import dataclass
from typing import Any, Dict, List, Optional
from .base import Tool, ToolContext, ToolResult
_LEGACY_PATH_PREFIXES = ()
def _path_prefix_rewrites() -> List[tuple[str, str]]:
"""Return configured and built-in mount-prefix relocations."""
rewrites: List[tuple[str, str]] = []
raw = os.environ.get("STREAMO_PATH_PREFIX_REWRITES", "")
for item in raw.split(","):
if "=" not in item:
continue
old, new = (part.strip().rstrip("/") for part in item.split("=", 1))
if old and new:
rewrites.append((old, new))
rewrites.extend(_LEGACY_PATH_PREFIXES)
return rewrites
def resolve_relocated_media_path(path: str) -> Optional[str]:
"""Resolve a media path even when a symlink retains an old mount root."""
candidates = [os.path.abspath(path)]
if os.path.islink(path):
target = os.readlink(path)
if not os.path.isabs(target):
target = os.path.join(os.path.dirname(path), target)
candidates.append(os.path.abspath(target))
for candidate in list(candidates):
for old, new in _path_prefix_rewrites():
if candidate == old or candidate.startswith(old + os.sep):
candidates.append(new + candidate[len(old):])
for candidate in candidates:
if os.path.isfile(candidate):
return os.path.realpath(candidate)
return None
# ---------------------------------------------------------------------------
# Adapter interface
# ---------------------------------------------------------------------------
class ASRAdapter:
def transcribe(self, sample_id: str, t_start: int, t_end: int
) -> Dict[str, Any]:
raise NotImplementedError
def speaker(self, sample_id: str, t: int) -> Dict[str, Any]:
raise NotImplementedError
def event(self, sample_id: str, t: int) -> Dict[str, Any]:
raise NotImplementedError
@dataclass
class MockASR(ASRAdapter):
def transcribe(self, sample_id, t_start, t_end):
return {"text": "", "lang": "und", "confidence": 0.0,
"t0": t_start, "t1": t_end}
def speaker(self, sample_id, t):
return {"speaker": None, "confidence": 0.0, "t": t}
def event(self, sample_id, t):
return {"label": None, "confidence": 0.0, "t": t}
# ---------------------------------------------------------------------------
# Precomputed fixed-window adapter (opt-in)
# ---------------------------------------------------------------------------
class SidecarASR(ASRAdapter):
"""Read precomputed ASR windows without loading Whisper at runtime."""
def __init__(
self,
sidecar_dir: Optional[str] = None,
video_dir: Optional[str] = None,
) -> None:
self.sidecar_dir = sidecar_dir or os.environ.get(
"STREAMO_ASR_SIDECAR_DIR", ""
)
if not self.sidecar_dir:
raise ValueError(
"STREAMO_ASR_SIDECAR_DIR is required for sidecar ASR"
)
self.video_dir = video_dir or os.environ.get(
"STREAMO_VIDEO_DIR",
"data/"
"OVO-Bench/chunked_videos_asi_full",
)
self._payload_cache: Dict[str, Dict[str, Any]] = {}
def _load(self, sample_id: str) -> Dict[str, Any]:
sid = str(sample_id)
if sid in self._payload_cache:
return self._payload_cache[sid]
path = os.path.join(self.sidecar_dir, f"{sid}.json")
with open(path, encoding="utf-8") as handle:
payload = json.load(handle)
if (
not isinstance(payload, dict)
or str(payload.get("sample_id")) != sid
or not isinstance(payload.get("question_time"), int)
or not isinstance(payload.get("segments"), list)
):
raise ValueError(f"invalid ASR sidecar: {path}")
previous_end = 0
for segment in payload["segments"]:
if not isinstance(segment, dict):
raise ValueError(f"invalid ASR sidecar window: {path}")
t0 = segment.get("t0")
t1 = segment.get("t1")
if (
not isinstance(t0, int)
or not isinstance(t1, int)
or t0 < 0
or t1 <= t0
or t0 < previous_end
or t1 > payload["question_time"]
):
raise ValueError(f"invalid ASR sidecar window: {path}")
previous_end = t1
media = payload.get("media") or {}
source = resolve_relocated_media_path(
os.path.join(self.video_dir, f"{sid}.mp4")
)
if source is None:
raise FileNotFoundError(f"ASR sidecar media missing: {sid}")
stat = os.stat(source)
if (
os.path.realpath(source) != str(media.get("path") or "")
or stat.st_size != media.get("size")
or stat.st_mtime_ns != media.get("mtime_ns")
):
raise ValueError(f"stale ASR sidecar media fingerprint: {sid}")
self._payload_cache[sid] = payload
return payload
def ready_segments(
self,
sample_id: str,
*,
after_t: int,
current_t: int,
question_time: int,
) -> List[Dict[str, Any]]:
"""Return newly visible windows, never the question-time boundary."""
payload = self._load(sample_id)
if int(payload["question_time"]) != int(question_time):
raise ValueError(
f"ASR sidecar question time mismatch for {sample_id}: "
f"{payload['question_time']} != {question_time}"
)
return [
dict(segment)
for segment in payload["segments"]
if (
int(segment["t1"]) > int(after_t)
and int(segment["t1"]) <= int(current_t)
and int(segment["t1"]) < int(question_time)
)
]
def transcribe(self, sample_id, t_start, t_end):
"""Approximate an arbitrary past-only request from complete windows."""
payload = self._load(str(sample_id))
visible_end = min(int(t_end), int(payload["question_time"]) - 1)
segments = [
segment
for segment in payload["segments"]
if (
int(segment["t1"]) <= visible_end
and int(segment["t1"]) > int(t_start)
and str(segment.get("text") or "").strip()
)
]
text = " ".join(
str(segment.get("text") or "").strip()
for segment in segments
)
return {
"text": text,
"lang": (
str(segments[-1].get("lang") or "und")
if segments else "und"
),
"confidence": 1.0 if text else 0.0,
"t0": int(t_start),
"t1": int(t_end),
"sidecar_windows": len(segments),
}
def speaker(self, sample_id, t):
return {"speaker": None, "confidence": 0.0, "t": t}
def event(self, sample_id, t):
return {"label": None, "confidence": 0.0, "t": t}
# ---------------------------------------------------------------------------
# Whisper-backed adapter (opt-in)
# ---------------------------------------------------------------------------
class WhisperASR(ASRAdapter):
"""Real ASR using openai-whisper. Lazy model + per-sample audio cache."""
def __init__(self, model_name: str = "base",
video_dir: Optional[str] = None,
ffmpeg_bin: Optional[str] = None,
download_root: Optional[str] = None,
device: Optional[str] = None) -> None:
self.model_name = model_name
self.video_dir = video_dir or os.environ.get(
"STREAMO_VIDEO_DIR",
"data/OVO-Bench/chunked_videos_asi_full",
)
self.download_root = download_root or os.environ.get(
"STREAMO_WHISPER_DOWNLOAD_ROOT",
"checkpoints/whisper",
)
# torch_npu's CUDA compatibility layer can make Whisper select an NPU
# implicitly. Keep ASR on CPU unless a validated device is requested.
self.device = device or os.environ.get(
"STREAMO_WHISPER_DEVICE", "cpu"
)
self.ffmpeg_bin = ffmpeg_bin or self._resolve_ffmpeg()
# whisper.load_audio() invokes a bare `ffmpeg` subprocess and
# ignores any explicit binary path, so we must make the resolved
# binary discoverable on PATH.
self._inject_ffmpeg_on_path()
self._model = None
self._lock = threading.Lock()
# sample_id -> path to extracted 16k mono wav (or "" if no audio)
self._wav_cache: Dict[str, str] = {}
self._transcript_cache: Dict[tuple[str, int, int], Dict[str, Any]] = {}
self.cache_dir = os.environ.get(
"STREAMO_ASR_CACHE_DIR",
os.path.join(
os.path.dirname(os.path.abspath(self.video_dir)),
".cache",
"whisper_windows",
),
)
# tmpdir for wav extracts (lazy)
self._tmpdir: Optional[str] = None
def _inject_ffmpeg_on_path(self) -> None:
if not self.ffmpeg_bin or not os.path.exists(self.ffmpeg_bin):
return
# If the resolved binary is named exactly `ffmpeg` already, just
# prepend its directory to PATH. Otherwise, drop a symlink named
# `ffmpeg` into a private dir and prepend that dir.
bin_dir = os.path.dirname(self.ffmpeg_bin)
bin_name = os.path.basename(self.ffmpeg_bin)
if bin_name == "ffmpeg":
shim_dir = bin_dir
else:
shim_dir = tempfile.mkdtemp(prefix="streamo_ffmpeg_shim_")
shim_path = os.path.join(shim_dir, "ffmpeg")
try:
if not os.path.exists(shim_path):
os.symlink(self.ffmpeg_bin, shim_path)
except OSError:
# symlink may not be permitted; copy instead
try:
shutil.copy2(self.ffmpeg_bin, shim_path)
os.chmod(shim_path, 0o755)
except Exception:
return
cur = os.environ.get("PATH", "")
if shim_dir not in cur.split(os.pathsep):
os.environ["PATH"] = shim_dir + os.pathsep + cur
# -- helpers ------------------------------------------------------
@staticmethod
def _resolve_ffmpeg() -> str:
env = os.environ.get("STREAMO_FFMPEG")
if env and os.path.exists(env):
return env
which = shutil.which("ffmpeg")
if which:
return which
try:
import imageio_ffmpeg # type: ignore
return imageio_ffmpeg.get_ffmpeg_exe()
except Exception:
return "ffmpeg" # last-resort, will fail loudly
def _ensure_tmpdir(self) -> str:
if self._tmpdir is None:
self._tmpdir = tempfile.mkdtemp(prefix="streamo_asr_")
return self._tmpdir
def _ensure_model(self):
if self._model is None:
with self._lock:
if self._model is None:
import whisper # lazy import (heavy)
if self.download_root:
os.makedirs(self.download_root, exist_ok=True)
self._model = whisper.load_model(
self.model_name,
download_root=self.download_root,
device=self.device,
)
else:
self._model = whisper.load_model(
self.model_name,
device=self.device,
)
return self._model
def _extract_audio(self, sample_id: str) -> str:
if sample_id in self._wav_cache:
return self._wav_cache[sample_id]
# Try a few common id->mp4 mappings (OVO-Bench: chunked_videos/<id>.mp4)
candidates = [os.path.join(self.video_dir, f"{sample_id}.mp4")]
# strip trailing "_step_*" suffixes if present
if "_step_" in sample_id:
base = sample_id.split("_step_")[0]
candidates.append(os.path.join(self.video_dir, f"{base}.mp4"))
mp4 = next(
(
resolved
for candidate in candidates
if (resolved := resolve_relocated_media_path(candidate))
),
None,
)
if not mp4:
self._wav_cache[sample_id] = ""
return ""
wav = os.path.join(self._ensure_tmpdir(), f"{sample_id}.wav")
if os.path.exists(wav):
self._wav_cache[sample_id] = wav
return wav
# ffmpeg: -vn drop video, 16k mono pcm wav
cmd = [self.ffmpeg_bin, "-y", "-i", mp4, "-vn",
"-ac", "1", "-ar", "16000", "-f", "wav", wav]
try:
r = subprocess.run(cmd, capture_output=True, timeout=180)
if r.returncode != 0 or not os.path.exists(wav):
self._wav_cache[sample_id] = ""
return ""
except Exception:
self._wav_cache[sample_id] = ""
return ""
self._wav_cache[sample_id] = wav
return wav
def _cache_path(
self,
sample_id: str,
t_start: int,
t_end: int,
) -> str:
source = resolve_relocated_media_path(
os.path.join(self.video_dir, f"{sample_id}.mp4")
)
if source is None:
fingerprint = "missing"
else:
stat = os.stat(source)
fingerprint = hashlib.sha256(
(
f"{os.path.realpath(source)}\0{stat.st_size}\0"
f"{stat.st_mtime_ns}"
).encode()
).hexdigest()[:20]
safe_id = hashlib.sha256(str(sample_id).encode()).hexdigest()[:16]
filename = (
f"{safe_id}_{fingerprint}_{self.model_name}_"
f"{int(t_start)}_{int(t_end)}.json"
)
return os.path.join(self.cache_dir, filename)
def _load_cached_transcript(
self,
sample_id: str,
t_start: int,
t_end: int,
) -> Optional[Dict[str, Any]]:
key = (str(sample_id), int(t_start), int(t_end))
if key in self._transcript_cache:
return dict(self._transcript_cache[key])
path = self._cache_path(sample_id, t_start, t_end)
try:
with open(path, encoding="utf-8") as handle:
payload = json.load(handle)
except (OSError, ValueError, json.JSONDecodeError):
return None
if (
not isinstance(payload, dict)
or payload.get("sample_id") != str(sample_id)
or payload.get("model") != self.model_name
or payload.get("t0") != int(t_start)
or payload.get("t1") != int(t_end)
or not isinstance(payload.get("result"), dict)
):
return None
result = dict(payload["result"])
result["cache_hit"] = True
self._transcript_cache[key] = result
return dict(result)
def _store_cached_transcript(
self,
sample_id: str,
t_start: int,
t_end: int,
result: Dict[str, Any],
) -> None:
cache_key = (str(sample_id), int(t_start), int(t_end))
stored = {
field: value
for field, value in result.items()
if field != "cache_hit"
}
self._transcript_cache[cache_key] = dict(stored)
path = self._cache_path(sample_id, t_start, t_end)
try:
os.makedirs(os.path.dirname(path), exist_ok=True)
fd, tmp = tempfile.mkstemp(
prefix=os.path.basename(path) + ".",
suffix=".tmp",
dir=os.path.dirname(path),
)
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
json.dump(
{
"sample_id": str(sample_id),
"model": self.model_name,
"t0": int(t_start),
"t1": int(t_end),
"result": stored,
},
handle,
ensure_ascii=False,
)
handle.write("\n")
os.replace(tmp, path)
finally:
if os.path.exists(tmp):
os.unlink(tmp)
except OSError:
# Cache failure must not make ASR unavailable.
return
# -- adapter methods ---------------------------------------------
def transcribe(self, sample_id, t_start, t_end):
cached = self._load_cached_transcript(sample_id, t_start, t_end)
if cached is not None:
return cached
wav = self._extract_audio(sample_id)
if not wav:
return {"text": "", "lang": "und", "confidence": 0.0,
"t0": t_start, "t1": t_end, "no_audio": True}
try:
import numpy as np # noqa: F401
import whisper
audio = whisper.load_audio(wav)
sr = 16000
i0 = max(0, int(t_start) * sr)
i1 = min(len(audio), int(t_end) * sr)
if i1 <= i0:
return {"text": "", "lang": "und", "confidence": 0.0,
"t0": t_start, "t1": t_end}
clip = audio[i0:i1]
model = self._ensure_model()
# Whisper only supports fp16 on CUDA; CPU must stay fp32.
use_fp16 = str(self.device).startswith("cuda")
res = model.transcribe(clip, fp16=use_fp16)
text = (res.get("text") or "").strip()
lang = res.get("language") or "und"
result = {
"text": text,
"lang": lang,
"confidence": 1.0 if text else 0.0,
"t0": t_start,
"t1": t_end,
}
self._store_cached_transcript(
sample_id, t_start, t_end, result
)
return result
except Exception as e: # noqa: BLE001
return {"text": "", "lang": "und", "confidence": 0.0,
"t0": t_start, "t1": t_end, "error": str(e)[:160]}
def speaker(self, sample_id, t):
# whisper does not provide speaker diarization; return neutral.
return {"speaker": None, "confidence": 0.0, "t": t,
"note": "whisper has no speaker-id; returning neutral"}
def event(self, sample_id, t):
return {"label": None, "confidence": 0.0, "t": t,
"note": "whisper has no audio-event detection"}
def _build_default_asr() -> ASRAdapter:
backend = os.environ.get("STREAMO_ASR_BACKEND", "mock").lower()
if backend == "sidecar":
try:
return SidecarASR()
except Exception:
return MockASR()
if backend == "whisper":
try:
return WhisperASR(
model_name=os.environ.get("STREAMO_WHISPER_MODEL", "base"),
)
except Exception:
return MockASR()
return MockASR()
DEFAULT_ASR: ASRAdapter = _build_default_asr()
def _ensure_audio_cache(ctx: ToolContext) -> Dict[str, Any]:
if ctx.audio_cache is None:
ctx.audio_cache = {"asr": [], "speakers": [], "events": []}
return ctx.audio_cache
# ---------------------------------------------------------------------------
# Tools
# ---------------------------------------------------------------------------
class ASRSegmentTool(Tool):
name = "asr_segment"
description = (
"Run automatic speech recognition on the audio span "
"[t_start, t_end]. Result text is cached in memory.audio_segments "
"for later retrieval; useful whenever the visible query depends "
"on speech, identity, event details, or repetition evidence."
)
parameters = {
"type": "object",
"properties": {
"t_start": {"type": "integer"},
"t_end": {"type": "integer"},
},
"required": ["t_start", "t_end"],
}
def run(self, args: Dict[str, Any], ctx: ToolContext) -> ToolResult:
try:
t0 = int(args["t_start"])
t1 = int(args["t_end"])
except (KeyError, TypeError, ValueError) as e:
return ToolResult(self.name, False, error=f"bad args: {e}")
if t1 <= t0:
return ToolResult(self.name, False, error="t_end must be > t_start")
cache = _ensure_audio_cache(ctx)
try:
res = DEFAULT_ASR.transcribe(ctx.sample_id, t0, t1)
except Exception as e: # noqa: BLE001
return ToolResult(self.name, False, error=f"asr: {e}")
cache["asr"].append(res)
if ctx.memory is not None:
ctx.memory.add_audio(t0, t1, res.get("text", ""), None)
if res.get("text"):
ctx.memory.write_event(
t0, "audio",
f"asr[{t0}-{t1}s]: {res['text'][:80]}",
ref_frame=t0, salience=1.5,
)
return ToolResult(self.name, True, data=res)
class SpeakerIDTool(Tool):
name = "speaker_id"
description = (
"Identify which speaker is talking at time `t` (returns a stable "
"label such as `S0`, `S1`, ...). Cached per sample."
)
parameters = {
"type": "object",
"properties": {"t": {"type": "integer"}},
"required": ["t"],
}
def run(self, args: Dict[str, Any], ctx: ToolContext) -> ToolResult:
try:
t = int(args["t"])
except (KeyError, TypeError, ValueError) as e:
return ToolResult(self.name, False, error=f"bad t: {e}")
cache = _ensure_audio_cache(ctx)
try:
res = DEFAULT_ASR.speaker(ctx.sample_id, t)
except Exception as e: # noqa: BLE001
return ToolResult(self.name, False, error=f"asr: {e}")
cache["speakers"].append(res)
return ToolResult(self.name, True, data=res)
class AudioEventTool(Tool):
name = "audio_event"
description = (
"Detect non-speech audio event at time `t` (laughter, vehicle, "
"music, ...). Returns label + confidence."
)
parameters = {
"type": "object",
"properties": {"t": {"type": "integer"}},
"required": ["t"],
}
def run(self, args: Dict[str, Any], ctx: ToolContext) -> ToolResult:
try:
t = int(args["t"])
except (KeyError, TypeError, ValueError) as e:
return ToolResult(self.name, False, error=f"bad t: {e}")
cache = _ensure_audio_cache(ctx)
try:
res = DEFAULT_ASR.event(ctx.sample_id, t)
except Exception as e: # noqa: BLE001
return ToolResult(self.name, False, error=f"asr: {e}")
cache["events"].append(res)
return ToolResult(self.name, True, data=res)
|