
Description
Although there are many tools to communicate, there are two basic types of communication: written and spoken. Machine learning has made it possible to convert one to the other, resulting in speech that is very close to that of a human voice. In this hands-on lab, you’ll step through the process for utilizing the Google Cloud Text-to-Speech API, transforming text in a JSON format to an audio-ready MP3 file.
Enable the Cloud Text-to-Speech API
- From the Google Cloud console’s main navigation, choose APIs & Services > Library.
- Search for “text”, and select Cloud Text-to-Speech API.

- If necessary, click Enable.
Set Up Service Account
- From the main navigation, choose IAM & admin > Service accounts.
- Click Create Service Account.
- Enter a name for the service account (
ai-text-to-speech
), and click Create.

- Skip choosing a role, and click Continue.

- Click Create Key.
- Set the Key type to JSON, and click Create.

- Save the key to your system, and click Close.
- Click Done.
Retrieve Working Files
- Activate the Cloud Shell.
- Retrieve the working files from Github:
git clone https://github.com/rajeevkghosh/content-gc-ai-services-deepdive.git

- In the Cloud Shell, change directories:
cd content-gc-ai-services-deepdive/ai-conversations/

- Review code:

text-to-speech-request.json
{
"input":{
"text":"India, officially the Republic of India is a country in South Asia. It is the seventh-largest country by area, the second-most populous country, and the most populous democracy in the world. "
},
"voice":{
"languageCode":"en-au",
"name":"en-AU-Wavenet-B",
"ssmlGender":"MALE"
},
"audioConfig":{
"audioEncoding":"MP3"
}
}
- Now from your system, upload the stored JSON private key in the same directory and rename it to key.json.

- Validate the renamed service account key.

Send Request to Cloud Text-to-Speech API
- In the Cloud Shell enter the following command:
export GOOGLE_APPLICATION_CREDENTIALS=key.json
- Call the Cloud Text-to-Speech API:
curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @text-to-speech-request.json \
https://texttospeech.googleapis.com/v1/text:synthesize \
> synthesize-text.txt \

- In the Shell Editor, open
synthesize-text.txt

- Remove the following from the beginning of the file:
{ "audioContent": "
- From the end of the file, remove the following:
" }
- Save the file. It should look something like below after modification.

Convert Response to MP3
- In the Cloud Shell, enter the following command:
base64 synthesize-text.txt --decode > synthesized-audio.mp3

- Download the MP3 file to your system:
cloudshell download synthesized-audio.mp3
- Click Download.

- Open the downloaded MP3 file to hear the results.

- Try to open the file. it should play the audio.

- You may download the file from below:
Thank you for your shening. I am worried that I lack creative ideas. It is your enticle that makes me full of hope. Thank you. But, I have a question, can you help me? https://accounts.binance.com/en/register?ref=P9L9FQKY
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.