Case study

Audiobook AI

Any document, read aloud.

Turns a PDF, EPUB, DOCX, web page, or pasted text into a continuous audiobook, synthesized with the open-source Kokoro text-to-speech model on PyTorch.

Turning arbitrary documents into listenable audio is a pipeline problem, not a single API call. This page documents the pipeline and the hardening that keeps it running under load.

Kokoro TTS · PyTorch · FastAPI · Worker Queue · PWA · Cloudflare Tunnel

A messy document becomes one continuous, listenable audiobook.

What goes in

A 300-page PDF with headers, footnote markers, “$4.2M in 2019”, and a paragraph of Chinese.
messy layoutfootnote noisenumbers and currencymixed languages

What comes out

reads“four point two million dollars in twenty nineteen”
chineserouted to a Chinese voice (auto-detected)
outputMP3 chapters plus SRT
playbackcontinuous

Real documents are messy.

PDFs, EPUBs, and web pages carry headers, footnote markers, and broken layout that a model would read aloud verbatim.

Numbers and other scripts do not read themselves.

“$4.2M”, “2019”, and a line of Chinese each need to become the words a listener expects to hear.

Long books cannot be synthesized in one pass.

Text has to be chunked into segments and the audio stitched back into one continuous track, without seams the ear can catch.

One ingest layer resolves each format down to the same clean chapter text, so the rest of the pipeline never has to know where the document came from.

PDFParsed with PyMuPDF, with runaway whitespace collapsed.
EPUBChapters extracted with EbookLib and cleaned with BeautifulSoup.
DOCXParagraph text read straight from the document XML.
Web URLMain content isolated with trafilatura, behind URL-fetch safety checks.
Plain textPasted or uploaded text, taken as a single chapter.

Ingest, normalize, chunk, synthesize with Kokoro, then assemble. Each stage hands a single clean shape to the next.

01Ingest
inPDF, EPUB, DOCX, a web URL, or pasted text
transformPyMuPDF, EbookLib, and trafilatura with BeautifulSoup extract chapters from each format
outClean chapter text
02Normalize
inRaw chapter text
transformNumbers, currency, years, and abbreviations expand into words via num2words; language detection routes Chinese text through cn2an, jieba, and pypinyin
outSpeakable text
03Chunk
inSpeakable text
transformSplit on sentence boundaries, pack short sentences together, and break over-long spans on safe punctuation
outSynthesizable segments
04Synthesize
inSegments
transformThe open-source Kokoro model on PyTorch generates audio segment by segment, emitting timed cues
outAudio plus subtitle cues
05Assemble
inPer-chapter audio
transformStream to WAV, transcode to constant-bitrate MP3 with ffmpeg for accurate seeking, and write an SRT track
outContinuous chapter files
06Deliver
inAssembled chapters
transformStream or download through the web app
outA listenable audiobook

Synthesis is slow and heavy, so it is kept off the request path. A FastAPI server accepts jobs and a separate worker process does the work, with the guardrails below drawn from the production environment flags.

API and worker splitThe web server never runs TTS itself, so heavy synthesis cannot freeze it. A separate worker process pulls jobs and does the work.
Persistent job queueJobs live in a SQLite store, so queue state survives a restart and the worker resumes where it left off.
Stale-job recoveryA recovery loop requeues jobs stuck in processing past a threshold, up to a maximum number of attempts.
Queue and rate limitsPer-user active-job and global queue caps, plus a minimum interval between creates, keep any one caller from starving the rest.
Input capsUpload and text size limits bound each job; URL fetches enforce byte and redirect caps and reject non-public hosts.
Disk-pressure cleanupExpired copies and orphan uploads are swept, and disk-pressure cleanup keeps the output directory from filling the disk.

The front end is a progressive web app served over HTTPS through a Cloudflare Tunnel, so a finished audiobook behaves like a native one.

Offline retention

A service worker caches finished audio per user, so a downloaded book keeps playing with no connection, including range requests for seeking.

Lock-screen playback

Audio plays from the phone lock screen with native transport controls, which is why HTTPS is required rather than optional.

Install and preview

A web app manifest lets the site install to the home screen, and a voice-samples page lets a listener hear a voice before committing a whole book to it.

This is the text-to-speech side of the same voice craft as KOTA. KOTA takes messy speech and resolves it into a clean, verified result; Audiobook AI takes messy documents and resolves them into clean, continuous audio. Same shape, run in the other direction: messy input, a hardened pipeline, an output you can trust.

Audiobook AI is live. You can open it and run a document through the pipeline yourself.

agent protocol