--- title: Live Whisper MLX Speech To Text emoji: πŸŽ™οΈ colorFrom: blue colorTo: purple sdk: gradio sdk_version: 6.26.0 app_file: app.py pinned: false suggested_hardware: cpu-basic --- # Live Speech-to-Text β€” Whisper on MLX Real-time transcription with `openai/whisper-tiny.en` running on [MLX](https://github.com/ml-explore/mlx). **No PyTorch anywhere in the dependency tree** β€” 35 packages installed, none of them torch. Built for Apple Silicon, where MLX uses Metal. It still builds and runs on Hugging Face's free Linux CPU tier via `mlx[cpu]`. ## How it works - The browser streams microphone chunks via `gr.Audio(streaming=True)`. - Each chunk is converted to mono, resampled to 16 kHz with an anti-aliased polyphase filter, and turned into a mel spectrogram. - A per-session **AlignAtt streaming decoder** consumes each chunk and emits only the tokens its cross-attention says have settled. ### Why streaming, not a rolling buffer The obvious approach β€” keep the last N seconds of audio and re-run `model.generate()` on it every chunk β€” costs more as the buffer grows, and hallucinates. Measured on one 2.8s clip of real speech: | approach | output | |---|---| | `generate()` on the buffer | `The quick brown fox jumps over the lazy dog. The The The The The The…` | | streaming decoder | `the` β†’ `quick brown fox` β†’ `jumps over` β†’ `the lazy dog.` | Whisper pads its input to 30 seconds internally and keeps decoding into that padding, so the filler is structural rather than bad luck β€” it survived greedy decoding, disabling `condition_on_previous_text`, and trimming trailing silence. The streaming decoder never has the problem because it only emits what the audio actually supports. Throughput on an M-series Mac: roughly **8Γ— realtime** for `tiny.en`. ### MLX and Gradio: all model work runs on one thread Worth knowing if you adapt this app. **MLX streams are thread-local**, and an array created under one thread's stream can't be used from another β€” it raises `RuntimeError: There is no Stream(gpu, 0) in current thread.` Gradio runs event handlers in anyio's worker pool, which hands out different threads over time, so a model loaded at import and used directly from a handler fails on essentially every chunk. Two fixes that look right and aren't: wrapping the call in `mx.stream()`, and calling `mx.set_default_device(mx.gpu)` inside the worker. Both still raise β€” the stream can't be adopted by another thread at all. What works is keeping every MLX call on one consistent thread, here a single-worker `ThreadPoolExecutor`. That also serializes `stream` against `stop_recording`, which are separate Gradio events and could otherwise touch the same decoder concurrently. ## Hardware Designed for **`cpu-basic`**, which is all it needs β€” the model runs on MLX, not CUDA. Setting a Space to ZeroGPU (`zero-a10g`) when it doesn't need one is still worth avoiding: it costs GPU quota, and tools that read the tier will treat the Space as GPU-dependent even though `@spaces.GPU` is inert outside Hugging Face and the app would run fine on CPU elsewhere. `suggested_hardware` above only advises people who *duplicate* this Space. The tier this Space actually runs on is a repository setting β€” **Settings β†’ Hardware** in the Space UI β€” and must be changed there. ## Run locally ```bash pip install -r requirements.txt python app.py ``` Or through [sidepage](https://github.com/kalpi-4/sidepage), which fetches the Space, shows you the plan, and asks before running anything: ```bash sidepage pull huggingface.co/spaces// sidepage serve # prompts for confirmation, then serves sidepage serve --anon # ...and gives you a public URL ``` ## Tuning | Variable | Default | What it does | |---|---|---| | `WHISPER_MODEL` | `openai/whisper-tiny.en` | `openai/whisper-base.en` is more accurate and slower; `openai/whisper-small.en` more so | | `WHISPER_STREAM_EVERY` | `0.75` | Seconds between chunks β€” lower is more responsive, and more CPU | Use the standard `openai/*` repos, not the pre-converted `mlx-community/whisper-*-mlx` ones: mlx-audio converts the weights itself on load and needs the Hugging Face processor files, which those repos don't ship. Loading one fails with *"Processor not found."*