Repository navigation
Unsloth Studio: read aloud starts speaking after the first sentence instead of after the whole reply - #16955
Conversation
…er the whole clip is synthesized
…entence before it
…names like llama.cpp
|
Codex review on the fork mirror (LeoBorcherding#777) found no major issues at |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…ancelled on start [skip ci]
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
What this fixes
A user in the Discord help thread reported that read aloud in chat takes 10 to 20 seconds before it says anything, while the same voice on the Audio › Text to Speech page answers in about half a second.
Two separate delays were behind that report:
/v1/models), which can rescan everything on disk. Studio: stop read aloud from waiting on a model catalog rescan #16831 fixed that: it now asks only for loaded models (/api/inference/loaded-models, a few milliseconds).#16831 removed the wait before the request; this PR removes the wait inside it. Together they cover the report.
How it works
Read aloud now splits the reply into sentences and speaks it in pieces:
It uses the same sentence splitter and chunk planner as voice mode (#14084):
voice/tts-chunk-plan.tsis identical to voice mode's, andvoice/speech-text.tscarries onlysplitIntoSentences(voice mode's streaming and text-cleanup helpers are not needed here, so they are left to #14084). Whichever of the two lands second keeps the splitter fixes below. A few splitter cases were fixed here along the way, and should be carried into voice mode's copy too:llama.cpp,config.jsonand "J. R. R. Tolkien".,!or?(plus any closing quote) is followed by whitespace or the end, so3.5,.75,.env,./train.py,llama.cppandD.C.are never cut, and a stray quote or...never becomes a request of its ownIt covers the "Unsloth model" read-aloud engine (the one that uses a loaded Unsloth Studio speech model). A one-sentence reply is still one request, exactly as before.
Colab NVIDIA L4, Unsloth Studio at 20719f8, Kokoro 82M loaded. In the server log for this click, the first piece came back 0.95 s after pressing Read aloud, the second right after it, and the third was requested 8.3 s later, the moment the first piece (8.2 s of audio) finished playing.
Numbers
All runs: Colab NVIDIA L4 (22.5 GB), Linux, torch 2.11 + CUDA 13.0. "Before" sends main's single whole-reply request to the same Unsloth Studio backend; this PR only changes the frontend, so the backend is identical either way.
Time to first audio
Measured at 48de6f3 (latest main with #16831 and this PR), median of 5 runs after 1 warm-up. The first piece of every reply is byte-identical at 20719f8, and a rerun there gave 0.48 to 0.52 s (Kokoro) and 17.2 to 18.6 s (Orpheus) for the long reply.
The whole reply, start to finish
Measured at 20719f8, long reply (2,132 characters, 10 pieces), median of 5 runs per model. Playback is modelled from each request's return time and the length of its audio: a piece plays once it has arrived and the one before has finished.
Two things to know about the slow model (Orpheus on an L4):
stop) after 32 to 51 s of speech, and the rest, roughly two thirds of the text, was never spoken. With this PR every piece is spoken.GPU memory
Measured at 20719f8, whole-GPU memory sampled every 100 ms through NVML (what
nvidia-smireports) while reading the long reply aloud. Empty GPU before any load: 0 GB.Speaking in pieces never needs more memory than the single request, and on Orpheus it peaks about 1.1 GB lower, because each request carries a shorter text and generates less audio. Kokoro: 5 runs of each, alternated on one load. Orpheus keeps its high-water mark in PyTorch's cache, so each pattern ran twice from its own fresh load. In an earlier session that alternated the two on one load, the whole-reply requests pushed it to 10.8 and then 11.9 GB, and the pieces never raised it.
The math
Each model makes speech at a fairly steady rate, so a request's time is roughly its length divided by that rate. From every piece request on the L4 (at 48de6f3 and 20719f8, same backend):
Time to first audio ≈ characters in the first request ÷ synthesis rate. Before, the first request is the whole reply; now it's one sentence.
The one miss is the point: the long Orpheus request should have taken about 330 s, but it stopped early. Its 43 to 51 s of audio at 0.41x realtime is 105 to 125 s of work, which is what was measured.
Why a model below 1x realtime pauses. Piece n+1 is requested when piece n arrives. If the model can't keep up, piece n starts playing the moment it arrives, so the gap before piece n+1 is
pause ≈ time to make piece n+1 − time to play piece n
and there's no pause when that comes out negative. With a realtime factor r, making a piece takes (its audio length) ÷ r, so for two pieces of similar size the pause is about (audio length) × (1/r − 1).
In general there's no pause when the next piece is no more than r times the length of the current one. Kokoro has 17x headroom; Orpheus on an L4 has none.
The model lookup that #16831 replaced took 0.07 to 2.9 s on this VM, against 3 to 5 ms now. That box had only a couple of models on disk, so it understates what a full model folder costs.
Tests
tests/read-aloud-sentence-chunks.test.ts(new) covers:The existing read-aloud tests are updated to load the shared splitter.