1
votes

I am trying to transcribe an audio file uploaded by a user from a webapp running on a compute engine instance which has access to all cloud APIs and client libraries installed using composer. However I am not getting any response from cloud speech API after trying to send my request using PHP. Someone please help me figure out what am missing.Here is my PHP code where the audio file is directed to from a form with file selector. I also tried to transcribe and audio file from cloud storage still did'nt get anything

<?php
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Speech\V1\SpeechClient;
use Google\Cloud\Speech\V1\RecognitionAudio;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;

$audioFile = $_FILES["my_audio"]; //from file picker
$encoding = AudioEncoding::LINEAR16;
$sampleRateHertz = 32000;
$languageCode = 'en-US';

$content = file_get_contents($audioFile);
$audio = (new RecognitionAudio())
   ->setContent($content);
$config = (new RecognitionConfig())
   ->setEncoding($encoding)
   ->setSampleRateHertz($sampleRateHertz)
   ->setLanguageCode($languageCode);

$client = new SpeechClient();
$operation = $client->longRunningRecognize($config, $audio);
$operation->pollUntilComplete();

if ($operation->operationSucceeded()) {
   $response = $operation->getResult();

// each result is for a consecutive portion of the audio. iterate
// through them to get the transcripts for the entire audio file.
    foreach ($response->getResults() as $result) {
        $alternatives = $result->getAlternatives();
        $mostLikely = $alternatives[0];
        $transcript = $mostLikely->getTranscript();
        $confidence = $mostLikely->getConfidence();
        printf('Transcript: %s' . PHP_EOL, $transcript);
        printf('Confidence: %s' . PHP_EOL, $confidence);
    }
} else {
    print_r($operation->getError());
}

$client->close();
?>
1

1 Answers

0
votes

There is not an absolute path in $_FILES["my_audio"]. Since you mentioned that the file is uploaded, I wonder if you are setting something like this:

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

On the other hand, the fact that you don't receive any response from the Speech API service, it could mean to me that the request is not started/triggered. You can perform validations during file upload or check for errors using $_FILES["pictures"]["error"] to see if everything is going OK.

If the issue persists even with GCS using $uri = 'gs://your-bucket-name/your-audio-file' the issue could not be the code, in such case, please elaborate on the other components involved in your use case. You mentioned 'composer' and I was wondering if you mean Cloud Composer.

UPDATE

I'm not a PHP expert, but I made it work with this example:

After comparing your file, I found that you have:

$uri = "gs://speech-text-audio/h_ana1.flac";
$content = file_get_contents($uri);
$audio = (new RecognitionAudio())
    ->setContent($content);

Whiles, it worked for me:

$uri = 'gs://cloud-samples-tests/speech/brooklyn.flac';
$audio = (new RecognitionAudio())
    ->setUri($uri);