One script Several Different Versions of Speech

API.audio allows you to create several different speech files from simply one script. With convenient personalisation parameters you can organise your content efficiently, version it (eg for personalisation or to make it dynamic) and arrange it so it is ready for production.

Let's look at an example of how to create several different speech files from a single script, in one go.

First, define a list. In this case we define a list of names to provide personalised audio tracks for each person.

# Create a list 

import aflr
aflr.api_key ="API_KEY"

names = ['Michael', 'Christopher', 'Jessica', 'Matthew', 'Ashley']

Now, let's create a script and define a {{username}} parameter.

# CREATE SCRIPT ONE TIME
script = aflr.Script().create(scriptText="You are almost at the finish line {{username}}")
print(script)

Retrieve the scriptId and store it. You can always create speech directly with the scriptId, only passing the username variable.

# GET THE SCRIPT ID FROM THE SCRIPT CREATED AND STORE IT
scriptId = script.get("scriptId")

Now, let's create speech for every placeholder in our list, for every name.

#CREATE SPEECH & RETRIEVE IN A LOOP TO CREATE SPEECH FOR EVERY PLACEHOLDER IN THE LIST
for name in names:
    response = aflr.Speech().create(scriptId=scriptId, audience=[{"username": name}])
    print(response)
    response = aflr.Speech().download(scriptId=scriptId, section="default", parameters={"username": name})
    print(response)

See complete example below:

import aflr
aflr.api_key ="API_KEY"


names = ['Michael', 'Christopher', 'Jessica', 'Matthew', 'Ashley']


# CREATE SCRIPT ONE TIME
script = aflr.Script().create(scriptText="You are almost at the finish line {{username}}")
print(script)

# GET THE SCRIPT ID FROM THE SCRIPT CREATED AND STORE IT
scriptId = script.get("scriptId")

#CREATE SPEECH & RETRIEVE IN A LOOP TO CREATE SPEECH FOR EVERY PLACEHOLDER IN THE LIST
for name in names:
    response = aflr.Speech().create(scriptId=scriptId, audience=[{"username": name}])
    print(response)
    response = aflr.Speech().download(scriptId=scriptId, section="default", parameters={"username": name})
    print(response)

Did this page help you?