audio_tts / app.py
Krixter's picture
Update app.py
5736d05 verified
Raw History Blame Contribute Delete
40.1 kB
import gradio as gr
import os
import tempfile
import requests
import subprocess
import zipfile
import shutil
import time
import asyncio
import re
import json
from collections import defaultdict
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor, as_completed
# ─── CONFIGURATION ──────────────────────────────────────────────────────────
FIXED_SPEED = 1.6
THREADS = 10
MAX_CHUNK = 200
# ─── CONCURRENCY GATES ──────────────────────────────────────────────────────
download_door = asyncio.Semaphore(1)
kitchen_door = asyncio.Semaphore(1)
# ════════════════════════════════════════════════════════════════════════════
# TEXT EXTRACTION
# ════════════════════════════════════════════════════════════════════════════
def extract_local_text(path, temp_dir):
content = ""
ext = os.path.splitext(path)[1].lower()
try:
if ext == ".txt":
with open(path, "r", encoding="utf-8", errors="ignore") as f:
content = f.read()
elif ext == ".epub":
with zipfile.ZipFile(path, "r") as z:
for filename in sorted(z.namelist()):
if filename.endswith((".html", ".xhtml")):
with z.open(filename) as f:
soup = BeautifulSoup(f.read(), "html.parser")
part = soup.get_text(separator="\n")
if part:
content += part + "\n"
elif ext == ".pdf":
import PyPDF2
reader = PyPDF2.PdfReader(path)
for page in reader.pages:
txt = page.extract_text()
if txt:
content += txt + "\n"
except Exception:
return None
if not content:
return None
# Normalise horizontal whitespace on each line, but keep paragraph breaks
content = re.sub(r"[ \t]+", " ", content)
# Collapse runs of 3+ blank lines down to exactly two newlines (one blank line)
content = re.sub(r"\n{3,}", "\n\n", content)
# Trim trailing spaces on every line
content = "\n".join(line.rstrip() for line in content.splitlines())
out = os.path.join(temp_dir, "extracted.txt")
with open(out, "w", encoding="utf-8") as f:
f.write(content)
return out
# ════════════════════════════════════════════════════════════════════════════
# TTS ENGINE
# ════════════════════════════════════════════════════════════════════════════
class GoogleTTSEngine:
def split_text(self, text, max_len=MAX_CHUNK):
chunks = []
while text:
if len(text) <= max_len:
chunks.append(text.strip())
break
best_i = -1
for punct in [". ", "! ", "? ", "; "]:
i = text[:max_len].rfind(punct)
if i > best_i:
best_i = i
if best_i == -1:
best_i = text[:max_len].rfind(" ")
if best_i == -1:
best_i = max_len
else:
best_i += 1
chunks.append(text[:best_i].strip())
text = text[best_i:].lstrip()
return [c for c in chunks if c]
def strip_id3(self, mp3_data):
if mp3_data[:3] == b"ID3":
size = (
(mp3_data[6] & 0x7F) << 21
| (mp3_data[7] & 0x7F) << 14
| (mp3_data[8] & 0x7F) << 7
| (mp3_data[9] & 0x7F)
)
mp3_data = mp3_data[10 + size:]
if len(mp3_data) > 128 and mp3_data[-128:-125] == b"TAG":
mp3_data = mp3_data[:-128]
return mp3_data
def download_chunk(self, text):
url = "https://translate.google.com/translate_tts"
params = {"ie": "UTF-8", "q": text, "tl": "en-IN", "client": "tw-ob"}
headers = {"User-Agent": "Mozilla/5.0"}
for _ in range(5):
try:
r = requests.get(url, params=params, headers=headers, timeout=8)
if r.status_code == 200:
return self.strip_id3(r.content)
except Exception:
time.sleep(0.5)
return None
# ════════════════════════════════════════════════════════════════════════════
# AUDIO HELPERS
# ════════════════════════════════════════════════════════════════════════════
def get_duration_ms(path):
result = subprocess.run(
["ffprobe", "-v", "error",
"-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1", path],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
)
try:
return int(float(result.stdout.strip()) * 1000)
except Exception:
return 0
def process_chunk_audio(args):
in_path, proc_path, speed = args
ret = subprocess.run(
["ffmpeg", "-y", "-i", in_path,
"-af", "atempo=" + str(speed),
"-threads", "1", proc_path],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
if ret.returncode != 0 or not os.path.exists(proc_path):
shutil.copy2(in_path, proc_path)
return get_duration_ms(proc_path)
# ════════════════════════════════════════════════════════════════════════════
# CHAPTER PARSING
# ════════════════════════════════════════════════════════════════════════════
def parse_chapters(full_text):
splits = re.split(
r"(?i)(\b(?:chapter|ch\.?)\s*[-:]?\s*\d+\b[^\n]*|\b\d+\s*:(?!\d)[^\n]*)",
full_text,
)
chapters = []
if splits[0].strip():
chapters.append(["Intro", splits[0]]) # keep raw text
for i in range(1, len(splits), 2):
title = splits[i].strip()
body = splits[i + 1] if i + 1 < len(splits) else ""
if len(title) > 100:
body = title[100:] + "\n" + body
title = title[:100] + "..."
num_match = re.search(r"\d+", title)
current_num = num_match.group() if num_match else title
if chapters:
prev_nm = re.search(r"\d+", chapters[-1][0])
prev_num = prev_nm.group() if prev_nm else chapters[-1][0]
if current_num == prev_num:
chapters[-1][1] += "\n" + body
continue
if len(chapters[-1][1]) < 150:
chapters[-1][1] += "\n" + title + ".\n" + body
chapters[-1][0] = title
continue
chapters.append([title, title + ".\n" + body])
if not chapters:
chapters = [["Full Book", full_text]]
return chapters
# ════════════════════════════════════════════════════════════════════════════
# MAIN SYNC PIPELINE
# ════════════════════════════════════════════════════════════════════════════
def sync_process_book(file_path, temp_dir, progress_obj, book_idx, total_books):
text_file = extract_local_text(file_path, temp_dir)
if not text_file:
return None, None, None, None
base_name = os.path.splitext(os.path.basename(file_path))[0]
book_label = "[Book %d/%d]" % (book_idx, total_books)
with open(text_file, "r", encoding="utf-8") as f:
full_text = f.read()
chapters = parse_chapters(full_text)
engine = GoogleTTSEngine()
all_chunks = []
for c_idx, (_, c_text) in enumerate(chapters):
tts_text = c_text.replace("\n", " ") # flat copy for TTS only
tts_text = re.sub(r" {2,}", " ", tts_text).strip()
for local_idx, chunk_text in enumerate(engine.split_text(tts_text)):
all_chunks.append((c_idx, local_idx, chunk_text))
chunk_text_map = {(c, l): t for c, l, t in all_chunks}
total_chunks = len(all_chunks)
chunk_dir = os.path.join(temp_dir, "chunks_raw")
proc_dir = os.path.join(temp_dir, "chunks_proc")
os.makedirs(chunk_dir, exist_ok=True)
os.makedirs(proc_dir, exist_ok=True)
# ── PHASE 1: DOWNLOAD (parallel) ─────────────────────────────────────
start_time = time.time()
completed = 0
with ThreadPoolExecutor(max_workers=THREADS) as pool:
futures = {
pool.submit(engine.download_chunk, text): (c_idx, local_idx)
for c_idx, local_idx, text in all_chunks
}
for fut in as_completed(futures):
c_idx, local_idx = futures[fut]
data = fut.result()
if data:
fpath = os.path.join(chunk_dir, "c%03d_%05d.mp3" % (c_idx, local_idx))
with open(fpath, "wb") as af:
af.write(data)
completed += 1
elapsed = time.time() - start_time
avg = elapsed / completed
eta = avg * (total_chunks - completed)
progress_obj(
completed / (total_chunks * 2),
desc="%s Downloading: %d/%d | ETA %02d:%02d" % (
book_label, completed, total_chunks,
int(eta // 60), int(eta % 60)),
)
# ── PHASE 2: SPEED-PROCESS (parallel ffmpeg) ──────────────────────────
downloaded = sorted(f for f in os.listdir(chunk_dir) if f.endswith(".mp3"))
process_args = [
(os.path.join(chunk_dir, cf), os.path.join(proc_dir, cf), FIXED_SPEED)
for cf in downloaded
]
durations = {}
with ThreadPoolExecutor(max_workers=THREADS) as pool:
fut_map = {pool.submit(process_chunk_audio, a): a[1] for a in process_args}
proc_done = 0
for fut in as_completed(fut_map):
proc_path = fut_map[fut]
dur_ms = fut.result()
durations[os.path.basename(proc_path)] = dur_ms
proc_done += 1
progress_obj(
0.50 + (proc_done / max(len(downloaded), 1)) * 0.30,
desc="%s Speed-processing: %d/%d" % (book_label, proc_done, len(downloaded)),
)
# ── PHASE 3: BUILD TIMESTAMPS ─────────────────────────────────────────
chunk_metadata = []
current_ms = 0
chunk_idx = 0
for cf in downloaded:
m = re.match(r"c(\d+)_(\d+)\.mp3", cf)
if not m:
continue
c_idx = int(m.group(1))
local_idx = int(m.group(2))
dur_ms = durations.get(cf, 0)
text = chunk_text_map.get((c_idx, local_idx), "")
chunk_metadata.append({
"idx": chunk_idx,
"cIdx": c_idx,
"text": text,
"start": current_ms,
"end": current_ms + dur_ms,
})
current_ms += dur_ms
chunk_idx += 1
chunks_by_chapter = defaultdict(list)
for cm in chunk_metadata:
chunks_by_chapter[cm["cIdx"]].append(cm)
chapter_info = []
for c_idx, (title, raw_body) in enumerate(chapters):
first = chunks_by_chapter[c_idx][0] if chunks_by_chapter[c_idx] else None
chapter_info.append({
"idx": c_idx,
"title": title,
"start": first["start"] if first else 0,
"chunkStart": first["idx"] if first else 0,
"raw_text": raw_body, # preserved for paragraph rendering
})
# ── PHASE 4: FINAL MERGE (stream-copy concat) ─────────────────────────
progress_obj(0.85, desc=book_label + " Merging audio...")
concat_path = os.path.join(temp_dir, "concat.txt")
with open(concat_path, "w") as f:
for cf in downloaded:
p = os.path.join(proc_dir, cf)
if os.path.exists(p):
f.write("file '" + os.path.abspath(p).replace("\\", "/") + "'\n")
output_mp3 = os.path.join(tempfile.gettempdir(), base_name + "_Audio.mp3")
subprocess.run(
["ffmpeg", "-y", "-f", "concat", "-safe", "0",
"-i", concat_path, "-c", "copy", output_mp3],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
shutil.rmtree(temp_dir)
return output_mp3, chunk_metadata, chapter_info, base_name
# ════════════════════════════════════════════════════════════════════════════
# HTML TEMPLATE
#
# Stored as a plain (non-f) string. Dynamic content is injected with
# .replace() using __SENTINEL__ tokens — no curly-brace escaping needed,
# so CSS / JS never causes SyntaxErrors regardless of Python version.
# ════════════════════════════════════════════════════════════════════════════
_HTML = (
"<!DOCTYPE html>\n"
'<html lang="en" data-theme="light">\n'
"<head>\n"
' <meta charset="UTF-8">\n'
' <meta name="viewport" content="width=device-width, initial-scale=1.0">\n'
" <title>__BOOK_TITLE__ &mdash; WebBook</title>\n"
" <style>\n"
" :root {\n"
" --bg: #f5f0e8;\n"
" --text: #2c2c2c;\n"
" --border: #e0d9cc;\n"
" --ch-title: #1a1a2e;\n"
" --hl-bg: #ffe082;\n"
" --hl-text: #1a1200;\n"
" --chunk-hover: rgba(200,134,10,0.13);\n"
" --sb-bg: #1a1a2e;\n"
" --sb-text: #cccce0;\n"
" --sb-hover: #2d2d4a;\n"
" --sb-accent: #e8b86d;\n"
" --pl-bg: rgba(14,14,30,0.97);\n"
" --accent: #e8b86d;\n"
" }\n"
" [data-theme=dark] {\n"
" --bg: #0f0f1a;\n"
" --text: #e0e0f0;\n"
" --border: #2d2d4a;\n"
" --ch-title: #e8b86d;\n"
" --hl-bg: #5c4400;\n"
" --hl-text: #ffe082;\n"
" --chunk-hover: rgba(232,184,109,0.1);\n"
" }\n"
" *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}\n"
" html{font-size:18px;scroll-behavior:smooth}\n"
" body{\n"
' font-family:Georgia,"Times New Roman",serif;\n'
" background:var(--bg);color:var(--text);\n"
" overflow-x:hidden;transition:background .25s,color .25s;\n"
" }\n"
" #player-bar{\n"
" position:fixed;top:0;left:0;right:0;height:62px;\n"
" background:var(--pl-bg);\n"
" backdrop-filter:blur(18px);-webkit-backdrop-filter:blur(18px);\n"
" display:flex;align-items:center;gap:10px;\n"
" padding:0 14px;z-index:1000;\n"
" box-shadow:0 2px 24px rgba(0,0,0,.5);\n"
" }\n"
" #audio-el{display:none}\n"
" .bk-meta{display:flex;flex-direction:column;flex:0 0 auto;max-width:170px}\n"
" #bk-title-pl{\n"
" font-size:.78rem;font-weight:bold;color:var(--accent);\n"
" white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-family:sans-serif;\n"
" }\n"
" #ch-ind{\n"
" font-size:.68rem;color:rgba(255,255,255,.42);\n"
" white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-family:sans-serif;\n"
" }\n"
" .pl-btn{\n"
" background:none;border:none;color:#fff;\n"
" cursor:pointer;font-size:1rem;padding:6px 8px;\n"
" border-radius:50%;transition:background .15s;flex-shrink:0;\n"
" }\n"
" .pl-btn:hover{background:rgba(255,255,255,.1)}\n"
" #btn-play{font-size:1.3rem}\n"
" #seek-wrap{flex:1;display:flex;align-items:center;gap:8px;min-width:0}\n"
" input[type=range]{\n"
" -webkit-appearance:none;height:4px;\n"
" border-radius:2px;outline:none;cursor:pointer;\n"
" }\n"
" input[type=range]::-webkit-slider-thumb{\n"
" -webkit-appearance:none;width:13px;height:13px;\n"
" border-radius:50%;background:var(--accent);cursor:pointer;\n"
" }\n"
" #seek-sl{\n"
" flex:1;\n"
" background:linear-gradient(to right,var(--accent) 0%,rgba(255,255,255,.18) 0%);\n"
" }\n"
" #time-disp{\n"
" font-size:.7rem;color:rgba(255,255,255,.6);\n"
" white-space:nowrap;font-family:monospace;flex-shrink:0;\n"
" }\n"
" .ctrl-grp{display:flex;align-items:center;gap:5px;flex-shrink:0}\n"
" .ctrl-lbl{font-size:.67rem;color:rgba(255,255,255,.42);font-family:sans-serif}\n"
" #spd-sl{width:64px;background:rgba(255,255,255,.18)}\n"
" #spd-val{font-size:.7rem;color:rgba(255,255,255,.7);font-family:monospace;min-width:34px}\n"
" #vol-sl{width:68px;background:rgba(255,255,255,.18)}\n"
" #vol-ico{cursor:pointer;font-size:.9rem}\n"
" #theme-btn{\n"
" background:none;border:1px solid rgba(255,255,255,.2);\n"
" color:#fff;padding:3px 9px;border-radius:20px;\n"
" cursor:pointer;font-size:.7rem;font-family:sans-serif;flex-shrink:0;\n"
" }\n"
" #sb-toggle{\n"
" display:none;background:none;border:none;\n"
" color:#fff;font-size:1.2rem;cursor:pointer;padding:4px 8px;\n"
" }\n"
" #layout{display:flex;margin-top:62px;min-height:calc(100vh - 62px)}\n"
" #sidebar{\n"
" width:255px;flex-shrink:0;\n"
" background:var(--sb-bg);color:var(--sb-text);\n"
" position:fixed;top:62px;left:0;bottom:0;\n"
" overflow-y:auto;overflow-x:hidden;z-index:100;\n"
" display:flex;flex-direction:column;\n"
" transition:transform .3s ease;\n"
" }\n"
" #sidebar::-webkit-scrollbar{width:4px}\n"
" #sidebar::-webkit-scrollbar-thumb{background:rgba(255,255,255,.12);border-radius:2px}\n"
" #sb-hdr{padding:18px 16px 14px;border-bottom:1px solid rgba(255,255,255,.07)}\n"
" #bk-title-sb{\n"
" font-size:.9rem;font-weight:bold;\n"
" color:var(--sb-accent);line-height:1.3;font-family:Georgia,serif;\n"
" }\n"
" #sb-sub{\n"
" font-size:.67rem;color:rgba(255,255,255,.35);\n"
" margin-top:4px;text-transform:uppercase;\n"
" letter-spacing:.06em;font-family:sans-serif;\n"
" }\n"
" #ch-list{list-style:none;padding:6px 0;flex:1}\n"
" .sb-item{\n"
" padding:9px 16px;cursor:pointer;\n"
" font-size:.8rem;font-family:sans-serif;line-height:1.4;\n"
" border-left:3px solid transparent;transition:all .15s;\n"
" }\n"
" .sb-item:hover{background:var(--sb-hover);color:#fff}\n"
" .sb-item.active{\n"
" color:var(--sb-accent);border-left-color:var(--sb-accent);\n"
" background:rgba(232,184,109,.08);font-weight:600;\n"
" }\n"
" #content{margin-left:255px;flex:1;padding:44px 64px 100px;max-width:900px}\n"
" .chapter{margin-bottom:60px}\n"
" .chapter-title{\n"
" font-family:Georgia,serif;font-size:1.45rem;font-weight:bold;\n"
" color:var(--ch-title);margin-bottom:18px;padding-bottom:12px;\n"
" border-bottom:2px solid var(--border);line-height:1.3;\n"
" }\n"
" .chapter-body{line-height:1.9;font-size:1rem}\n"
" .para{display:block;margin-bottom:1.1em;text-indent:0}\n"
" .chunk{\n"
" cursor:pointer;border-radius:3px;padding:1px 0;\n"
" display:inline;transition:background .12s,color .12s;\n"
" }\n"
" .chunk:hover{background:var(--chunk-hover)}\n"
" .chunk.active{\n"
" background:var(--hl-bg);color:var(--hl-text);\n"
" padding:1px 3px;margin:0 -3px;\n"
" }\n"
" @media(max-width:780px){\n"
" #sb-toggle{display:block}\n"
" #sidebar{transform:translateX(-100%)}\n"
" #sidebar.open{transform:translateX(0)}\n"
" #content{margin-left:0;padding:24px 18px 80px}\n"
" .bk-meta,#ch-ind,.ctrl-grp{display:none}\n"
" }\n"
" </style>\n"
"</head>\n"
"<body>\n"
'\n<audio id="audio-el" src="__AUDIO_SRC__" preload="metadata"></audio>\n'
"\n"
'<header id="player-bar">\n'
' <button id="sb-toggle" onclick="toggleSB()">&#9776;</button>\n'
' <div class="bk-meta">\n'
' <span id="bk-title-pl">__BOOK_TITLE__</span>\n'
' <span id="ch-ind">Loading&hellip;</span>\n'
" </div>\n"
' <button class="pl-btn" onclick="seekRel(-10)" title="Back 10s">&#9198;</button>\n'
' <button class="pl-btn" id="btn-play" onclick="togglePlay()" title="Space=play/pause">&#9654;</button>\n'
' <button class="pl-btn" onclick="seekRel(10)" title="Forward 10s">&#9197;</button>\n'
' <div id="seek-wrap">\n'
' <input type="range" id="seek-sl" min="0" max="10000" value="0"\n'
' oninput="onSeekInput(this)" onchange="onSeekChange(this)">\n'
" </div>\n"
' <span id="time-disp">0:00 / 0:00</span>\n'
' <div class="ctrl-grp">\n'
' <span class="ctrl-lbl">Speed</span>\n'
' <input type="range" id="spd-sl" min="0.5" max="2.5" step="0.1" value="1.0"\n'
' oninput="onSpdChange(this)">\n'
' <span id="spd-val">1.0&times;</span>\n'
" </div>\n"
' <div class="ctrl-grp">\n'
' <span id="vol-ico" onclick="toggleMute()">&#128266;</span>\n'
' <input type="range" id="vol-sl" min="0" max="1" step="0.01" value="1"\n'
' oninput="onVolChange(this)">\n'
" </div>\n"
' <button id="theme-btn" onclick="toggleTheme()">&#127769; Dark</button>\n'
"</header>\n"
"\n"
'<div id="layout">\n'
' <nav id="sidebar">\n'
' <div id="sb-hdr">\n'
' <h1 id="bk-title-sb">__BOOK_TITLE__</h1>\n'
' <p id="sb-sub">Table of Contents</p>\n'
" </div>\n"
' <ul id="ch-list">\n'
"__SIDEBAR_ITEMS__\n"
" </ul>\n"
" </nav>\n"
' <main id="content">\n'
"__CHAPTERS_HTML__\n"
" </main>\n"
"</div>\n"
"\n"
"<script>\n"
"var CHUNKS = __CHUNKS_JSON__;\n"
"var CHAPTERS = __CHAPTERS_JSON__;\n"
"\n"
"var audio = document.getElementById('audio-el');\n"
"var btnPlay = document.getElementById('btn-play');\n"
"var seekSl = document.getElementById('seek-sl');\n"
"var timeDisp = document.getElementById('time-disp');\n"
"var spdSl = document.getElementById('spd-sl');\n"
"var spdVal = document.getElementById('spd-val');\n"
"var volSl = document.getElementById('vol-sl');\n"
"var chInd = document.getElementById('ch-ind');\n"
"var sbItems = Array.from(document.querySelectorAll('.sb-item'));\n"
"var chunkEls = document.querySelectorAll('.chunk');\n"
"\n"
"var activeChunk = -1, activeCh = -1;\n"
"var isSeeking = false, scrollTmr = null, isDark = false;\n"
"\n"
"function findChunkAt(ms) {\n"
" var lo = 0, hi = CHUNKS.length - 1;\n"
" while (lo <= hi) {\n"
" var mid = (lo + hi) >> 1;\n"
" if (CHUNKS[mid].end <= ms) { lo = mid + 1; }\n"
" else if (CHUNKS[mid].start > ms) { hi = mid - 1; }\n"
" else { return mid; }\n"
" }\n"
" return -1;\n"
"}\n"
"\n"
"function findChapterAt(ms) {\n"
" var best = 0;\n"
" for (var i = 0; i < CHAPTERS.length; i++) {\n"
" if (CHAPTERS[i].start <= ms) { best = i; } else { break; }\n"
" }\n"
" return best;\n"
"}\n"
"\n"
"audio.addEventListener('timeupdate', function() {\n"
" if (isSeeking) { return; }\n"
" var ms = audio.currentTime * 1000;\n"
" var total = (audio.duration || 0) * 1000 || 1;\n"
" var pct = ms / total;\n"
" seekSl.value = pct * 10000;\n"
" updateGrad(pct);\n"
" timeDisp.textContent = fmt(audio.currentTime) + ' / ' + fmt(audio.duration || 0);\n"
" var ni = findChunkAt(ms);\n"
" if (ni !== activeChunk) {\n"
" if (activeChunk >= 0) {\n"
" var prev = document.querySelector('[data-idx=\"' + activeChunk + '\"]');\n"
" if (prev) { prev.classList.remove('active'); }\n"
" }\n"
" if (ni >= 0) {\n"
" var el = document.querySelector('[data-idx=\"' + ni + '\"]');\n"
" if (el) {\n"
" el.classList.add('active');\n"
" if (!scrollTmr) {\n"
" scrollTmr = setTimeout(function() {\n"
" el.scrollIntoView({ behavior: 'smooth', block: 'center' });\n"
" scrollTmr = null;\n"
" }, 300);\n"
" }\n"
" }\n"
" }\n"
" activeChunk = ni;\n"
" }\n"
" var ci = findChapterAt(ms);\n"
" if (ci !== activeCh) {\n"
" activeCh = ci;\n"
" if (CHAPTERS[ci]) { chInd.textContent = CHAPTERS[ci].title; }\n"
" sbItems.forEach(function(e, i) { e.classList.toggle('active', i === ci); });\n"
" }\n"
"});\n"
"\n"
"audio.addEventListener('play', function() { btnPlay.innerHTML = '&#9646;&#9646;'; });\n"
"audio.addEventListener('pause', function() { btnPlay.innerHTML = '&#9654;'; });\n"
"audio.addEventListener('ended', function() { btnPlay.innerHTML = '&#9654;'; });\n"
"audio.addEventListener('loadedmetadata', function() {\n"
" timeDisp.textContent = '0:00 / ' + fmt(audio.duration);\n"
"});\n"
"\n"
"function togglePlay() { audio.paused ? audio.play() : audio.pause(); }\n"
"function seekRel(s) { audio.currentTime = Math.max(0, Math.min(audio.duration||0, audio.currentTime+s)); }\n"
"\n"
"function onSeekInput(s) {\n"
" isSeeking = true;\n"
" var pct = s.value / 10000;\n"
" updateGrad(pct);\n"
" timeDisp.textContent = fmt(pct*(audio.duration||0)) + ' / ' + fmt(audio.duration||0);\n"
"}\n"
"function onSeekChange(s) {\n"
" audio.currentTime = (s.value/10000)*(audio.duration||0);\n"
" isSeeking = false;\n"
"}\n"
"function onSpdChange(s) {\n"
" audio.playbackRate = parseFloat(s.value);\n"
" spdVal.textContent = parseFloat(s.value).toFixed(1) + '\\u00d7';\n"
"}\n"
"function onVolChange(s) {\n"
" audio.volume = parseFloat(s.value);\n"
" document.getElementById('vol-ico').textContent = audio.volume===0 ? '\\ud83d\\udd07' : '\\ud83d\\udd0a';\n"
"}\n"
"function toggleMute() {\n"
" audio.muted = !audio.muted;\n"
" document.getElementById('vol-ico').textContent = audio.muted ? '\\ud83d\\udd07' : '\\ud83d\\udd0a';\n"
"}\n"
"\n"
"chunkEls.forEach(function(el) {\n"
" el.addEventListener('click', function() {\n"
" var ms = parseInt(el.dataset.start, 10);\n"
" if (ms < 0) { return; }\n"
" audio.currentTime = ms / 1000;\n"
" if (audio.paused) { audio.play(); }\n"
" });\n"
"});\n"
"\n"
"sbItems.forEach(function(el) {\n"
" el.addEventListener('click', function() {\n"
" audio.currentTime = parseInt(el.dataset.start,10) / 1000;\n"
" if (audio.paused) { audio.play(); }\n"
" if (window.innerWidth <= 780) { document.getElementById('sidebar').classList.remove('open'); }\n"
" });\n"
"});\n"
"\n"
"document.addEventListener('keydown', function(e) {\n"
" if (e.target.tagName==='INPUT') { return; }\n"
" if (e.key===' ') { e.preventDefault(); togglePlay(); }\n"
" else if (e.key==='ArrowRight') { seekRel(10); }\n"
" else if (e.key==='ArrowLeft') { seekRel(-10); }\n"
" else if (e.key==='ArrowUp') { audio.volume=Math.min(1,audio.volume+0.1); volSl.value=audio.volume; }\n"
" else if (e.key==='ArrowDown') { audio.volume=Math.max(0,audio.volume-0.1); volSl.value=audio.volume; }\n"
"});\n"
"\n"
"function toggleTheme() {\n"
" isDark = !isDark;\n"
" document.documentElement.setAttribute('data-theme', isDark?'dark':'light');\n"
" document.getElementById('theme-btn').innerHTML = isDark ? '&#9728; Light' : '&#127769; Dark';\n"
"}\n"
"function toggleSB() { document.getElementById('sidebar').classList.toggle('open'); }\n"
"\n"
"function fmt(sec) {\n"
" if (!isFinite(sec)||sec<0) { return '0:00'; }\n"
" var h=Math.floor(sec/3600), m=Math.floor((sec%3600)/60), s=Math.floor(sec%60);\n"
" var p=function(n){return String(n).padStart(2,'0');};\n"
" return h>0 ? h+':'+p(m)+':'+p(s) : m+':'+p(s);\n"
"}\n"
"function updateGrad(pct) {\n"
" var v=(pct*100).toFixed(2)+'%';\n"
" seekSl.style.background='linear-gradient(to right,var(--accent) '+v+',rgba(255,255,255,.18) '+v+')';\n"
"}\n"
"</script>\n"
"</body>\n"
"</html>\n"
)
# ════════════════════════════════════════════════════════════════════════════
# HTML GENERATION
# ════════════════════════════════════════════════════════════════════════════
def _esc(text):
return (text
.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace('"', "&quot;"))
def _text_to_paragraphs(raw_text):
"""Split raw (multi-line) chapter text into a list of non-empty paragraph strings."""
paras = re.split(r"\n{2,}", raw_text)
result = []
for p in paras:
p = p.strip()
if p:
result.append(p)
return result
def generate_webbook_html(chunk_metadata, chapter_info, base_name, audio_filename):
chunks_by_chapter = defaultdict(list)
for cm in chunk_metadata:
chunks_by_chapter[cm["cIdx"]].append(cm)
chapters_html = ""
for ch in chapter_info:
c_idx = ch["idx"]
c_chunks = chunks_by_chapter[c_idx]
c_text = ch.get("raw_text", "")
chapters_html += (
' <section class="chapter" id="chapter-' + str(c_idx) + '">\n'
' <h2 class="chapter-title">' + _esc(ch["title"]) + '</h2>\n'
' <div class="chapter-body">\n'
)
if c_text and c_chunks:
# Build a flat sequence of (flat_char_offset, chunk) so we can
# decide which paragraph each chunk "belongs to" by comparing
# the chunk's text against the flattened paragraph text.
paragraphs = _text_to_paragraphs(c_text)
flat_full = " ".join(p.replace("\n", " ") for p in paragraphs)
# Map each chunk to its paragraph by scanning for its text
chunk_para = {}
search_pos = 0
for ck in c_chunks:
pos = flat_full.find(ck["text"].strip(), search_pos)
if pos == -1:
pos = flat_full.find(ck["text"].strip())
# Figure out which paragraph this position falls in
running = 0
para_idx = 0
for pi, para in enumerate(paragraphs):
flat_para = para.replace("\n", " ")
if running + len(flat_para) >= pos:
para_idx = pi
break
running += len(flat_para) + 1 # +1 for the join space
chunk_para[ck["idx"]] = para_idx
if pos != -1:
search_pos = pos + len(ck["text"])
# Group chunks by paragraph
from collections import defaultdict as _dd
para_chunks = _dd(list)
for ck in c_chunks:
para_chunks[chunk_para.get(ck["idx"], 0)].append(ck)
for pi, para in enumerate(paragraphs):
chapters_html += ' <p class="para">'
p_chunks = para_chunks.get(pi, [])
if p_chunks:
for ck in p_chunks:
chapters_html += (
'<span class="chunk"'
' data-idx="' + str(ck["idx"]) + '"'
' data-start="' + str(ck["start"]) + '"'
' data-end="' + str(ck["end"]) + '">'
+ _esc(ck["text"]) + '</span> '
)
else:
# Paragraph has no aligned chunk — render plain text
chapters_html += _esc(para.replace("\n", " "))
chapters_html += '</p>\n'
else:
# Fallback: no paragraph data — render chunks inline as before
for ck in c_chunks:
chapters_html += (
'<span class="chunk"'
' data-idx="' + str(ck["idx"]) + '"'
' data-start="' + str(ck["start"]) + '"'
' data-end="' + str(ck["end"]) + '">'
+ _esc(ck["text"]) + '</span> '
)
chapters_html += "\n </div>\n </section>\n"
sidebar_items = "\n".join(
' <li class="sb-item"'
' data-chapter="' + str(ch["idx"]) + '"'
' data-start="' + str(ch["start"]) + '">'
+ _esc(ch["title"]) + '</li>'
for ch in chapter_info
)
chapter_info_json = [
{k: v for k, v in ch.items() if k != "raw_text"}
for ch in chapter_info
]
return (
_HTML
.replace("__BOOK_TITLE__", _esc(base_name))
.replace("__AUDIO_SRC__", audio_filename)
.replace("__SIDEBAR_ITEMS__", sidebar_items)
.replace("__CHAPTERS_HTML__", chapters_html)
.replace("__CHUNKS_JSON__", json.dumps(chunk_metadata, ensure_ascii=False))
.replace("__CHAPTERS_JSON__", json.dumps(chapter_info_json, ensure_ascii=False))
)
# ════════════════════════════════════════════════════════════════════════════
# WEBBOOK PACKAGING
# ════════════════════════════════════════════════════════════════════════════
def build_webbook(output_mp3, chunk_metadata, chapter_info, base_name):
audio_filename = os.path.basename(output_mp3)
html_str = generate_webbook_html(chunk_metadata, chapter_info, base_name, audio_filename)
html_path = os.path.join(tempfile.gettempdir(), base_name + "_WebBook.html")
with open(html_path, "w", encoding="utf-8") as f:
f.write(html_str)
zip_path = os.path.join(tempfile.gettempdir(), base_name + "_WebBook.zip")
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
zf.write(html_path, arcname=os.path.basename(html_path))
zf.write(output_mp3, arcname=audio_filename)
return html_path, zip_path
# ════════════════════════════════════════════════════════════════════════════
# ASYNC ORCHESTRATION
# ════════════════════════════════════════════════════════════════════════════
async def process_single_book(file_path, progress_obj, book_idx, total_books):
temp_dir = tempfile.mkdtemp()
async with download_door:
result = await asyncio.to_thread(
sync_process_book,
file_path, temp_dir, progress_obj, book_idx, total_books,
)
output_mp3, chunk_metadata, chapter_info, base_name = result
if not output_mp3:
return []
async with kitchen_door:
html_path, zip_path = await asyncio.to_thread(
build_webbook, output_mp3, chunk_metadata, chapter_info, base_name,
)
return [zip_path, html_path, output_mp3]
async def process_books(file_paths, progress=gr.Progress()):
if not file_paths:
yield []
return
if not isinstance(file_paths, list):
file_paths = [file_paths]
total = len(file_paths)
tasks = [
asyncio.create_task(process_single_book(f, progress, idx + 1, total))
for idx, f in enumerate(file_paths)
]
all_files = []
for coro in asyncio.as_completed(tasks):
results = await coro
if results:
all_files.extend(results)
yield all_files
if total > 1:
master = os.path.join(tempfile.gettempdir(), "WebBooks_Batch.zip")
with zipfile.ZipFile(master, "w", zipfile.ZIP_DEFLATED) as zf:
for fp in all_files:
zf.write(fp, arcname=os.path.basename(fp))
all_files.insert(0, master)
yield all_files
# ════════════════════════════════════════════════════════════════════════════
# GRADIO UI
# ════════════════════════════════════════════════════════════════════════════
with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo")) as app:
gr.Markdown(
"# 📖 Interactive WebBook Studio\n"
"Upload `.txt`, `.epub`, or `.pdf` files and receive a fully synced audio + HTML WebBook.\n\n"
"**Per book:** `_WebBook.zip` (unzip → open HTML) &nbsp;|&nbsp; "
"`_WebBook.html` &nbsp;|&nbsp; `_Audio.mp3`"
)
with gr.Row():
with gr.Column(scale=1):
file_in = gr.File(label="Upload Books", file_count="multiple")
with gr.Row():
start_btn = gr.Button("Generate WebBook", variant="primary")
stop_btn = gr.Button("Stop", variant="stop")
with gr.Column(scale=1):
file_out = gr.File(label="Downloads", file_count="multiple", interactive=False)
event = start_btn.click(fn=process_books, inputs=file_in, outputs=file_out)
stop_btn.click(fn=None, inputs=None, outputs=None, cancels=[event])
if __name__ == "__main__":
app.launch()