-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.py
46 lines (36 loc) · 1.32 KB
/
speech.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import boto3
import config
import logging
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[
logging.FileHandler("debug.log"),
logging.StreamHandler()
])
def process_speech_text(text):
#text = text.replace(" Laverty ", " Lav Verty ")
text = text.replace("&", "and")
return text
def create_audio(path, text):
logging.info("========== Creating Audio File From Text ==========")
logging.info("Path : " + path)
text = process_speech_text(text)
polly_client = boto3.Session(
aws_access_key_id=config.aws_access_key_id,
aws_secret_access_key=config.aws_secret_access_key,
region_name='us-west-2').client('polly')
polly_text = f'<speak><prosody rate="85%">{text}</prosody></speak>'
logging.info("Speech : " + polly_text)
response = polly_client.synthesize_speech(
Engine='neural',
OutputFormat='mp3',
Text=polly_text,
TextType='ssml',
VoiceId='Olivia'
)
file = open(path, 'wb')
file.write(response['AudioStream'].read())
file.close()
logging.info("========== Finished Creating Audio File From Text ==========")