云语音转文本,cUrl 进行语音:识别请求,从我的存储桶访问文件

问题描述 投票:0回答:1

我正在使用本教程:https://cloud.google.com/speech-to-text/docs/transcribe-api?_ga=2.111344076.-975712240.1702582049

但我想更进一步,将教程中提供的音频文件复制到我自己的存储桶中。 但我不明白为什么它不起作用。我认为这是因为凭证。但我不明白。

# Set PROJECT_ID variable
export PROJECT_ID=$(gcloud config get-value project)

#enable Cloud Speech-To-Text API
gcloud services enable speech.googleapis.com

#Create a bucket
gcloud storage buckets create gs://${PROJECT_ID}_bucket --project=$PROJECT_ID

#Store Audio file in the bucket
gsutil cp gs://cloud-samples-tests/speech/brooklyn.flac gs://${PROJECT_ID}_bucket

#Grant public access
gcloud storage objects update gs://${PROJECT_ID}_bucket/brooklyn.flac --add-acl-grant=entity=AllUsers,role=READER

# Create a service account
gcloud iam service-accounts create ServiceAccount1 \
    --description="DESCRIPTION" \
    --display-name="ServiceAccount1"
gcloud projects add-iam-policy-binding $PROJECT_ID \
    --member=serviceAccount:ServiceAccount1@${PROJECT_ID}.iam.gserviceaccount.com \
    --role=roles/owner

# Create a key file for the service account
gcloud iam service-accounts keys create KEY_FILE.json \
    --iam-account=ServiceAccount1@${PROJECT_ID}.iam.gserviceaccount.com

# Set your authentication environment variable
export GOOGLE_APPLICATION_CREDENTIALS="KEY_FILE.json"

# Create a JSON request file
echo '{
  "config": {
      "encoding":"FLAC",
      "sampleRateHertz": 16000,
      "languageCode": "en-US",
      "enableWordTimeOffsets": false
  },
  "audio": {
      "uri":"gs://${PROJECT_ID}_bucket/brooklyn.flac"
  }
}' > sync-request.json


#Use cUrl to make a speech:recognize request
curl -s -H "Content-Type: application/json" \
    -H "Authorization: Bearer $(cat $GOOGLE_APPLICATION_CREDENTIALS | jq -r .private_key)" \
    https://speech.googleapis.com/v1/speech:recognize \
    -d @sync-request.json

顺便说一下,原来的 cUrl 请求是:

curl -s -H "Content-Type: application/json" \
    -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
    https://speech.googleapis.com/v1/speech:recognize \
    -d @sync-request.json

预期的响应是:(我对修改后的 cUrl 请求没有响应)

{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "how old is the Brooklyn Bridge",
          "confidence": 0.98267895
        }
      ]
    }
  ]
}

我遇到 404 错误: ''' { “错误”: { “代码”:404, "message": "未找到请求的实体。", “状态”:“NOT_FOUND” } } '''

google-cloud-platform gcloud google-cloud-speech
1个回答
0
投票

我犯了 2 个错误:我忘记为我的服务帐户分配角色,并且在 JSON 文件中留下了 $PROJECT_ID。

这是一个工作代码:

# https://cloud.google.com/speech-to-text/docs/transcribe-api?_ga=2.111344076.-975712240.1702582049

# Set PROJECT_ID variable
export PROJECT_ID=$(gcloud config get-value project)

#enable Cloud Speech-To-Text API
gcloud services enable speech.googleapis.com

#Create a bucket
gcloud storage buckets create gs://${PROJECT_ID}_bucket --project=$PROJECT_ID

#Store Audio file in the bucket
gsutil cp gs://cloud-samples-tests/speech/brooklyn.flac gs://${PROJECT_ID}_bucket

#Grant public access
gcloud storage objects update gs://${PROJECT_ID}_bucket/brooklyn.flac --add-acl-grant=entity=AllUsers,role=READER

# Create a service account
gcloud iam service-accounts create ServiceAccount1 \
    --description="DESCRIPTION" \
    --display-name="ServiceAccount1"

#give a owner role to the service account
gcloud projects add-iam-policy-binding $PROJECT_ID \
    --member=serviceAccount:ServiceAccount1@${PROJECT_ID}.iam.gserviceaccount.com \
    --role=roles/owner

# Create a key file for the service account
gcloud iam service-accounts keys create KEY_FILE.json \
    --iam-account=ServiceAccount1@${PROJECT_ID}.iam.gserviceaccount.com

# Set your authentication environment variable
export GOOGLE_APPLICATION_CREDENTIALS="KEY_FILE.json"

# Create a JSON request file with PROJECT_ID replaced
echo '{
  "config": {
      "encoding":"FLAC",
      "sampleRateHertz": 16000,
      "languageCode": "en-US",
      "enableWordTimeOffsets": false
  },
  "audio": {
      "uri":"gs://'${PROJECT_ID}'_bucket/brooklyn.flac"
  }
}' > sync-request.json

# Set the endpoint URL
ENDPOINT="https://speech.googleapis.com/v1/speech:recognize"

#Use cUrl to make a speech:recognize request
curl -s -H "Content-Type: application/json" \
    -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
    $ENDPOINT \
    -d @sync-request.json

© www.soinside.com 2019 - 2024. All rights reserved.