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),在設定過程需要使用者提供
- 使用者帳號
- 對接的google cloud project
- 驗證的金鑰檔(key_file)
gcloud init
performs the following setup steps
- Authorizes Cloud SDK tools by using user account credentials to accessed Google Cloud Platform.
- 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],該問題為
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.
有網友回覆
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
- Select configuration (one of the following)
gcloud config configurations create [my_configuration]
gcloud config configurations activate [my_configuration]
- Setup credentials (one of the following)
- (interactive)
gcloud auth login
gcloud config set account \[my\_existing\_credentials\]\
gcloud auth activate-service-account [account-service]
- (interactive)
- Set Project
gcloud config set project [my_project]
- List of accessible projects for set credentials can be seen via
gcloud projects list
- (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
- (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
- (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.ipynb
code會找別的地方說明
google_api_test_0924.ipynb
會測試紀錄五種情況
gcloud init
(case 1)gcloud init -1
(case 2)gcloud auth
但 activate-service-account 後多加參數 (case 3)- 沒有用os.environ而是用
!export GOOGLE_APPLICATIONS_CREDENTIALS=...
(case 4) - 正確的操作 (case 5)
Fig.1 測試 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 測試 case 2 colab response
Fig.2呈現我們在!gcloud init
後面加-1
把選項1輸入,colab 顯示
Error:(gcloud init) Invalid value for [-1] ...
代表gcloud init
無法辨認-1參數。
Fig.3 測試 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 permission denied of alternative method
系統呈現第26行出錯
Fig.4 測試 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
原因
!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 case 5 成功使用google cloud speech-to-text api colab response