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' "' 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