From 1ffb2918123a3ea43b952b0cf2fdc9fe322ca8ca Mon Sep 17 00:00:00 2001 From: Mend Renovate Date: Wed, 11 Jun 2025 23:02:17 +0000 Subject: [PATCH 1/3] fix(deps): update dependency google/cloud-speech to v2 --- speech/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/speech/composer.json b/speech/composer.json index 4e10c30a50..9f94ae8349 100644 --- a/speech/composer.json +++ b/speech/composer.json @@ -1,6 +1,6 @@ { "require": { - "google/cloud-speech": "^1.0.0", + "google/cloud-speech": "^2.0.0", "google/cloud-storage": "^1.20.1" } } From 5bf02d770c2d7be276c2056ca9b9ae98a7070130 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 11 Jun 2025 16:07:42 -0700 Subject: [PATCH 2/3] WIP --- speech/composer.json | 5 ++++- speech/quickstart.php | 13 ++++++++----- speech/src/multi_region_gcs.php | 13 ++++++++----- speech/src/profanity_filter.php | 13 ++++++++----- speech/src/profanity_filter_gcs.php | 13 ++++++++----- speech/src/streaming_recognize.php | 10 +++++----- speech/src/transcribe_async.php | 8 ++++---- speech/src/transcribe_async_gcs.php | 8 ++++---- speech/src/transcribe_async_words.php | 8 ++++---- speech/src/transcribe_auto_punctuation.php | 13 ++++++++----- speech/src/transcribe_enhanced_model.php | 13 ++++++++----- speech/src/transcribe_model_selection.php | 13 ++++++++----- speech/src/transcribe_sync.php | 13 ++++++++----- speech/src/transcribe_sync_gcs.php | 13 ++++++++----- 14 files changed, 93 insertions(+), 63 deletions(-) diff --git a/speech/composer.json b/speech/composer.json index 9f94ae8349..a2629350a1 100644 --- a/speech/composer.json +++ b/speech/composer.json @@ -1,6 +1,9 @@ { "require": { - "google/cloud-speech": "^2.0.0", + "google/cloud-speech": "^1.0.0", "google/cloud-storage": "^1.20.1" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.21" } } diff --git a/speech/quickstart.php b/speech/quickstart.php index 742e5892d7..8ce41c699e 100644 --- a/speech/quickstart.php +++ b/speech/quickstart.php @@ -20,10 +20,11 @@ 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; +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; # The name of the audio file to transcribe $gcsURI = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw'; @@ -43,7 +44,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) { diff --git a/speech/src/multi_region_gcs.php b/speech/src/multi_region_gcs.php index 2d65a9a783..3e4e4a2ff9 100644 --- a/speech/src/multi_region_gcs.php +++ b/speech/src/multi_region_gcs.php @@ -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 @@ -48,7 +49,9 @@ function multi_region_gcs(string $uri) $client = new SpeechClient($options); # 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) { diff --git a/speech/src/profanity_filter.php b/speech/src/profanity_filter.php index cbe5ba5554..fd159ef963 100644 --- a/speech/src/profanity_filter.php +++ b/speech/src/profanity_filter.php @@ -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 @@ -49,7 +50,9 @@ function profanity_filter(string $audioFile) $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) { diff --git a/speech/src/profanity_filter_gcs.php b/speech/src/profanity_filter_gcs.php index 609e19e9c1..cb8c5db371 100644 --- a/speech/src/profanity_filter_gcs.php +++ b/speech/src/profanity_filter_gcs.php @@ -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) @@ -46,7 +47,9 @@ function profanity_filter_gcs(string $uri) $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) { diff --git a/speech/src/streaming_recognize.php b/speech/src/streaming_recognize.php index 2465de4aee..f3e5b7439a 100644 --- a/speech/src/streaming_recognize.php +++ b/speech/src/streaming_recognize.php @@ -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 diff --git a/speech/src/transcribe_async.php b/speech/src/transcribe_async.php index 99fe72157c..d029507d6e 100644 --- a/speech/src/transcribe_async.php +++ b/speech/src/transcribe_async.php @@ -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 diff --git a/speech/src/transcribe_async_gcs.php b/speech/src/transcribe_async_gcs.php index 75d050091f..03d2317bef 100644 --- a/speech/src/transcribe_async_gcs.php +++ b/speech/src/transcribe_async_gcs.php @@ -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) diff --git a/speech/src/transcribe_async_words.php b/speech/src/transcribe_async_words.php index 0e7f12c0d3..135d0d3ffd 100644 --- a/speech/src/transcribe_async_words.php +++ b/speech/src/transcribe_async_words.php @@ -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 diff --git a/speech/src/transcribe_auto_punctuation.php b/speech/src/transcribe_auto_punctuation.php index 2eb1808f05..e6e0b905b4 100644 --- a/speech/src/transcribe_auto_punctuation.php +++ b/speech/src/transcribe_auto_punctuation.php @@ -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 @@ -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 diff --git a/speech/src/transcribe_enhanced_model.php b/speech/src/transcribe_enhanced_model.php index 8341552523..18b65e4512 100644 --- a/speech/src/transcribe_enhanced_model.php +++ b/speech/src/transcribe_enhanced_model.php @@ -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 @@ -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 diff --git a/speech/src/transcribe_model_selection.php b/speech/src/transcribe_model_selection.php index 3d5a97385f..02091a1344 100644 --- a/speech/src/transcribe_model_selection.php +++ b/speech/src/transcribe_model_selection.php @@ -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 @@ -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 diff --git a/speech/src/transcribe_sync.php b/speech/src/transcribe_sync.php index 82defef734..b0ec7b3dc3 100644 --- a/speech/src/transcribe_sync.php +++ b/speech/src/transcribe_sync.php @@ -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 @@ -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]; diff --git a/speech/src/transcribe_sync_gcs.php b/speech/src/transcribe_sync_gcs.php index 542f7c0e0f..a6ff74a5dc 100644 --- a/speech/src/transcribe_sync_gcs.php +++ b/speech/src/transcribe_sync_gcs.php @@ -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) @@ -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]; From 0c7baa1b4ed5b08a61ddbb0eb813f7dba34dae01 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 11 Jun 2025 16:32:14 -0700 Subject: [PATCH 3/3] rewrite speech quickstart for v2 --- speech/quickstart.php | 77 ++++++++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/speech/quickstart.php b/speech/quickstart.php index 8ce41c699e..753e906291 100644 --- a/speech/quickstart.php +++ b/speech/quickstart.php @@ -16,39 +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 +// Imports the Google Cloud client library 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\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 +// 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($config); + ->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];