2
votes

I am using Google cloud speech-to-text API and setup project in localhost and live with credential and environment variable. It working with only command line while I run it through browser it gives me below error

Fatal error: Uncaught DomainException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information in /jet/app/www/default/speech/vendor/google/auth/src/ApplicationDefaultCredentials.php:156 Stack trace: #0 /jet/app/www/default/speech/vendor/google/gax/src/CredentialsWrapper.php(197): Google\Auth\ApplicationDefaultCredentials::getCredentials(Array, Object(Google\Auth\HttpHandler\Guzzle6HttpHandler), NULL, NULL) #1 /jet/app/www/default/speech/vendor/google/gax/src/CredentialsWrapper.php(114): Google\ApiCore\CredentialsWrapper::buildApplicationDefaultCredentials(Array, Object(Google\Auth\HttpHandler\Guzzle6HttpHandler)) #2 /jet/app/www/default/speech/vendor/google/gax/src/GapicClientTrait.php(326): Google\ApiCore\CredentialsWrapper::build(Array) #3 /jet/app/www/default/speech/vendor/google/gax/src/GapicClientTrait.php(308): Google\Cloud\Speech\V1\Gapic\SpeechGapicClient->createCredentialsWrapper(NULL, Array) #4 /jet/app/www/default/speech in /jet/app/www/default/speech/vendor/google/gax/src/CredentialsWrapper.php on line 200

I am using Below Google Speech code:

https://github.com/GoogleCloudPlatform/php-docs-samples

https://cloud.google.com/speech-to-text/docs/streaming-recognize

namespace Google\Cloud\Samples\Speech;

require __DIR__ . '/vendor/autoload.php';

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

$inputDefinition = new InputDefinition([
    new InputArgument('audio-file', InputArgument::REQUIRED, 'The audio file to transcribe'),
    new InputOption('model', null, InputOption::VALUE_REQUIRED, 'The model to use'),
    new InputOption('encoding', null, InputOption::VALUE_REQUIRED,
        'The encoding of the audio file. This is required if the encoding is ' .
        'unable to be determined. '
    )
]);

$application = new Application('Cloud Speech');
$application->add(new Command('transcribe'))
    ->setDefinition($inputDefinition)
    ->setDescription('Transcribe an audio file using Google Cloud Speech API')
    ->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file using the
Google Cloud Speech API.

<info>php %command.full_name% audio_file.wav</info>

EOF
    )
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        $audioFile = $input->getArgument('audio-file');
        transcribe_sync($audioFile);
    });

$application->add(new Command('transcribe-gcs'))
    ->setDefinition($inputDefinition)
    ->setDescription('Transcribe audio from a Storage Object using Google Cloud Speech API')
    ->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a Cloud Storage
Object using the Google Cloud Speech API.

<info>php %command.full_name% gs://my-bucket/audio_file.wav</info>

EOF
    )
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        $audioFile = $input->getArgument('audio-file');
        if (!preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $audioFile, $matches)) {
            throw new \Exception('Invalid file name. Must be gs://[bucket]/[audiofile]');
        }
        transcribe_sync_gcs($audioFile);
    });

$application->add(new Command('transcribe-model'))
    ->setDefinition($inputDefinition)
    ->setDescription('Transcribe an audio file, with selected model, using Google Cloud Speech API')
    ->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file, with the 
selected model, using the Google Cloud Speech API.

<info>php %command.full_name% audio_file.wav model_name</info>

EOF
    )
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        $audioFile = $input->getArgument('audio-file');
        $modelName = $input->getOption('model');
        transcribe_model_selection($audioFile, $modelName);
    });

$application->add(new Command('transcribe-enhanced'))
    ->setDefinition($inputDefinition)
    ->setDescription('Transcribe an audio file, with an enhanced model, using Google Cloud Speech API')
    ->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file, with an enhanced 
model, using the Google Cloud Speech API.

<info>php %command.full_name% audio_file.wav model_name</info>

EOF
    )
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        $path = $input->getArgument('audio-file');
        transcribe_enhanced_model($path);
    });

$application->add(new Command('transcribe-punctuation'))
    ->setDefinition($inputDefinition)
    ->setDescription('Transcribe an audio file, with proper punctuation, using Google Cloud Speech API')
    ->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file, with 
proper punctuation, using the Google Cloud Speech API.

<info>php %command.full_name% audio_file.wav</info>

EOF
    )
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        $path = $input->getArgument('audio-file');
        transcribe_auto_punctuation($path);
    });

$application->add(new Command('transcribe-async'))
    ->setDefinition($inputDefinition)
    ->setDescription('Transcribe an audio file asynchronously using Google Cloud Speech API')
    ->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file using the
Google Cloud Speech API asynchronously.

<info>php %command.full_name% audio_file.wav</info>

EOF
    )
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        $audioFile = $input->getArgument('audio-file');
        transcribe_async($audioFile);
    });

$application->add(new Command('transcribe-async-gcs'))
    ->setDefinition($inputDefinition)
    ->setDescription('Transcribe audio asynchronously from a Storage Object using Google Cloud Speech API')
    ->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a Cloud Storage
object asynchronously using the Google Cloud Speech API.

<info>php %command.full_name% gs://my-bucket/audio_file.wav</info>

EOF
    )
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        $audioFile = $input->getArgument('audio-file');
        if (!preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $audioFile, $matches)) {
            throw new \Exception('Invalid file name. Must be gs://[bucket]/[audiofile]');
        }
        transcribe_async_gcs($audioFile);
    });

$application->add(new Command('transcribe-async-words'))
    ->setDefinition($inputDefinition)
    ->setDescription('Transcribe an audio file asynchronously and print word time offsets using Google Cloud Speech API')
    ->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a file using the
Google Cloud Speech API asynchronously and prints word time offsets.

<info>php %command.full_name% audio_file.wav</info>

EOF
    )
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        $audioFile = $input->getArgument('audio-file');
        transcribe_async_words($audioFile);
    });

$application->add(new Command('transcribe-stream'))
    ->setDefinition($inputDefinition)
    ->setDescription('Transcribe a stream of audio using Google Cloud Speech API')
    ->setHelp(<<<EOF
The <info>%command.name%</info> command transcribes audio from a stream using
the Google Cloud Speech API.

<info>php %command.full_name% audio_file.wav</info>

EOF
    )
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        streaming_recognize(
            $input->getArgument('audio-file')
        );
    });

// for testing
if (getenv('PHPUNIT_TESTS') === '1') {
    return $application;
}

$application->run();
1
You need to secify env variable GOOGLE_APPLICATION_CREDENTIALS for service account key, see the link in the warningMaksim
I have already did that And its working fine in command line while i run file through coomand line "php speech.php" it will working and executing result as expected but while same file run into browser it gives me above errorbhavyesh dhaduk
Try to var_dump(getenv('GOOGLE_APPLICATION_CREDENTIALS')); in browser. Next - try to chown www-data:www-data to key file.Maksim

1 Answers

1
votes

put it to manual

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-credentials.json');