2

我在本地主机中使用谷歌云语音到文本 API 和设置项目,并使用凭据和环境变量。当我通过浏览器运行它时它只使用命令行它给我下面的错误

致命错误:未捕获的 DomainException:无法加载默认凭据。浏览到 https://developers.google.com/accounts/docs/application-default-credentials 有关 /jet/app/www/default/speech/vendor/google/auth/src/ApplicationDefaultCredentials.php:156 中的更多信息 堆栈跟踪:#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 ): 谷歌\云\语音\V1\Gapic\SpeechGapicClient->createCredentialsWrapper(NULL, Array) #4 /jet/app/www/default/speech in /jet/app/www/default/speech/vendor/google/gax/src/CredentialsWrapper.php 第 200 行

我正在使用以下 Google 语音代码:

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

1 回答 1

2

把它手动

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-credentials.json');
于 2020-11-05T13:11:25.187 回答