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();
?>