Spaces:
Sleeping
Sleeping
Constrain podcast script length via model prompt and rewrite pass
Browse files17 unmodified lines18192021222323 unmodified lines4748495051522 unmodified lines555657585960132 unmodified lines19319419519619719817 unmodified linesMODEL = "claude-haiku-4-5-20251001"MAX_TOKENS = 2048MAX_TTS_INPUT_CHARS = 4096# Use facebook/mms-tts-eng for both — it's free and reliable# We differentiate voices by slightly modifying the text (different speaking rates aren't23 unmodified lines- No stage directions, no asterisks, no markdown, no headers- Each line is one continuous paragraph — no line breaks within a turn- End with a closing exchange where both hosts wrap upExample format:Alex: Hey everyone, welcome back! Today we're diving into something really fascinating. Sam, want to kick us off?2 unmodified linesNow write the podcast script:"""def _parse_script_lines(raw: str) -> list[tuple[str, str]]:lines = []for line in raw.strip().splitlines():132 unmodified linesraw_script = response.content[0].text or ""lines = _parse_script_lines(raw_script)if not lines:raise ValueError("No speaker lines parsed from response.")17 unmodified lines1819202122232423 unmodified lines48495051525354552 unmodified lines58596061626364656667132 unmodified lines20020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223317 unmodified linesMODEL = "claude-haiku-4-5-20251001"MAX_TOKENS = 2048MAX_TTS_INPUT_CHARS = 4096TARGET_SCRIPT_CHARS = 3900# Use facebook/mms-tts-eng for both — it's free and reliable# We differentiate voices by slightly modifying the text (different speaking rates aren't23 unmodified lines- No stage directions, no asterisks, no markdown, no headers- Each line is one continuous paragraph — no line breaks within a turn- End with a closing exchange where both hosts wrap up- Keep the ENTIRE script under {TARGET_SCRIPT_CHARS} characters total (including speaker labels)- If needed, prioritize clarity and completeness over extra detail so the script ends naturallyExample format:Alex: Hey everyone, welcome back! Today we're diving into something really fascinating. Sam, want to kick us off?2 unmodified linesNow write the podcast script:"""def _script_char_len(lines: list[tuple[str, str]]) -> int:return len("\n".join([f"{speaker}: {text}" for speaker, text in lines]))def _parse_script_lines(raw: str) -> list[tuple[str, str]]:lines = []for line in raw.strip().splitlines():132 unmodified linesraw_script = response.content[0].text or ""lines = _parse_script_lines(raw_script)if lines and _script_char_len(lines) > TARGET_SCRIPT_CHARS:logger.warning("Podcast script exceeded target size (%d chars), requesting concise rewrite",_script_char_len(lines),)rewrite_prompt = (f"Rewrite this podcast script so it is <= {TARGET_SCRIPT_CHARS} characters total, ""keeps the same key points, and ends with a natural closing exchange.\n\n""RULES:\n""- Keep the same Alex/Sam format\n""- Every line must start with Alex: or Sam:\n""- No markdown or stage directions\n\n"f"SCRIPT TO REWRITE:\n{raw_script}")rewrite_response = client.messages.create(model=MODEL,max_tokens=MAX_TOKENS,system=("You are a podcast editor. Compress scripts while preserving meaning and flow. ""Output only Alex/Sam dialogue lines."),messages=[{"role": "user", "content": rewrite_prompt}],)rewritten = rewrite_response.content[0].text or ""rewritten_lines = _parse_script_lines(rewritten)if rewritten_lines:lines = rewritten_linesif not lines:raise ValueError("No speaker lines parsed from response.")
services/podcast_service.py+35