Я хотел бы создать экземпляр TextToSpeechClient()
с помощью ключа API, а не учетных данных учетной записи службы, предоставляемых в глобальной переменной GOOGLE_APPLICATION_CREDENTIALS
. В частности, я хотел бы использовать следующую функцию, представленную на соответствующем веб-сайте, но я не могу создать экземпляр client
из-за отсутствия учетных данных.
def synthesize_text(text):
"""Synthesizes speech from the input string of text."""
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.types.SynthesisInput(text=text)
# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
name='en-US-Standard-C',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
response = client.synthesize_speech(input_text, voice, 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"')
С другой стороны, я справился с проблемой учетных данных, когда попытался использовать JSON с веб-сайта Google для преобразования текста в речь по адресу https://cloud.google.com/text-to-speech и пакет requests
следующим образом:
r = requests.post('https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=API_KEY', data = json.dumps(data))
и я получаю ответ с текстовым содержанием. Если бы я должен был сделать это таким образом, как я могу сохранить полученное содержимое в виде аудиофайла?