Skip to content

Instantly share code, notes, and snippets.

@CPsridharCP
Created August 29, 2019 16:02
Show Gist options
  • Save CPsridharCP/a1114a8472f7c377b60e98c10d2a6fbc to your computer and use it in GitHub Desktop.
Save CPsridharCP/a1114a8472f7c377b60e98c10d2a6fbc to your computer and use it in GitHub Desktop.
polly = boto3.client('polly')
response = polly.synthesize_speech(OutputFormat='mp3',Text = text,VoiceId = "Salli")
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
audio = base64.encodebytes(stream.read())
out = str(audio.decode("ascii")).replace('\n', '')
logger.info(out)
logger.info(len(out))
try:
if len(out)>28000:
for count in range(int(len(out)/28000)):
iterRem = int(len(out)/28000)-count
if count == 0:
data = {'say': out[28000*count:28000*(count+1)], 'type': 'say', 'next' : count+1, 'text' : text }
else:
data = {'say': out[28000*count:28000*(count+1)], 'type': 'say', 'next' : count+1, 'text' : text }
_ = pubnub.publish().channel("<pubnub-channel>").message(data).pn_async(publish_callback)
time.sleep(0.5)
data = {'say': out[28000*(count+1):], 'type': 'say', 'next' : "None", 'text' : text}
_ = pubnub.publish().channel("<pubnub-channel>").message(data).pn_async(publish_callback)
time.sleep(0.5)
else:
data = {'say': out, 'type': 'say', 'next' : "None", 'text' : text}
_ = pubnub.publish().channel("<pubnub-channel>").message(data).pn_async(publish_callback)
time.sleep(0.5)
return {'statusCode': 200,'body' : json.dumps(text)}
except:
logger.info("FAIL IN LAMBDA TTS")
return {'statusCode': 200,'body' : json.dumps("Some Error in Lambda , Please Retry")}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment