Google-cloud-php: Speech to Text API does not return results

Created on 17 Jan 2020  路  6Comments  路  Source: googleapis/google-cloud-php

Observer behavior

When sending long FLAC audios to the Speech to Text API via the PHP Client library, the code returns an empty response, like it was a not successful but an Operation ID is shown. The output is similar to:

name: 1946...
Erro: 

Additional, if we fetch that Operation ID via the CLI, we get the transcript of our audio files.

curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
    -H "Content-Type: application/json; chatset=utf-8" \
    https://speech.googleapis.com/v1/operations/1946...
{
    "name": "1946...",
    "metadata": {
      ...
     },
    "done": true
    ...
}  
  • We would like to know what are we missing with the implementation of this code.

The audio files have been generated with this command:

ffmpeg -y -i video.mp4' -vn -ac 1 -ar 48000 video.flac

Code used

private function converteText ()
{
   echo date ( 'c' ) . "\n";

   // set string as audio content
   $audio = ( new RecognitionAudio() )
       ->setUri ( $this->audioFile );

   echo " FLAC " . FfmpegUtil::$sampleRateHertz . "hz \n";
   $config = ( new RecognitionConfig() )
       //->setEncoding ( AudioEncoding::FLAC )
       //->setSampleRateHertz ( FfmpegUtil::$sampleRateHertz )
       ->setEnableWordTimeOffsets ( true )
       ->setLanguageCode ( 'pt-BR' );

   // create the speech client
   $client = new SpeechClient();

   // create the asyncronous recognize operation
   $operation = $client->longRunningRecognize ( $config, $audio );
   $operation->pollUntilComplete ();

   echo "name: {$operation->getName()}\n";


   if ( $operation->operationSucceeded () ) {
       echo "OK\n\n";
       $response = $operation->getResult ();

       $saida = [ 'transcript' => [], 'word' => [] ];

       foreach ( $response->getResults () as $result ) {
           $alternatives = $result->getAlternatives ();
           $mostLikely   = $alternatives[ 0 ];
           $transcript   = $mostLikely->getTranscript ();
           $confidence   = $mostLikely->getConfidence ();

           $saida[ 'transcripts' ][] = [
               'transcript' => $transcript,
               'confidence' => $confidence
           ];

           foreach ( $mostLikely->getWords () as $wordInfo ) {

               $saida[ 'words' ][] = [
                   'word'  => $wordInfo->getWord (),
                   'start' => GPBUtil::formatDuration ( $wordInfo->getStartTime () ),
                   'end'   => GPBUtil::formatDuration ( $wordInfo->getEndTime () )
               ];
           }

           return $saida;
       }
   } else {
       echo "Erro: " . $operation->getError () . "\n";
       echo "\n" . date ( 'c' ) . "\n";
       return null;
   }

   $client->close ();

   return null;
}

speech question

Most helpful comment

I'm subscribing/commenting this as I finally tracked this down to this. We jumped from v0.27.0 to v1.2.0 (for google/cloud-speech) as part of finally joining the GA v1 release.

Did a few tests with small FLAC files and everything was fine. Now that we've hit large FLAC files (300mb+) usually 60-70 minutes of audio. We are seeing same exact thing. longRunningRecognize -> pollUntilComplete completes, but the job isn't actually done.

  • isDone - false

All 6 comments

Hi @Google-K,

Are you able to share a sample audio file which raises the issue? Perhaps you could generate a file which doesn't contain any private or secret information but which when used demonstrates the behavior you're asking about, and attach it to this issue?

The files are found here:

https://drive.google.com/drive/folders/1rJiLT4JPLOtsBzafnwt43qvqw_EqLwk9?usp=sharing

After several tests we noticed the following:

  • In the latest PHP Client library there were changes for the method pollUntilComplete. These changes make the connection ends before the operation finishes. This makes that, internally, operation->operationSuceeded() results in NULL, making PHP think that the Operation was not successful.

  • However, the operation ran, we can confirm this because we can fetch the results via the CLI.

  • If an operation runs and we can fetch results, it means that there were no errors. But as mentioned before, the operation "looks" like unsuccessful for PHP. This is why we don't see anything in the line operation->getError().

  • As a workaround, we changed this line:

$operation->pollUntilComplete();

for these lines:

while(!$operation->isDone()){
       $operation->pollUntilComplete();
}

The issue we're now facing is that the line

$operation->getResult ()->getResults ()

is only returning 1 minute from the transcript when our audio files are avg 30 minutes long.

Hi @jdpedrie,

Google support opened this up in relation to my case 21748687.

After the update "google/cloud-speech": "0.30.0", it stopped showing error "".

Now, in the answer comes only 1 alternatives.

echo "count: " . count ( $alternatives );
returned "count: 1"

however via CLI returned 31 alternatives in JSON

Any update to this? We are seeing similar issues.

I'm subscribing/commenting this as I finally tracked this down to this. We jumped from v0.27.0 to v1.2.0 (for google/cloud-speech) as part of finally joining the GA v1 release.

Did a few tests with small FLAC files and everything was fine. Now that we've hit large FLAC files (300mb+) usually 60-70 minutes of audio. We are seeing same exact thing. longRunningRecognize -> pollUntilComplete completes, but the job isn't actually done.

  • isDone - false

I can confirm that pollUntilComplete finishes before isDone returns true. I am using V1p1beta1 in v1.3.2.

        while (!$operation->isDone()) {
            $operation->pollUntilComplete();

            if (!$operation->isDone()) {
                $this->warn("pollUntilComplete finished too early");
            }
        }

        $this->info("Operation has finished");

The code snippet produces the following result:

[2021-01-31 13:35:42] pollUntilComplete finished too early
[2021-01-31 13:40:42] pollUntilComplete finished too early
[2021-01-31 13:44:35] Operation has finished  
Was this page helpful?
0 / 5 - 0 ratings

Related issues

joseflorido picture joseflorido  路  3Comments

ammopt picture ammopt  路  6Comments

tmatsuo picture tmatsuo  路  4Comments

mjniuz picture mjniuz  路  6Comments

castaneai picture castaneai  路  4Comments