gpt4 book ai didi

python - 导入错误 : cannot import name 'texttospeech' from 'google.cloud' (unknown location)

转载 作者:行者123 更新时间:2023-12-05 00:56:02 25 4
gpt4 key购买 nike

尝试为我的不和谐机器人进行扩展,将用户文本的输入转换为音频文件,以便我的机器人可以播放该音频。我就是想不通为什么我不能导入 google 模块。

from google.cloud import texttospeech


def synthesize_text(text):

"""Synthesizes speech from the input string of text."""

client = texttospeech.TextToSpeechClient()

input_text = texttospeech.SynthesisInput(text=text)

# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().

voice = texttospeech.VoiceSelectionParams(

language_code="en-US",
name="en-US-Standard-C",
ssml_gender=texttospeech.SsmlVoiceGender.FEMALE,
)

audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)

response = client.synthesize_speech(request={"input": input_text, "voice": voice, "audio_config": audio_config})

# The response's audio_content is binary.

with open("output.mp3", "wb") as out:


out.write(response.audio_content)

print('Audio content written to file "output.mp3"')

最佳答案

如上面评论中所述,您需要先在您的机器上安装该库,然后才能在您的应用中调用该函数。

pip install google-cloud-texttospeech

Read through official documentation on usage if required.

根据您的代码,我相信您也检查了以下项目。

为了使用这个库,首先需要经过以下步骤:

  1. 选择或创建一个 Cloud Platform 项目。
  2. 为您的项目启用结算功能。
  3. 启用 Cloud Text-to-Speech API。
  4. 设置身份验证。

关于python - 导入错误 : cannot import name 'texttospeech' from 'google.cloud' (unknown location),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63065699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com