Spaces:
Paused

File size: 4,347 Bytes
1fa72d8
c86091f
3c40194
 
 
1fa72d8
c86091f
1fa72d8
 
c86091f
1fa72d8
 
c86091f
3c40194
c86091f
 
 
 
 
 
3c40194
 
 
c86091f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fab89cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c86091f
 
fab89cc
 
 
 
 
 
 
c86091f
 
 
 
3c40194
 
 
 
 
 
 
 
c86091f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c40194
c86091f
 
 
fab89cc
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
---
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(<stream captured on the main thread>)`, 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/<owner>/<name>
sidepage serve <name>            # prompts for confirmation, then serves
sidepage serve <name> --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."*