forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathr_silence_compression_test.py
More file actions
320 lines (269 loc) · 10.9 KB
/
Copy pathr_silence_compression_test.py
File metadata and controls
320 lines (269 loc) · 10.9 KB
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
"""
Test: Can we compress silence sent to Modulate to save costs?
Sends identical speech audio with varying silence durations between utterances:
- 0s (no silence — back-to-back speech)
- 0.5s
- 1s
- 5s
- 10s (baseline — generous padding)
If WER stays consistent across all silence durations, we can compress silence
and save bandwidth/costs while keeping Modulate's continuous stream intact.
Usage:
cd backend && python3 scripts/stt/r_silence_compression_test.py
"""
import asyncio
import json
import os
import re
import subprocess
import sys
import time
import urllib.parse
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, cast
import websockets
MODULATE_API_KEY = os.getenv('MODULATE_API_KEY', '')
LIBRISPEECH_DIR = Path('/tmp/librispeech/LibriSpeech/test-clean')
CHUNK_SIZE = 3200
CHUNK_INTERVAL_S = 0.1
SAMPLE_RATE = 16000
PUNCT_RE = re.compile(r'[^\w\s]', re.UNICODE)
SILENCE_DURATIONS = [0, 0.5, 1, 5, 10]
def normalize(text: str) -> str:
text = PUNCT_RE.sub(' ', text).upper()
return ' '.join(text.split())
def compute_wer(ref: str, hyp: str) -> float:
ref_words = ref.split()
hyp_words = hyp.split()
if not ref_words:
return 0.0 if not hyp_words else 1.0
d = [[0] * (len(hyp_words) + 1) for _ in range(len(ref_words) + 1)]
for i in range(len(ref_words) + 1):
d[i][0] = i
for j in range(len(hyp_words) + 1):
d[0][j] = j
for i in range(1, len(ref_words) + 1):
for j in range(1, len(hyp_words) + 1):
if ref_words[i - 1] == hyp_words[j - 1]:
d[i][j] = d[i - 1][j - 1]
else:
d[i][j] = 1 + min(d[i - 1][j], d[i][j - 1], d[i - 1][j - 1])
return d[len(ref_words)][len(hyp_words)] / len(ref_words)
def build_playlist(target_s: float) -> Tuple[List[Dict[str, Any]], float]:
playlist: List[Dict[str, Any]] = []
total_s = 0.0
for reader_dir in sorted(LIBRISPEECH_DIR.iterdir()):
if not reader_dir.is_dir():
continue
for chapter_dir in sorted(reader_dir.iterdir()):
if not chapter_dir.is_dir():
continue
trans_file = list(chapter_dir.glob('*.trans.txt'))
if not trans_file:
continue
transcripts: Dict[str, str] = {}
for line in trans_file[0].read_text().strip().split('\n'):
parts = line.strip().split(' ', 1)
if len(parts) == 2:
transcripts[parts[0]] = parts[1]
for flac in sorted(chapter_dir.glob('*.flac')):
uid = flac.stem
ref = transcripts.get(uid, '')
result = subprocess.run(
[
'ffprobe',
'-v',
'error',
'-show_entries',
'format=duration',
'-of',
'default=noprint_wrappers=1:nokey=1',
str(flac),
],
capture_output=True,
text=True,
)
dur = float(result.stdout.strip()) if result.returncode == 0 else 5.0
playlist.append({'flac': str(flac), 'ref': ref, 'uid': uid, 'duration_s': dur})
total_s += dur
if total_s >= target_s:
return playlist, total_s
return playlist, total_s
def convert_to_pcm16(flac_path: str) -> Optional[bytes]:
result = subprocess.run(
['ffmpeg', '-y', '-i', flac_path, '-f', 's16le', '-ar', str(SAMPLE_RATE), '-ac', '1', 'pipe:1'],
capture_output=True,
)
return result.stdout if result.returncode == 0 else None
# Pre-convert all audio once
_pcm_cache: Dict[str, Optional[bytes]] = {}
def get_pcm(flac_path: str) -> Optional[bytes]:
if flac_path not in _pcm_cache:
_pcm_cache[flac_path] = convert_to_pcm16(flac_path)
return _pcm_cache[flac_path]
async def test_with_silence(playlist: List[Dict[str, Any]], silence_s: float, ref_text: str) -> Dict[str, Any]:
"""Send audio to Modulate with specific silence duration between utterances."""
params = {
'api_key': MODULATE_API_KEY,
'speaker_diarization': 'true',
'partial_results': 'true',
'sample_rate': str(SAMPLE_RATE),
'audio_format': 's16le',
'num_channels': '1',
'language': 'en',
}
uri = f'wss://modulate-developer-apis.com/api/velma-2-stt-streaming?{urllib.parse.urlencode(params)}'
ws = await websockets.connect(uri, ping_timeout=30, ping_interval=10, max_size=None)
utterances: List[Dict[str, Any]] = []
partials: List[Dict[str, Any]] = []
last_partial_text = ''
done_event = asyncio.Event()
t0 = time.monotonic()
async def recv() -> None:
nonlocal last_partial_text
try:
async for raw in ws:
msg: Dict[str, Any] = json.loads(raw)
mt = msg.get('type', '')
if mt == 'utterance':
utt = msg.get('utterance', msg)
utterances.append(cast(Dict[str, Any], utt))
last_partial_text = ''
elif mt == 'partial_utterance':
pu = msg.get('partial_utterance', msg)
partials.append(cast(Dict[str, Any], pu))
last_partial_text = str(cast(Dict[str, Any], pu).get('text', '')).strip()
elif mt == 'done':
done_event.set()
break
elif mt == 'error':
done_event.set()
break
except websockets.exceptions.ConnectionClosed:
pass
finally:
done_event.set()
recv_task = asyncio.create_task(recv())
# Send audio with specified silence duration
total_bytes = 0
silence_bytes = b'\x00' * int(SAMPLE_RATE * 2 * silence_s) if silence_s > 0 else b''
total_silence_bytes = 0
for i, sample in enumerate(playlist):
pcm = get_pcm(str(sample['flac']))
if not pcm:
continue
# Send speech audio in chunks
offset = 0
while offset < len(pcm):
chunk = pcm[offset : offset + CHUNK_SIZE]
try:
await ws.send(chunk)
except Exception:
break
offset += CHUNK_SIZE
total_bytes += len(chunk)
await asyncio.sleep(CHUNK_INTERVAL_S)
# Send silence between utterances (except after last)
if i < len(playlist) - 1 and silence_bytes:
try:
await ws.send(silence_bytes)
total_bytes += len(silence_bytes)
total_silence_bytes += len(silence_bytes)
except Exception:
break
# Pace silence to real-time
await asyncio.sleep(silence_s)
elapsed_send = time.monotonic() - t0
# Wait for results
try:
await asyncio.wait_for(done_event.wait(), timeout=90)
except asyncio.TimeoutError:
pass
recv_task.cancel()
try:
await ws.close()
except Exception:
pass
elapsed_total = time.monotonic() - t0
# Build transcript
utt_text = ' '.join(str(u.get('text', '')) for u in utterances).strip()
# Include last partial if not already in an utterance
full_text = utt_text
if last_partial_text and not utt_text.endswith(last_partial_text):
full_text = (utt_text + ' ' + last_partial_text).strip() if utt_text else last_partial_text
ref_norm = normalize(ref_text)
hyp_norm = normalize(full_text)
wer = compute_wer(ref_norm, hyp_norm)
word_count = len(hyp_norm.split()) if hyp_norm else 0
ref_count = len(ref_norm.split())
speech_bytes = total_bytes - total_silence_bytes
savings_pct = (total_silence_bytes / total_bytes * 100) if total_bytes > 0 else 0
return {
'silence_s': silence_s,
'wer': wer,
'word_count': word_count,
'ref_count': ref_count,
'utterances': len(utterances),
'partials': len(partials),
'total_bytes': total_bytes,
'speech_bytes': speech_bytes,
'silence_bytes': total_silence_bytes,
'savings_pct': savings_pct,
'send_time': elapsed_send,
'total_time': elapsed_total,
'text_sample': full_text[:150],
}
async def main() -> None:
print('Building playlist (target: 30s)...')
playlist, total_s = build_playlist(30)
if not playlist:
print('ERROR: No LibriSpeech data found. Download first.')
sys.exit(1)
ref_text = ' '.join(str(s['ref']) for s in playlist)
ref_norm = normalize(ref_text)
ref_words = len(ref_norm.split())
print(f' {len(playlist)} utterances, {total_s:.1f}s speech, {ref_words} ref words')
# Pre-cache PCM
for s in playlist:
get_pcm(str(s['flac']))
results: List[Dict[str, Any]] = []
for silence_s in SILENCE_DURATIONS:
label = f'{silence_s}s' if silence_s > 0 else '0s (back-to-back)'
print(f'\n{"=" * 60}')
print(f'Testing silence = {label}')
print(f'{"=" * 60}')
result = await test_with_silence(playlist, silence_s, ref_text)
results.append(result)
print(f' WER: {result["wer"] * 100:.1f}%')
print(f' Words: {result["word_count"]} / {result["ref_count"]}')
print(f' Utterances: {result["utterances"]}')
print(f' Total bytes: {result["total_bytes"] / 1024:.0f} KB')
print(f' Silence bytes:{result["silence_bytes"] / 1024:.0f} KB ({result["savings_pct"]:.1f}% of total)')
print(f' Send time: {result["send_time"]:.1f}s')
print(f' Text sample: {result["text_sample"]}')
# Summary table
print(f'\n{"=" * 60}')
print('SUMMARY: Silence Compression Results')
print(f'{"=" * 60}')
print(f'{"Silence":>10} {"WER":>8} {"Words":>8} {"UTTs":>6} {"Total KB":>10} {"Silence KB":>12} {"Savings":>10}')
print('-' * 70)
for r in results:
label = f'{r["silence_s"]}s'
print(
f'{label:>10} {r["wer"] * 100:>7.1f}% {r["word_count"]:>5}/{r["ref_count"]:<3}'
f' {r["utterances"]:>5} {r["total_bytes"] / 1024:>9.0f} {r["silence_bytes"] / 1024:>11.0f}'
f' {r["savings_pct"]:>9.1f}%'
)
# Verdict
baseline = results[-1] # 10s silence = baseline
print(f'\nBaseline (10s silence): WER = {baseline["wer"] * 100:.1f}%')
for r in results[:-1]:
delta = (r['wer'] - baseline['wer']) * 100
direction = 'worse' if delta > 0 else 'better' if delta < 0 else 'same'
print(f' {r["silence_s"]}s silence: WER = {r["wer"] * 100:.1f}% ({delta:+.1f}% {direction})')
# Save raw results
with open('/tmp/modulate_silence_test.json', 'w') as f:
json.dump(results, f, indent=2)
print(f'\nRaw results saved to /tmp/modulate_silence_test.json')
if __name__ == '__main__':
asyncio.run(main())