Search code examples
javascriptxmlhttprequestgoogle-cloud-speech

Google Cloud Speech Error 500 (Internal server error)


I have a simple JS code running in xampp apache.

var rawFile;
var allText;
var byteArray = [];

rawFile = new XMLHttpRequest();
rawFile.open("GET", "brooklyn.flac", false);
rawFile.onreadystatechange = function ()
{
    if(rawFile.readyState === 4)
    {
        if(rawFile.status === 200 || rawFile.status == 0)
        {
          allText = rawFile.responseText;
            //alert(allText);

        }
    }
     if (rawFile.status != 200) return byteArray;
        for (var i = 0; i < rawFile.responseText.length; ++i) {
        byteArray.push(rawFile.responseText.charCodeAt(i) & 0xff)
        }
     }

rawFile.send(null);

function send(){
var oAjaxReq = new XMLHttpRequest();

var payload = {
  config:{
    encoding: "FLAC",
    sampleRateHertz: 16000,
    languageCode:"en-US"
  },
  audio: {
    content: rawFile
  }
};

oAjaxReq.open("post", "https://speech.googleapis.com/v1/speech:recognize?key=???", true);
oAjaxReq.setRequestHeader("Content-Type", "application/json");

//object ot json
const jsonPayload = JSON.stringify(payload);

//Length of the jsonPayload
const payLoadLength= jsonPayload.length;
oAjaxReq.setRequestHeader("Content-Length", payLoadLength);
oAjaxReq.withCredentials = true;

 //Send Json to Google Cloud Speech Service
 oAjaxReq.send(jsonPayload);
 }

I am trying to use the Google Cloud Speech API. I am loading a local audio file with the name "brooklyn.flac", which I downloaded from "https://storage.googleapis.com/cloud-samples-tests/speech/brooklyn.flac", via xmlHTTPRequest.

However, I always recieve the following error:

{
  "error": {
    "code": 500,
    "message": "Internal error encountered.",
    "status": "INTERNAL"
  }
}

When I change the audio part of my payLoad object from "content: rawFile" to "uri: "gs://cloud-samples-tests/speech/brooklyn.flac" it´s working properly.

Is the error because of the "rawFile"? If yes, how can I correctly load this local file to send it to the cloud service?


Solution

  • TL;DR - Except for your audio content, the rest of the payload looks good to me. You most likely are not encoding the audio using base64 (since you haven't mentioned anything about it) and hence hitting the server error. Passing the base64 encoded version of the audio content will fix the issue.

    API types and sources for audio content

    There are 2 main ways to use the Cloud Speech API:

    Both of them support the audio content from either:

    • Google Cloud Storage
    • a raw file you send directly in the request.

    Using Google Cloud Storage is the simplest of the 2 approaches IMO. You upload the file to GCS and you pass the URI of the file (eg. gs://cloud-samples-tests/speech/brooklyn.flac).

    Encoding the audio content

    If you are sending the raw file as part of the request, depending on the API you're using you will need to handle the content accordingly:

    • JSON based REST API requires that the audio content be base64 encoded. The link also has examples on how to perform the base64 encoding in Python, Node.js and Java.

    • gRPC based API supports binary file uploads and hence you can pass the raw file as-is without any additional processing.

    Google Cloud client library

    I'm not sure if Node.js is an option, but if so - you could use the Google Cloud client library (which supports Speech Recognition in Alpha only at the time of writing). This library can automatically base64 encode the file for you and offers other convenient features too.