Created
July 3, 2025 11:37
-
-
Save senstella/cede9b781f9b8c1a6c27ecad85d1fa4b to your computer and use it in GitHub Desktop.
A demo of Parakeet-MLX's streaming capability using Rich.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /// script | |
| # requires-python = ">=3.12" | |
| # dependencies = [ | |
| # "parakeet-mlx", | |
| # "sounddevice", | |
| # "rich", | |
| # ] | |
| # /// | |
| import queue | |
| import mlx.core as mx | |
| import numpy as np | |
| import sounddevice as sd | |
| import parakeet_mlx | |
| from rich.live import Live | |
| from rich.text import Text | |
| model = parakeet_mlx.from_pretrained("senstella/parakeet-tdt-0.6b-v2-mlx") | |
| SAMPLE_RATE = model.preprocessor_config.sample_rate | |
| CHUNK_SIZE = int(SAMPLE_RATE * 1.5) | |
| audio_queue = queue.Queue() | |
| def audio_callback(indata, frames, time, status): | |
| audio_queue.put(indata.copy()) | |
| def stream(): | |
| audio_buffer = [] | |
| buffer_samples = 0 | |
| passed_time = 0 | |
| with Live(refresh_per_second=4) as live: | |
| with sd.InputStream( | |
| callback=audio_callback, samplerate=SAMPLE_RATE, channels=1, dtype="float32" | |
| ): | |
| with model.transcribe_stream() as transcriber: | |
| while True: | |
| try: | |
| chunk = audio_queue.get(timeout=1.0) | |
| audio_buffer.append(chunk.flatten()) | |
| buffer_samples += len(chunk) | |
| if buffer_samples >= CHUNK_SIZE: | |
| combined = np.concatenate(audio_buffer)[:CHUNK_SIZE] | |
| transcriber.add_audio(mx.array(combined)) | |
| result = transcriber.result | |
| if len(result.tokens) > 0: | |
| text = Text() | |
| text.append(''.join(token.text for token in transcriber.finalized_tokens), style="bold") | |
| text.append(''.join(token.text for token in transcriber.draft_tokens), style="yellow") | |
| live.update(text) | |
| remaining = np.concatenate(audio_buffer)[CHUNK_SIZE:] | |
| audio_buffer = [remaining] if len(remaining) > 0 else [] | |
| buffer_samples = len(remaining) if len(remaining) > 0 else 0 | |
| except (queue.Empty, KeyboardInterrupt): | |
| break | |
| if __name__ == '__main__': | |
| stream() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment