-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWS-Polly.py
30 lines (27 loc) · 908 Bytes
/
AWS-Polly.py
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
from boto3 import Session
from botocore.exceptions import BotoCoreError, ClientError
from contextlib import closing
import os
import sys
import subprocess
from tempfile import gettempdir
session = Session(profile_name="default")
polly = session.client("polly")
try:
response = polly.synthesize_speech(Text="Hello world!", OutputFormat="mp3",
VoiceId="Joanna" )
except (BotoCoreError, ClientError) as error:
print(error)
sys.exit(-1)
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
output = os.path.join(gettempdir(), "/home/ubuntu/speech.mp3")
try:
with open(output, "wb") as file:
file.write(stream.read())
except IOError as error:
print(error)
sys.exit(-1)
else:
print("Could not stream audio")
sys.exit(-1)