user655941
user655941

Reputation: 397

I can't seem to make the google.cloud.texttospeech to work

Im using Python 3.8 and i copy pasted this code as a test.

from google.cloud import texttospeech

# Instantiates a client
client = texttospeech.TextToSpeechClient()

# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")

# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
    language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)

# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(
    audio_encoding=texttospeech.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(
    input=synthesis_input, voice=voice, audio_config=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"')

This is the code that is shown by google as can be seen here : GOOGLE LINK

Now my problem is that i get this error

PS C:\Users\User\Desktop> & C:/Users/User/AppData/Local/Programs/Python/Python38/python.exe "c:/Users/User/Desktop/from google.cloud import texttospeech.py"
Traceback (most recent call last):
  File "c:/Users/User/Desktop/from google.cloud import texttospeech.py", line 7, in <module>
    synthesis_input = texttospeech.types.SynthesisInput(text="Hello, World!")
AttributeError: module 'google.cloud.texttospeech' has no attribute 'types'
PS C:\Users\User\Desktop>

I tried changeing this to add the credentials inside the code but the problem persists. This is the line i changed:

client = texttospeech.TextToSpeechClient(credentials="VoiceAutomated-239f1c05600c.json")

Upvotes: 5

Views: 3605

Answers (5)

Mithlesh kumar
Mithlesh kumar

Reputation: 41

It will work Python 3.6 but it won't work with Python 3.7 with latest update of google-cloud-texttospeech. If you want us it with Python 3.7 Try the below code.

from google.cloud import texttospeech
def foo():
    client = texttospeech.TextToSpeechClient(credentials=your_google_creds_here)
    translated_text = Text


    synthesis_input = texttospeech.types.SynthesisInput(text=translated_text)
    pitch = 1
    speaking_rate = 1
    lang_code = 'en-us' # your_lang_code_hear
    gender = 'male'

    gender_data = {
                    'NEUTRAL': texttospeech.enums.SsmlVoiceGender.NEUTRAL,
                    'FEMALE': texttospeech.enums.SsmlVoiceGender.FEMALE,
                    'MALE': texttospeech.enums.SsmlVoiceGender.MALE
                }

    voice = texttospeech.types.VoiceSelectionParams(language_code=lang_code, ssml_gender=gender_data[gender.upper()])
    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.MP3, speaking_rate=float(speaking_rate), pitch=float(pitch)
    )

    print('Voice config and Audio config : ', voice, audio_config)
    response = client.synthesize_speech(
        synthesis_input, voice, audio_config)

Upvotes: 1

Orion
Orion

Reputation: 1

You need to migrate to version 2.0 visit the site below for details on the changes you need to make since you most likely followed a tutorial using an older version of texttospeech:

https://googleapis.dev/python/texttospeech/2.0.0/UPGRADING.html

I will also include an example using the beta version of 2.0.0.

import google.cloud.texttospeech_v1beta1 as ts
import time

nm = "en-US-Wavenet-I"
hz = 48000
def useTextToSpeech(speaking, lang, speed,stinger):

    client = ts.TextToSpeechClient()
    synthesis_input = ts.SynthesisInput(text=speaking)
    voice = ts.VoiceSelectionParams(

        language_code=lang,
        ssml_gender=ts.SsmlVoiceGender.MALE,
        name=nm,
    )
    audio_config = ts.AudioConfig(
        audio_encoding=ts.AudioEncoding.OGG_OPUS,
        speaking_rate=speed,
        pitch = 1.2,
        sample_rate_hertz=hz,
         effects_profile_id=['headphone-class-device' ],
    )
    response = client.synthesize_speech(
        request={
            "input": synthesis_input,
            "voice":voice,
            "audio_config":audio_config
                }
                                        )


    with open((stinger+'.opus'), 'wb') as out:
        out.write(response.audio_content)
        print('Audio content written to file  as "'+stinger+'.opus"')
    from playsound import playsound
    import os
    #playsound(os.path.abspath((stinger+'.opus')))
output = str("Make sure when you follow tutorials they are using the most up to date version of the Api!")
useTextToSpeech(output, "en-US-Wavenet-I",1.0,("example"+str(1)))

Upvotes: 0

jccastiblancor
jccastiblancor

Reputation: 21

I debug the code and to get it to work i had to write enums and types when needed. Taking the text to speech google documentation example and including some little adjusments:

"""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
import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./config/credentials.json"

# Instantiates a client
client = texttospeech.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(
    input_=synthesis_input, voice=voice, audio_config=audio_config
)

# The response's audio_content is binary.
with open("./output_tts/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"')

hope this works for you

Upvotes: 2

gamerkore
gamerkore

Reputation: 1115

I got the same error when running that script, i checked the source code and the interface has changed, basically you need to delete all "enums" and "types". It will look similar to this:

# Instantiates a client
client = texttospeech.TextToSpeechClient()

# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")

# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
    language_code='en-US',
    ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL)

# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(
    audio_encoding=texttospeech.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(input=synthesis_input, voice=voice, audio_config=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"')

Upvotes: 3

Daniel
Daniel

Reputation: 51

I could solve this error by downgrading the library:
pip3 install "google-cloud-texttospeech<2.0.0"

Upvotes: 5

Related Questions