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 = ( "\n" '\n' "\n" ' \n' ' \n' " __BOOK_TITLE__ — WebBook\n" " \n" "\n" "\n" '\n\n' "\n" '
\n' ' \n' '
\n' ' __BOOK_TITLE__\n' ' Loading…\n' "
\n" ' \n' ' \n' ' \n' '
\n' ' \n' "
\n" ' 0:00 / 0:00\n' '
\n' ' Speed\n' ' \n' ' 1.0×\n' "
\n" '
\n' ' 🔊\n' ' \n' "
\n" ' \n' "
\n" "\n" '
\n' ' \n" '
\n' "__CHAPTERS_HTML__\n" "
\n" "
\n" "\n" "\n" "\n" "\n" ) # ════════════════════════════════════════════════════════════════════════════ # HTML GENERATION # ════════════════════════════════════════════════════════════════════════════ def _esc(text): return (text .replace("&", "&") .replace("<", "<") .replace(">", ">") .replace('"', """)) 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 += ( '
\n' '

' + _esc(ch["title"]) + '

\n' '
\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_chunks = para_chunks.get(pi, []) if p_chunks: for ck in p_chunks: chapters_html += ( '' + _esc(ck["text"]) + ' ' ) else: # Paragraph has no aligned chunk — render plain text chapters_html += _esc(para.replace("\n", " ")) chapters_html += '

\n' else: # Fallback: no paragraph data — render chunks inline as before for ck in c_chunks: chapters_html += ( '' + _esc(ck["text"]) + ' ' ) chapters_html += "\n
\n
\n" sidebar_items = "\n".join( '
  • ' + _esc(ch["title"]) + '
  • ' 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)  |  " "`_WebBook.html`  |  `_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()