Repository navigation
Studio: keep the Read aloud speech model loaded beside the chat model - #14191
danielhanchen merged 15 commits into
Conversation
Read aloud with an Unsloth Studio speech model could not stay loaded next to the chat model. Loading a TTS model on the Audio page replaced the chat model, even with Keep multiple models loaded set to When they fit; the chat model's automatic reload then replaced the TTS model; and Read aloud named no model, so it reached the chat model and failed with "No speech model is loaded" even when both were loaded. - Audio TTS loads and the chat auto-loads send alongside, so both stay loaded under When they fit or Always. Off is unchanged. - Read aloud and Audio generation name the loaded speech model when it sits beside the chat model. - The Audio page shows, generates with and ejects its own speech model, never the chat model, and switching speech models unloads the old one first. - Keep multiple models loaded is read from every status, so a session that never opened Chat or Settings still knows it. - Settings > Voice: the engine reads "Unsloth Studio model", the TTS model row shows the loaded model, and under Off a row explains the trade-off with a "Keep both loaded" action. The Audio load toast says the same. - Loads say what they unloaded; read-aloud errors point at Audio > Text to Speech; memory settings copy mentions speech models. All 14 locales.
Codex Review SummaryThis 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. |
- Read aloud showed "[object Object]" when the named speech model had been
unloaded: the refusal nests its message ({detail: {error: {message}}}), so
read it with formatApiErrorBody like other routes.
- With a saved voice, a loaded clone-only speech model now counts for read
aloud (the server runs a saved voice as a clone).
- The Audio page lost a speech model kept beside the chat model once the chat
model was ejected (a kept model is never promoted); it now probes the kept
models whenever the active model is not audio.
- Switching speech models unloads the old one only after the new one loads, so
a failed load keeps it.
- A speech load kept beside the chat model, and ejecting a kept speech model,
no longer ask to stop running chats or cancel queued prompts, as Chat's own
keep-others load does.
- A speech load kept beside the chat model, and ejecting a speech model while the chat model stays loaded, no longer stop the local chat prompt queues: an empty requestLocalPromptQueueStop still stops every local queue. - Ejecting a speech model that is the active model with the chat model kept beside it is handled like ejecting a kept one: no question, no queue stop. - The Audio page routes, generates with and ejects a kept speech model by the id it was found under (a local model's path), not the short name its status shows, which a chat or other local model can share.
When a custom-folder speech model is the active model with the chat model kept beside it, its status names it by the file stem. Switching speech models unloaded it by that name, which may not match it or may match another model; use its model_identifier (the path) instead.
Two local GGUFs can share a file stem. The Audio page skipped every serving entry with the active model's name, so a speech model kept beside a chat model of the same stem was missed; skip only the active model's own checkpoint.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bbccc70e02
ℹ️ 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".
| // Read aloud is not a Speak run, so it stays out of Speak's history. | ||
| persist: false, | ||
| max_tokens: TTS_MAX_TOKENS, | ||
| ...(model ? { model } : {}), |
There was a problem hiding this comment.
Route read aloud by the speech slot's checkpoint
When the primary chat model and a kept TTS model are local GGUFs with the same file stem (for example /models/chat/Voice.gguf and /models/tts/Voice.gguf), /v1/models represents them with the same path-free id and loadedSpeechModelId returns Voice. The backend gives the primary slot precedence for that id, so this named request routes back to the chat model and is rejected as “not an audio model” even though the TTS slot is loaded. Resolve read aloud with the slot's serving_checkpoints identifier, as describeAudioSlot already does, instead of the catalog id.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing: it needs a chat GGUF and a TTS GGUF with the same file stem from different local folders, which is outside the cases this targets; Hub and catalog models have distinct ids.
…eside chat [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ 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". |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 382fcc4d32
ℹ️ 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".
| const replacesChat = | ||
| !useChatRuntimeStore.getState().keepModelsLoaded && | ||
| !statusRef.current?.is_audio && | ||
| (statusRef.current?.serving?.length ?? 0) > 0; |
There was a problem hiding this comment.
Detect chat replacement beside an existing speech model
When the user loads chat first and keeps a speech model beside it under fit/always, then changes Model memory to Off and picks another speech model, statusRef.current can still be the kept audio view (the setting unloads idle slots in a backend background task and this page gets no lifecycle refresh). This makes !statusRef.current?.is_audio false even though the ensuing Off-mode load replaces the still-primary chat model, so both the advance warning and the post-load “Keep both loaded” recovery action are omitted; determine displacement from the slot/serving identity or refresh status after the mode change instead.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing: this only omits an advisory toast line after a mid-session mode change; the load itself behaves correctly and the next status read corrects the view.
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ 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". |
…ad goes out [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 96f805631f
ℹ️ 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".
| }); | ||
| if (options?.signal?.aborted) | ||
| throw options.signal.reason ?? new DOMException("Aborted", "AbortError"); | ||
| await options?.beforeRequest?.(); |
There was a problem hiding this comment.
Handle cancellation during the pre-request unload
When Keep multiple models loaded is on and the user leaves Audio while useAudioModelSlot's beforeRequest unload is pending, the deactivation effect aborts pending.controller while requestStarted is still false. This await is not abortable and has no post-hook cancellation rollback, so after it removes the old speech slot, onRequestStart runs and authFetch receives an already-aborted signal; the replacement /load never goes out. Fresh evidence beyond the earlier fixed preflight report is this new cancellation window inside the awaited hook itself; make cancellation during the hook restore/retain the previous slot or otherwise complete the replacement atomically.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing: it needs the user to leave Audio during the short unload of a model they asked to replace; leaving mid-load cancels the load by design, and loading it again restores the state.
|
@codex review |
1 similar comment
|
@codex review |
…and offer Keep both loaded to the owner only [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ddc76c558d
ℹ️ 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".
| const keepsChat = useChatRuntimeStore.getState().keepModelsLoaded; | ||
| const stopDecision = keepsChat |
There was a problem hiding this comment.
Distinguish fit mode before skipping chat confirmation
When Model memory is fit, keepModelsLoaded is true even though alongside: true only preserves the primary when the new model actually fits; the backend's load_model_gated fallback (covered by test_the_replace_fallback_refuses_while_the_active_model_generates) replaces the primary and returns 409 if that chat is generating. This branch therefore skips confirmStopRunningChatsIfNeeded and sends force_cancel_active: false, so choosing a TTS model during a running chat succeeds when the pair fits but always fails when it does not, instead of offering the previous stop/force flow. Preserve the distinction between fit and guaranteed coexistence, or retry the replacement through confirmation.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing: it needs a running chat plus a speech model that does not fit beside it; the server refuses with its active-generation 409 and nothing is lost, the same as Chat's own keep-others load.
…s, and stub the new helpers in the load harnesses
# Conflicts: # studio/frontend/scripts/check-bundle-budget.ts
# Conflicts: # studio/frontend/scripts/check-bundle-budget.ts
… model (unslothai#14369) Fixes unslothai#14320 ## Summary With Keep multiple models loaded set to When they fit or Always, a speech model loaded from the Audio page already stays beside the chat model (unslothai#14191). An API request does not. When `/v1/audio/speech` names a speech model while a chat model is loaded: - Always: the request is refused with `Loading this model would unload the chat model ..., and Keep multiple models loaded is set to Always.` - When they fit: the speech model replaces the chat model, and the next chat request swaps them back. The same happens in the other order, a chat request naming the chat model while a speech model is loaded. The automatic load behind API requests always loaded into the primary seat. It never asked for a kept slot the way the Audio page and the chat auto-load do with `alongside`. ## Changes - Under When they fit or Always, when the requested model and the loaded one are different kinds (speech vs text), the automatic load now goes through `load_model_gated` with `alongside=True`. That is the same path `POST /load` uses: the kept slot, the fit checks, unloading idle models first under When they fit, and the refusal under Always when the new model does not fit. The request is then served from the new slot. - Off is unchanged. So is a request naming another model of the same kind: a second chat model still replaces the chat model, and a second speech model still replaces the speech model. - `load_model_gated` and `_run_tracked_load_model_impl` take an optional `impl_kwargs`, so the speech codec staging the switch prepares still reaches the load. ## Testing Kaggle T4x2, both Studios installed with `install.sh --local` from main `4b8447f63a` (base on GPU0, base plus this change on GPU1). Chat model `unsloth/Qwen3.5-2B-GGUF` Q4_K_M, speech model `audio-cpp/audio.cpp-gguf/Kokoro-82M-GGUF` through the API. "Serving" is what `/api/inference/status` reports. | Mode | Scenario | main | this PR | |---|---|---|---| | When they fit | chat loaded, then `/v1/audio/speech` | 200, serving only Kokoro | 200, serving Qwen and Kokoro | | When they fit | then `/v1/chat/completions`, then speech again | 200 / 200, chat reloaded, then Kokoro reloaded | 200 / 200, no reload | | When they fit | speech first, then chat | serving only Qwen | serving Kokoro and Qwen | | Always | chat loaded, then `/v1/audio/speech` | 409 (the error in the issue) | 200, serving Qwen and Kokoro | | Always | speech first, then chat | chat 409 | 200, serving Kokoro and Qwen | | Off | chat loaded, then `/v1/audio/speech` | speech model replaces chat | unchanged | | any | Audio page load with `alongside: true` | both kept | unchanged | The clips are the same size on both sides (106844, 75644 and 72044 bytes). Unit tests in `tests/test_openai_auto_switch.py`: - A speech switch under When they fit and Always loads beside a chat model. - A chat switch loads beside a speech model. - Off, or a second speech model, still replaces the primary. The three beside cases fail on main. `test_openai_auto_switch.py`, `test_multi_model_always.py` and `test_multi_model_serving.py` all pass (965 passed). The 65 backend test files that touch the changed functions pass too. The 5 failures under `-n 24` also pass serially on both main and this branch, so they are parallel-run flakes. ## Before / after Colab T4, both Studios built with `install.sh --local` (main `4b8447f63` and this PR at `c74df642c`), Keep multiple models loaded set to When they fit. One `/v1/chat/completions` request naming `unsloth/Qwen3.5-2B-GGUF`, then one `/v1/audio/speech` request naming `audio-cpp/audio.cpp-gguf/Kokoro-82M-GGUF`, then the loaded-models card on the chat page.  The same run on the final head repeats the table above: under When they fit and Always both models stay served in either order, Off still replaces, and the Kokoro clips are the same size on both sides (121244 bytes).
…thai#16831) With the "Unsloth model" read-aloud engine, Read aloud in chat can take seconds longer than Generate on the Audio page for the same text and the same loaded speech model. A user on Discord reported 10 to 20 s in chat against about half a second on the Audio page. ## Cause Before every utterance, `generateStudioTtsAudio` looks up the loaded speech model with `GET /v1/models` (`resolveReadAloudModel`, added in unslothai#14191) so it can name it in `/audio/generate`. `/v1/models` lists every downloaded model, and the scan behind it is cached for only 30 s (`_CATALOG_TTL_S`). A click more than 30 s after the last listing, which is the usual case for reading a finished reply, waits for a full rescan of the downloaded models. The Audio page never lists the catalog, so it does not pay this cost. Auto read-aloud and the Settings › Voice preview use the same function and wait the same way. ## Fix Read aloud now lists models from `GET /api/inference/loaded-models`, which unslothai#10728 added so agent startup would not wait on the same scan. Its rows are the same `_openai_model_objects` entries that `/v1/models` marks `loaded: true`, including `task` and `audio_workflows` for a speech model kept beside the chat model, so the chosen model id does not change. There are no backend changes. The "Custom endpoint" engine and system voices do not use this lookup and are unchanged. If the reporter was using "Custom endpoint", this PR does not address their delay. Not addressed: the same report says auto read-aloud does not speak a scheduled task's reply when the chat is opened. `AutoReadAloud` only speaks a reply that finishes while the chat is open, which is how it treats every chat opened from history. Speaking unread scheduled results on open would need a separate product decision. ## Verification Live on `origin/main` (`16060aba73`) with a CPU-only Studio, `gemma-4-E2B-it` Q4_K_M as the chat model and Kokoro-82M loaded beside it. I sent the request sequence the adapter sends (the listing, then `/audio/generate` naming the speech model it found), waiting 35 s before each one. Listing time: | Round | `/v1/models` (main) | `/api/inference/loaded-models` (this PR) | | --- | --- | --- | | 1 | 3.25 s | 0.02 s | | 2 | 13.16 s | 0.01 s | | 3 | 3.78 s | 0.01 s | Generation took about 9 s on both (Kokoro on the CPU). Both listings returned the same loaded rows, and every response came from Kokoro. - `read-aloud-studio-voice.test.ts` now serves the loaded-models listing, and a new test checks that read aloud never requests `/v1/models`. It fails on main. `tts-custom-connections-disabled.test.ts` mocks the new path. - 380 tests in the read-aloud, TTS and model-catalog files pass. Full frontend suite: 14943 pass and 18 fail. 17 of those also fail with main's versions of these files. The remaining one is a find-in-page timing test that passes 3 out of 3 times when run alone. - Typecheck passes. Biome and ESLint report the same findings on these files as on main. - I did not click Read aloud in a browser.
Summary
Fixes #9163 (its second ask, reading replies aloud automatically, shipped in #13357).
Read aloud with an Unsloth Studio speech model could not run next to the chat model. On main, loading a TTS model in Audio unloaded the chat model, even with Keep multiple models loaded set to When they fit. The next chat message reloaded the chat model and unloaded the TTS model. And with both loaded, Read aloud still failed with "No speech model is loaded", because the request named no model and reached the chat model.
The backend already supports both models loaded at once, so all fixes are in the frontend:
alongside: true. Under When they fit or Always, both models stay loaded. Off behaves as before, since the server ignoresalongsidethere./v1/modelsand names it in/audio/generate. Audio page Generate does the same when its model sits beside the chat model.Copy changes, in all 14 locales:
generateStudioTtsAudio(text, signal)keeps its signature, so it stays compatible with the Voice mode work in #12384.Testing
Colab L4 (23 GB), Qwen3-1.7B UD-Q4_K_XL for chat, Kokoro-82M Q8_0 and KittenTTS for speech, headless Chrome driven by Playwright.
Before the fix (main), these failed:
alongside), Read aloud returned 400 "No speech model is loaded".After the fix (main 2fee36f + this branch), these passed:
model=Kokoroand returns 200.alongside: trueand both stay loaded. Read aloud sendsmodel=Kokoroand returns 200./audiounder When they fit: loading Kokoro keeps chat loaded, and Read aloud on a chat reply returns 200.Local checks:
bundle:checkis over budget on main too (2005.9 KB vs 2003.5 KB). This change adds about 0.6 KB transfer.New and updated tests:
read-aloud-beside-chat.test.ts,audio-page-policy.test.ts,read-aloud-studio-voice.test.ts,openai-model-catalog.test.ts,audio-run-api.test.ts,tts-custom-connections-disabled.test.ts.Not tested: Always mode, transformers (non-audio.cpp) TTS models, saved or cloned voices, Windows, macOS, the desktop app, and audible playback (checked by HTTP 200 and the WAV payload only).
Before and after
Two isolated Studio installs (merge base
2fee36ffavs this branch), CPU only, Keep multiple models loaded set to When they fit, with Qwen3-0.6B Q4_K_M for chat and Kokoro-82M Q8_0 for speech loaded together.Audio page: main shows the chat model ("The loaded model is not a TTS audio model"), this branch shows Kokoro.
Read aloud on a chat reply: main sends no model and gets 400 "No model loaded.", this branch sends
model=audio-cpp/audio.cpp-gguf/Kokoro-82M-GGUFand gets 200.Settings > Voice under Off, after loading Kokoro: the engine option is now "Unsloth model" (shot before it lost "Studio" for the Desktop branding contract, so the image still reads "Unsloth Studio model"), the TTS model row names the loaded model, and a row offers "Keep both loaded".