Skip to main content
Inspired by NotebookLM’s podcast generation feature and a recent open source implementation of Open Notebook LM. In this guide we will implement a walkthrough of how you can build a PDF to podcast pipeline. Given any PDF we will generate a conversation between a host and a guest discussing and explaining the contents of the PDF. In doing so we will learn the following:
  1. How we can use JSON mode and structured generation with open models like Llama 3 70b to extract a script for the Podcast given text from the PDF.
  2. How we can use TTS models to bring this script to life as a conversation.

Define Dialogue Schema with Pydantic

We need a way of telling the LLM what the structure of the podcast script between the guest and host will look like. We will do this using pydantic models. Below we define the required classes:
  • The overall conversation consists of lines said by either the host or the guest. The DialogueItem class specifies the structure of these lines.
  • The full script is a combination of multiple lines performed by the speakers, here we also include a scratchpad field to allow the LLM to ideate and brainstorm the overall flow of the script prior to actually generating the lines. The Dialogue class specifies this.
Python
The inclusion of a scratchpad field is very important - it allows the LLM compute and tokens to generate an unstructured overview of the script prior to generating a structured line by line enactment.

System Prompt for Script Generation

Next we need to define a detailed prompt template engineered to guide the LLM through the generation of the script. Feel free to modify and update the prompt below.
Python

Download PDF and Extract Contents

Here we will load in an academic paper that proposes the use of many open source language models in a collaborative manner together to outperform proprietary models that are much larger! We will use the text in the PDF as content to generate the podcast with!
Download the PDF file and then extract text contents using the function below.
Shell
Python

Generate Podcast Script using JSON Mode

Below we call Llama3.1 70B with JSON mode to generate a script for our podcast. JSON mode makes it so that the LLM will only generate responses in the format specified by the Script class. We will also be able to read its scratchpad and see how it structured the overall conversation.
Python
Above we are also handling the erroneous case which will let us know if the script was not generated following the Script class. Now we can have a look at the script that is generated:

Generate Podcast Using TTS

Below we read through the script and choose the TTS voice depending on the speaker. We define a speaker and guest voice id.
Python
We can loop through the lines in the script and generate them by a call to the TTS model with specific voice and lines configurations. The lines all appended to the same buffer and once the script finishes we write this out to a wav file, ready to be played.
Python
Once this code executes you will have a podcast.wav file saved on disk that can be played! If you’re ready to create your own PDF to podcast app like above sign up for Together AI today and make your first query in minutes!