murmur-asr 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- murmur/__init__.py +26 -0
- murmur/api.py +226 -0
- murmur/audio.py +129 -0
- murmur/cli.py +79 -0
- murmur/drafting/__init__.py +16 -0
- murmur/drafting/_vllm_site/sitecustomize.py +10 -0
- murmur/drafting/vllm_proposer.py +445 -0
- murmur/drafting/whisper_drafts.py +52 -0
- murmur/metrics.py +130 -0
- murmur/models/__init__.py +24 -0
- murmur/models/base.py +94 -0
- murmur/models/moss.py +80 -0
- murmur/models/vibevoice.py +168 -0
- murmur/server.py +128 -0
- murmur/stitch.py +104 -0
- murmur/types.py +44 -0
- murmur_asr-0.2.0.dist-info/METADATA +197 -0
- murmur_asr-0.2.0.dist-info/RECORD +23 -0
- murmur_asr-0.2.0.dist-info/WHEEL +5 -0
- murmur_asr-0.2.0.dist-info/entry_points.txt +2 -0
- murmur_asr-0.2.0.dist-info/licenses/LICENSE +202 -0
- murmur_asr-0.2.0.dist-info/licenses/NOTICE +5 -0
- murmur_asr-0.2.0.dist-info/top_level.txt +1 -0
murmur/__init__.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Murmur: efficient long-context ASR inference with hierarchical chunking.
|
|
2
|
+
|
|
3
|
+
from murmur import Murmur
|
|
4
|
+
m = Murmur.load("vibevoice")
|
|
5
|
+
print(m.transcribe("meeting.wav"))
|
|
6
|
+
|
|
7
|
+
Imports are lazy so that the vLLM server environment can import
|
|
8
|
+
``murmur.drafting.vllm_proposer`` without the client's dependencies.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
__version__ = "0.2.0"
|
|
12
|
+
|
|
13
|
+
__all__ = ["Murmur", "load", "Segment", "Transcript", "evaluate"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def __getattr__(name):
|
|
17
|
+
if name in ("Murmur", "load"):
|
|
18
|
+
from . import api
|
|
19
|
+
return getattr(api, name)
|
|
20
|
+
if name in ("Segment", "Transcript"):
|
|
21
|
+
from . import types
|
|
22
|
+
return getattr(types, name)
|
|
23
|
+
if name == "evaluate":
|
|
24
|
+
from .metrics import evaluate
|
|
25
|
+
return evaluate
|
|
26
|
+
raise AttributeError(f"module 'murmur' has no attribute {name!r}")
|
murmur/api.py
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"""``Murmur``: long-form speaker-attributed ASR with hierarchical chunking.
|
|
2
|
+
|
|
3
|
+
from murmur import Murmur
|
|
4
|
+
m = Murmur.load("vibevoice") # starts a vLLM server (or pass url=...)
|
|
5
|
+
t = m.transcribe("meeting.wav")
|
|
6
|
+
print(t) # [ 12.40- 15.10] SPEAKER_01: ...
|
|
7
|
+
|
|
8
|
+
Pipeline per recording (paper Sec. 4):
|
|
9
|
+
1. diarization (pyannote) gives speech regions and speakers;
|
|
10
|
+
2. target chunks of up to ``chunk_s`` seconds are cut between speech regions;
|
|
11
|
+
3. draft windows (optional): a short-window ASR transcribes each chunk, and the
|
|
12
|
+
server-side drafter turns that transcript, format rules and the model's own
|
|
13
|
+
output into drafts that the model verifies (output unchanged);
|
|
14
|
+
4. all chunks are decoded in parallel on the vLLM server;
|
|
15
|
+
5. chunk outputs are stitched: timestamps shifted, speakers linked across chunks.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import logging
|
|
21
|
+
import shutil
|
|
22
|
+
import tempfile
|
|
23
|
+
import threading
|
|
24
|
+
import time
|
|
25
|
+
import uuid
|
|
26
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
27
|
+
from pathlib import Path
|
|
28
|
+
|
|
29
|
+
from .audio import SR, Diarizer, Timer, chunk_bounds, load_audio, resample, slice_audio
|
|
30
|
+
from .models import ModelClient, get_model
|
|
31
|
+
from .stitch import chunk_speaker_count, stitch
|
|
32
|
+
from .types import Transcript
|
|
33
|
+
|
|
34
|
+
log = logging.getLogger(__name__)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class Murmur:
|
|
38
|
+
"""A loaded model plus the Murmur pipeline around it. Create with :meth:`load`."""
|
|
39
|
+
|
|
40
|
+
def __init__(self, client: ModelClient, *, server=None, chunk_s: float = 330.0, batch_size: int = 8,
|
|
41
|
+
draft_windows: bool = False, draft_dir: str | Path | None = None, language: str = "en",
|
|
42
|
+
diarizer: Diarizer | None = None, drafter=None, keep_failed_output: bool = False,
|
|
43
|
+
owns_draft_dir: bool = False):
|
|
44
|
+
self.client, self.server = client, server
|
|
45
|
+
self.chunk_s, self.batch_size = chunk_s, batch_size
|
|
46
|
+
self.draft_windows, self.draft_dir, self.language = draft_windows, draft_dir, language
|
|
47
|
+
self.diarizer, self.drafter = diarizer, drafter
|
|
48
|
+
self.keep_failed_output = keep_failed_output
|
|
49
|
+
self._owns_draft_dir = owns_draft_dir
|
|
50
|
+
|
|
51
|
+
@classmethod
|
|
52
|
+
def load(cls, model: str = "vibevoice", *, url: str | None = None, draft_windows: bool | None = None,
|
|
53
|
+
speculative: str | None = None, chunk_s: float = 330.0, batch_size: int = 8, language: str = "en",
|
|
54
|
+
gpu: int | str | None = None, vllm_bin: str | None = None, draft_dir: str | Path | None = None,
|
|
55
|
+
diarize: bool = True, hf_token: str | None = None, served_name: str | None = None,
|
|
56
|
+
server_log: str | Path | None = None, single_pass_server: bool = False,
|
|
57
|
+
gpu_memory_utilization: float | None = None, keep_failed_output: bool = False) -> "Murmur":
|
|
58
|
+
"""Load a model.
|
|
59
|
+
|
|
60
|
+
model "vibevoice" (VibeVoice-ASR) or "moss" (MOSS-Transcribe-Diarize).
|
|
61
|
+
url an already running server (``murmur serve``); otherwise one is started
|
|
62
|
+
with the ``vllm`` of this model's environment (see :mod:`murmur.server`).
|
|
63
|
+
draft_windows speculative decoding from 30 s draft windows; default on for VibeVoice-ASR
|
|
64
|
+
(7B decoder) and off for MOSS-Transcribe-Diarize (0.6B), as in the paper,
|
|
65
|
+
and off when ``speculative`` is "ngram".
|
|
66
|
+
speculative server-side mode when starting a server: "murmur" (implied by
|
|
67
|
+
draft_windows), "ngram" (vLLM prompt lookup, a baseline) or None.
|
|
68
|
+
chunk_s target chunk length in seconds (330 in the paper).
|
|
69
|
+
batch_size chunks decoded concurrently (and the server's --max-num-seqs).
|
|
70
|
+
language "en" or "zh"; picks the draft model (Whisper-tiny / Whisper-small).
|
|
71
|
+
draft_dir directory the client and server share for drafts (required with url=
|
|
72
|
+
and draft windows; it must match the server's MURMUR_DRAFT_DIR).
|
|
73
|
+
diarize use pyannote for chunk boundaries and speaker linking (needs a HF token).
|
|
74
|
+
gpu_memory_utilization share of the GPU the started server takes (default 0.70, leaving
|
|
75
|
+
room for pyannote and the draft model on the same GPU).
|
|
76
|
+
"""
|
|
77
|
+
client_cls = get_model(model)
|
|
78
|
+
if draft_windows is None:
|
|
79
|
+
draft_windows = client_cls.draft_windows_default and speculative in (None, "murmur")
|
|
80
|
+
if speculative is None and draft_windows:
|
|
81
|
+
speculative = "murmur"
|
|
82
|
+
if draft_windows and speculative != "murmur":
|
|
83
|
+
raise ValueError("draft_windows=True needs speculative='murmur'")
|
|
84
|
+
owns = False
|
|
85
|
+
if draft_windows and draft_dir is None:
|
|
86
|
+
if url is not None:
|
|
87
|
+
raise ValueError("with url= and draft windows, pass the server's draft_dir (MURMUR_DRAFT_DIR)")
|
|
88
|
+
draft_dir, owns = tempfile.mkdtemp(prefix="murmur_drafts_"), True
|
|
89
|
+
server = None
|
|
90
|
+
if url is None:
|
|
91
|
+
from .server import VLLMServer
|
|
92
|
+
server = VLLMServer(client_cls, gpu=gpu, batch_size=batch_size, speculative=speculative,
|
|
93
|
+
draft_dir=draft_dir, vllm_bin=vllm_bin, log_path=server_log,
|
|
94
|
+
single_pass=single_pass_server, gpu_memory_utilization=gpu_memory_utilization)
|
|
95
|
+
url = server.url
|
|
96
|
+
client = client_cls(url, served_name=served_name)
|
|
97
|
+
diarizer = Diarizer(hf_token) if diarize else None
|
|
98
|
+
drafter = None
|
|
99
|
+
if draft_windows:
|
|
100
|
+
from .drafting.whisper_drafts import WhisperDrafter
|
|
101
|
+
drafter = WhisperDrafter(language)
|
|
102
|
+
return cls(client, server=server, chunk_s=chunk_s, batch_size=batch_size, draft_windows=draft_windows,
|
|
103
|
+
draft_dir=draft_dir, language=language, diarizer=diarizer, drafter=drafter,
|
|
104
|
+
keep_failed_output=keep_failed_output, owns_draft_dir=owns)
|
|
105
|
+
|
|
106
|
+
def transcribe(self, audio, sr: int | None = None, *, single_pass: bool = False,
|
|
107
|
+
timeline=None) -> Transcript:
|
|
108
|
+
"""Transcribe one recording (a path, or a waveform with ``sr``).
|
|
109
|
+
|
|
110
|
+
single_pass decode the whole recording in one request (the baseline; no chunking).
|
|
111
|
+
timeline a precomputed diarization timeline [(start, end, speaker), ...] to use
|
|
112
|
+
instead of running pyannote (its time is then not counted).
|
|
113
|
+
"""
|
|
114
|
+
return self.transcribe_batch([audio], sr=sr, single_pass=single_pass,
|
|
115
|
+
timelines=None if timeline is None else [timeline])[0]
|
|
116
|
+
|
|
117
|
+
def transcribe_batch(self, audios: list, sr: int | None = None, *, single_pass: bool = False,
|
|
118
|
+
timelines: list | None = None) -> list[Transcript]:
|
|
119
|
+
"""Transcribe several recordings, decoding all their chunks through one shared pool
|
|
120
|
+
of ``batch_size`` concurrent requests (dataset throughput)."""
|
|
121
|
+
timer = Timer()
|
|
122
|
+
wavs = [load_audio(a, sr) for a in audios]
|
|
123
|
+
durs = [w.shape[0] / SR for w in wavs]
|
|
124
|
+
cs = self.client.sample_rate
|
|
125
|
+
|
|
126
|
+
# 1. diarization and chunk boundaries
|
|
127
|
+
tls: list = [None] * len(wavs)
|
|
128
|
+
if single_pass:
|
|
129
|
+
jobs = [(r, 0.0, durs[r]) for r in range(len(wavs))]
|
|
130
|
+
else:
|
|
131
|
+
with timer("diarization"):
|
|
132
|
+
for r, w in enumerate(wavs):
|
|
133
|
+
if timelines is not None and timelines[r] is not None:
|
|
134
|
+
tls[r] = [tuple(t) for t in timelines[r]]
|
|
135
|
+
elif self.diarizer is not None:
|
|
136
|
+
tls[r] = self.diarizer(w)
|
|
137
|
+
jobs = [(r, a, b) for r in range(len(wavs)) for a, b in chunk_bounds(durs[r], self.chunk_s, tls[r])]
|
|
138
|
+
arrays = [resample(wavs[r], cs) if single_pass else slice_audio(wavs[r], a, b, cs) for r, a, b in jobs]
|
|
139
|
+
run_id = uuid.uuid4().hex[:8]
|
|
140
|
+
keys = [f"{run_id}_{i}" for i in range(len(jobs))]
|
|
141
|
+
|
|
142
|
+
# 2. draft windows (a background thread, overlapped with decoding)
|
|
143
|
+
ready = None
|
|
144
|
+
draft_thread = None
|
|
145
|
+
if self.draft_windows and not single_pass:
|
|
146
|
+
from .drafting.whisper_drafts import write_draft
|
|
147
|
+
ready = [threading.Event() for _ in jobs]
|
|
148
|
+
|
|
149
|
+
def drafting():
|
|
150
|
+
t0 = time.perf_counter()
|
|
151
|
+
for i, (r, a, b) in enumerate(jobs):
|
|
152
|
+
try:
|
|
153
|
+
words = self.drafter(slice_audio(wavs[r], a, b, SR))
|
|
154
|
+
diar = ([list(t) for t in tls[r] if t[1] > a and t[0] < b]
|
|
155
|
+
if self.client.name == "moss" and tls[r] else [])
|
|
156
|
+
write_draft(Path(self.draft_dir), keys[i], words, self.language, a, diar)
|
|
157
|
+
except Exception: # a chunk without drafts still decodes (structure + self copy)
|
|
158
|
+
log.exception("drafting failed for chunk %d", i)
|
|
159
|
+
ready[i].set()
|
|
160
|
+
timer.stages["drafting"] = time.perf_counter() - t0
|
|
161
|
+
draft_thread = threading.Thread(target=drafting, daemon=True)
|
|
162
|
+
|
|
163
|
+
# 3. decode all chunks in parallel
|
|
164
|
+
def decode(i):
|
|
165
|
+
if ready is not None:
|
|
166
|
+
ready[i].wait()
|
|
167
|
+
return self.client.decode(arrays[i], request_id=keys[i] if ready is not None else None)
|
|
168
|
+
|
|
169
|
+
t0 = time.perf_counter()
|
|
170
|
+
if draft_thread is not None:
|
|
171
|
+
draft_thread.start()
|
|
172
|
+
with ThreadPoolExecutor(max_workers=self.batch_size) as ex:
|
|
173
|
+
replies = list(ex.map(decode, range(len(jobs))))
|
|
174
|
+
if draft_thread is not None:
|
|
175
|
+
draft_thread.join()
|
|
176
|
+
timer.stages["decoding"] = time.perf_counter() - t0
|
|
177
|
+
self._cleanup_drafts(keys)
|
|
178
|
+
|
|
179
|
+
# 4. stitch per recording (with the model-informed speaker-count hint)
|
|
180
|
+
out = []
|
|
181
|
+
for r in range(len(wavs)):
|
|
182
|
+
idx = [i for i, j in enumerate(jobs) if j[0] == r]
|
|
183
|
+
pieces = [(jobs[i][1], jobs[i][2], self.client.parse(replies[i].text, keep_raw=self.keep_failed_output))
|
|
184
|
+
for i in idx]
|
|
185
|
+
tl = tls[r]
|
|
186
|
+
if tl and self.diarizer is not None and not single_pass:
|
|
187
|
+
k = chunk_speaker_count([p[2] for p in pieces])
|
|
188
|
+
if k > 1 and len({t[2] for t in tl}) < k:
|
|
189
|
+
with timer("rediarization"):
|
|
190
|
+
tl = self.diarizer(wavs[r], min_speakers=k) or tl
|
|
191
|
+
segs = stitch(pieces, None if single_pass else tl, merge_gap_s=self.client.merge_gap_s,
|
|
192
|
+
clamp=self.client.name == "vibevoice")
|
|
193
|
+
stats = {"n_chunks": len(idx), "chunks_failed": sum(self.client.flagged(replies[i]) for i in idx),
|
|
194
|
+
"request_s": [round(replies[i].seconds, 3) for i in idx],
|
|
195
|
+
"chunks": [(jobs[i][1], jobs[i][2]) for i in idx], "stages_s": dict(timer.stages)}
|
|
196
|
+
out.append(Transcript(segs, durs[r], stats))
|
|
197
|
+
return out
|
|
198
|
+
|
|
199
|
+
def _cleanup_drafts(self, keys):
|
|
200
|
+
if self.draft_dir is None:
|
|
201
|
+
return
|
|
202
|
+
for k in keys:
|
|
203
|
+
try:
|
|
204
|
+
(Path(self.draft_dir) / f"{k}.json").unlink()
|
|
205
|
+
except FileNotFoundError:
|
|
206
|
+
pass
|
|
207
|
+
|
|
208
|
+
def close(self) -> None:
|
|
209
|
+
"""Stop the server Murmur started (if any) and remove its temporary draft directory."""
|
|
210
|
+
if self.server is not None:
|
|
211
|
+
self.server.stop()
|
|
212
|
+
self.server = None
|
|
213
|
+
if self._owns_draft_dir and self.draft_dir:
|
|
214
|
+
shutil.rmtree(self.draft_dir, ignore_errors=True)
|
|
215
|
+
|
|
216
|
+
def __enter__(self):
|
|
217
|
+
return self
|
|
218
|
+
|
|
219
|
+
def __exit__(self, *exc):
|
|
220
|
+
self.close()
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def load(model: str = "vibevoice", **kw) -> Murmur:
|
|
224
|
+
"""Shortcut for :meth:`Murmur.load`."""
|
|
225
|
+
return Murmur.load(model, **kw)
|
|
226
|
+
|
murmur/audio.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"""Audio loading, pyannote diarization, and target-chunk boundaries."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
import threading
|
|
8
|
+
import time
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
import torch
|
|
13
|
+
|
|
14
|
+
log = logging.getLogger(__name__)
|
|
15
|
+
SR = 16_000
|
|
16
|
+
|
|
17
|
+
Timeline = list[tuple[float, float, str]] # (start_s, end_s, speaker)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def load_audio(audio, sr: int | None = None) -> torch.Tensor:
|
|
21
|
+
"""16 kHz mono float32 waveform from a path, numpy array or tensor (give ``sr`` for arrays)."""
|
|
22
|
+
import torchaudio
|
|
23
|
+
if isinstance(audio, (str, Path)):
|
|
24
|
+
import soundfile as sf
|
|
25
|
+
data, sr = sf.read(str(audio), dtype="float32", always_2d=True) # (frames, channels)
|
|
26
|
+
wav = torch.from_numpy(data.mean(axis=1))
|
|
27
|
+
else:
|
|
28
|
+
wav = audio if isinstance(audio, torch.Tensor) else torch.as_tensor(np.asarray(audio, dtype=np.float32))
|
|
29
|
+
if sr is None:
|
|
30
|
+
raise ValueError("pass sr= with a raw waveform")
|
|
31
|
+
wav = wav.float()
|
|
32
|
+
if wav.ndim > 1:
|
|
33
|
+
wav = wav.mean(0) if wav.shape[0] < wav.shape[-1] else wav.mean(-1)
|
|
34
|
+
if sr != SR:
|
|
35
|
+
wav = torchaudio.functional.resample(wav.unsqueeze(0), sr, SR).squeeze(0)
|
|
36
|
+
return wav.contiguous()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def resample(wav16: torch.Tensor, target_sr: int) -> np.ndarray:
|
|
40
|
+
if target_sr == SR:
|
|
41
|
+
return wav16.cpu().numpy().astype(np.float32)
|
|
42
|
+
import torchaudio
|
|
43
|
+
return torchaudio.functional.resample(wav16.unsqueeze(0), SR, target_sr).squeeze(0).cpu().numpy().astype(np.float32)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def hf_token(token: str | None = None) -> str | None:
|
|
47
|
+
if token:
|
|
48
|
+
return token
|
|
49
|
+
if os.environ.get("HF_TOKEN"):
|
|
50
|
+
return os.environ["HF_TOKEN"]
|
|
51
|
+
p = Path(os.path.expanduser("~/.cache/huggingface/token"))
|
|
52
|
+
return p.read_text().strip() if p.exists() else None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class Diarizer:
|
|
56
|
+
"""pyannote speaker-diarization-3.1. Gives target-chunk boundaries and links speakers across chunks.
|
|
57
|
+
|
|
58
|
+
Needs ``pyannote.audio`` and a Hugging Face token with access to the gated pyannote models.
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
def __init__(self, token: str | None = None, device: str | None = None):
|
|
62
|
+
from pyannote.audio import Pipeline
|
|
63
|
+
tok = hf_token(token)
|
|
64
|
+
if not tok:
|
|
65
|
+
raise RuntimeError("pyannote needs a Hugging Face token: set HF_TOKEN or run `huggingface-cli login`, "
|
|
66
|
+
"and accept the terms of pyannote/speaker-diarization-3.1.")
|
|
67
|
+
dev = torch.device(device or ("cuda" if torch.cuda.is_available() else "cpu"))
|
|
68
|
+
self.pipe = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1", token=tok).to(dev)
|
|
69
|
+
self._lock = threading.Lock()
|
|
70
|
+
|
|
71
|
+
def __call__(self, wav16: torch.Tensor, min_speakers: int | None = None) -> Timeline:
|
|
72
|
+
kw = {"min_speakers": min_speakers} if min_speakers else {}
|
|
73
|
+
with self._lock:
|
|
74
|
+
# cuDNN autotuning: without it the embedding model is ~4x slower on recent torch/cuDNN
|
|
75
|
+
prev, torch.backends.cudnn.benchmark = torch.backends.cudnn.benchmark, True
|
|
76
|
+
try:
|
|
77
|
+
raw = self.pipe({"waveform": wav16.unsqueeze(0), "sample_rate": SR}, **kw)
|
|
78
|
+
finally:
|
|
79
|
+
torch.backends.cudnn.benchmark = prev
|
|
80
|
+
if torch.cuda.is_available():
|
|
81
|
+
torch.cuda.empty_cache() # the vLLM server shares this GPU
|
|
82
|
+
ann = raw if hasattr(raw, "itertracks") else (getattr(raw, "speaker_diarization", None)
|
|
83
|
+
or getattr(raw, "diarization", None)
|
|
84
|
+
or next(iter(vars(raw).values())))
|
|
85
|
+
return [(float(t.start), float(t.end), str(spk)) for t, _, spk in ann.itertracks(yield_label=True)]
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def chunk_bounds(duration_s: float, chunk_s: float, timeline: Timeline | None) -> list[tuple[float, float]]:
|
|
89
|
+
"""Target chunks of at most ``chunk_s`` seconds, cut between speech regions.
|
|
90
|
+
|
|
91
|
+
Speech regions come from the diarization timeline (in its order); without one,
|
|
92
|
+
the audio is cut into fixed ``chunk_s`` windows.
|
|
93
|
+
"""
|
|
94
|
+
if not timeline:
|
|
95
|
+
n = max(1, int(np.ceil(duration_s / chunk_s)))
|
|
96
|
+
return [(i * chunk_s, min(duration_s, (i + 1) * chunk_s)) for i in range(n)]
|
|
97
|
+
out = []
|
|
98
|
+
start, end = timeline[0][0], timeline[0][1]
|
|
99
|
+
for t0, t1, _ in timeline[1:]:
|
|
100
|
+
if t1 - start <= chunk_s:
|
|
101
|
+
end = t1
|
|
102
|
+
else:
|
|
103
|
+
out.append((start, end))
|
|
104
|
+
start, end = t0, t1
|
|
105
|
+
out.append((start, end))
|
|
106
|
+
return out
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def slice_audio(wav16: torch.Tensor, start: float, end: float, target_sr: int) -> np.ndarray:
|
|
110
|
+
a, b = int(start * SR), min(int(end * SR), wav16.shape[0])
|
|
111
|
+
return resample(wav16[a:b], target_sr)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class Timer:
|
|
115
|
+
"""Wall-clock stage timer: ``with t("vad"): ...``; ``t.stages`` maps name -> seconds."""
|
|
116
|
+
|
|
117
|
+
def __init__(self):
|
|
118
|
+
self.stages: dict[str, float] = {}
|
|
119
|
+
|
|
120
|
+
def __call__(self, name):
|
|
121
|
+
timer = self
|
|
122
|
+
|
|
123
|
+
class _Ctx:
|
|
124
|
+
def __enter__(self):
|
|
125
|
+
self.t0 = time.perf_counter()
|
|
126
|
+
|
|
127
|
+
def __exit__(self, *a):
|
|
128
|
+
timer.stages[name] = timer.stages.get(name, 0.0) + time.perf_counter() - self.t0
|
|
129
|
+
return _Ctx()
|
murmur/cli.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Command line: ``murmur serve`` starts a model's vLLM server; ``murmur transcribe`` transcribes files.
|
|
2
|
+
|
|
3
|
+
# in the model's vLLM environment
|
|
4
|
+
murmur serve vibevoice --port 8000 --draft-dir /tmp/murmur_drafts
|
|
5
|
+
# in the client environment
|
|
6
|
+
murmur transcribe meeting.wav --model vibevoice --url http://localhost:8000 --draft-dir /tmp/murmur_drafts
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import logging
|
|
13
|
+
import os
|
|
14
|
+
import sys
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _serve(a) -> int:
|
|
18
|
+
from .models import get_model
|
|
19
|
+
from .server import server_command
|
|
20
|
+
client = get_model(a.model)
|
|
21
|
+
spec = None if a.speculative == "none" else a.speculative
|
|
22
|
+
if spec == "murmur":
|
|
23
|
+
os.makedirs(a.draft_dir, exist_ok=True)
|
|
24
|
+
cmd, env = server_command(client, port=a.port, batch_size=a.batch_size, speculative=spec,
|
|
25
|
+
draft_dir=a.draft_dir if spec == "murmur" else None, vllm_bin=a.vllm_bin,
|
|
26
|
+
single_pass=a.single_pass, gpu_memory_utilization=a.gpu_memory_utilization)
|
|
27
|
+
print(" ".join(cmd), file=sys.stderr)
|
|
28
|
+
os.execvpe(cmd[0], cmd, {**os.environ, **env}) # replace this process with vLLM
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _transcribe(a) -> int:
|
|
32
|
+
from .api import Murmur
|
|
33
|
+
with Murmur.load(a.model, url=a.url, draft_dir=a.draft_dir, chunk_s=a.chunk_s, batch_size=a.batch_size,
|
|
34
|
+
language=a.language, gpu=a.gpu, diarize=not a.no_diarize,
|
|
35
|
+
draft_windows=None if a.draft_windows == "auto" else a.draft_windows == "on") as m:
|
|
36
|
+
for f in a.files:
|
|
37
|
+
t = m.transcribe(f, single_pass=a.single_pass)
|
|
38
|
+
print(t.to_json(indent=1) if a.json else f"== {f}\n{t}")
|
|
39
|
+
return 0
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def main(argv=None) -> int:
|
|
43
|
+
p = argparse.ArgumentParser(prog="murmur", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
44
|
+
sub = p.add_subparsers(dest="cmd", required=True)
|
|
45
|
+
|
|
46
|
+
s = sub.add_parser("serve", help="start a vLLM server for a model, with Murmur's drafter")
|
|
47
|
+
s.add_argument("model", choices=["vibevoice", "moss"])
|
|
48
|
+
s.add_argument("--port", type=int, default=8000)
|
|
49
|
+
s.add_argument("--batch-size", type=int, default=8)
|
|
50
|
+
s.add_argument("--speculative", choices=["murmur", "ngram", "none"], default="murmur")
|
|
51
|
+
s.add_argument("--draft-dir", default="/tmp/murmur_drafts", help="shared with the client")
|
|
52
|
+
s.add_argument("--single-pass", action="store_true", help="MOSS: settings for whole-recording requests")
|
|
53
|
+
s.add_argument("--vllm-bin", default=None)
|
|
54
|
+
s.add_argument("--gpu-memory-utilization", type=float, default=None,
|
|
55
|
+
help="share of the GPU for vLLM (default 0.70; raise it if the client runs elsewhere)")
|
|
56
|
+
s.set_defaults(func=_serve)
|
|
57
|
+
|
|
58
|
+
t = sub.add_parser("transcribe", help="transcribe audio files")
|
|
59
|
+
t.add_argument("files", nargs="+")
|
|
60
|
+
t.add_argument("--model", default="vibevoice", choices=["vibevoice", "moss"])
|
|
61
|
+
t.add_argument("--url", default=None, help="running server; default: start one")
|
|
62
|
+
t.add_argument("--draft-dir", default=None)
|
|
63
|
+
t.add_argument("--draft-windows", choices=["auto", "on", "off"], default="auto")
|
|
64
|
+
t.add_argument("--chunk-s", type=float, default=330.0)
|
|
65
|
+
t.add_argument("--batch-size", type=int, default=8)
|
|
66
|
+
t.add_argument("--language", default="en", choices=["en", "zh"])
|
|
67
|
+
t.add_argument("--gpu", default=None)
|
|
68
|
+
t.add_argument("--no-diarize", action="store_true")
|
|
69
|
+
t.add_argument("--single-pass", action="store_true")
|
|
70
|
+
t.add_argument("--json", action="store_true")
|
|
71
|
+
t.set_defaults(func=_transcribe)
|
|
72
|
+
|
|
73
|
+
a = p.parse_args(argv)
|
|
74
|
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
|
|
75
|
+
return a.func(a)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
if __name__ == "__main__":
|
|
79
|
+
sys.exit(main())
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Draft windows and the server-side drafter.
|
|
2
|
+
|
|
3
|
+
Kept import-light on purpose: the vLLM server imports ``murmur.drafting.vllm_proposer``
|
|
4
|
+
in an environment that has vLLM and transformers but not the client dependencies.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
#: Directory to put on the vLLM server's PYTHONPATH (holds sitecustomize.py).
|
|
10
|
+
SITE_DIR = Path(__file__).resolve().parent / "_vllm_site"
|
|
11
|
+
|
|
12
|
+
VIBEVOICE_SPEC_CONFIG = '{"method":"ngram","num_speculative_tokens":8,"prompt_lookup_max":4,"prompt_lookup_min":2}'
|
|
13
|
+
MOSS_SPEC_CONFIG = '{"model":"murmur.drafting.vllm_proposer.MurmurMossProposer","num_speculative_tokens":8}'
|
|
14
|
+
NGRAM_SPEC_CONFIG = VIBEVOICE_SPEC_CONFIG # vLLM's built-in prompt-lookup baseline
|
|
15
|
+
|
|
16
|
+
__all__ = ["SITE_DIR", "VIBEVOICE_SPEC_CONFIG", "MOSS_SPEC_CONFIG", "NGRAM_SPEC_CONFIG"]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Put this directory on the vLLM server's PYTHONPATH (Murmur does this when it
|
|
2
|
+
# starts the server). With MURMUR_MODEL set, it turns vLLM's n-gram proposer
|
|
3
|
+
# into Murmur's drafter, for vLLM builds without a custom-proposer hook
|
|
4
|
+
# (VibeVoice-ASR's vLLM 0.14).
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
if os.environ.get("MURMUR_MODEL"):
|
|
8
|
+
from murmur.drafting import vllm_proposer as _m
|
|
9
|
+
|
|
10
|
+
_m.patch_ngram(os.environ["MURMUR_MODEL"])
|