mumblebee 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mumblebee-0.1.0/LICENSE +21 -0
- mumblebee-0.1.0/PKG-INFO +252 -0
- mumblebee-0.1.0/README.md +217 -0
- mumblebee-0.1.0/pyproject.toml +56 -0
- mumblebee-0.1.0/setup.cfg +4 -0
- mumblebee-0.1.0/src/mumblebee/__init__.py +3 -0
- mumblebee-0.1.0/src/mumblebee/__main__.py +8 -0
- mumblebee-0.1.0/src/mumblebee/cli.py +77 -0
- mumblebee-0.1.0/src/mumblebee/cover.py +197 -0
- mumblebee-0.1.0/src/mumblebee/epub.py +198 -0
- mumblebee-0.1.0/src/mumblebee/hw.py +50 -0
- mumblebee-0.1.0/src/mumblebee/kokoro_hip.py +127 -0
- mumblebee-0.1.0/src/mumblebee/prep.py +1361 -0
- mumblebee-0.1.0/src/mumblebee/pronunciation.py +397 -0
- mumblebee-0.1.0/src/mumblebee/tts_kokoro.py +933 -0
- mumblebee-0.1.0/src/mumblebee/tts_tinyvoca.py +533 -0
- mumblebee-0.1.0/src/mumblebee.egg-info/PKG-INFO +252 -0
- mumblebee-0.1.0/src/mumblebee.egg-info/SOURCES.txt +26 -0
- mumblebee-0.1.0/src/mumblebee.egg-info/dependency_links.txt +1 -0
- mumblebee-0.1.0/src/mumblebee.egg-info/entry_points.txt +2 -0
- mumblebee-0.1.0/src/mumblebee.egg-info/requires.txt +22 -0
- mumblebee-0.1.0/src/mumblebee.egg-info/top_level.txt +1 -0
- mumblebee-0.1.0/tests/test_chapters.py +80 -0
- mumblebee-0.1.0/tests/test_cli.py +84 -0
- mumblebee-0.1.0/tests/test_epub.py +74 -0
- mumblebee-0.1.0/tests/test_kokoro_hip.py +151 -0
- mumblebee-0.1.0/tests/test_prep.py +173 -0
- mumblebee-0.1.0/tests/test_pronunciation.py +120 -0
mumblebee-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sergey Plis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
mumblebee-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mumblebee
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Turn messy PDFs, EPUBs, and OCR'd books into clean, chaptered, cover-tagged local audiobooks using local LLMs and fast local TTS (roccoro HIP / ONNX).
|
|
5
|
+
Author: Sergey Plis
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/sergeyplis/mumblebee
|
|
8
|
+
Project-URL: Repository, https://github.com/sergeyplis/mumblebee
|
|
9
|
+
Project-URL: Issues, https://github.com/sergeyplis/mumblebee/issues
|
|
10
|
+
Keywords: audiobook,tts,kokoro,roccoro,epub,pdf,ocr,opus,local-llm
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: numpy>=1.24
|
|
18
|
+
Requires-Dist: onnxruntime>=1.18
|
|
19
|
+
Requires-Dist: pillow>=10.0
|
|
20
|
+
Requires-Dist: soundfile>=0.12
|
|
21
|
+
Requires-Dist: tqdm>=4.66
|
|
22
|
+
Requires-Dist: huggingface-hub>=0.24
|
|
23
|
+
Provides-Extra: kokoro
|
|
24
|
+
Requires-Dist: kokoro>=0.9.4; extra == "kokoro"
|
|
25
|
+
Requires-Dist: torch>=2.4; extra == "kokoro"
|
|
26
|
+
Provides-Extra: roccoro
|
|
27
|
+
Requires-Dist: roccoro>=0.1.1; extra == "roccoro"
|
|
28
|
+
Provides-Extra: kokoro-hip
|
|
29
|
+
Requires-Dist: roccoro>=0.1.1; extra == "kokoro-hip"
|
|
30
|
+
Provides-Extra: tiny-voca
|
|
31
|
+
Requires-Dist: torch>=2.4; extra == "tiny-voca"
|
|
32
|
+
Provides-Extra: all
|
|
33
|
+
Requires-Dist: mumblebee[kokoro,roccoro]; extra == "all"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
# mumblebee
|
|
37
|
+
|
|
38
|
+
**Turn messy PDFs, EPUBs, and OCR'd books into clean, chaptered, cover-tagged local audiobooks.**
|
|
39
|
+
|
|
40
|
+
`mumblebee` pairs a **local LLM copy-editor** with **parallel, lossless ONNX TTS** ([Kokoro-82M](https://huggingface.co/hexgrad/Kokoro-82M) and [tiny-voca](https://github.com/n4ze3m/tiny-voca)) to turn real-world books—not just pristine novels, but academic non-fiction and noisy 1980s OCR scans—into pleasant, uninterrupted `.opus` audiobooks with chapter files, metadata tags, and `cover.png` art ready for players like [Voice (F-Droid)](https://f-droid.org/packages/de.ph1b.audiobook/) and Audiobookshelf.
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Motivation
|
|
45
|
+
|
|
46
|
+
If you have ever piped a real PDF or non-fiction EPUB straight into a text-to-speech engine, you know how quickly the listening experience falls apart:
|
|
47
|
+
|
|
48
|
+
- The narrator reads running headers and page numbers right in the middle of a sentence (*"…the cranial flexure ASHLEY MONTAGU 12 Growing Young is retained…"*).
|
|
49
|
+
- Footnote numbers glued to words (`century14`), bottom-of-page footnotes, parenthetical citations, floating tables, and multi-page figure captions/diagram labels interrupt the prose.
|
|
50
|
+
- OCR'd books scatter mid-word splits (`Sometim es`, `ad u lth oo d`, `A n n a H e m p s t e a d`) that sound like gibberish when spoken aloud.
|
|
51
|
+
- Chapter titles slam directly into the first sentence with zero pause—or get spoken twice—and 80 pages of endnotes, bibliographies, and indexes trail onto the end of the book.
|
|
52
|
+
|
|
53
|
+
Meanwhile, cloud audiobook/TTS services are expensive, upload your personal library to third-party servers, and still struggle with messy PDF layouts.
|
|
54
|
+
|
|
55
|
+
`mumblebee` exists to make **local, private, high-craft audiobook conversion** practical on your own machine: clean the text semantically *without ever rewriting the author*, verify the result with automated quality checks, and synthesize fast, cleanly paced chapters in lossless FP32.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## Goals & Non-Goals
|
|
60
|
+
|
|
61
|
+
### Goals
|
|
62
|
+
- **100% verbatim prose preservation**: Strip print clutter and repair OCR scars without summarizing, paraphrasing, abridging, or inventing words.
|
|
63
|
+
- **Auditable, decoupled pipeline**: Produce a clean, human-readable `*_tts_ready.txt` and `*_prep_report.txt` before audio synthesis. Audit the text once; re-render cheaply with any voice, pacing, or TTS engine.
|
|
64
|
+
- **Thoughtful audiobook mastering out of the box**:
|
|
65
|
+
- Deterministic silence beats after chapter titles (`1.2s`), standalone subheadings/epigraphs (`0.7s`), and paragraphs (`0.45s`), plus a `0.25s` lead-in.
|
|
66
|
+
- Automatic chapter title deduplication so headings aren't spoken twice or glued onto the opening sentence.
|
|
67
|
+
- Native `1.0×` speech speed by default so mobile players can speed up playback in a single pass without double time-stretching artifacts.
|
|
68
|
+
- Compact Vorbis-tagged `.opus` chapters (`~13–14 MB/hour`) plus automatic `cover.png` lookup.
|
|
69
|
+
- **Fast & 100% local**:
|
|
70
|
+
- Two-tier local LLM cleaning (4-slot `Qwen3-4B` with lossless prompt-lookup `ngram-simple` speculative decoding + PDF prose pre-unwrapping + Tier-2 validator escalation) cleans a 66k-word EPUB in **~3.3 min** (**400–435 tok/s**) and an 86k-word 1980s OCR PDF in **~5.5 min** (**~300 tok/s**, **2.7×–4.0× faster** than baseline).
|
|
71
|
+
- **Native HIP Kokoro-82M via [roccoro](https://github.com/sergeyplis/roccoro) (`--backend roccoro` / `--backend hip-native`)** on AMD ROCm / Strix Halo (`gfx1151`) synthesizes at **~245× real-time** (**~219–223× RT end-to-end**, **~2m 14s–2m 27s** for an 8–9h audiobook including G2P + `cl=10` Opus encoding) using only **~831 MB GTT** and **zero `torch`/`onnxruntime`/`transformers`** at inference time — coexisting safely alongside a resident 85 GB `llama-server`.
|
|
72
|
+
- Parallel CPU ONNX Runtime TTS runs at **31–35× real-time** (Kokoro-82M) or **~1,150× real-time** (`tiny-voca`) using **0 MB GPU memory** (or **~40.5× RT** in Hybrid CPU+GPU mode).
|
|
73
|
+
- **No vendor lock-in**: Works with any local OpenAI-compatible LLM endpoint (`llama-server`, Ollama, vLLM, LM Studio).
|
|
74
|
+
|
|
75
|
+
### Non-Goals
|
|
76
|
+
- **Not a DRM tool**: Bring your own DRM-free `.epub`, `.pdf`, `.txt`, or `.md` files.
|
|
77
|
+
- **Not a dramatic voice-cloning / multi-cast studio**: Focused on clean, dependable single-narrator long-form narration for non-fiction and fiction.
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Pipeline Overview
|
|
82
|
+
|
|
83
|
+
```mermaid
|
|
84
|
+
flowchart LR
|
|
85
|
+
SRC["Book (.epub / .pdf / .txt / .md)"] --> PREP["mumblebee prep\nStage 1: Structure & TOC\nStage 2: Fast LLM + Validator + Tier-2 Fallback"]
|
|
86
|
+
PREP --> TXT["Book_tts_ready.txt\n(<<<CHAPTER>>> markers)"]
|
|
87
|
+
PREP --> RPT["Book_prep_report.txt\n(Audit & escalation log)"]
|
|
88
|
+
TXT --> KOKORO["mumblebee kokoro\n(Parallel ONNX / Hybrid TTS\n+ deterministic pauses)"]
|
|
89
|
+
TXT --> TV["mumblebee tinyvoca\n(Ultra-fast 4.5M ONNX TTS)"]
|
|
90
|
+
SRC -.-> COVER["mumblebee cover\n(EPUB -> Open Library ->\nGoogle Books -> PDF p.1)"]
|
|
91
|
+
KOKORO --> OUT["Book_Title/\n01_Preface.opus\n02_Chapter_1.opus\n...\ncover.png"]
|
|
92
|
+
COVER --> OUT
|
|
93
|
+
TV --> OUT2["Book_Title.opus"]
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Commands & Performance
|
|
99
|
+
|
|
100
|
+
Measured on an AMD Ryzen AI MAX+ 395 (16 Zen 5 cores / Radeon 8060S APU, 256 GB/s unified memory):
|
|
101
|
+
|
|
102
|
+
| Command | Purpose | Typical Speed |
|
|
103
|
+
|---|---|---|
|
|
104
|
+
| **`mumblebee <book>`** | End-to-end shorthand: cleans `.epub`/`.pdf`/`.txt`/`.md` into `*_tts_ready.txt` + audit report, then renders chaptered `.opus` + `cover.png` via Kokoro (`--tts tiny-voca` to switch engines). | **~3–6 min prep + ~2.5 min TTS** (`hip-native`) for an 86k-word book |
|
|
105
|
+
| **`mumblebee prep <book>`** | Cleans and audits book text only (`*_tts_ready.txt` with `<<<BOOK_TITLE>>>`, `<<<BOOK_AUTHOR>>>`, and `<<<CHAPTER: ...>>>` markers + `*_prep_report.txt`). | **~300–435 agg tok/s** (4-slot `Qwen3-4B` + lossless `ngram-simple` prompt-lookup + `q8_0` KV) |
|
|
106
|
+
| **`mumblebee kokoro`** | Parallel Kokoro-82M TTS producing one Vorbis-tagged `.opus` file per chapter + `cover.png`, with deterministic chapter/heading/paragraph pauses. | **~245× RT** synth / **~219× RT** end-to-end (`hip-native`, 831 MB GTT) · **~31–35× RT** (ONNX CPU FP32, 0 MB GPU) · **~40.5× RT** (Hybrid) |
|
|
107
|
+
| **`mumblebee tinyvoca`** | Ultra-fast 4.5M-parameter parallel ONNX TTS (`M2`/`F2` voices) with pacing controls, live progress bars, and tagged `.opus` output. | **~1,150–1,200× RT** synthesis (~850× RT end-to-end WAV) |
|
|
108
|
+
| **`mumblebee cover`** | Zero-API-key cover fetcher saving `cover.png`: embedded EPUB cover → Open Library → Google Books → PDF page 1 fallback. Also called automatically by `mumblebee kokoro`. | ~1–2 s |
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## Installation
|
|
113
|
+
|
|
114
|
+
Requires Python 3.10+, `ffmpeg` (with `libopus`), and `poppler-utils` (`pdftotext`, `pdftoppm`) for `.pdf` inputs.
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
# Lightweight torch-free install for native AMD ROCm/HIP Kokoro-82M via roccoro (~245x RT):
|
|
118
|
+
pip install "mumblebee[roccoro]"
|
|
119
|
+
roccoro build
|
|
120
|
+
roccoro export-weights
|
|
121
|
+
|
|
122
|
+
# Or with Kokoro-82M CPU ONNX / PyTorch:
|
|
123
|
+
pip install "mumblebee[kokoro]"
|
|
124
|
+
|
|
125
|
+
# Or with tiny-voca TTS (installed from GitHub since tiny-voca/dizione are not on PyPI):
|
|
126
|
+
pip install "mumblebee[tiny-voca]"
|
|
127
|
+
pip install git+https://github.com/n4ze3m/tiny-voca git+https://github.com/n4ze3m/dizione
|
|
128
|
+
|
|
129
|
+
# Or from a local clone:
|
|
130
|
+
git clone https://github.com/sergeyplis/mumblebee.git
|
|
131
|
+
cd mumblebee && pip install -e ".[roccoro]"
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Usage
|
|
137
|
+
|
|
138
|
+
### 1. End-to-End (Book → Clean Text → Per-Chapter Opus + Cover)
|
|
139
|
+
|
|
140
|
+
Point `mumblebee` at any OpenAI-compatible LLM server (`llama-server`, Ollama, vLLM, LM Studio, etc.). See [examples/strix-halo-llama-server.sh](examples/strix-halo-llama-server.sh) for a reference 4-slot `Qwen3-4B-Instruct` launcher.
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
# Default shorthand runs full prep + Kokoro TTS:
|
|
144
|
+
mumblebee MyBook.epub --output-dir ./books --resume
|
|
145
|
+
|
|
146
|
+
# Or specify custom LLM endpoints explicitly:
|
|
147
|
+
mumblebee MyBook.epub \
|
|
148
|
+
--api-url http://127.0.0.1:8081/v1/chat/completions --model qwen3-4b-instruct \
|
|
149
|
+
--fallback-api-url http://127.0.0.1:8080/v1/chat/completions --fallback-model qwen3.8-flash-next \
|
|
150
|
+
--output-dir ./books --resume
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Environment variables (`MUMBLEBEE_API_URL`, `MUMBLEBEE_MODEL`, `MUMBLEBEE_FALLBACK_API_URL`, `MUMBLEBEE_FALLBACK_MODEL`, `MUMBLEBEE_CACHE_DIR`, `MUMBLEBEE_FAST_SERVER_CMD`, `OPENAI_API_KEY`) can also be set so you don't have to pass URLs on every run.
|
|
154
|
+
|
|
155
|
+
### 2. Step-by-Step Workflow (Recommended for OCR PDFs)
|
|
156
|
+
|
|
157
|
+
#### Step A — Clean & Audit Text (`mumblebee prep`)
|
|
158
|
+
```bash
|
|
159
|
+
# Clean an EPUB or PDF (keeps Preface/Intro, drops endnotes/bibliography/index):
|
|
160
|
+
mumblebee prep MyBook.pdf --resume
|
|
161
|
+
|
|
162
|
+
# Keep narrative Appendices too:
|
|
163
|
+
mumblebee prep MyBook.pdf --keep-appendices --resume
|
|
164
|
+
|
|
165
|
+
# Fast sanity test on the first 3 sections only (skip or bound pronunciation audit):
|
|
166
|
+
mumblebee prep MyBook.pdf --max-chapters 3 --pronunciation-timeout 60 --resume
|
|
167
|
+
|
|
168
|
+
# Skip Stage 3 Kokoro pronunciation audit completely:
|
|
169
|
+
mumblebee prep MyBook.pdf --no-pronunciation-audit --resume
|
|
170
|
+
```
|
|
171
|
+
Review `MyBook_prep_report.txt` (shows any escalated chunks and pronunciation fixes), `MyBook_pronunciations.json` (auto-loaded by `mumblebee kokoro`), and `MyBook_tts_ready.txt` before rendering audio.
|
|
172
|
+
|
|
173
|
+
#### Step B1 — Render Per-Chapter Opus with Kokoro (`mumblebee kokoro`)
|
|
174
|
+
```bash
|
|
175
|
+
# Auto-detects <<<CHAPTER: ...>>>, title/author tags, cover.png, and safe backend:
|
|
176
|
+
mumblebee kokoro --input MyBook_tts_ready.txt --output-dir ./books
|
|
177
|
+
|
|
178
|
+
# Customize voice, pauses, bitrate, or playback speed:
|
|
179
|
+
mumblebee kokoro --input MyBook_tts_ready.txt \
|
|
180
|
+
--voice af_heart \
|
|
181
|
+
--chapter-pause 1.2 --heading-pause 0.7 --paragraph-pause 0.45 \
|
|
182
|
+
--bitrate 32k --output-dir ./books
|
|
183
|
+
|
|
184
|
+
# Native AMD ROCm/HIP Kokoro-82M via roccoro (~245x RT synth, ~222x RT end-to-end, 831 MB GTT, auto-selected when installed):
|
|
185
|
+
mumblebee kokoro --input MyBook_tts_ready.txt --backend roccoro --output-dir ./books
|
|
186
|
+
|
|
187
|
+
# Force 0-GPU ONNX CPU mode (~31-35x RT, leaves GPU free for LLMs):
|
|
188
|
+
mumblebee kokoro --input MyBook_tts_ready.txt --backend onnx --output-dir ./books
|
|
189
|
+
|
|
190
|
+
# Force Hybrid CPU + GPU mode (~40.5x RT when GPU is free):
|
|
191
|
+
mumblebee kokoro --input MyBook_tts_ready.txt --backend hybrid --output-dir ./books
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
#### Step B2 — Render with `tiny-voca` (`mumblebee tinyvoca`)
|
|
195
|
+
```bash
|
|
196
|
+
mumblebee tinyvoca \
|
|
197
|
+
--text-file MyBook_tts_ready.txt \
|
|
198
|
+
--voice M2 --speed 0.88 --sentence-pause 0.15 --paragraph-pause 0.45 \
|
|
199
|
+
--bitrate 32k --artist "Author Name" --album "Book Title" --title "Book Title" \
|
|
200
|
+
-o ./books/MyBook_M2.opus
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### Step C — Standalone Cover Fetch (`mumblebee cover`)
|
|
204
|
+
```bash
|
|
205
|
+
mumblebee cover --title "Growing Young" --author "Ashley Montagu" --pdf GrowingYoung.pdf -o ./books/Growing_Young/cover.png
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
## Design Reasoning (The "Why" Behind the Architecture)
|
|
211
|
+
|
|
212
|
+
These empirical lessons shaped every major design decision—recorded here because code alone rarely explains *why* alternatives were rejected:
|
|
213
|
+
|
|
214
|
+
### 1. Why LLM preprocessing instead of regex/grep
|
|
215
|
+
Real books—especially academic non-fiction and OCR'd PDFs—entangle layout noise semantically with prose. A regex cannot reliably tell whether `century14` is a footnote number or a measurement, cannot repair arbitrary OCR word splits (`ad u lth oo d`, `Sometim es`), cannot stitch a sentence interrupted mid-clause by a 20-line floating table or 4 pages of skull diagrams, and cannot distinguish a keep-worthy Preface from 80 pages of endnotes. Decoupling **`mumblebee prep` → `*_tts_ready.txt` → TTS** lets you verify the text once and re-synthesize cheaply with any voice or engine.
|
|
216
|
+
|
|
217
|
+
### 2. Why two-tier LLM cleaning (fast 4B + validator escalation) beats one giant model
|
|
218
|
+
- On 256 GB/s unified memory, an ~85 GB frontier model is memory-bandwidth-bound (~24–32 tok/s, ~70 min/book). Verbatim copy-editing does not require frontier reasoning on 90% of chunks.
|
|
219
|
+
- A 4B instruction model (`Qwen3-4B-Instruct-2507` Q4_K_M, 2.4 GB) running **4 continuous-batching slots** with **`q8_0` KV cache (`~5.1 GB` GTT)** and **lossless prompt-lookup speculative decoding (`--spec-type ngram-simple --spec-ngram-simple-size-n 4 --spec-ngram-simple-size-m 48`)** reaches **402–435 agg tok/s** on clean EPUBs (97% of output tokens accepted from prompt lookup, **8/8 bit-for-bit identical** to unaccelerated decoding, **~3.3 min/book**) and **~298–303 agg tok/s** on dirty OCR PDFs (**~5.5 min/book**).
|
|
220
|
+
- **Why PDF pre-unwrapping unlocked `ngram-simple`**: Raw `pdftotext` output breaks every ~10 words with hard `\n` wraps and recurring 2-letter OCR splits (`o f`, `i n`, `t o`), which originally shattered 12-token n-gram matches (9–12% acceptance). Conservatively unwrapping mid-sentence column wraps (while preserving paragraph breaks, captions, tables, and ALL-CAPS blocks) and fixing strictly unambiguous 2-letter OCR splits before sending to the primary model boosts draft acceptance on OCR PDFs from **12% -> 63–81%** (**90.5% of output tokens drafted**), delivering a **2.7×–4.0× lossless speedup** (and Tier-2 escalations still receive the raw unaltered chunk).
|
|
221
|
+
- **Why automated chunk validation + Tier-2 escalation**: On the ~10% of chunks dominated by multi-page diagrams (`Fig. 1–5`, `PROBOSCIS MONKEY`, running headers) or subtle BPE-masked OCR splits (`Sometim es`, `L a Barre`, `A s`), a 4B model under a strict verbatim prompt can occasionally copy the artifact verbatim. `validate_cleaned_chunk()` checks every chunk for leaked headers/page numbers, `Fig.`/`TABLE` captions, ALL-CAPS diagram clusters, merged >650-word walls, broken lowercase paragraph starts, letter-spaced text (`A n n a ...`), and single-space mid-word OCR splits—then automatically escalates *only* failing chunks with concrete error feedback to the stronger fallback model.
|
|
222
|
+
|
|
223
|
+
### 3. Critical PDF & Chunking Traps We Hit and Fixed
|
|
224
|
+
- **Never use `str.splitlines()` on PDFs**: Python's `splitlines()` silently splits on and strips `\f` form-feed page breaks, blinding chapter detection. Always use `split("\n")`.
|
|
225
|
+
- **Never split chunks on `\f` page breaks**: Printed page breaks frequently cut mid-sentence or across floating tables. Splitting at `\f` causes small LLMs to hallucinate an ending for the dangling clause while the next chunk opens mid-air (`maximus (the large muscle...)`). Keep `<PAGEBREAK>` *inside* chunks and split only after blocks ending in terminal sentence punctuation whose next real prose line (skipping headers and page numbers) starts cleanly.
|
|
226
|
+
- **PDF paragraph restoration**: Non-layout `pdftotext` (required to de-column spreads) omits blank lines between paragraphs, causing LLMs to merge whole pages into 1,500-word walls. Re-inserting blank lines after short sentence-ending lines restores paragraphs before chunking.
|
|
227
|
+
- **Prefer `.epub` over `.pdf` whenever available**: Pure-stdlib EPUB extraction provides exact spine reading order, Dublin Core metadata, clean structural pre-stripping of `<figcaption>`, `<aside epub:type="footnote">`, and `<sup>` footnote markers (while preserving ordinals like `2nd`), plus the embedded publisher cover.
|
|
228
|
+
|
|
229
|
+
### 4. Deterministic Pacing & Clean Speed belong in the TTS Layer (Not the LLM)
|
|
230
|
+
- **Never rely on an LLM for audio timing**: Even if a heading loses a blank line or repeats the `<<<CHAPTER: ...>>>` title, `mumblebee kokoro` deterministically strips duplicate leading chapter titles (with or without `Chapter N:` prefixes), appends terminal punctuation so Kokoro uses a clean falling intonation, and streams exact silence beats (`--lead-in`, `--chapter-pause`, `--heading-pause`, `--paragraph-pause`) straight into `ffmpeg`.
|
|
231
|
+
- **Why `--speed` defaults to `1.0`**: Baking `ffmpeg atempo=1.3` into a lossy `.opus` file and then listening at `1.2×` in an audiobook player applies WSOLA time-stretching *twice* across lossy compression. Generating at clean `1.0×` lets your player handle speed adjustment in a single pass with noticeably cleaner audio.
|
|
232
|
+
|
|
233
|
+
### 5. Why Native HIP Kernels ([roccoro](https://github.com/sergeyplis/roccoro)) Reach 245× RT — and Why ONNX CPU Beats PyTorch ROCm
|
|
234
|
+
- **`Kokoro-82M` native HIP engine ([`sergeyplis/roccoro`](https://github.com/sergeyplis/roccoro), `--backend roccoro` / `--backend hip-native`)**: PyTorch ROCm (`cudnn=True`) triggers a MIOpen workspace fallback (`705 MB required, provided 0`) on Kokoro's 1D transposed convolutions (< 1× RT), and even with `cudnn=False` + 6 FP16 streams PyTorch tops out at ~59× RT while consuming **9.47 GB GTT**. Existing GGML ports (`kokoro.cpp` Vulkan 49.6× RT, `audio.cpp` HIP 16.4× RT) bottleneck on unfused `AdaIN`/`Snake1d`, `im2col`, and unbatched BiLSTM kernel launches. Our standalone C++/HIP engine (`roccoro`) writes a roofline-tuned implicit-GEMM `gfx1151` wave32 WMMA Conv1d/GEMM kernel (`27.6 TFLOP/s`, fusing residual add + epilogue AdaIN stats), a single-launch persistent FP32 BiLSTM kernel (12–16× faster with exact FP32 durations), and on-GPU `SineGen` + 20-pt STFT/iSTFT. Combined with longest-first chapter scheduling across up to 8 concurrent `ffmpeg` Opus encoders, it reaches **245.3× RT synthesis / 222× RT end-to-end** (full 8–9h audiobook in **~2m 15s–2m 27s**) in **831 MB GTT** (`env_mel_lsd = 0.0395 dB`, `env_mel_cos = 0.9999986` vs PyTorch) with **zero `torch`/`onnxruntime`/`transformers`** at inference time.
|
|
235
|
+
- **`Kokoro-82M` CPU fallback**: When no ROCm GPU / native binary is present, **16-core ONNX Runtime CPU FP32** hits **~31–35× RT** with **0 MB GPU**, and **Hybrid (CPU ONNX + 4 GPU streams)** reaches **~40.5× RT**.
|
|
236
|
+
- **`tiny-voca` (4.5M params)**: Kernel-launch bound on GPU (~98–343× RT on Radeon 8060S). Running 1 single-threaded ONNX session per physical core (`16w × 1T`, pre-optimized `--optimized` + `ORT_ENABLE_ALL`, LPT longest-first scheduling, `dizione` frontend) hits **~1,150–1,200× RT** in lossless FP32 with 0 MB GPU.
|
|
237
|
+
- **Why INT8 ONNX was rejected for both models**: On `tiny-voca`, INT8 MatMul gave only +15% speed while altering durations on 115/119 sentences; on `Kokoro-82M`, INT8 was *slower* (21.4× vs 31.1× RT) and shifted 27/28 chunk lengths. Lossless FP32 (or FP32-accumulated WMMA with FP32 BiLSTM duration prediction) won on both quality and reliability.
|
|
238
|
+
|
|
239
|
+
### 6. Why Opus (`32k` VBR, `-application voip`, 24 kHz mono)
|
|
240
|
+
Best speech intelligibility per megabyte (~13–14 MB/hour — ~3–4× smaller than MP3, ~12× smaller than WAV), native Vorbis tags (`title`, `artist`, `album`, `track`, `genre`, `comment`), and streaming PCM-to-`ffmpeg` piping that avoids multi-GB temporary WAV files.
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
|
|
244
|
+
## Running Tests
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## License
|
|
251
|
+
|
|
252
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# mumblebee
|
|
2
|
+
|
|
3
|
+
**Turn messy PDFs, EPUBs, and OCR'd books into clean, chaptered, cover-tagged local audiobooks.**
|
|
4
|
+
|
|
5
|
+
`mumblebee` pairs a **local LLM copy-editor** with **parallel, lossless ONNX TTS** ([Kokoro-82M](https://huggingface.co/hexgrad/Kokoro-82M) and [tiny-voca](https://github.com/n4ze3m/tiny-voca)) to turn real-world books—not just pristine novels, but academic non-fiction and noisy 1980s OCR scans—into pleasant, uninterrupted `.opus` audiobooks with chapter files, metadata tags, and `cover.png` art ready for players like [Voice (F-Droid)](https://f-droid.org/packages/de.ph1b.audiobook/) and Audiobookshelf.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Motivation
|
|
10
|
+
|
|
11
|
+
If you have ever piped a real PDF or non-fiction EPUB straight into a text-to-speech engine, you know how quickly the listening experience falls apart:
|
|
12
|
+
|
|
13
|
+
- The narrator reads running headers and page numbers right in the middle of a sentence (*"…the cranial flexure ASHLEY MONTAGU 12 Growing Young is retained…"*).
|
|
14
|
+
- Footnote numbers glued to words (`century14`), bottom-of-page footnotes, parenthetical citations, floating tables, and multi-page figure captions/diagram labels interrupt the prose.
|
|
15
|
+
- OCR'd books scatter mid-word splits (`Sometim es`, `ad u lth oo d`, `A n n a H e m p s t e a d`) that sound like gibberish when spoken aloud.
|
|
16
|
+
- Chapter titles slam directly into the first sentence with zero pause—or get spoken twice—and 80 pages of endnotes, bibliographies, and indexes trail onto the end of the book.
|
|
17
|
+
|
|
18
|
+
Meanwhile, cloud audiobook/TTS services are expensive, upload your personal library to third-party servers, and still struggle with messy PDF layouts.
|
|
19
|
+
|
|
20
|
+
`mumblebee` exists to make **local, private, high-craft audiobook conversion** practical on your own machine: clean the text semantically *without ever rewriting the author*, verify the result with automated quality checks, and synthesize fast, cleanly paced chapters in lossless FP32.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Goals & Non-Goals
|
|
25
|
+
|
|
26
|
+
### Goals
|
|
27
|
+
- **100% verbatim prose preservation**: Strip print clutter and repair OCR scars without summarizing, paraphrasing, abridging, or inventing words.
|
|
28
|
+
- **Auditable, decoupled pipeline**: Produce a clean, human-readable `*_tts_ready.txt` and `*_prep_report.txt` before audio synthesis. Audit the text once; re-render cheaply with any voice, pacing, or TTS engine.
|
|
29
|
+
- **Thoughtful audiobook mastering out of the box**:
|
|
30
|
+
- Deterministic silence beats after chapter titles (`1.2s`), standalone subheadings/epigraphs (`0.7s`), and paragraphs (`0.45s`), plus a `0.25s` lead-in.
|
|
31
|
+
- Automatic chapter title deduplication so headings aren't spoken twice or glued onto the opening sentence.
|
|
32
|
+
- Native `1.0×` speech speed by default so mobile players can speed up playback in a single pass without double time-stretching artifacts.
|
|
33
|
+
- Compact Vorbis-tagged `.opus` chapters (`~13–14 MB/hour`) plus automatic `cover.png` lookup.
|
|
34
|
+
- **Fast & 100% local**:
|
|
35
|
+
- Two-tier local LLM cleaning (4-slot `Qwen3-4B` with lossless prompt-lookup `ngram-simple` speculative decoding + PDF prose pre-unwrapping + Tier-2 validator escalation) cleans a 66k-word EPUB in **~3.3 min** (**400–435 tok/s**) and an 86k-word 1980s OCR PDF in **~5.5 min** (**~300 tok/s**, **2.7×–4.0× faster** than baseline).
|
|
36
|
+
- **Native HIP Kokoro-82M via [roccoro](https://github.com/sergeyplis/roccoro) (`--backend roccoro` / `--backend hip-native`)** on AMD ROCm / Strix Halo (`gfx1151`) synthesizes at **~245× real-time** (**~219–223× RT end-to-end**, **~2m 14s–2m 27s** for an 8–9h audiobook including G2P + `cl=10` Opus encoding) using only **~831 MB GTT** and **zero `torch`/`onnxruntime`/`transformers`** at inference time — coexisting safely alongside a resident 85 GB `llama-server`.
|
|
37
|
+
- Parallel CPU ONNX Runtime TTS runs at **31–35× real-time** (Kokoro-82M) or **~1,150× real-time** (`tiny-voca`) using **0 MB GPU memory** (or **~40.5× RT** in Hybrid CPU+GPU mode).
|
|
38
|
+
- **No vendor lock-in**: Works with any local OpenAI-compatible LLM endpoint (`llama-server`, Ollama, vLLM, LM Studio).
|
|
39
|
+
|
|
40
|
+
### Non-Goals
|
|
41
|
+
- **Not a DRM tool**: Bring your own DRM-free `.epub`, `.pdf`, `.txt`, or `.md` files.
|
|
42
|
+
- **Not a dramatic voice-cloning / multi-cast studio**: Focused on clean, dependable single-narrator long-form narration for non-fiction and fiction.
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Pipeline Overview
|
|
47
|
+
|
|
48
|
+
```mermaid
|
|
49
|
+
flowchart LR
|
|
50
|
+
SRC["Book (.epub / .pdf / .txt / .md)"] --> PREP["mumblebee prep\nStage 1: Structure & TOC\nStage 2: Fast LLM + Validator + Tier-2 Fallback"]
|
|
51
|
+
PREP --> TXT["Book_tts_ready.txt\n(<<<CHAPTER>>> markers)"]
|
|
52
|
+
PREP --> RPT["Book_prep_report.txt\n(Audit & escalation log)"]
|
|
53
|
+
TXT --> KOKORO["mumblebee kokoro\n(Parallel ONNX / Hybrid TTS\n+ deterministic pauses)"]
|
|
54
|
+
TXT --> TV["mumblebee tinyvoca\n(Ultra-fast 4.5M ONNX TTS)"]
|
|
55
|
+
SRC -.-> COVER["mumblebee cover\n(EPUB -> Open Library ->\nGoogle Books -> PDF p.1)"]
|
|
56
|
+
KOKORO --> OUT["Book_Title/\n01_Preface.opus\n02_Chapter_1.opus\n...\ncover.png"]
|
|
57
|
+
COVER --> OUT
|
|
58
|
+
TV --> OUT2["Book_Title.opus"]
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Commands & Performance
|
|
64
|
+
|
|
65
|
+
Measured on an AMD Ryzen AI MAX+ 395 (16 Zen 5 cores / Radeon 8060S APU, 256 GB/s unified memory):
|
|
66
|
+
|
|
67
|
+
| Command | Purpose | Typical Speed |
|
|
68
|
+
|---|---|---|
|
|
69
|
+
| **`mumblebee <book>`** | End-to-end shorthand: cleans `.epub`/`.pdf`/`.txt`/`.md` into `*_tts_ready.txt` + audit report, then renders chaptered `.opus` + `cover.png` via Kokoro (`--tts tiny-voca` to switch engines). | **~3–6 min prep + ~2.5 min TTS** (`hip-native`) for an 86k-word book |
|
|
70
|
+
| **`mumblebee prep <book>`** | Cleans and audits book text only (`*_tts_ready.txt` with `<<<BOOK_TITLE>>>`, `<<<BOOK_AUTHOR>>>`, and `<<<CHAPTER: ...>>>` markers + `*_prep_report.txt`). | **~300–435 agg tok/s** (4-slot `Qwen3-4B` + lossless `ngram-simple` prompt-lookup + `q8_0` KV) |
|
|
71
|
+
| **`mumblebee kokoro`** | Parallel Kokoro-82M TTS producing one Vorbis-tagged `.opus` file per chapter + `cover.png`, with deterministic chapter/heading/paragraph pauses. | **~245× RT** synth / **~219× RT** end-to-end (`hip-native`, 831 MB GTT) · **~31–35× RT** (ONNX CPU FP32, 0 MB GPU) · **~40.5× RT** (Hybrid) |
|
|
72
|
+
| **`mumblebee tinyvoca`** | Ultra-fast 4.5M-parameter parallel ONNX TTS (`M2`/`F2` voices) with pacing controls, live progress bars, and tagged `.opus` output. | **~1,150–1,200× RT** synthesis (~850× RT end-to-end WAV) |
|
|
73
|
+
| **`mumblebee cover`** | Zero-API-key cover fetcher saving `cover.png`: embedded EPUB cover → Open Library → Google Books → PDF page 1 fallback. Also called automatically by `mumblebee kokoro`. | ~1–2 s |
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Installation
|
|
78
|
+
|
|
79
|
+
Requires Python 3.10+, `ffmpeg` (with `libopus`), and `poppler-utils` (`pdftotext`, `pdftoppm`) for `.pdf` inputs.
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
# Lightweight torch-free install for native AMD ROCm/HIP Kokoro-82M via roccoro (~245x RT):
|
|
83
|
+
pip install "mumblebee[roccoro]"
|
|
84
|
+
roccoro build
|
|
85
|
+
roccoro export-weights
|
|
86
|
+
|
|
87
|
+
# Or with Kokoro-82M CPU ONNX / PyTorch:
|
|
88
|
+
pip install "mumblebee[kokoro]"
|
|
89
|
+
|
|
90
|
+
# Or with tiny-voca TTS (installed from GitHub since tiny-voca/dizione are not on PyPI):
|
|
91
|
+
pip install "mumblebee[tiny-voca]"
|
|
92
|
+
pip install git+https://github.com/n4ze3m/tiny-voca git+https://github.com/n4ze3m/dizione
|
|
93
|
+
|
|
94
|
+
# Or from a local clone:
|
|
95
|
+
git clone https://github.com/sergeyplis/mumblebee.git
|
|
96
|
+
cd mumblebee && pip install -e ".[roccoro]"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Usage
|
|
102
|
+
|
|
103
|
+
### 1. End-to-End (Book → Clean Text → Per-Chapter Opus + Cover)
|
|
104
|
+
|
|
105
|
+
Point `mumblebee` at any OpenAI-compatible LLM server (`llama-server`, Ollama, vLLM, LM Studio, etc.). See [examples/strix-halo-llama-server.sh](examples/strix-halo-llama-server.sh) for a reference 4-slot `Qwen3-4B-Instruct` launcher.
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
# Default shorthand runs full prep + Kokoro TTS:
|
|
109
|
+
mumblebee MyBook.epub --output-dir ./books --resume
|
|
110
|
+
|
|
111
|
+
# Or specify custom LLM endpoints explicitly:
|
|
112
|
+
mumblebee MyBook.epub \
|
|
113
|
+
--api-url http://127.0.0.1:8081/v1/chat/completions --model qwen3-4b-instruct \
|
|
114
|
+
--fallback-api-url http://127.0.0.1:8080/v1/chat/completions --fallback-model qwen3.8-flash-next \
|
|
115
|
+
--output-dir ./books --resume
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Environment variables (`MUMBLEBEE_API_URL`, `MUMBLEBEE_MODEL`, `MUMBLEBEE_FALLBACK_API_URL`, `MUMBLEBEE_FALLBACK_MODEL`, `MUMBLEBEE_CACHE_DIR`, `MUMBLEBEE_FAST_SERVER_CMD`, `OPENAI_API_KEY`) can also be set so you don't have to pass URLs on every run.
|
|
119
|
+
|
|
120
|
+
### 2. Step-by-Step Workflow (Recommended for OCR PDFs)
|
|
121
|
+
|
|
122
|
+
#### Step A — Clean & Audit Text (`mumblebee prep`)
|
|
123
|
+
```bash
|
|
124
|
+
# Clean an EPUB or PDF (keeps Preface/Intro, drops endnotes/bibliography/index):
|
|
125
|
+
mumblebee prep MyBook.pdf --resume
|
|
126
|
+
|
|
127
|
+
# Keep narrative Appendices too:
|
|
128
|
+
mumblebee prep MyBook.pdf --keep-appendices --resume
|
|
129
|
+
|
|
130
|
+
# Fast sanity test on the first 3 sections only (skip or bound pronunciation audit):
|
|
131
|
+
mumblebee prep MyBook.pdf --max-chapters 3 --pronunciation-timeout 60 --resume
|
|
132
|
+
|
|
133
|
+
# Skip Stage 3 Kokoro pronunciation audit completely:
|
|
134
|
+
mumblebee prep MyBook.pdf --no-pronunciation-audit --resume
|
|
135
|
+
```
|
|
136
|
+
Review `MyBook_prep_report.txt` (shows any escalated chunks and pronunciation fixes), `MyBook_pronunciations.json` (auto-loaded by `mumblebee kokoro`), and `MyBook_tts_ready.txt` before rendering audio.
|
|
137
|
+
|
|
138
|
+
#### Step B1 — Render Per-Chapter Opus with Kokoro (`mumblebee kokoro`)
|
|
139
|
+
```bash
|
|
140
|
+
# Auto-detects <<<CHAPTER: ...>>>, title/author tags, cover.png, and safe backend:
|
|
141
|
+
mumblebee kokoro --input MyBook_tts_ready.txt --output-dir ./books
|
|
142
|
+
|
|
143
|
+
# Customize voice, pauses, bitrate, or playback speed:
|
|
144
|
+
mumblebee kokoro --input MyBook_tts_ready.txt \
|
|
145
|
+
--voice af_heart \
|
|
146
|
+
--chapter-pause 1.2 --heading-pause 0.7 --paragraph-pause 0.45 \
|
|
147
|
+
--bitrate 32k --output-dir ./books
|
|
148
|
+
|
|
149
|
+
# Native AMD ROCm/HIP Kokoro-82M via roccoro (~245x RT synth, ~222x RT end-to-end, 831 MB GTT, auto-selected when installed):
|
|
150
|
+
mumblebee kokoro --input MyBook_tts_ready.txt --backend roccoro --output-dir ./books
|
|
151
|
+
|
|
152
|
+
# Force 0-GPU ONNX CPU mode (~31-35x RT, leaves GPU free for LLMs):
|
|
153
|
+
mumblebee kokoro --input MyBook_tts_ready.txt --backend onnx --output-dir ./books
|
|
154
|
+
|
|
155
|
+
# Force Hybrid CPU + GPU mode (~40.5x RT when GPU is free):
|
|
156
|
+
mumblebee kokoro --input MyBook_tts_ready.txt --backend hybrid --output-dir ./books
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### Step B2 — Render with `tiny-voca` (`mumblebee tinyvoca`)
|
|
160
|
+
```bash
|
|
161
|
+
mumblebee tinyvoca \
|
|
162
|
+
--text-file MyBook_tts_ready.txt \
|
|
163
|
+
--voice M2 --speed 0.88 --sentence-pause 0.15 --paragraph-pause 0.45 \
|
|
164
|
+
--bitrate 32k --artist "Author Name" --album "Book Title" --title "Book Title" \
|
|
165
|
+
-o ./books/MyBook_M2.opus
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### Step C — Standalone Cover Fetch (`mumblebee cover`)
|
|
169
|
+
```bash
|
|
170
|
+
mumblebee cover --title "Growing Young" --author "Ashley Montagu" --pdf GrowingYoung.pdf -o ./books/Growing_Young/cover.png
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
## Design Reasoning (The "Why" Behind the Architecture)
|
|
176
|
+
|
|
177
|
+
These empirical lessons shaped every major design decision—recorded here because code alone rarely explains *why* alternatives were rejected:
|
|
178
|
+
|
|
179
|
+
### 1. Why LLM preprocessing instead of regex/grep
|
|
180
|
+
Real books—especially academic non-fiction and OCR'd PDFs—entangle layout noise semantically with prose. A regex cannot reliably tell whether `century14` is a footnote number or a measurement, cannot repair arbitrary OCR word splits (`ad u lth oo d`, `Sometim es`), cannot stitch a sentence interrupted mid-clause by a 20-line floating table or 4 pages of skull diagrams, and cannot distinguish a keep-worthy Preface from 80 pages of endnotes. Decoupling **`mumblebee prep` → `*_tts_ready.txt` → TTS** lets you verify the text once and re-synthesize cheaply with any voice or engine.
|
|
181
|
+
|
|
182
|
+
### 2. Why two-tier LLM cleaning (fast 4B + validator escalation) beats one giant model
|
|
183
|
+
- On 256 GB/s unified memory, an ~85 GB frontier model is memory-bandwidth-bound (~24–32 tok/s, ~70 min/book). Verbatim copy-editing does not require frontier reasoning on 90% of chunks.
|
|
184
|
+
- A 4B instruction model (`Qwen3-4B-Instruct-2507` Q4_K_M, 2.4 GB) running **4 continuous-batching slots** with **`q8_0` KV cache (`~5.1 GB` GTT)** and **lossless prompt-lookup speculative decoding (`--spec-type ngram-simple --spec-ngram-simple-size-n 4 --spec-ngram-simple-size-m 48`)** reaches **402–435 agg tok/s** on clean EPUBs (97% of output tokens accepted from prompt lookup, **8/8 bit-for-bit identical** to unaccelerated decoding, **~3.3 min/book**) and **~298–303 agg tok/s** on dirty OCR PDFs (**~5.5 min/book**).
|
|
185
|
+
- **Why PDF pre-unwrapping unlocked `ngram-simple`**: Raw `pdftotext` output breaks every ~10 words with hard `\n` wraps and recurring 2-letter OCR splits (`o f`, `i n`, `t o`), which originally shattered 12-token n-gram matches (9–12% acceptance). Conservatively unwrapping mid-sentence column wraps (while preserving paragraph breaks, captions, tables, and ALL-CAPS blocks) and fixing strictly unambiguous 2-letter OCR splits before sending to the primary model boosts draft acceptance on OCR PDFs from **12% -> 63–81%** (**90.5% of output tokens drafted**), delivering a **2.7×–4.0× lossless speedup** (and Tier-2 escalations still receive the raw unaltered chunk).
|
|
186
|
+
- **Why automated chunk validation + Tier-2 escalation**: On the ~10% of chunks dominated by multi-page diagrams (`Fig. 1–5`, `PROBOSCIS MONKEY`, running headers) or subtle BPE-masked OCR splits (`Sometim es`, `L a Barre`, `A s`), a 4B model under a strict verbatim prompt can occasionally copy the artifact verbatim. `validate_cleaned_chunk()` checks every chunk for leaked headers/page numbers, `Fig.`/`TABLE` captions, ALL-CAPS diagram clusters, merged >650-word walls, broken lowercase paragraph starts, letter-spaced text (`A n n a ...`), and single-space mid-word OCR splits—then automatically escalates *only* failing chunks with concrete error feedback to the stronger fallback model.
|
|
187
|
+
|
|
188
|
+
### 3. Critical PDF & Chunking Traps We Hit and Fixed
|
|
189
|
+
- **Never use `str.splitlines()` on PDFs**: Python's `splitlines()` silently splits on and strips `\f` form-feed page breaks, blinding chapter detection. Always use `split("\n")`.
|
|
190
|
+
- **Never split chunks on `\f` page breaks**: Printed page breaks frequently cut mid-sentence or across floating tables. Splitting at `\f` causes small LLMs to hallucinate an ending for the dangling clause while the next chunk opens mid-air (`maximus (the large muscle...)`). Keep `<PAGEBREAK>` *inside* chunks and split only after blocks ending in terminal sentence punctuation whose next real prose line (skipping headers and page numbers) starts cleanly.
|
|
191
|
+
- **PDF paragraph restoration**: Non-layout `pdftotext` (required to de-column spreads) omits blank lines between paragraphs, causing LLMs to merge whole pages into 1,500-word walls. Re-inserting blank lines after short sentence-ending lines restores paragraphs before chunking.
|
|
192
|
+
- **Prefer `.epub` over `.pdf` whenever available**: Pure-stdlib EPUB extraction provides exact spine reading order, Dublin Core metadata, clean structural pre-stripping of `<figcaption>`, `<aside epub:type="footnote">`, and `<sup>` footnote markers (while preserving ordinals like `2nd`), plus the embedded publisher cover.
|
|
193
|
+
|
|
194
|
+
### 4. Deterministic Pacing & Clean Speed belong in the TTS Layer (Not the LLM)
|
|
195
|
+
- **Never rely on an LLM for audio timing**: Even if a heading loses a blank line or repeats the `<<<CHAPTER: ...>>>` title, `mumblebee kokoro` deterministically strips duplicate leading chapter titles (with or without `Chapter N:` prefixes), appends terminal punctuation so Kokoro uses a clean falling intonation, and streams exact silence beats (`--lead-in`, `--chapter-pause`, `--heading-pause`, `--paragraph-pause`) straight into `ffmpeg`.
|
|
196
|
+
- **Why `--speed` defaults to `1.0`**: Baking `ffmpeg atempo=1.3` into a lossy `.opus` file and then listening at `1.2×` in an audiobook player applies WSOLA time-stretching *twice* across lossy compression. Generating at clean `1.0×` lets your player handle speed adjustment in a single pass with noticeably cleaner audio.
|
|
197
|
+
|
|
198
|
+
### 5. Why Native HIP Kernels ([roccoro](https://github.com/sergeyplis/roccoro)) Reach 245× RT — and Why ONNX CPU Beats PyTorch ROCm
|
|
199
|
+
- **`Kokoro-82M` native HIP engine ([`sergeyplis/roccoro`](https://github.com/sergeyplis/roccoro), `--backend roccoro` / `--backend hip-native`)**: PyTorch ROCm (`cudnn=True`) triggers a MIOpen workspace fallback (`705 MB required, provided 0`) on Kokoro's 1D transposed convolutions (< 1× RT), and even with `cudnn=False` + 6 FP16 streams PyTorch tops out at ~59× RT while consuming **9.47 GB GTT**. Existing GGML ports (`kokoro.cpp` Vulkan 49.6× RT, `audio.cpp` HIP 16.4× RT) bottleneck on unfused `AdaIN`/`Snake1d`, `im2col`, and unbatched BiLSTM kernel launches. Our standalone C++/HIP engine (`roccoro`) writes a roofline-tuned implicit-GEMM `gfx1151` wave32 WMMA Conv1d/GEMM kernel (`27.6 TFLOP/s`, fusing residual add + epilogue AdaIN stats), a single-launch persistent FP32 BiLSTM kernel (12–16× faster with exact FP32 durations), and on-GPU `SineGen` + 20-pt STFT/iSTFT. Combined with longest-first chapter scheduling across up to 8 concurrent `ffmpeg` Opus encoders, it reaches **245.3× RT synthesis / 222× RT end-to-end** (full 8–9h audiobook in **~2m 15s–2m 27s**) in **831 MB GTT** (`env_mel_lsd = 0.0395 dB`, `env_mel_cos = 0.9999986` vs PyTorch) with **zero `torch`/`onnxruntime`/`transformers`** at inference time.
|
|
200
|
+
- **`Kokoro-82M` CPU fallback**: When no ROCm GPU / native binary is present, **16-core ONNX Runtime CPU FP32** hits **~31–35× RT** with **0 MB GPU**, and **Hybrid (CPU ONNX + 4 GPU streams)** reaches **~40.5× RT**.
|
|
201
|
+
- **`tiny-voca` (4.5M params)**: Kernel-launch bound on GPU (~98–343× RT on Radeon 8060S). Running 1 single-threaded ONNX session per physical core (`16w × 1T`, pre-optimized `--optimized` + `ORT_ENABLE_ALL`, LPT longest-first scheduling, `dizione` frontend) hits **~1,150–1,200× RT** in lossless FP32 with 0 MB GPU.
|
|
202
|
+
- **Why INT8 ONNX was rejected for both models**: On `tiny-voca`, INT8 MatMul gave only +15% speed while altering durations on 115/119 sentences; on `Kokoro-82M`, INT8 was *slower* (21.4× vs 31.1× RT) and shifted 27/28 chunk lengths. Lossless FP32 (or FP32-accumulated WMMA with FP32 BiLSTM duration prediction) won on both quality and reliability.
|
|
203
|
+
|
|
204
|
+
### 6. Why Opus (`32k` VBR, `-application voip`, 24 kHz mono)
|
|
205
|
+
Best speech intelligibility per megabyte (~13–14 MB/hour — ~3–4× smaller than MP3, ~12× smaller than WAV), native Vorbis tags (`title`, `artist`, `album`, `track`, `genre`, `comment`), and streaming PCM-to-`ffmpeg` piping that avoids multi-GB temporary WAV files.
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## Running Tests
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## License
|
|
216
|
+
|
|
217
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "mumblebee"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Turn messy PDFs, EPUBs, and OCR'd books into clean, chaptered, cover-tagged local audiobooks using local LLMs and fast local TTS (roccoro HIP / ONNX)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
authors = [{ name = "Sergey Plis" }]
|
|
13
|
+
requires-python = ">=3.10"
|
|
14
|
+
keywords = ["audiobook", "tts", "kokoro", "roccoro", "epub", "pdf", "ocr", "opus", "local-llm"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Operating System :: POSIX :: Linux",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Topic :: Multimedia :: Sound/Audio :: Speech",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"numpy>=1.24",
|
|
22
|
+
"onnxruntime>=1.18",
|
|
23
|
+
"pillow>=10.0",
|
|
24
|
+
"soundfile>=0.12",
|
|
25
|
+
"tqdm>=4.66",
|
|
26
|
+
"huggingface-hub>=0.24",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
kokoro = [
|
|
31
|
+
"kokoro>=0.9.4",
|
|
32
|
+
"torch>=2.4",
|
|
33
|
+
]
|
|
34
|
+
roccoro = [
|
|
35
|
+
"roccoro>=0.1.1",
|
|
36
|
+
]
|
|
37
|
+
kokoro-hip = [
|
|
38
|
+
"roccoro>=0.1.1",
|
|
39
|
+
]
|
|
40
|
+
tiny-voca = [
|
|
41
|
+
"torch>=2.4",
|
|
42
|
+
]
|
|
43
|
+
all = [
|
|
44
|
+
"mumblebee[kokoro,roccoro]",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[project.urls]
|
|
48
|
+
Homepage = "https://github.com/sergeyplis/mumblebee"
|
|
49
|
+
Repository = "https://github.com/sergeyplis/mumblebee"
|
|
50
|
+
Issues = "https://github.com/sergeyplis/mumblebee/issues"
|
|
51
|
+
|
|
52
|
+
[project.scripts]
|
|
53
|
+
mumblebee = "mumblebee.cli:main"
|
|
54
|
+
|
|
55
|
+
[tool.setuptools.packages.find]
|
|
56
|
+
where = ["src"]
|