How I built my own custom AI-generated podcast using AWS Bedrock, Polly, and Python
Building your own automated podcasts is now easy with AWS generative AI
1
2
3
4
5
6
soup = BeautifulSoup(aws_feed.text, features='xml')
rss_items = soup.find_all('item')
for item in rss_items:
category = item.find('category').text
if 'serverless' in category:
post_link = item.find('link').text
1
2
3
4
5
6
post_page = requests.get(post_link)
post_soup = BeautifulSoup(post_page.text, "html.parser")
text_boxes = post_soup.find("main", {"id": "aws-page-content-main"}).find_all("div", {"class": "aws-text-box"})
for box in text_boxes:
prompt += box.text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
bedrock_client = boto3.client('bedrock-runtime', region_name="us-east-1")
body = json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 8000,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
}
]
}
],
"system": system_message
})
response = bedrock_client.invoke_model(
body=body,
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
accept="application/json",
contentType="application/json"
)
script = json.loads(response['body'].read().decode('utf-8'))["content"][0]["text"]
full_podcast = intro_spiel
for line in script.splitlines():
if "Quinn: " in line:
full_podcast.append(["Ruth", line.split("Quinn: ")[1].strip()])
if "Ashcroft: " in line:
full_podcast.append(["Gregory", line.split("Ashcroft: ")[1].strip()])
pydub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
polly_client = boto3.client('polly', region_name="us-east-1")
i=1
for voice, text in full_podcast:
print(voice, text)
speech_file_path = Path(__file__).parent / f"{str(i)}.mp3"
print(speech_file_path)
print("sending request")
response = polly_client.synthesize_speech(
Engine="long-form",
OutputFormat='mp3',
Text=text,
TextType='text',
VoiceId=voice,
)
with open(speech_file_path, 'wb') as file:
file.write(response['AudioStream'].read())
i+=1
print(i)
full_pod = AudioSegment.from_mp3(Path(__file__).parent / "1.mp3")
os.remove(Path(__file__).parent / "1.mp3")
for pos in range(2, i):
full_pod += AudioSegment.from_mp3(Path(__file__).parent / f"{str(pos)}.mp3")
os.remove(Path(__file__).parent / f"{str(pos)}.mp3")
full_pod.export(pod_file_name, format="mp3")