Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from watson_developer_cloud import TextToSpeechV1
- 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
- TTS_USER_WATSON = os.environ.get("TTS_USER_WATSON")
- TTS_PASS_WATSON = os.environ.get("TTS_PASS_WATSON")
- #Following line is used to save all the console output into a text file
- sys.stdout = open('speech_api_test_text_tts_output.txt', 'w')
- 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 speech_tts():
- #Initialize TextToSpeech function using the API credentials
- tts = TextToSpeechV1(
- username = TTS_USER_WATSON,
- password = TTS_PASS_WATSON
- )
- #Parameters required to send the API request
- voice = 'en-US_AllisonVoice'
- accept = 'audio/wav'
- with open('watson_test_text_tts.wav', 'wb') as audio_file:
- audio_file.write(
- tts.synthesize(text, voice = voice, accept = accept)) #This line will synthesize the text file and convert it into audio format and generate a '.wav' format audio file
- 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)
- speech_tts()
- end_time = time.monotonic()
- print("Execution_Time:", timedelta(seconds = end_time - start_time))
Add Comment
Please, Sign In to add comment