Google Cloud Speech API не работает в браузере - PullRequest
0 голосов
/ 28 марта 2019

Я использую облачный API Google для преобразования текста в текст и настройки проекта в localhost и работаю с учетными данными и переменной среды. Он работает только с командной строкой, пока я запускаю его через браузер, он выдает ошибку ниже

Неустранимая ошибка: Uncaught 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 (массив, объект (Google \ Auth \ HttpHandler \ Guzzle6HttpHandler), NULL, NULL) # 1 / jet / app /www / default / speech / vendor / google / gax / src / CredentialsWrapper.php (114): Google \ ApiCore \ CredentialsWrapper :: buildApplicationDefaultCredentials (массив, объект (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 в строке 200

Я использую код Google Speech ниже:

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