Google Cloud: Setting Up Google Cloud Text-to-Speech

Share At:

Why You Should Use Text To Speech Services? - Gadget Advisor

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

  1. From the Google Cloud console’s main navigation, choose APIs & Services > Library.
  2. Search for “text”, and select Cloud Text-to-Speech API.
  1. If necessary, click Enable.

Set Up Service Account

  1. From the main navigation, choose IAM & admin > Service accounts.
  2. Click Create Service Account.
  3. Enter a name for the service account (ai-text-to-speech), and click Create.
  1. Skip choosing a role, and click Continue.
  1. Click Create Key.
  2. Set the Key type to JSON, and click Create.
  1. Save the key to your system, and click Close.
  2. Click Done.

Retrieve Working Files

  1. Activate the Cloud Shell.
  2. Retrieve the working files from Github:
git clone https://github.com/rajeevkghosh/content-gc-ai-services-deepdive.git
  1. In the Cloud Shell, change directories:
cd content-gc-ai-services-deepdive/ai-conversations/
  1. 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"
  }
}
  1. Now from your system, upload the stored JSON private key in the same directory and rename it to key.json.
  1. Validate the renamed service account key.

Send Request to Cloud Text-to-Speech API

  1. In the Cloud Shell enter the following command:
export GOOGLE_APPLICATION_CREDENTIALS=key.json
  1. 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 \
  1. In the Shell Editor, open synthesize-text.txt
  1. Remove the following from the beginning of the file:
{ "audioContent": "
  1. From the end of the file, remove the following:
" }
  1. Save the file. It should look something like below after modification.

Convert Response to MP3

  1. In the Cloud Shell, enter the following command:
base64 synthesize-text.txt --decode > synthesized-audio.mp3
  1. Download the MP3 file to your system:
cloudshell download synthesized-audio.mp3
  1. Click Download.
  1. Open the downloaded MP3 file to hear the results.
  1. Try to open the file. it should play the audio.
  1. You may download the file from below:

Congratulations !! You have just converted text to Speech using Google Cloud Speech API.

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Back To Top

Contact Us