Skip to content

feat: Upgrade speech to v2 #2127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions speech/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"require": {
"google/cloud-speech": "^1.0.0",
"google/cloud-storage": "^1.20.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.21"
}
}
84 changes: 62 additions & 22 deletions speech/quickstart.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,76 @@
*/

# [START speech_quickstart]
# Includes the autoloader for libraries installed with composer
// Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
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;
// Imports the Google Cloud client library
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig;
use Google\Cloud\Speech\V2\Recognizer;
use Google\Cloud\Speech\V2\RecognizeRequest;
use Google\Cloud\Speech\V2\CreateRecognizerRequest;
use Google\Cloud\Speech\V2\GetRecognizerRequest;
use Google\ApiCore\ApiException;

# The name of the audio file to transcribe
$gcsURI = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw';
// The name of the audio file to transcribe
$gcsUri = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw';

# set string as audio content
$audio = (new RecognitionAudio())
->setUri($gcsURI);
// Populate these variables with your own values
$projectId = 'YOUR_PROJECT_ID';
$recognizerId = 'speech-v2-recognizer-php-quickstart';
$location = 'global';

# The audio file's encoding, sample rate and language
$config = new RecognitionConfig([
'encoding' => AudioEncoding::LINEAR16,
'sample_rate_hertz' => 16000,
'language_code' => 'en-US'
]);

# Instantiates a client
// Instantiates a client
$client = new SpeechClient();

# Detects speech in the audio file
$response = $client->recognize($config, $audio);
// The name of the recognizer to create
$recognizerName = $client->recognizerName($projectId, $location, $recognizerId);
$getRecognizerRequest = (new GetRecognizerRequest())->setName($recognizerName);

try {
$recognizer = $client->getRecognizer($getRecognizerRequest);
} catch (ApiException $e) {
if ($e->getStatus() === 'NOT_FOUND') {
// If the recognizer does not exist, create it.

// Create an explicit decoding config because .raw files have no header.
$explicitConfig = (new ExplicitDecodingConfig())
->setEncoding(ExplicitDecodingConfig\AudioEncoding::LINEAR16)
->setSampleRateHertz(16000)
->setAudioChannelCount(1); // The brooklyn_bridge audio is single-channel (mono)

$config = (new RecognitionConfig())
->setLanguageCodes(['en-US'])
->setModel('long') // Or other models like 'telephony', 'medical_dictation'
->setExplicitDecodingConfig($explicitConfig);

$recognizer = (new Recognizer())
->setName($recognizerName)
->setDefaultRecognitionConfig($config);

$createRecognizerRequest = (new CreateRecognizerRequest())
->setParent($client->locationName($projectId, $location))
->setRecognizer($recognizer)
->setRecognizerId($recognizerId);
$operation = $client->createRecognizer($createRecognizerRequest);

$operation->pollUntilComplete();
$recognizer = $operation->getResult();
printf('Created Recognizer: %s' . PHP_EOL, $recognizer->getName());
} else {
throw $e;
}
}

// Detects speech in the audio file
$recognizeRequest = (new RecognizeRequest())
->setRecognizer($recognizerName)
->setUri($gcsUri);
$response = $client->recognize($recognizeRequest);

# Print most likely transcription
// Print most likely transcription
foreach ($response->getResults() as $result) {
$alternatives = $result->getAlternatives();
$mostLikely = $alternatives[0];
Expand Down
13 changes: 8 additions & 5 deletions speech/src/multi_region_gcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

# [START speech_transcribe_with_multi_region_gcs]
# Imports the Google Cloud client library
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
use Google\Cloud\Speech\V2\RecognitionAudio;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\RecognizeRequest;

/**
* @param string $uri The Cloud Storage object to transcribe
Expand All @@ -31,7 +32,7 @@
function multi_region_gcs(string $uri)
{
# set string as audio content
$audio = (new RecognitionAudio())

Check failure on line 35 in speech/src/multi_region_gcs.php

View workflow job for this annotation

GitHub Actions / staticanalysis

Instantiated class Google\Cloud\Speech\V2\RecognitionAudio not found.

Check failure on line 35 in speech/src/multi_region_gcs.php

View workflow job for this annotation

GitHub Actions / staticanalysis

Call to method setUri() on an unknown class Google\Cloud\Speech\V2\RecognitionAudio.
->setUri($uri);

# The audio file's encoding, sample rate and language
Expand All @@ -48,7 +49,9 @@
$client = new SpeechClient($options);

# Detects speech in the audio file
$response = $client->recognize($config, $audio);
$recognizeRequest = (new RecognizeRequest())
->setRecognizer($config);

Check failure on line 53 in speech/src/multi_region_gcs.php

View workflow job for this annotation

GitHub Actions / staticanalysis

Parameter #1 $var of method Google\Cloud\Speech\V2\RecognizeRequest::setRecognizer() expects string, Google\Cloud\Speech\V2\RecognitionConfig given.
$response = $client->recognize($recognizeRequest);

# Print most likely transcription
foreach ($response->getResults() as $result) {
Expand Down
13 changes: 8 additions & 5 deletions speech/src/profanity_filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_profanity_filter]
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
use Google\Cloud\Speech\V2\RecognitionAudio;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\RecognizeRequest;

/**
* @param string $audioFile path to an audio file
Expand All @@ -35,11 +36,11 @@
$content = file_get_contents($audioFile);

// set string as audio content
$audio = (new RecognitionAudio())

Check failure on line 39 in speech/src/profanity_filter.php

View workflow job for this annotation

GitHub Actions / staticanalysis

Instantiated class Google\Cloud\Speech\V2\RecognitionAudio not found.

Check failure on line 39 in speech/src/profanity_filter.php

View workflow job for this annotation

GitHub Actions / staticanalysis

Call to method setContent() on an unknown class Google\Cloud\Speech\V2\RecognitionAudio.
->setContent($content);

// set config
$config = (new RecognitionConfig())

Check failure on line 43 in speech/src/profanity_filter.php

View workflow job for this annotation

GitHub Actions / staticanalysis

Call to an undefined method Google\Cloud\Speech\V2\RecognitionConfig::setEncoding().
->setEncoding($encoding)
->setSampleRateHertz($sampleRateHertz)
->setLanguageCode($languageCode)
Expand All @@ -49,7 +50,9 @@
$client = new SpeechClient();

# Detects speech in the audio file
$response = $client->recognize($config, $audio);
$recognizeRequest = (new RecognizeRequest())
->setRecognizer($config);
$response = $client->recognize($recognizeRequest);

# Print most likely transcription
foreach ($response->getResults() as $result) {
Expand Down
13 changes: 8 additions & 5 deletions speech/src/profanity_filter_gcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_profanity_filter_gcs]
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
use Google\Cloud\Speech\V2\RecognitionAudio;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\RecognizeRequest;

/**
* @param string $uri The Cloud Storage object to transcribe (gs://your-bucket-name/your-object-name)
Expand All @@ -32,11 +33,11 @@
$profanityFilter = true;

// set string as audio content
$audio = (new RecognitionAudio())

Check failure on line 36 in speech/src/profanity_filter_gcs.php

View workflow job for this annotation

GitHub Actions / staticanalysis

Instantiated class Google\Cloud\Speech\V2\RecognitionAudio not found.

Check failure on line 36 in speech/src/profanity_filter_gcs.php

View workflow job for this annotation

GitHub Actions / staticanalysis

Call to method setUri() on an unknown class Google\Cloud\Speech\V2\RecognitionAudio.
->setUri($uri);

// set config
$config = (new RecognitionConfig())

Check failure on line 40 in speech/src/profanity_filter_gcs.php

View workflow job for this annotation

GitHub Actions / staticanalysis

Call to an undefined method Google\Cloud\Speech\V2\RecognitionConfig::setEncoding().
->setEncoding($encoding)
->setSampleRateHertz($sampleRateHertz)
->setLanguageCode($languageCode)
Expand All @@ -46,7 +47,9 @@
$client = new SpeechClient();

# Detects speech in the audio file
$response = $client->recognize($config, $audio);
$recognizeRequest = (new RecognizeRequest())
->setRecognizer($config);
$response = $client->recognize($recognizeRequest);

# Print most likely transcription
foreach ($response->getResults() as $result) {
Expand Down
10 changes: 5 additions & 5 deletions speech/src/streaming_recognize.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_transcribe_streaming]
use Google\Cloud\Speech\V1\SpeechClient;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\StreamingRecognitionConfig;
use Google\Cloud\Speech\V1\StreamingRecognizeRequest;
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\StreamingRecognitionConfig;
use Google\Cloud\Speech\V2\StreamingRecognizeRequest;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;

/**
* @param string $audioFile path to an audio file
Expand All @@ -42,7 +42,7 @@

$speechClient = new SpeechClient();
try {
$config = (new RecognitionConfig())

Check failure on line 45 in speech/src/streaming_recognize.php

View workflow job for this annotation

GitHub Actions / staticanalysis

Call to an undefined method Google\Cloud\Speech\V2\RecognitionConfig::setEncoding().
->setEncoding($encoding)
->setSampleRateHertz($sampleRateHertz)
->setLanguageCode($languageCode);
Expand Down
8 changes: 4 additions & 4 deletions speech/src/transcribe_async.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_transcribe_async]
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\RecognitionAudio;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;

/**
* @param string $audioFile path to an audio file
Expand Down
8 changes: 4 additions & 4 deletions speech/src/transcribe_async_gcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_transcribe_async_gcs]
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\RecognitionAudio;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;

/**
* @param string $uri The Cloud Storage object to transcribe (gs://your-bucket-name/your-object-name)
Expand Down
8 changes: 4 additions & 4 deletions speech/src/transcribe_async_words.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_transcribe_async_word_time_offsets_gcs]
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\RecognitionAudio;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;

/**
* @param string $audioFile path to an audio file
Expand Down
13 changes: 8 additions & 5 deletions speech/src/transcribe_auto_punctuation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_transcribe_auto_punctuation]
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
use Google\Cloud\Speech\V2\RecognitionAudio;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\RecognizeRequest;

/**
* @param string $audioFile path to an audio file
Expand Down Expand Up @@ -57,7 +58,9 @@ function transcribe_auto_punctuation(string $audioFile)
$client = new SpeechClient();

// make the API call
$response = $client->recognize($config, $audio);
$recognizeRequest = (new RecognizeRequest())
->setRecognizer($config);
$response = $client->recognize($recognizeRequest);
$results = $response->getResults();

// print results
Expand Down
13 changes: 8 additions & 5 deletions speech/src/transcribe_enhanced_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_transcribe_enhanced_model]
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
use Google\Cloud\Speech\V2\RecognitionAudio;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\RecognizeRequest;

/**
* @param string $audioFile path to an audio file
Expand Down Expand Up @@ -58,7 +59,9 @@ function transcribe_enhanced_model(string $audioFile)
$client = new SpeechClient();

// make the API call
$response = $client->recognize($config, $audio);
$recognizeRequest = (new RecognizeRequest())
->setRecognizer($config);
$response = $client->recognize($recognizeRequest);
$results = $response->getResults();

// print results
Expand Down
13 changes: 8 additions & 5 deletions speech/src/transcribe_model_selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_transcribe_model_selection]
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
use Google\Cloud\Speech\V2\RecognitionAudio;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\RecognizeRequest;

/**
* @param string $audioFile path to an audio file
Expand Down Expand Up @@ -58,7 +59,9 @@ function transcribe_model_selection(string $audioFile, string $model)
$client = new SpeechClient();

// make the API call
$response = $client->recognize($config, $audio);
$recognizeRequest = (new RecognizeRequest())
->setRecognizer($config);
$response = $client->recognize($recognizeRequest);
$results = $response->getResults();

// print results
Expand Down
13 changes: 8 additions & 5 deletions speech/src/transcribe_sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_transcribe_sync]
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
use Google\Cloud\Speech\V2\RecognitionAudio;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\RecognizeRequest;

/**
* @param string $audioFile path to an audio file
Expand Down Expand Up @@ -56,7 +57,9 @@ function transcribe_sync(string $audioFile)
$client = new SpeechClient();

try {
$response = $client->recognize($config, $audio);
$recognizeRequest = (new RecognizeRequest())
->setRecognizer($config);
$response = $client->recognize($recognizeRequest);
foreach ($response->getResults() as $result) {
$alternatives = $result->getAlternatives();
$mostLikely = $alternatives[0];
Expand Down
13 changes: 8 additions & 5 deletions speech/src/transcribe_sync_gcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
namespace Google\Cloud\Samples\Speech;

# [START speech_transcribe_sync_gcs]
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;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
use Google\Cloud\Speech\V2\RecognitionAudio;
use Google\Cloud\Speech\V2\RecognitionConfig;
use Google\Cloud\Speech\V2\RecognizeRequest;

/**
* @param string $uri The Cloud Storage object to transcribe (gs://your-bucket-name/your-object-name)
Expand All @@ -53,7 +54,9 @@ function transcribe_sync_gcs(string $uri)
$client = new SpeechClient();

try {
$response = $client->recognize($config, $audio);
$recognizeRequest = (new RecognizeRequest())
->setRecognizer($config);
$response = $client->recognize($recognizeRequest);
foreach ($response->getResults() as $result) {
$alternatives = $result->getAlternatives();
$mostLikely = $alternatives[0];
Expand Down