4
votes

Je n'arrive pas à faire fonctionner google.cloud.texttospeech

J'utilise Python 3.8 et j'ai copié ce code comme test.

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

Voici le code affiché par google comme on peut le voir ici: GOOGLE LINK

Maintenant, mon problème est que j'obtiens cette erreur

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>

J'ai essayé de changer cela pour ajouter les informations d'identification à l'intérieur du code mais le problème persiste. Voici la ligne que j'ai modifiée:

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"')


2 commentaires

Pourquoi n'avez-vous pas modifié la ligne à l'origine de l'erreur?


J'ai essayé mais cela soulève une autre erreur XD, mais comme je l'ai dit, je l'ai copié à partir du site google, donc je suppose qu'il ne devrait pas avoir de problème


4 Réponses :


4
votes

Je pourrais résoudre cette erreur en rétrogradant la bibliothèque:
pip3 install "google-cloud-texttospeech<2.0.0"


2 commentaires

C'est littéralement le seul et unique résultat Google pour l'ensemble d'Internet qui apparaît pour cette erreur exacte si vous recherchez des correspondances exactes, et cela m'a sauvé deux fois. Merci beaucoup.


google-cloud-texttospeech 1.0.1 pyh9f0ad1d_0 conda-forge la même erreur que l'OP et ma version est <2.0: google-cloud-texttospeech 1.0.1 pyh9f0ad1d_0 conda-forge



1
votes

J'ai eu la même erreur lors de l'exécution de ce script, j'ai vérifié le code source et l'interface a changé, fondamentalement, vous devez supprimer tous les "enums" et "types". Cela ressemblera à ceci:

# 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"')


2 commentaires

J'obtiens la même erreur que l'OP en utilisant votre mod qui est en fait identique à la version github de quickstart.py. De plus, conformément à une réponse précédente, ma version est <2.0: google-cloud-texttospeech 1.0.1 pyh9f0ad1d_0 conda-forge


J'utilise quotidiennement un script presque identique depuis la date de cette réponse sans problème, que faire si vous mettez à jour vos dépendances? J'ai ceux-ci: python = "^ 3.8" google-api-python-client = "^ 1.10.0" google-cloud-texttospeech = "^ 2.1.0" grpcio = "^ 1.30.0"



2
votes

Je débogue le code et pour le faire fonctionner, je devais écrire des énumérations et des types en cas de besoin. Prenant l'exemple de la documentation Google de synthèse vocale et incluant quelques petits ajustements:

"""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"')

J'espère que cela fonctionne pour toi


0 commentaires

1
votes

Vous devez migrer vers la version 2.0, visitez le site ci-dessous pour plus de détails sur les modifications que vous devez apporter puisque vous avez probablement suivi un tutoriel en utilisant une ancienne version de texttospeech:

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)))

Je vais également inclure un exemple utilisant la version bêta de 2.0.0.

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


0 commentaires