def test_list_voices_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = texttospeech_v1.TextToSpeechClient(channel=channel) with pytest.raises(CustomException): client.list_voices()
def text_to_speech(): with open("introduction.txt", "r") as intro_ssml: ssml_text = intro_ssml.read() input = texttospeech.types.SynthesisInput(ssml=ssml_text) # voice = texttospeech.types.VoiceSelectionParams( # language_code='uk-UA', # name='uk-UA-Wavenet-A', # ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE) voice = texttospeech.types.VoiceSelectionParams( language_code='en-US', # name='uk-UA-Wavenet-A', ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE) audio_config = texttospeech.types.AudioConfig(audio_encoding=texttospeech.enums.AudioEncoding.MP3) client = texttospeech.TextToSpeechClient() # print(client.list_voices()) response = client.synthesize_speech(input, voice, audio_config) with open('output.mp3', 'wb') as out: # Write the response to the output file. out.write(response.audio_content) print('Audio content written to file "output.mp3"')
def run_quickstart(): # [START tts_quickstart] """Synthesizes speech from the input string of text or ssml. Note: ssml must be well-formed according to: https://www.w3.org/TR/speech-synthesis/ """ from google.cloud import texttospeech_v1 # Instantiates a client client = texttospeech_v1.TextToSpeechClient() # Set the text input to be synthesized synthesis_input = texttospeech.types.SynthesisInput(text="Hello, World!") # Build the voice request, select the language code ("en-US") and the ssml # voice gender ("neutral") voice = texttospeech.types.VoiceSelectionParams( language_code='en-US', ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL) # Select the type of audio file you want returned audio_config = texttospeech.types.AudioConfig( audio_encoding=texttospeech.enums.AudioEncoding.MP3) # Perform the text-to-speech request on the text input with the selected # voice parameters and audio file type response = client.synthesize_speech(synthesis_input, voice, audio_config) # The response's audio_content is binary. with open('output.mp3', 'wb') as out: # Write the response to the output file. out.write(response.audio_content) print('Audio content written to file "output.mp3"')
def test_synthesize_speech(self): # Setup Expected Response audio_content = b'16' expected_response = {'audio_content': audio_content} expected_response = cloud_tts_pb2.SynthesizeSpeechResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = texttospeech_v1.TextToSpeechClient() # Setup Request input_ = {} voice = {} audio_config = {} response = client.synthesize_speech(input_, voice, audio_config) assert expected_response == response assert len(channel.requests) == 1 expected_request = cloud_tts_pb2.SynthesizeSpeechRequest( input=input_, voice=voice, audio_config=audio_config) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_voices_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = texttospeech_v1.TextToSpeechClient() with pytest.raises(CustomException): client.list_voices()
def test_synthesize_speech_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = texttospeech_v1.TextToSpeechClient(channel=channel) # Setup request input_ = {} voice = {} audio_config = {} with pytest.raises(CustomException): client.synthesize_speech(input_, voice, audio_config)
def test_synthesize_speech(self): client = texttospeech_v1.TextToSpeechClient() synthesis_input = texttospeech_v1.types.SynthesisInput( text="Hello, World!") voice = texttospeech_v1.types.VoiceSelectionParams( language_code="en-US", ssml_gender=texttospeech_v1.enums.SsmlVoiceGender.NEUTRAL, ) audio_config = texttospeech_v1.types.AudioConfig( audio_encoding=texttospeech_v1.enums.AudioEncoding.MP3) response = client.synthesize_speech(synthesis_input, voice, audio_config) assert response.audio_content is not None
def test_synthesize_speech_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = texttospeech_v1.TextToSpeechClient() # Setup request input_ = {} voice = {} audio_config = {} with pytest.raises(CustomException): client.synthesize_speech(input_, voice, audio_config)
def test_list_voices(self): # Setup Expected Response expected_response = {} expected_response = cloud_tts_pb2.ListVoicesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = texttospeech_v1.TextToSpeechClient(channel=channel) response = client.list_voices() assert expected_response == response assert len(channel.requests) == 1 expected_request = cloud_tts_pb2.ListVoicesRequest() actual_request = channel.requests[0][1] assert expected_request == actual_request
def __init__(self, voice_name="en-US-Wavenet-D"): self._channel = TextToSpeechGrpcTransport.create_channel( options=[("grpc.max_receive_message_length", 24 * 1024 * 1024)] ) self._transport = TextToSpeechGrpcTransport(channel=self._channel) self.client = texttospeech_v1.TextToSpeechClient(transport=self._transport) self.voice = texttospeech.VoiceSelectionParams( language_code="en-US", name=voice_name ) self.audio_config = texttospeech.AudioConfig( audio_encoding=texttospeech.AudioEncoding.LINEAR16 ) # rate limit stuff self._minute = -1 self._requests_this_minute = 0 self._chars_this_minute = 0 self._chunk_counter = 1 self._coverfile = '' self._used_characters = 0
def useTextToSpeech(speaking, lang, speed): from google.cloud import texttospeech_v1 as texttospeech client = texttospeech.TextToSpeechClient() synthesis_input = texttospeech.types.SynthesisInput(text=speaking) voice = texttospeech.types.VoiceSelectionParams( language_code=lang, ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE, ) audio_config = texttospeech.types.AudioConfig( audio_encoding=texttospeech.enums.AudioEncoding.MP3, speaking_rate=speed) response = client.synthesize_speech(synthesis_input, voice, audio_config) with open('output.mp3', 'wb') as out: out.write(response.audio_content) # print('Audio content written to file "output.mp3"') from playsound import playsound import os playsound(os.path.abspath('output.mp3'))
def test_list_voices(self): # Setup Expected Response expected_response = {} expected_response = cloud_tts_pb2.ListVoicesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = texttospeech_v1.TextToSpeechClient() response = client.list_voices() assert expected_response == response assert len(channel.requests) == 1 expected_request = cloud_tts_pb2.ListVoicesRequest() actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_synthesize_speech(self): # Setup Expected Response audio_content = b'16' expected_response = {'audio_content': audio_content} expected_response = cloud_tts_pb2.SynthesizeSpeechResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = texttospeech_v1.TextToSpeechClient(channel=channel) # Setup Request input_ = {} voice = {} audio_config = {} response = client.synthesize_speech(input_, voice, audio_config) assert expected_response == response assert len(channel.requests) == 1 expected_request = cloud_tts_pb2.SynthesizeSpeechRequest( input=input_, voice=voice, audio_config=audio_config) actual_request = channel.requests[0][1] assert expected_request == actual_request
Note: ssml must be well-formed according to: https://www.w3.org/TR/speech-synthesis/ """ # export GOOGLE_APPLICATION_CREDENTIALS='/home/luolu/Downloads/my-project-82452-txt2speech-d2f355055141.json' # export GOOGLE_CLOUD_PROJECT="my-project-82452-txt2speech" # export GCLOUD_PROJECT=$GOOGLE_CLOUD_PROJECT from google.cloud import texttospeech from google.cloud import texttospeech_v1 from google.cloud import texttospeech_v1beta1 import os # os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/home/luolu/PycharmProjects/video-processing/pqAmazon/my-project-82452-txt2speech-32250ea466e8.json" os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/home/luolu/Downloads/my-project-82452-txt2speech-d2f355055141.json" # Instantiates a client client = texttospeech_v1.TextToSpeechClient() print("client list: ", client.list_voices()) with open("/home/luolu/Desktop/amazontest/test/B07F81VDL2.txt", 'r') as file: txt_data = file.read().replace('\n', '') # Set the text input to be synthesized synthesis_input = texttospeech_v1.SynthesisInput(text=txt_data) # Build the voice request, select the language code ("en-US") and the ssml # voice gender ("neutral") voice = texttospeech_v1.VoiceSelectionParams( language_code="en-US", name="en-US-Wavenet-E", ssml_gender=texttospeech_v1.SsmlVoiceGender.FEMALE )
from ebooklib import epub # Google Cloud Imports from google.cloud import texttospeech from google.cloud import texttospeech_v1 # NOTE: This is here to resolve the issues with the ProtoBuf size-restrictions with the Python implementation of the # Google Cloud API and will hopefully not be required with future releases of this API # See: https://github.com/googleapis/python-texttospeech/issues/5 from google.cloud.texttospeech_v1.services.text_to_speech.transports.grpc import ( TextToSpeechGrpcTransport, ) channel = TextToSpeechGrpcTransport.create_channel( options=[("grpc.max_receive_message_length", 24 * 1024 * 1024)]) transport = TextToSpeechGrpcTransport(channel=channel) client = texttospeech_v1.TextToSpeechClient(transport=transport) def TextToSpeech(voice_name, text): """ Processes the input text and returns the wav-encoded audio""" # TODO Check the Voice, OutputFilename and Text meet requirements language_code = "-".join(voice_name.split("-")[:2]) text_input = texttospeech_v1.SynthesisInput(text=text) voice_params = texttospeech_v1.VoiceSelectionParams( language_code=language_code, name=voice_name) audio_config = texttospeech_v1.AudioConfig( audio_encoding=texttospeech.AudioEncoding.LINEAR16) response = client.synthesize_speech(input=text_input, voice=voice_params, audio_config=audio_config)
MINIO_HOST = os.environ.get('MINIO_HOST', None) MINIO_ACCESS_KEY = os.environ.get('MINIO_ACCESS_KEY', None) MINIO_SECRET_KEY = os.environ.get('MINIO_SECRET_KEY', None) MINIO_BUCKET = os.environ.get('MINIO_BUCKET', None) if MINIO_HOST: minioClient = Minio(MINIO_HOST, access_key=MINIO_ACCESS_KEY, secret_key=MINIO_SECRET_KEY, secure=True) else: minioClient = None GOOGLE_APPLICATION_CREDENTIALS = os.environ.get( "GOOGLE_APPLICATION_CREDENTIALS", None) if GOOGLE_APPLICATION_CREDENTIALS: TTSClient = tts.TextToSpeechClient() STTClient = stt.SpeechClient() else: TTSClient = None STTClient = None # Global Variables COUNT_MSG = 0 USERS = {} COUNT_PM = {} LASTMSG = {} ENABLE_KILLME = True
def test_list_voices(self): client = texttospeech_v1.TextToSpeechClient() voices = client.list_voices() assert len(voices.voices) > 0
SECTION_BREAK = 2 # sec CAPTION_BREAK = 1.5 # sec # prediction labels LABEL_BODY = "body" LABEL_HEADER = "header" LABEL_CAPTION = "caption" LABEL_OTHER = "other" FEATURE_CSV_HEADER = ( "id,text,chars,width,height,area,char_size,pos_x,pos_y,aspect,layout") # ML API clients project_id = os.environ.get("GCLOUD_PROJECT") # os.environ["GCP_PROJECT"] vision_client = vision.ImageAnnotatorClient() storage_client = storage.Client() speech_client = texttospeech.TextToSpeechClient() client_options = {'api_endpoint': 'eu-automl.googleapis.com:443'} automl_client = automl.TablesClient(project=project_id, region=compute_region, client_options=client_options) def p2a_gcs_trigger(file, context): # get bucket and blob file_name = file["name"] bucket = None file_blob = None while bucket == None or file_blob == None: # retry bucket = storage_client.get_bucket(file["bucket"]) file_blob = bucket.get_blob(file_name)
from google.cloud import texttospeech_v1 as texttospeech speech_api = texttospeech.TextToSpeechClient() def text_to_audio_file(text, audio_filename): """ Convert `text` into an audio file, written at `audio_filename` """ input_text = texttospeech.types.SynthesisInput(text=text) voice = texttospeech.types.VoiceSelectionParams(language_code='en-US', name='en-US-Wavenet-F') audio_config = texttospeech.types.AudioConfig( speaking_rate=1.3, audio_encoding=texttospeech.enums.AudioEncoding.MP3) response = speech_api.synthesize_speech(input_text, voice, audio_config) with open(audio_filename, 'wb') as out: out.write(response.audio_content)