This guide will walk you through generating a conversational-style podcast from a PDF using the PlayNote API. We’ll use the PlayNote API to take a PDF source file, synthesize it into an audio conversation between two voices, and retrieve the generated podcast URL.

Prerequisites

Before you start, ensure you have the following:

  • Access your credentials (API key and user ID)
  • Development environment for your chosen programming language
  • Python’s requests library installed (pip install requests)

Steps

1

Set Up Environment Variables

Add your API key and user ID to your environment variables.

echo 'export PLAYAI_API_KEY="your_api_key_here"' >> ~/.zshrc
echo 'export PLAYAI_USER_ID="your_user_id_here"' >> ~/.zshrc
source ~/.zshrc
2

Configure API Access

Create a script with the following authentication setup:

import requests
import os

# Define the URL of your PDF file
SOURCE_FILE_URL = "https://godinton.kent.sch.uk/media/2601/goldilocks-story.pdf"

# PlayNote API URL
url = "https://api.play.ai/api/v1/playnotes"

# Retrieve API key and User ID from environment variables
api_key = os.getenv("PLAYAI_API_KEY")
user_id = os.getenv("PLAYAI_USER_ID")

# Set up headers with authorization details
headers = {
    'AUTHORIZATION': api_key,
    'X-USER-ID': user_id,
    'accept': 'application/json'
}
3

Send Request to Generate PlayNote

Configure and send the request to create your podcast:

# Configure the request parameters
files = {
    'sourceFileUrl': (None, SOURCE_FILE_URL),
    'synthesisStyle': (None, 'podcast'),
    'voice1': (None, 's3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json'),
    'voice1Name': (None, 'Angelo'),
    'voice2': (None, 's3://voice-cloning-zero-shot/e040bd1b-f190-4bdb-83f0-75ef85b18f84/original/manifest.json'),
    'voice2Name': (None, 'Deedee'),
}

# Send the POST request
response = requests.post(url, headers=headers, files=files)

if response.status_code == 201:
    print("Request sent successfully!")
    playNoteId = response.json().get('id')
    print(f"Generated PlayNote ID: {playNoteId}")
else:
    print(f"Failed to generate PlayNote: {response.text}")
4

Poll for Completion

Check the status of your PlayNote generation:

import urllib.parse
import time

# Double-encode the PlayNote ID for the URL
double_encoded_id = urllib.parse.quote(playNoteId, safe='')

# Construct the final URL to check the status
status_url = f"https://api.play.ai/api/v1/playnotes/{double_encoded_id}"

# Poll for completion
while True:
    response = requests.get(status_url, headers=headers)
    if response.status_code == 200:
        playnote_data = response.json()
        status = playnote_data['status']
        if status == 'completed':
            print("PlayNote generation complete!")
            print("Audio URL:", playnote_data['audioUrl'])
            break
        elif status == 'generating':
            print("Please wait, your PlayNote is still generating...")
            time.sleep(120)  # Wait for 2 minutes before polling again
        else:
            print("PlayNote creation failed, please try again.")
            break
    else:
        print(f"Error polling for PlayNote status: {response.text}")
        break
5

Run and Test

Follow these steps to run your code:

  1. Save your code as playnote_generator.py
  2. Open terminal in your code directory
  3. Run: python3 playnote_generator.py
  4. Wait for the generation process to complete
  5. Access your generated audio using the provided URL

Complete Code

import requests
import os
import urllib.parse
import time

# Define the URL of your PDF file
SOURCE_FILE_URL = "https://godinton.kent.sch.uk/media/2601/goldilocks-story.pdf"

# PlayNote API URL
url = "https://api.play.ai/api/v1/playnotes"

# Retrieve API key and User ID from environment variables
api_key = os.getenv("PLAYAI_API_KEY")
user_id = os.getenv("PLAYAI_USER_ID")

# Set up headers with authorization details
headers = {
    'AUTHORIZATION': api_key,
    'X-USER-ID': user_id,
    'accept': 'application/json'
}

# Configure the request parameters
files = {
    'sourceFileUrl': (None, SOURCE_FILE_URL),
    'synthesisStyle': (None, 'podcast'),
    'voice1': (None, 's3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json'),
    'voice1Name': (None, 'Angelo'),
    'voice2': (None, 's3://voice-cloning-zero-shot/e040bd1b-f190-4bdb-83f0-75ef85b18f84/original/manifest.json'),
    'voice2Name': (None, 'Deedee'),
}

# Send the POST request
response = requests.post(url, headers=headers, files=files)

if response.status_code == 201:
    print("Request sent successfully!")
    playNoteId = response.json().get('id')
    print(f"Generated PlayNote ID: {playNoteId}")

    # Double-encode the PlayNote ID for the URL
    double_encoded_id = urllib.parse.quote(playNoteId, safe='')

    # Construct the final URL to check the status
    status_url = f"https://api.play.ai/api/v1/playnotes/{double_encoded_id}"

    # Poll for completion
    while True:
        response = requests.get(status_url, headers=headers)
        if response.status_code == 200:
            playnote_data = response.json()
            status = playnote_data['status']
            if status == 'completed':
                print("PlayNote generation complete!")
                print("Audio URL:", playnote_data['audioUrl'])
                break
            elif status == 'generating':
                print("Please wait, your PlayNote is still generating...")
                time.sleep(120)  # Wait for 2 minutes before polling again
            else:
                print("PlayNote creation failed, please try again.")
                break
        else:
            print(f"Error polling for PlayNote status: {response.text}")
            break
else:
    print(f"Failed to generate PlayNote: {response.text}")

Troubleshooting

If you encounter issues, check these common problems:

  • Authentication Issues:

    • Verify your API key and user ID are correctly set in your environment
    • Confirm the AUTHORIZATION header is properly formatted
  • Source File Issues:

    • Ensure your PDF URL is publicly accessible
    • Verify the PDF file is not corrupted or password-protected
  • Generation Time:

    • The process typically takes 5-10 minutes depending on the PDF size
    • If the status remains “generating” for an extended period, try creating a new request
  • API Endpoint Errors:

    • Verify you’re using the correct PlayNote API endpoint
    • Check that your request payload matches the expected format

This guide provides a simple yet powerful way to turn text content from a PDF into a rich, conversational podcast format using PlayNote API. Modify the voice parameters to customize the conversation to match your desired style.