Google Cloud Speech-to-Text API using Client Libraries (on Colab)

What is gcloud?

https://cloud.google.com/sdk/gcloud/reference/

gcloud is command line tool that provides the primary command-line interface to Google Cloud Platform.

gcloud can be used to perform many common platform tasks either from the command-line, in scripts or other automations

gcloud is a part of the Google Cloud SDK.

gcloud is enabled after downloading and installing the SDK.

Initializing Cloud SDK (gcloud init)

https://cloud.google.com/sdk/docs/initializing

After installing Google Cloud SDK, gcloud init command is the next step to perform initial setup tasks.

gcloud init 是互動式指令(interactive command),在設定過程需要使用者提供

  1. 使用者帳號
  2. 對接的google cloud project
  3. 驗證的金鑰檔(key_file)

gcloud init performs the following setup steps:\colon

  1. Authorizes Cloud SDK tools by using user account credentials to accessed Google Cloud Platform.
  2. Sets up Google Cloud SDK configuration and base set of properties. Base set of properties includes active account, the current project, the default Google Compute Engine region and the default Google Compute Engine zone.

After first initialization, gcloud init can be used to change settings of current configuration or create a new configuration.


Automate gcloud init

Fig.0 Question of Automating Google Cloud SDK on StackOverflow

曾經在StackOverflow看到有人發問[1],該問題為:\colon
Documentation on Google Cloud SDK directs one to run gcloud init after installing it. Is there a way to automate this step given that gcloud init is an interactive command.

有網友回覆:\colon

You does not need to run gcloud init. Main goal is to make sure credentials are configured and perhaps the project property is set. If you have service account credentials, gcloud can be configured and ready to go via

gcloud auth activate-service-account [service_account] --key-file=[crediential_key.json]
gcloud config set project [my-project]

(指令1) gcloud configuration with service_account

For completeness gcloud init essentially runs the following steps:\colon

  1. Select configuration (one of the following)
    • gcloud config configurations create [my_configuration]
    • gcloud config configurations activate [my_configuration]
  2. Setup credentials (one of the following)
    • (interactive) gcloud auth login
    • gcloud config set account \[my\_existing\_credentials\]\
    • gcloud auth activate-service-account [account-service]
  3. Set Project
    • gcloud config set project [my_project]
    • List of accessible projects for set credentials can be seen via gcloud projects list
  4. (Optional) Set default GCE zone (Compute API must be enabled)
    • gcloud config set compute/zone [my_default_gce_zone]
    • List of zones can be obtained via gcloud compute zones list
  5. (Optional) Set default GCE region (Compute API must be enabled)
    • gcloud config set compute/region [my_default_gce_region]
    • List of regions can be obtained via gcloud compute regions list
  6. (Optional) create default config file for gsutil
    • gsutil config -n -o ~/.boto

google_api_test_0924.ipynb Code Realization

為了實際測試StackOverflow的教學,我們在Colab上建立一個iPython Notebook檔google_api_test_0924.ipynb

詳細的google_api_test_0924.ipynbcode會找別的地方說明

google_api_test_0924.ipynb會測試紀錄五種情況:\colon

  1. gcloud init (case 1)
  2. gcloud init -1 (case 2)
  3. gcloud auth 但 activate-service-account 後多加參數 (case 3)
  4. 沒有用os.environ而是用!export GOOGLE_APPLICATIONS_CREDENTIALS=... (case 4)
  5. 正確的操作 (case 5)

Fig.1:\colon 測試 case 1 colab response

Fig.1 呈現type !gcloud init後的colab response,因為gcloud init是一個interactive command,在

Pick Configuration to use 
... 
Please enter your numeric choice:

系統會等使用輸入1或2來決定使用的configuration,因為colab response無法接受使用者輸入,所以就無法繼續設定。

2018/10/4 Colab 更新後已經能夠使用!gcloud init

Fig.2:\colon 測試 case 2 colab response

Fig.2呈現我們在!gcloud init後面加-1把選項1輸入,colab 顯示

Error:(gcloud init) Invalid value for [-1] ...

代表gcloud init無法辨認-1參數。


Fig.3:\colon 測試 case 3 colab response

[email protected]是service account。

GC-AI-Challenge-baedb4836026.json是credential key file。

gc-ai-challenge-1529370474866是GCP project。

執行quickstart.py 的python code on cola (figure 2下半部加工)


Figure 3:\colon permission denied of alternative method

系統呈現第26行出錯

Fig.4:\colon 測試 case 4 colab response

將指令1移除service account

將指令1移除service account, 也就是移除[email protected]

原本是

!gcloud auth activate-service-account starting-account-e4whhkxv1a2x@gc-ai-challenge-1529370474866.iam.gserviceaccount.com --key-file=GC-AI-Challenge-4dc21e90cad0.json

後來變成

!gcloud auth activate-service-account --key-file=GC-AI-Challenge-4dc21e90cad0.json

原因:\colon

!gcloud auth activate-service-account starting-account-e4whhkxv1a2x@gc-ai-challenge-1529370474866.iam.gserviceaccount.com --key-file=GC-AI-Challenge-4dc21e90cad0.json

根據[2],我們原本打的指令裡面[email protected]是多餘的,只需要key

!gcloud auth activate-service-account --key-file=GC-AI-Challenge-4dc21e90cad0.json

key file裡面就有activate-service-account的資訊。

另外依照[3]的說明我們還要把!export GOOGLE_APPLICATION_CREDENTIALS="GC-AI-Challenge-4dc21e90cad0.json"
改成

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="GC-AI-Challenge-4dc21e90cad0.json"

不然會出現GOOGLE_APPLICATION_CREDENTIALS Error [要再截圖呈現]


Fig.5:\colon case 5 成功使用google cloud speech-to-text api colab response

[1]
https://stackoverflow.com/questions/42379685/can-i-automate-google-cloud-sdk-gcloud-init-interactive-command

[2]
https://blog.hoyo.idv.tw/?p=4544

[3]
https://stackoverflow.com/questions/45501082/set-google-application-credentials-in-python-project-to-use-google-api

results matching ""

    No results matching ""