Last active
January 17, 2018 17:19
-
-
Save anuragmishra1/4cfd0318700fd942e3cb68f5d427a63b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import time | |
from datetime import timedelta | |
import sys | |
import os | |
import argparse | |
#We need to get our API credentials in the code for authentication that we have stored as Environment Variables locally. | |
os.environ.get("AWS_ACCESS_KEY_ID") | |
os.environ.get("AWS_SECRET_ACCESS_KEY") | |
os.environ.get("AWS_REGION") | |
#Following line is used to save all the console outputs in a text file. | |
sys.stdout = open('polly_output.txt','a') | |
start_time = time.monotonic() | |
def input_file(text_file_path): | |
global text | |
if os.path.isfile(text_file_path): | |
with open(text_file_path, 'r') as text_file: | |
text = text_file.read() | |
else: | |
print("File doesn't exist in the directory!") | |
def polly_test(): | |
#Initialize amazon_polly client function | |
polly_service = boto3.client( | |
'polly', | |
) | |
#If we execute using full text input, the code throws Word Limit Exceeded Exception! | |
my_text = text[0:300] | |
polly_response = polly_service.synthesize_speech( | |
OutputFormat = 'mp3', | |
Text = my_text, | |
TextType = 'text', | |
VoiceId = 'Joanna' #English - American female voice | |
) | |
#Following code block saves the audio stream as an mp3 file | |
with open('polly_stream_test_text.mp3', 'wb') as audio_file: | |
audio_file.write(polly_response['AudioStream'].read()) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
description = __doc__, | |
formatter_class = argparse.RawDescriptionHelpFormatter) | |
parser.add_argument( | |
'text_file_path', | |
help = 'The complete file path of the text file you want to convert from text to speech.') | |
args = parser.parse_args() | |
input_file(args.text_file_path) | |
polly_test() | |
end_time = time.monotonic() | |
print("Execution_Time:", timedelta(seconds = end_time - start_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment