pythontensorflowgoogle-cloud-platformgoogle-text-to-speechspeech-synthesis

Exception: synthesize_speech() takes from 1 to 2 positional arguments but 4 were given


I am new a coding tourist struggling to fix a chabot project that worked well in the past. The project is built on Python 3.7.0. It began a Tensorflow 1.15 but has been upgraded to Tensorflow 2.9.1. . When running google text to speech (TTS) I get the following error that I can not figure out how to fix. Any help you can offer is appreciate :

Exception: synthesize_speech() takes from 1 to 2 positional arguments but 4 were given

# <https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries>
# <https://cloud.google.com/text-to-speech/docs/basics>

import os
import config
import tempfile
#import simpleaudio as sa

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'assets/google.json'

from google.cloud import texttospeech_v1

client = texttospeech_v1.TextToSpeechClient()

voice = texttospeech_v1.VoiceSelectionParams(
    language_code='en-US',
    name='en-US-Wavenet-C',
    ssml_gender=texttospeech_v1.SsmlVoiceGender.FEMALE)
audio_config = texttospeech_v1.AudioConfig(
    audio_encoding=texttospeech_v1.AudioEncoding.LINEAR16, # LINEAR16=wav; or MP3
    **config.VOICE)

def say(text):
    """assumes ogg is the output format"""
    synthesis_input = texttospeech_v1.SynthesisInput(text=text)
    response = client.synthesize_speech(synthesis_input, voice, audio_config)
    tmpfile = tempfile.NamedTemporaryFile(delete=False)
    tmpfile.write(response.audio_content)
    wave_obj = sa.WaveObject.from_wave_file(tmpfile.name)
    audio = wave_obj.play()
    while audio.is_playing():
        continue

Solution

  • Instead of passing as positional arguments

    response = client.synthesize_speech(synthesis_input, voice, audio_config)
    

    pass as named arguments

    response = client.synthesize_speech(
        input=synthesis_input,
        voice=voice,
        audio_config=audio_config
    )
    

    the full signature of synthesize_speech is (source)

    def synthesize_speech(
        self,
        request: Union[cloud_tts.SynthesizeSpeechRequest, dict] = None,
        *,
        input: cloud_tts.SynthesisInput = None,
        voice: cloud_tts.VoiceSelectionParams = None,
        audio_config: cloud_tts.AudioConfig = None,
        retry: OptionalRetry = gapic_v1.method.DEFAULT,
        timeout: float = None,
        metadata: Sequence[Tuple[str, str]] = (),
    ) -> cloud_tts.SynthesizeSpeechResponse: