-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_parallel_models.py
More file actions
274 lines (215 loc) · 7.92 KB
/
test_parallel_models.py
File metadata and controls
274 lines (215 loc) · 7.92 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
#!/usr/bin/env python3
"""
Parallel model comparison test - BOTH in real-time mode.
Runs Nemotron (streaming) and Parakeet (RMS-based chunking) simultaneously
on the same live microphone input to compare transcription quality.
Usage:
python test_parallel_models.py
Press Enter to start recording, Enter again to stop.
"""
import sys
import threading
import queue
import time
import numpy as np
import sounddevice as sd
import tempfile
import soundfile as sf
SAMPLE_RATE = 16000
CHUNK_SIZE_MS = 1120 # Nemotron streaming chunk size
CHUNK_SIZE_SAMPLES = int(SAMPLE_RATE * CHUNK_SIZE_MS / 1000)
# RMS detection settings (same as main app)
RMS_THRESHOLD = 0.01
SILENCE_DURATION_S = 1.0
MIN_CHUNK_DURATION_S = 0.5
# Shared state
nemotron_queue = queue.Queue() # For Nemotron streaming
parakeet_queue = queue.Queue() # For Parakeet batch chunks
is_recording = False
stop_event = threading.Event()
# RMS-based chunking state for Parakeet
rms_audio_buffer = []
rms_silence_s = 0.0
rms_is_speaking = False
def load_models():
"""Load both models."""
print("Loading models...")
# Load Nemotron
print(" Loading Nemotron (streaming)...")
from models.nemotron import NemotronModel
nemotron = NemotronModel()
nemotron.load_model()
# Load Parakeet MLX
print(" Loading Parakeet MLX (batch with RMS)...")
from models.parakeet_mlx import ParakeetMLXModel
parakeet = ParakeetMLXModel()
parakeet.load_model()
print("Both models loaded!\n")
return nemotron, parakeet
def audio_callback(indata, frames, time_info, status):
"""Audio callback - feeds both pipelines."""
global rms_audio_buffer, rms_silence_s, rms_is_speaking
if not is_recording:
return
chunk = indata.copy().flatten()
chunk_duration_s = float(frames) / SAMPLE_RATE
# === NEMOTRON: Fixed-interval streaming ===
nemotron_queue.put(chunk)
# === PARAKEET: RMS-based silence detection ===
rms = np.sqrt(np.mean(chunk**2))
is_speech = rms >= RMS_THRESHOLD
if is_speech:
rms_audio_buffer.append(chunk)
rms_silence_s = 0.0
rms_is_speaking = True
elif rms_is_speaking:
rms_silence_s += chunk_duration_s
if rms_silence_s >= SILENCE_DURATION_S:
# Silence threshold reached - queue segment for Parakeet
if rms_audio_buffer:
segment = np.concatenate(rms_audio_buffer)
segment_duration = len(segment) / SAMPLE_RATE
if segment_duration >= MIN_CHUNK_DURATION_S:
parakeet_queue.put(segment)
rms_audio_buffer = []
rms_silence_s = 0.0
rms_is_speaking = False
else:
rms_audio_buffer.append(chunk)
def nemotron_worker(model, results):
"""Worker thread for Nemotron streaming."""
accumulated_samples = []
chunk_count = 0
while not stop_event.is_set() or not nemotron_queue.empty():
try:
chunk = nemotron_queue.get(timeout=0.1)
accumulated_samples.append(chunk)
# Process when we have enough samples
total_samples = sum(len(c) for c in accumulated_samples)
if total_samples >= CHUNK_SIZE_SAMPLES:
audio_data = np.concatenate(accumulated_samples).flatten()
chunk_to_process = audio_data[:CHUNK_SIZE_SAMPLES]
remainder = audio_data[CHUNK_SIZE_SAMPLES:]
if len(remainder) > 0:
accumulated_samples = [remainder]
else:
accumulated_samples = []
chunk_count += 1
text = model.stream_chunk(chunk_to_process)
if text:
results['nemotron'] = text
# Live render Nemotron output with chunk count and text length
display = text[-70:] if len(text) > 70 else text # Show END of text
print(f"\n [N#{chunk_count} len={len(text)}] ...{display}", end='', flush=True)
nemotron_queue.task_done()
except queue.Empty:
continue
# Process any remaining audio with is_final=True
if accumulated_samples:
remaining = np.concatenate(accumulated_samples).flatten()
if len(remaining) > 0:
chunk_count += 1
text = model.stream_chunk(remaining, is_final=True)
if text:
results['nemotron'] = text
# Finalize
final_text = model.finalize_streaming()
results['nemotron'] = final_text
results['nemotron_chunks'] = chunk_count
def parakeet_worker(model, results):
"""Worker thread for Parakeet with RMS-based real-time chunking."""
transcriptions = []
segment_count = 0
while not stop_event.is_set() or not parakeet_queue.empty():
try:
segment = parakeet_queue.get(timeout=0.1)
segment_count += 1
# Save to temp file and transcribe
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
sf.write(f.name, segment, SAMPLE_RATE)
temp_path = f.name
result = model.transcribe_batch([temp_path])
if result and result[0]:
transcriptions.append(result[0])
# Show running transcription
current_text = " ".join(transcriptions)
display = current_text[:70] + "..." if len(current_text) > 70 else current_text
print(f"\n [P] {display}", end='', flush=True)
# Cleanup
import os
os.unlink(temp_path)
parakeet_queue.task_done()
except queue.Empty:
continue
results['parakeet'] = " ".join(transcriptions)
results['parakeet_segments'] = segment_count
def main():
global is_recording, rms_audio_buffer, rms_silence_s, rms_is_speaking
print("=" * 60)
print("PARALLEL MODEL COMPARISON TEST (Both Real-Time)")
print("=" * 60)
print()
print("Nemotron: Fixed 1120ms chunks (streaming API)")
print("Parakeet: RMS-based silence detection (batch per segment)")
print()
# Load models
nemotron, parakeet = load_models()
# Results storage
results = {
'nemotron': '',
'parakeet': '',
'nemotron_chunks': 0,
'parakeet_segments': 0
}
input("Press ENTER to start recording...")
print("\nRecording... (press ENTER to stop)\n")
# Initialize Nemotron streaming
nemotron.init_streaming()
# Reset state
rms_audio_buffer = []
rms_silence_s = 0.0
rms_is_speaking = False
stop_event.clear()
is_recording = True
# Start workers
nemotron_thread = threading.Thread(target=nemotron_worker, args=(nemotron, results))
parakeet_thread = threading.Thread(target=parakeet_worker, args=(parakeet, results))
nemotron_thread.start()
parakeet_thread.start()
# Start audio stream
stream = sd.InputStream(
samplerate=SAMPLE_RATE,
channels=1,
callback=audio_callback,
dtype=np.float32
)
stream.start()
# Wait for user to stop
input()
# Stop recording
print("\n\nStopping...")
is_recording = False
# Queue any remaining audio for Parakeet
if rms_audio_buffer:
segment = np.concatenate(rms_audio_buffer)
if len(segment) / SAMPLE_RATE >= MIN_CHUNK_DURATION_S:
parakeet_queue.put(segment)
stop_event.set()
stream.stop()
stream.close()
# Wait for workers
print(" Waiting for Nemotron...")
nemotron_thread.join(timeout=10)
print(" Waiting for Parakeet...")
parakeet_thread.join(timeout=30)
# Print results
print("\n" + "=" * 60)
print("RESULTS")
print("=" * 60)
print(f"\n[NEMOTRON - Streaming] ({results['nemotron_chunks']} chunks)")
print(f" \"{results['nemotron']}\"")
print(f"\n[PARAKEET - RMS Chunked] ({results['parakeet_segments']} segments)")
print(f" \"{results['parakeet']}\"")
print("\n" + "=" * 60)
if __name__ == "__main__":
main()