0

I'd like to be able to make a speech:recognize request on and with my own cloud-hosted resources, so I can simply log into the Google Cloud Platform console, run a command in the Cloud Shell, and see the results. Much like https://cloud.google.com/speech-to-text/docs/quickstart-protocol, except without making use of anything locally.

Not sure what other important info to share, but the .json and .flac files in my cloud have public read access.

How can I achieve this?

my request:

curl -H "Content-Type: application/json" https://speech.googleapis.com/v1/speech:recognize?key=[my-api-key] -d @https://storage.googleapis.com/[bucket]/[json-request-filename].json

the response:

Warning: Couldn't read data from file
Warning: "https://storage.googleapis.com/[bucket]/[json-request-filename].json",
Warning: this makes an empty POST.
{
  "error": {
    "code": 400,
    "message": "RecognitionAudio not set.",
    "status": "INVALID_ARGUMENT"
  }
}

here's the .json hosted in google cloud storage:

{
  "config": {
      "encoding":"FLAC",
      "sampleRateHertz": 16000,
      "languageCode": "en-US",
      "enableWordTimeOffsets": false
  },
  "audio": {
      "uri":"gs://[bucket]/[audio-filename].flac"
  }
}

No new info, but here's how it all looks the Google Cloud Platform Shell:

[my-account]@cloudshell:~ ([my-project])$ curl -H "Content-Type: application/json" https://speech.googleapis.com/v1/speech:recognize?key=[my-api-key] -d @https://storage.googleapis.com/[bucket]/[json-request-filename].json
Warning: Couldn't read data from file
Warning: "https://storage.googleapis.com/[bucket]/[json-request-filename].json",
Warning: this makes an empty POST.
{
  "error": {
    "code": 400,
    "message": "RecognitionAudio not set.",
    "status": "INVALID_ARGUMENT"
  }
}

2 Answers2

0

The -d flag in the curl command tells curl to read data from the filename right after it, and use that data as the body of the request. curl does not recognize a web URL as a valid file. curl can't read that JSON file, so it acts as if it were an empty file, and builds a request with an empty body. The request that's sent to the API doesn't have any information about that JSON file.

The speech API receives the request with an empty body, and can't do anything with it. The API doesn't even know that you specified a Google Cloud object in the curl command.

The speech:recognize method is documented at https://cloud.google.com/speech-to-text/docs/reference/rest/v1p1beta1/speech/recognize. It does not have any way to get the parameters it needs except from the body of the request. You can't tell it to go read those parameters from somewhere else, such as a URL or Google Cloud object. You have to include them in the request, so the program that builds the request needs to know them.

Charles Engelke
  • 5,569
  • 1
  • 29
  • 26
0

You can find your API key using the following code but you have to install gcloud using this link https://cloud.google.com/sdk/docs/install#linux

gcloud auth application-default print-access-token
curl -H "Authorization: Bearer YOUR_API_KEY" -H "Content-Type: application/json; charset=utf-8" --data "{
  'input':{
    'text':'I\'ve added the event to your calendar.'
  },
  'voice':{
    'languageCode':'en-gb',
    'name':'en-GB-Standard-A',
    'ssmlGender':'FEMALE'
  },
  'audioConfig':{
    'audioEncoding':'MP3'
  }
}" "https://texttospeech.googleapis.com/v1/text:synthesize"
hardika
  • 572
  • 5
  • 17