Implement Speech and Language Solutions with Pre-trained APIs

Solution for Implement Speech and Language Solutions with Pre-trained APIs. 1 lab: ARC132. Fast copy-paste commands for Google Cloud.

ARC132 — Implement Speech and Language Solutions with Pre-trained APIs: Challenge Lab

Estimated time: 15 minutes

# Cloud Speech API Challenge Lab This Challenge Lab assigns **random file names, API keys, and foreign language sentences** to every learner. Follow the execution sequence below exactly as shown. ## 🚀 Step-by-Step Instructions ### 🔹 Step 1 — Generate Your API Key (Manual) Before running any commands: 1. Open the **Google Cloud Console**. 2. Navigate to: - **Navigation Menu → APIs & Services → Credentials** 3. Click: - **Create Credentials → API Key** 4. Copy the generated **API Key*

clear
CYAN='\033[0;96m'
GREEN='\033[0;92m'
YELLOW='\033[0;93m'
BOLD='\033[1m'
RESET='\033[0m'

echo -e "${CYAN}${BOLD}"
echo "    ____        _    _ _            __  ___             "
echo "   / __ \      | |  (_) |          / _| / _ \             "
echo "  | |  | |_ __ | |__  _| |_  ___   | |_ | | | |_ __ ___  "
echo "  | |  | | '__| '_ \| | __| / _ \  |  _|| | | | '_ \ / __| "
echo "  | |__| | |  | |_) | | |_ | (_) | | |  | |_| | |_) \__ \ "
echo "   \____/|_|  |_.__/|_|\__| \___/  |_|   \___/| .__/|___/ "
echo "                                              | |         "
echo "                                              |_|         "
echo -e "${RESET}"

echo -e "${YELLOW}${BOLD}▶ Please enter the exact values from your lab manual:${RESET}"
read -p 

\033[0;96m\033[1mEnter Task 2 Output File Name: \033[0m' TASK_2_FILE
read -p 

\033[0;96m\033[1mEnter Task 3 Request File Name: \033[0m' TASK_3_REQ
read -p 

\033[0;96m\033[1mEnter Task 3 Response File Name: \033[0m' TASK_3_RES
read -p 

\033[0;96m\033[1mEnter Task 4 Sentence to Translate: \033[0m' TASK_4_SENTENCE
read -p 

\033[0;96m\033[1mEnter Task 4 Output File Name: \033[0m' TASK_4_FILE
read -p 

\033[0;96m\033[1mEnter Task 5 Sentence to Detect: \033[0m' TASK_5_SENTENCE
read -p 

\033[0;96m\033[1mEnter Task 5 Output File Name: \033[0m' TASK_5_FILE

export TASK_2_FILE TASK_3_REQ TASK_3_RES TASK_4_SENTENCE TASK_4_FILE TASK_5_SENTENCE TASK_5_FILE
echo -e "\n${GREEN}${BOLD}✓ Variables saved securely. You can now run Command 2.${RESET}"
CYAN='\033[0;96m'
GREEN='\033[0;92m'
YELLOW='\033[0;93m'
BOLD='\033[1m'
RESET='\033[0m'

echo -e "\n${CYAN}${BOLD}▶ Orbit of Ops: Initiating AI API Automation...${RESET}"

echo -e "${YELLOW}▶ Enabling required API services...${RESET}"
gcloud services enable apikeys.googleapis.com \
    speech.googleapis.com \
    texttospeech.googleapis.com \
    translate.googleapis.com

echo -e "${YELLOW}▶ Creating API Key with required restrictions...${RESET}"
gcloud services api-keys create --display-name="arc132-key" \
    --api-target=service=speech.googleapis.com \
    --api-target=service=texttospeech.googleapis.com \
    --api-target=service=translate.googleapis.com

echo -e "${YELLOW}▶ Waiting for key creation (10s)...${RESET}"
sleep 10

KEY_NAME=$(gcloud services api-keys list --format="value(name)" --filter="displayName=arc132-key" --limit=1)
export API_KEY=$(gcloud services api-keys get-key-string "$KEY_NAME" --format="value(keyString)")

echo -e "${GREEN}${BOLD}✓ API Key created: ${API_KEY}${RESET}"

export PROJECT_ID=$(gcloud config get-value project)
export ZONE=$(gcloud compute instances list --format="value(zone)" | head -n 1)
export VM_NAME=$(gcloud compute instances list --format="value(name)" | head -n 1)

echo -e "${YELLOW}▶ Preparing remote payload for ${VM_NAME}...${RESET}"
cat > remote_payload.sh <<EOF
#!/bin/bash
export API_KEY="${API_KEY}"
export task_2_file_name="${TASK_2_FILE}"
export task_3_request_file="${TASK_3_REQ}"
export task_3_response_file="${TASK_3_RES}"
export task_4_sentence="${TASK_4_SENTENCE}"
export task_4_file="${TASK_4_FILE}"
export task_5_sentence="${TASK_5_SENTENCE}"
export task_5_file="${TASK_5_FILE}"
EOF

cat >> remote_payload.sh <<'EOF_SCRIPT'
source venv/bin/activate

cat > synthesize-text.json <<EOF
{
    "input":{
        "text":"Cloud Text-to-Speech API allows developers to include natural-sounding, synthetic human speech as playable audio in their applications. The Text-to-Speech API converts text or Speech Synthesis Markup Language (SSML) input into audio data like MP3 or LINEAR16 (the encoding used in WAV files)."
    },
    "voice":{
        "languageCode":"en-gb",
        "name":"en-GB-Standard-A",
        "ssmlGender":"FEMALE"
    },
    "audioConfig":{
        "audioEncoding":"MP3"
    }
}
EOF

curl -s -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @synthesize-text.json "https://texttospeech.googleapis.com/v1/text:synthesize" \
> $task_2_file_name

cat > "$task_3_request_file" <<EOF
{
   "config": {
      "encoding": "FLAC",
      "sampleRateHertz": 44100,
      "languageCode": "fr-FR"
   },
   "audio": {
      "uri": "gs://cloud-samples-data/speech/corbeau_renard.flac"
   }
}
EOF

curl -s -X POST -H "Content-Type: application/json" \
--data-binary @"$task_3_request_file" \
"https://speech.googleapis.com/v1/speech:recognize?key=${API_KEY}" \
-o "$task_3_response_file"

curl -s -X POST \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d "{\"q\": \"$task_4_sentence\"}" \
"https://translation.googleapis.com/language/translate/v2?key=${API_KEY}&source=ja&target=en" \
> "$task_4_file"

curl -s -X POST \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d "{\"q\": [\"$task_5_sentence\"]}" \
"https://translation.googleapis.com/language/translate/v2/detect?key=${API_KEY}" \
-o "$task_5_file"
EOF_SCRIPT

echo -e "${YELLOW}▶ Deploying payload to ${VM_NAME}...${RESET}"
gcloud compute scp remote_payload.sh $VM_NAME:~ --zone=$ZONE --quiet

echo -e "${YELLOW}▶ Executing tasks on ${VM_NAME}...${RESET}"
gcloud compute ssh $VM_NAME --zone=$ZONE --quiet --command="bash ~/remote_payload.sh"

rm remote_payload.sh
echo -e "${GREEN}${BOLD}✓ All API operations completed successfully on the remote VM.${RESET}"