TTS-Arena-JA / utils.py
alan
added google api
0414b49
raw
history blame
1.57 kB
import os
import json
import tempfile
from google.cloud import texttospeech
def get_credentials():
creds_json_str = os.getenv("GCP_CREDENTIAL_JSON") # get json credentials stored as a string
# create a temporary file
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") as temp:
temp.write(creds_json_str) # write in json format
temp_filename = temp.name
return temp_filename
def get_google_tts(text, local_filename):
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text=text)
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
language_code="ja-JP", 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(local_filename, "wb") as out:
# Write the response to the output file.
out.write(response.audio_content)
print(f'Audio content written to file {local_filename}')
return local_filename