Skip to content

Move Pocket TTS to Kyutai 4.x models, add Dutch, harden the server, polish the demo - #49

Merged
ronibhakta1 merged 3 commits into
readium:mainfrom
ronibhakta1:feature/pocket-small-models
Oct 28, 2026
Merged

ronibhakta1 merged 3 commits into
readium:mainfrom
ronibhakta1:feature/pocket-small-models

Conversation

@ronibhakta1

@ronibhakta1 ronibhakta1 commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Moves Pocket TTS to Kyutai's 4.x models, adds Dutch, brings the demo page up to the current @readium/speech, and fixes a set of problems found while auditing and testing the server end to end.

Closes #35
Closes #36
Closes #37
Closes #38
Closes #39
Closes #40
Closes #41
Closes #42
Closes #43
Closes #44
Closes #45
Closes #46
Closes #47
Closes #48

What changed

Models (#35)

  • pocket-tts 4.0.1. English uses the 18M flagship (english_18m_2026-10, ~111 MB); fr/it/de/es/pt/nl use their retrained 6-layer models (~219 MB). The old 24-layer _24l models and the old english default are no longer supported.
  • Dutch (nl) added to every voice's cross-language set, plus a native Dutch voice, daan. 27 voices.
  • Wizard, planner, prune script, docs and tests updated.

Demo (#36, #37, #45, #47)

  • @readium/speech 0.18.0: submitPreferences(new SpeechPreferences(...)) replaces the removed setVoice/setRate/setPitch/setSpeakInContentLanguage.
  • Replay after the clip ended reuses the loaded audio instead of calling /synthesize again.
  • Word highlighting fixed (Demo: word highlighting never paints (decorate() called with the wrong shape) #48): decorate() needs the quote under text: { highlight, before, after }; the demo passed them at the top level, so nothing was ever painted.
  • Opus is selected by default, and the engine is rebuilt so the request really uses it.
  • UI refinement (Demo: smaller type, voice picker grouping, loading feedback and polish #47), same design: smaller type and spacing so the page fits one screen; voice picker groups by language, native first; elapsed time on the Speak button while synthesizing; character counter from the server limit; GitHub button and underlined endpoint links in the header; shadcn/ui look in plain CSS (zinc tokens; button, select, badge, card, accordion and alert styles; no React or build step); empty-state hint under the player; plain "server is busy" message on a 429.

Server fixes (#38–#44, #46)

  • CORS (CORS_ORIGINS, default https://readium.org) and nginx OPTIONS for the preflight.
  • Deploy gates on /readyz with --wait; healthcheck start period 600 s.
  • ElevenLabs: production warning when uncapped; usage counter on the weights volume.
  • Busy server: queue wait capped, 429 with Retry-After.
  • API_KEY_ENABLED=true is refused at startup (nothing enforced it).
  • Circuit breaker half-open admits one trial at a time.
  • Wizard: no crash without pydantic; stale cross-language pairs dropped when languages change; server warns about ignored pairs.
  • Integration test fixture initialises the semaphore.

Audio (#45)

  • With no output.bitrate: Opus 32 kbps (-application voip), MP3 64 kbps. Chosen on measured wideband PESQ against the 24 kHz source: Opus 32k 4.50 / 48k 4.55 / 64k 4.48; MP3 32k 3.81 / 64k 4.45 / 128k 4.63. A client bitrate still wins.
  • nginx serves HTTP/2, and answers rate-limit rejections with 429 instead of 503.

Testing

Check Result
ruff, ruff format --check, mypy app/ clean
pytest -m 'not integration and not slow' 120 passed
pytest -m integration (real en/fr/nl models) 6 passed, 1 skipped
nginx -t (CI method) ok
Real server, API /service, /voices per language, synthesis in nl/fr/en, clean 404 for unsupported voice+language, default Opus 14 KB / MP3 26 KB / WAV 157 KB for the same sentence
CORS allowed origin gets headers, other origin 400, Retry-After and X-Request-Id exposed
Busy server 3 parallel requests, 1 slot: 1 × 200, 2 × 429 with Retry-After: 5
Full nginx stack (self-signed cert) burst of 12 requests: 7 × 429, 5 × 200; HTTP/2 negotiated, http→https redirect, security headers, preflight through nginx 200, PUT and GET /synthesize 403, 413 above 32 KB, dotfiles 403
Demo in a browser Dutch sample and voices, Opus request (20 KB vs 247 KB), replay makes 0 new requests, grouped voice picker for en/fr/nl, elapsed-time label, busy message on a forced 429, desktop and phone width, no console errors
Weights prune (dry run) keeps english_18m, french, dutch; would remove old french_24l, italian, spanish, english_7m
Wizard helpers stale pairs dropped, valid pairs and *:* kept, plan view works through the uv fallback

CodeRabbit CLI: 3 findings, all already satisfied by the current code (daan is in voices.json, the planner supports nl and its self-test passes with 27 voices / 30 pairs, pocket-tts is pinned >=4.0.1 and locked at 4.0.1). One earlier finding (a late result from an overlapping half-open probe) was skipped on purpose.

Not tested

  • ElevenLabs paths against the real API (need a live key). Word highlighting was checked with stubbed word timings on real Pocket audio, not with real ElevenLabs timings.
  • A real deploy to the VM, and nginx with a real Let's Encrypt cert.
  • The interactive wizard end to end (only its functions were run).
  • Opus 32k was not A/B-listened against 48k; band levels up to 12 kHz are identical across 24–64k.

Observations, not changed

  • Pocket's 6-layer models (fr, nl, …) run near real time (about 2.2–3.0 s for 3 s of audio) against 0.64 s for English; that is model size, not encoding.
  • MAX_CONCURRENT_SYNTHESES only parallelises different languages (each model is single-threaded behind a lock). For several readers in one language, raise WORKERS; this is now noted in docs/configuration.md.
  • voices.json marks alba as male (already on main) while docs/voices.md says female. Not changed; needs a source.
  • The engine's reaction to nginx's plain 429 (no Retry-After) was not tested.
  • With http2 on, a mismatched Host header gets 421 instead of the catch-all's 444; still rejected.

Pocket TTS
- Bump pocket-tts to 4.0.1 (uv.lock refreshed).
- English now uses the 18.2M flagship (english_18m_2026-10): ~111 MB
  instead of 219 MB, ~3.5x faster on CPU at matching quality.
- French switches from french_24l to the new 6-layer french. The 24-layer
  _24l models are no longer used.
- Add Dutch (nl) with the native voice daan; every voice declares nl.
- configure.sh and pocket_plan.py know about nl.
- prune_weights.py docstring: pocket-tts 4.x pins files to mixed
  revisions, so pruning stays folder-based.

Demo
- Pin @readium/speech to 0.18.0 (was 0.5.0). Replace the removed
  setVoice/setRate/setPitch/setSpeakInContentLanguage calls with
  submitPreferences(new SpeechPreferences(...)).

Docs and tests
- Measured sizes and RAM in configuration.md and pocket.md; fix the stale
  README download size and the API.md language-row fallback claim.
- Tests for the model map, installed model configs, voice language
  coverage and Dutch; integration test for en/fr/nl.

Refs readium#35, readium#36
Server
- CORS for the Readium Speech demos (CORS_ORIGINS, default https://readium.org);
  nginx now allows OPTIONS for the preflight and answers rate limits with 429.
- Deploy gates on /readyz (healthcheck + docker compose up --wait), so a failed
  model load fails the job instead of going green.
- ElevenLabs: warn in production when the daily cap is 0; keep the usage
  counter on the weights volume so a redeploy does not reset it.
- A busy server returns 429 with Retry-After after SYNTHESIS_QUEUE_TIMEOUT_SECONDS
  instead of queueing without limit.
- API_KEY_ENABLED=true is refused at startup (nothing enforces the key).
- Circuit breaker admits one half-open trial at a time.
- Wizard: no crash without pydantic, drops cross-language pairs for languages
  that are no longer selected; the server warns about ignored pairs.
- Pocket integration fixture initialises the semaphore.

Audio
- Default bitrate when the request gives none: Opus 32 kbps (voip), MP3 64 kbps,
  chosen on measured PESQ against the 24 kHz source. A client bitrate still wins.
- nginx serves HTTP/2.

Demo
- Replay reuses the loaded audio instead of calling /synthesize again.
- Word highlighting works: decorate() needs the quote under text, the demo
  passed it at the top level so nothing was painted.
- Opus is the default format and the engine is rebuilt so the request matches.
- Smaller type and spacing, shadcn-style components in plain CSS, grouped voice
  picker, elapsed time on the Speak button, character counter, empty-state hint,
  GitHub button and clickable endpoint links.

Closes readium#36, readium#37, readium#38, readium#39, readium#40, readium#41, readium#42, readium#43, readium#44, readium#45, readium#46, readium#47, readium#48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved CORS gaps, blocked circuit-breaker recovery, an order-dependent integration test, an insufficient deployment timeout, and incomplete audio replay leave functional and reliability issues.

6 open issues
What changed in this PR

Updates the server’s Pocket TTS integration to Kyutai 4.x, adds Dutch support, and improves deployment reliability and the Readium Speech demo.

Changes:

  • Migrates language models, expands the catalog to 27 voices, and updates configuration tools and documentation.
  • Adds CORS, bounded synthesis queues, readiness-gated deployment, and provider safeguards.
  • Updates the demo to Readium Speech 0.18.0, improves replay and highlighting, and tunes audio defaults.
File Description
uv.lock Locks Pocket TTS 4.0.1 and updated dependencies.
tests/​test_pocket_models.py Adds model-map and Dutch catalog checks.
tests/​test_language_filter.py Updates language coverage and voice counts.
tests/​test_ffmpeg_bitrate.py Tests bitrate defaults and overrides.
tests/​test_cors.py Tests CORS, authentication guard, and busy responses.
tests/​test_circuit_breaker.py Tests half-open trial gating.
tests/​integration/​test_pocket_tts.py Initializes concurrency and adds multilingual synthesis checks.
scripts/​prune_weights.py Clarifies revision-aware cache-pruning documentation.
scripts/​pocket_plan.py Adds Dutch and updates planner expectations.
scripts/​configure.sh Adds Dutch, planner fallback, and stale-pair cleanup.
README.md Updates supported languages and download sizes.
pyproject.toml Raises the minimum Pocket TTS version.
nginx/​templates/​20-speech-server.conf.template Enables HTTP/2, preflight requests, and 429 limit responses.
nginx/​templates/​00-hardening.conf.template Corrects concurrency and authentication comments.
Makefile Prepares development tools for Docker checks.
docs/​voices.md Documents the expanded catalog and embedding sizes.
docs/​providers/​pocket.md Documents the new language models.
docs/​providers/​elevenlabs.md Clarifies usage-counter persistence.
docs/​development.md Updates Docker tooling and request-flow documentation.
docs/​deployment.md Documents readiness gates and deployment safeguards.
docs/​configuration.md Updates settings, model sizes, and concurrency guidance.
docs/​API.md Clarifies CORS, errors, language selection, and bitrates.
Dockerfile Switches health checks to readiness with longer startup grace.
docker-compose.yml Persists ElevenLabs usage on the weights volume.
docker-compose.dev.yml Mounts files needed by development commands.
app/​static/​demo.html Updates playback APIs, replay, highlighting, and UI.
app/​providers/​pocket_tts.py Migrates models and warns about ignored language pairs.
app/​main.py Adds CORS and uncapped ElevenLabs warnings.
app/​drivers/​ffmpeg.py Sets speech-oriented Opus and MP3 defaults.
app/​data/​voices/​pocket/​voices.json Adds Dutch coverage and the Daan voice.
app/​core/​registry.py Removes the registry lookup method.
app/​core/​daily_limit.py Removes the standalone self-check.
app/​core/​concurrency.py Bounds queue waits and returns retry guidance.
app/​core/​circuit_breaker.py Gates half-open trial requests.
app/​config/​settings.py Adds CORS and queue settings; rejects unsupported authentication.
app/​api/​routes/​synthesize.py Returns buffered audio directly and removes obsolete error metadata.
app/​api/​errors.py Supports error-response headers and removes an unused error class.
.github/​workflows/​deploy.yml Waits for application readiness before continuing deployment.

Review effort: Balanced

Comment thread .github/workflows/deploy.yml Resolved
Comment thread app/core/circuit_breaker.py Resolved
Comment thread app/main.py Resolved
Comment thread app/static/demo.html Resolved
Comment thread tests/integration/test_pocket_tts.py Resolved
… deploy

- deploy: give the SSH step a 20m command timeout, longer than the 12m
  readiness wait (the action's default is 10m).
- circuit breaker: a half-open trial that ends with a client/quota error
  (404, 429) no longer keeps the single trial slot; record_neutral() frees it
  without changing the breaker's state.
- CORS: unhandled errors are answered by Starlette's outermost layer, outside
  CORSMiddleware, so the 500 now carries the allow-origin header itself.
- nginx: its own rate-limit 429 responses now carry the CORS headers for
  NGINX_CORS_ORIGIN (default https://readium.org), Retry-After and a JSON body.
  Security headers move into a snippet so the named location can include them.
- demo: replay only when the first audio chunk is still there; a seek drops
  earlier chunks and replaying the tail as the whole clip would be wrong.
- integration tests: initialise the semaphore for every test, not only the
  ones using the fixture.
@ronibhakta1
ronibhakta1 merged commit b45f496 into readium:main Oct 28, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants