Skip to content

Unsloth Studio: read aloud starts speaking after the first sentence instead of after the whole reply - #16955

Merged
danielhanchen merged 14 commits into
unslothai:mainfrom
LeoBorcherding:read-aloud-sentences
Nov 7, 2026
Merged

danielhanchen merged 14 commits into
unslothai:mainfrom
LeoBorcherding:read-aloud-sentences

Conversation

@LeoBorcherding

@LeoBorcherding LeoBorcherding commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

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:

  1. Finding the speech model. Before each read, chat looked up which speech model was loaded by asking for the full model catalog (/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).
  2. Making the whole reply before playing any of it. Chat sends the entire reply as one speech request and waits for all of the audio before playing a single word. The Text to Speech page feels fast because you usually give it a sentence or two; a chat reply is often several paragraphs. This PR fixes that part.

#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:

  • The first piece is one sentence, so playback starts as soon as that sentence is ready.
  • While a piece is playing, the next one is already being made, so there's normally no gap.
  • Later pieces bundle sentences together (about 220 characters, never more than 320) so each request isn't too tiny.
  • Stop works at any point: it stops the current audio and nothing further is requested.

It uses the same sentence splitter and chunk planner as voice mode (#14084): voice/tts-chunk-plan.ts is identical to voice mode's, and voice/speech-text.ts carries only splitIntoSentences (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:

  • initialisms like "the U.S. is" and "e.g., the docs"
  • a closing quote before a new sentence
  • names like llama.cpp, config.json and "J. R. R. Tolkien"
  • "etc. Then"
  • a sentence now ends only where ., ! or ? (plus any closing quote) is followed by whitespace or the end, so 3.5, .75, .env, ./train.py, llama.cpp and D.C. are never cut, and a stray quote or ... never becomes a request of its own

It 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.

Read aloud playing a long reply Audio › Text to Speech with the same model loaded
Read aloud playing Text to Speech page

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

Time to first audio, before and after

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.

model reply before with this PR
Kokoro 82M (GGUF) short, 15 words 0.25 0.25
medium, 117 words 2.48 0.37
long, 386 words 8.03 0.50
Orpheus 3B (bf16) short, 15 words 10.8 12.0 (same request, run-to-run noise)
medium, 117 words 96.9 10.7
long, 386 words 106.9 18.2

The whole reply, start to finish

Whole long reply, before and after

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.

model time to hear all of it speech actually played
Kokoro 82M before 153 s 145 s
with this PR 146 s 146 s
Orpheus 3B before 164 s 47 s (about a third of the reply)
with this PR 339 s, of which about 185 s is pauses 133 s (all of it)

Two things to know about the slow model (Orpheus on an L4):

  • It now speaks the whole reply. Before, the single request for the long reply ended on its own (finish_reason 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.
  • It pauses between pieces. Orpheus makes audio slower than it plays on this GPU, so the next piece usually isn't ready yet. The math below shows why, and predicts the pauses to within a few seconds. Kokoro never paused.

GPU memory

Peak GPU memory, before and after

Measured at 20719f8, whole-GPU memory sampled every 100 ms through NVML (what nvidia-smi reports) while reading the long reply aloud. Empty GPU before any load: 0 GB.

model model loaded, idle peak, before peak, with this PR
Kokoro 82M (GGUF) 0.92 GB 3.74 GB 3.61 GB
Orpheus 3B (bf16) 6.93 GB 9.94 GB 8.83 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):

Kokoro 82M Orpheus 3B
synthesis rate (characters made per second) 252.5 6.5
speaking rate (characters per second of audio) 14.4 15.8
realtime factor (audio seconds per second of work) 17.5x 0.41x

Time to first audio ≈ characters in the first request ÷ synthesis rate. Before, the first request is the whole reply; now it's one sentence.

before: predicted / measured with this PR: predicted / measured
Kokoro, medium 596 ÷ 252.5 = 2.4 s / 2.48 s 56 ÷ 252.5 = 0.22 s / 0.37 s
Kokoro, long 2,132 ÷ 252.5 = 8.4 s / 8.03 s 137 ÷ 252.5 = 0.54 s / 0.50 s
Orpheus, medium 596 ÷ 6.5 = 92 s / 96.9 s 56 ÷ 6.5 = 8.7 s / 10.7 s
Orpheus, long 2,132 ÷ 6.5 = 330 s / 106.9 s 137 ÷ 6.5 = 21 s / 18.2 s

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).

  • Orpheus, r = 0.41: a typical 220-character piece plays for 220 ÷ 15.8 = 13.9 s and takes 220 ÷ 6.5 = 34 s to make, so each pause is about 20 s (13.9 × 1.44). Doing this piece by piece on the long reply predicts pauses of 36.5, 19.2, 17.5, 20.0, 23.7, 17.7, 12.7, 37.6 and 0 s, 185 s in all; the runs measured 184 to 189 s. The last pause is 0 because the final piece (81 characters) is short enough to be made while the 313-character piece before it plays. Total: 21 s to first audio + 135 s of speech + 185 s of pauses ≈ 341 s, against 332 to 342 s measured.
  • Kokoro, r = 17.5: even the largest piece (320 characters) takes 1.3 s to make, while the shortest piece before it plays for 9.5 s, so it never pauses and finishes in about the length of the speech (146.5 s for 146 s of audio).

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:

  • how a reply is split into pieces
  • that a one-sentence reply is one request
  • that playback starts before the rest is ready
  • that Stop requests nothing further
  • the splitter cases above

The existing read-aloud tests are updated to load the shared splitter.

@LeoBorcherding

Copy link
Copy Markdown
Collaborator Author

Codex review on the fork mirror (LeoBorcherding#777) found no major issues at 20719f8ebe, this PR's head: result.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T13:17:13.592996Z 945bfcc Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 945bfccd60

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants