|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
import re |
|
import tempfile |
|
import textwrap |
|
from pathlib import Path |
|
from typing import List, Dict, Tuple, Optional |
|
|
|
import gradio as gr |
|
from huggingface_hub import InferenceClient, HubHTTPError |
|
from PyPDF2 import PdfReader |
|
from smolagents import HfApiModel |
|
|
|
|
|
|
|
|
|
llm = HfApiModel( |
|
model_id="Qwen/Qwen2.5-Coder-32B-Instruct", |
|
max_tokens=2048, |
|
temperature=0.5, |
|
) |
|
|
|
|
|
|
|
|
|
client = InferenceClient(token=os.getenv("HF_TOKEN", None)) |
|
|
|
|
|
|
|
|
|
|
|
LANG_INFO: Dict[str, Dict[str, str]] = { |
|
"en": {"name": "English", "tts_model": "facebook/mms-tts-eng"}, |
|
"bn": {"name": "Bangla", "tts_model": "facebook/mms-tts-ben"}, |
|
"zh": {"name": "Chinese", "tts_model": "facebook/mms-tts-zho"}, |
|
"ur": {"name": "Urdu", "tts_model": "facebook/mms-tts-urd"}, |
|
"ne": {"name": "Nepali", "tts_model": "facebook/mms-tts-npi"}, |
|
} |
|
LANG_CODE_BY_NAME = {info["name"]: code for code, info in LANG_INFO.items()} |
|
|
|
|
|
|
|
|
|
PROMPT_TEMPLATE = textwrap.dedent( |
|
""" |
|
You are producing a lively two‑host educational podcast in {lang_name}. |
|
Summarize the following lecture content into a dialogue of **≈300 words**. |
|
Make it engaging: hosts ask questions, clarify ideas with analogies, and |
|
wrap up with a concise recap. Preserve technical accuracy. |
|
|
|
### Lecture Content |
|
{content} |
|
""" |
|
) |
|
|
|
|
|
|
|
def extract_pdf_text(pdf_path: str) -> str: |
|
reader = PdfReader(pdf_path) |
|
return "\n".join(page.extract_text() or "" for page in reader.pages) |
|
|
|
TOKEN_LIMIT = 4000 |
|
|
|
|
|
def truncate_text(text: str, limit: int = TOKEN_LIMIT) -> str: |
|
words = text.split() |
|
return " ".join(words[:limit]) |
|
|
|
|
|
|
|
|
|
CHUNK_CHAR_LIMIT = 280 |
|
|
|
def _split_to_chunks(text: str, limit: int = CHUNK_CHAR_LIMIT) -> List[str]: |
|
|
|
sentences = re.split(r"(?<=[.!?])\s+", text.strip()) |
|
chunks, current = [], "" |
|
for sent in sentences: |
|
if len(current) + len(sent) + 1 > limit: |
|
if current: |
|
chunks.append(current.strip()) |
|
current = sent |
|
else: |
|
current += " " + sent if current else sent |
|
if current: |
|
chunks.append(current.strip()) |
|
return chunks |
|
|
|
|
|
def synthesize_speech(text: str, model_id: str, tmpdir: Path) -> Path: |
|
"""Stream chunks through HF TTS and concatenate FLAC bytes.""" |
|
chunks = _split_to_chunks(text) |
|
flac_paths: List[Path] = [] |
|
for idx, chunk in enumerate(chunks): |
|
try: |
|
audio_bytes = client.text_to_speech(chunk, model=model_id) |
|
except HubHTTPError as e: |
|
raise RuntimeError(f"TTS request failed: {e}") from e |
|
part_path = tmpdir / f"part_{idx}.flac" |
|
part_path.write_bytes(audio_bytes) |
|
flac_paths.append(part_path) |
|
|
|
|
|
|
|
final_path = tmpdir / "podcast.flac" |
|
with open(final_path, "wb") as fout: |
|
for p in flac_paths: |
|
fout.write(p.read_bytes()) |
|
return final_path |
|
|
|
|
|
|
|
|
|
|
|
def generate_podcast(pdf: gr.File, selected_lang_names: List[str]): |
|
if not selected_lang_names: |
|
raise gr.Error("Please select at least one language.") |
|
|
|
selected_codes = [LANG_CODE_BY_NAME[name] for name in selected_lang_names] |
|
results: List[Optional[Tuple[str, None]]] = [] |
|
|
|
with tempfile.TemporaryDirectory() as td: |
|
tmpdir = Path(td) |
|
lecture_raw = extract_pdf_text(pdf.name) |
|
lecture_text = truncate_text(lecture_raw) |
|
|
|
for code, info in LANG_INFO.items(): |
|
if code not in selected_codes: |
|
results.append(None) |
|
continue |
|
|
|
|
|
prompt = PROMPT_TEMPLATE.format(lang_name=info["name"], content=lecture_text) |
|
dialogue: str = llm(prompt) |
|
|
|
|
|
tts_path = synthesize_speech(dialogue, info["tts_model"], tmpdir / code) |
|
|
|
results.append((str(tts_path), None)) |
|
|
|
return results |
|
|
|
|
|
|
|
|
|
language_choices = [info["name"] for info in LANG_INFO.values()] |
|
|
|
inputs = [ |
|
gr.File(label="Upload Lecture PDF", file_types=[".pdf"]), |
|
gr.CheckboxGroup( |
|
choices=language_choices, |
|
value=["English"], |
|
label="Select podcast language(s) to generate", |
|
), |
|
] |
|
|
|
outputs = [ |
|
gr.Audio(label=f"{info['name']} Podcast", type="filepath") for info in LANG_INFO.values() |
|
] |
|
|
|
iface = gr.Interface( |
|
fn=generate_podcast, |
|
inputs=inputs, |
|
outputs=outputs, |
|
title="Lecture → Podcast Generator (Choose Languages)", |
|
description=( |
|
"Upload a lecture PDF, choose language(s), and receive a two‑host " |
|
"audio podcast. Dialogue comes from Qwen‑32B; speech is streamed " |
|
"via the HF Inference API using open MMS‑TTS models. Long texts are " |
|
"automatically chunked to fit API limits." |
|
), |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|