From 07886447658dbdeefbc8491fc53614f694b9405c Mon Sep 17 00:00:00 2001 From: Alvaro Martinez Llamojha Date: Fri, 10 Jun 2022 17:15:42 +0100 Subject: [PATCH] Add TTS for AWS Polly --- tts/aws_polly.py | 31 +++++++++++++++++++++++++++++++ video_creation/voices.py | 7 ++++++- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tts/aws_polly.py diff --git a/tts/aws_polly.py b/tts/aws_polly.py new file mode 100644 index 0000000..f16352e --- /dev/null +++ b/tts/aws_polly.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +from boto3 import Session +from botocore.exceptions import BotoCoreError, ClientError +import sys + +max_chars = 0 + +def run(text, filepath): + session = Session(profile_name="polly") + polly = session.client("polly") + + try: + # Request speech synthesis + response = polly.synthesize_speech(Text=text, OutputFormat="mp3", + VoiceId="Joanna", Engine = 'neural') + except (BotoCoreError, ClientError) as error: + # The service returned an error, exit gracefully + print(error) + sys.exit(-1) + + # Access the audio stream from the response + if "AudioStream" in response: + file = open(filepath, 'wb') + file.write(response['AudioStream'].read()) + file.close() + #print_substep(f"Saved Text {idx} to MP3 files successfully.", style="bold green") + + else: + # The response didn't contain audio data, exit gracefully + print("Could not stream audio") + sys.exit(-1) diff --git a/video_creation/voices.py b/video_creation/voices.py index 32bae88..79a6650 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -3,10 +3,15 @@ import os from utils.console import print_step, print_substep, print_table from tts.engine_wrapper import TTSEngine import tts.google_translate +import tts.aws_polly + ## Add your provider here in the dictionary below -TTSProviders = {"GoogleTranslate": tts.google_translate} +TTSProviders = { + "GoogleTranslate": tts.google_translate, + "AWSPolly": tts.aws_polly + } def save_text_to_mp3(reddit_obj):