Skip to content

Commit dad87eb

Browse files
authored
feat: upgrade speech samples to new surface (GoogleCloudPlatform#1887)
1 parent 54369af commit dad87eb

19 files changed

+704
-450
lines changed

speech/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ API using the transcribe command:
4444

4545
```sh
4646
php src/transcribe_sync.php test/data/audio32KHz.raw
47-
php src/transcribe_async.php test/data/audio32KHz.raw
4847
php src/transcribe_async_words.php test/data/audio32KHz.raw
4948
```
5049
## Troubleshooting

speech/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-speech": "^1.0.0",
4-
"google/cloud-storage": "^1.20.1"
3+
"google/cloud-speech": "^2.2",
4+
"google/cloud-storage": "^1.36"
55
}
66
}

speech/quickstart.php

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016 Google Inc.
3+
* Copyright 2023 Google LLC.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -20,30 +20,64 @@
2020
require __DIR__ . '/vendor/autoload.php';
2121

2222
# Imports the Google Cloud client library
23-
use Google\Cloud\Speech\V1\SpeechClient;
24-
use Google\Cloud\Speech\V1\RecognitionAudio;
25-
use Google\Cloud\Speech\V1\RecognitionConfig;
26-
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
23+
24+
use Google\Cloud\Speech\V2\Client\SpeechClient;
25+
use Google\Cloud\Speech\V2\CreateRecognizerRequest;
26+
use Google\Cloud\Speech\V2\ExplicitDecodingConfig;
27+
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
28+
use Google\Cloud\Speech\V2\RecognitionConfig;
29+
use Google\Cloud\Speech\V2\Recognizer;
30+
use Google\Cloud\Speech\V2\RecognizeRequest;
2731

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

31-
# set string as audio content
32-
$audio = (new RecognitionAudio())
33-
->setUri($gcsURI);
35+
# Your Google Cloud Project ID and location
36+
$projectId = 'YOUR_PROJECT_ID';
37+
$location = 'global';
38+
39+
# Instantiates a client
40+
$speech = new SpeechClient();
3441

35-
# The audio file's encoding, sample rate and language
36-
$config = new RecognitionConfig([
37-
'encoding' => AudioEncoding::LINEAR16,
38-
'sample_rate_hertz' => 16000,
39-
'language_code' => 'en-US'
42+
// Create a Recognizer
43+
$createRecognizerRequest = new CreateRecognizerRequest([
44+
'parent' => SpeechClient::locationName($projectId, $location),
45+
'recognizer_id' => $recognizerId = 'quickstart-recognizer-' . uniqid(),
46+
'recognizer' => new Recognizer([
47+
'language_codes' => ['en-US'],
48+
'model' => 'latest_short'
49+
])
4050
]);
4151

42-
# Instantiates a client
43-
$client = new SpeechClient();
52+
$operation = $speech->createRecognizer($createRecognizerRequest);
53+
54+
// Wait for the operation to complete
55+
$operation->pollUntilComplete();
56+
if ($operation->operationSucceeded()) {
57+
$result = $operation->getResult();
58+
printf('Created Recognizer: %s' . PHP_EOL, $result->getName());
59+
} else {
60+
print_r($operation->getError());
61+
}
62+
63+
$config = (new RecognitionConfig())
64+
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
65+
// ->setAutoDecodingConfig(new AutoDetectDecodingConfig());
66+
67+
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
68+
'encoding' => AudioEncoding::LINEAR16,
69+
'sample_rate_hertz' => 16000,
70+
'audio_channel_count' => 1,
71+
]));
72+
73+
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
74+
$request = (new RecognizeRequest())
75+
->setRecognizer($recognizerName)
76+
->setConfig($config)
77+
->setUri($gcsURI);
4478

4579
# Detects speech in the audio file
46-
$response = $client->recognize($config, $audio);
80+
$response = $speech->recognize($request);
4781

4882
# Print most likely transcription
4983
foreach ($response->getResults() as $result) {
@@ -53,6 +87,4 @@
5387
printf('Transcript: %s' . PHP_EOL, $transcript);
5488
}
5589

56-
$client->close();
57-
58-
# [END speech_quickstart]
90+
$speech->close();

speech/src/create_recognizer.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Speech;
19+
20+
use Google\Cloud\Speech\V2\Client\SpeechClient;
21+
use Google\Cloud\Speech\V2\CreateRecognizerRequest;
22+
use Google\Cloud\Speech\V2\Recognizer;
23+
24+
/**
25+
* Create a new recognizer.
26+
*
27+
* @param string $projectId The Google Cloud project ID.
28+
* @param string $location The location of the recognizer.
29+
* @param string $recognizerId The ID of the recognizer to create.
30+
* @param string $model The recognizer model. Use "chirp_3" for diarization.
31+
*/
32+
function create_recognizer(
33+
string $projectId,
34+
string $location,
35+
string $recognizerId,
36+
string $model = "latest_short"
37+
): void {
38+
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
39+
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
40+
41+
// Create a Recognizer
42+
$recognizer = new Recognizer([
43+
'language_codes' => ['en-US'],
44+
'model' => $model,
45+
]);
46+
47+
// Create the CreateRecognizerRequest
48+
$createRecognizerRequest = new CreateRecognizerRequest([
49+
'parent' => SpeechClient::locationName($projectId, $location),
50+
'recognizer_id' => $recognizerId,
51+
'recognizer' => $recognizer
52+
]);
53+
54+
// Call the createRecognizer method
55+
$operation = $speech->createRecognizer($createRecognizerRequest);
56+
57+
// Wait for the operation to complete
58+
$operation->pollUntilComplete();
59+
60+
if ($operation->operationSucceeded()) {
61+
$result = $operation->getResult();
62+
printf('Created Recognizer: %s' . PHP_EOL, $result->getName());
63+
} else {
64+
print_r($operation->getError());
65+
}
66+
67+
$speech->close();
68+
}
69+
70+
// The following 2 lines are only needed to run the samples
71+
require_once __DIR__ . '/../../testing/sample_helpers.php';
72+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

speech/src/delete_recognizer.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Speech;
19+
20+
use Google\Cloud\Speech\V2\Client\SpeechClient;
21+
use Google\Cloud\Speech\V2\DeleteRecognizerRequest;
22+
23+
/**
24+
* Delete a recognizer.
25+
*
26+
* @param string $projectId The Google Cloud project ID.
27+
* @param string $location The location of the recognizer.
28+
* @param string $recognizerId The ID of the recognizer to delete.
29+
*/
30+
function delete_recognizer(string $projectId, string $location, string $recognizerId): void
31+
{
32+
$apiEndpoint = $location === 'global' ? null : sprintf('%s-speech.googleapis.com', $location);
33+
$speech = new SpeechClient(['apiEndpoint' => $apiEndpoint]);
34+
35+
// Create the DeleteRecognizerRequest
36+
$deleteRecognizerRequest = new DeleteRecognizerRequest([
37+
'name' => SpeechClient::recognizerName($projectId, $location, $recognizerId)
38+
]);
39+
40+
// Call the deleteRecognizer method
41+
$operation = $speech->deleteRecognizer($deleteRecognizerRequest);
42+
43+
// Wait for the operation to complete
44+
$operation->pollUntilComplete();
45+
46+
if ($operation->operationSucceeded()) {
47+
printf('Deleted Recognizer: %s' . PHP_EOL, $deleteRecognizerRequest->getName());
48+
} else {
49+
print_r($operation->getError());
50+
}
51+
52+
$speech->close();
53+
}
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

speech/src/multi_region_gcs.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2021 Google Inc.
3+
* Copyright 2023 Google Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -18,37 +18,43 @@
1818
namespace Google\Cloud\Samples\Speech;
1919

2020
# [START speech_transcribe_with_multi_region_gcs]
21-
# Imports the Google Cloud client library
22-
use Google\Cloud\Speech\V1\SpeechClient;
23-
use Google\Cloud\Speech\V1\RecognitionAudio;
24-
use Google\Cloud\Speech\V1\RecognitionConfig;
25-
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
21+
use Google\Cloud\Speech\V2\Client\SpeechClient;
22+
use Google\Cloud\Speech\V2\RecognitionConfig;
23+
use Google\Cloud\Speech\V2\ExplicitDecodingConfig;
24+
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
25+
use Google\Cloud\Speech\V2\RecognizeRequest;
2626

2727
/**
28+
* @param string $projectId The Google Cloud project ID.
29+
* @param string $location The location of the recognizer.
30+
* @param string $recognizerId The ID of the recognizer to use (other than global).
2831
* @param string $uri The Cloud Storage object to transcribe
2932
* e.x. gs://cloud-samples-data/speech/brooklyn_bridge.raw
3033
*/
31-
function multi_region_gcs(string $uri)
34+
function multi_region_gcs(string $projectId, string $location, string $recognizerId, string $uri)
3235
{
33-
# set string as audio content
34-
$audio = (new RecognitionAudio())
35-
->setUri($uri);
36+
$options = ['apiEndpoint' => sprintf('%s-speech.googleapis.com', $location)];
37+
$speech = new SpeechClient($options);
38+
39+
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
3640

37-
# The audio file's encoding, sample rate and language
38-
$config = new RecognitionConfig([
39-
'encoding' => AudioEncoding::LINEAR16,
40-
'sample_rate_hertz' => 16000,
41-
'language_code' => 'en-US'
42-
]);
41+
$config = (new RecognitionConfig())
42+
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
43+
// ->setAutoDecodingConfig(new AutoDetectDecodingConfig());
4344

44-
# Specify a new endpoint.
45-
$options = ['apiEndpoint' => 'eu-speech.googleapis.com'];
45+
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
46+
'encoding' => AudioEncoding::LINEAR16,
47+
'sample_rate_hertz' => 16000,
48+
'audio_channel_count' => 1,
49+
]));
4650

47-
# Instantiates a client
48-
$client = new SpeechClient($options);
51+
$request = (new RecognizeRequest())
52+
->setRecognizer($recognizerName)
53+
->setConfig($config)
54+
->setUri($uri);
4955

5056
# Detects speech in the audio file
51-
$response = $client->recognize($config, $audio);
57+
$response = $speech->recognize($request);
5258

5359
# Print most likely transcription
5460
foreach ($response->getResults() as $result) {
@@ -57,8 +63,6 @@ function multi_region_gcs(string $uri)
5763
$transcript = $mostLikely->getTranscript();
5864
printf('Transcript: %s' . PHP_EOL, $transcript);
5965
}
60-
61-
$client->close();
6266
}
6367
# [END speech_transcribe_with_multi_region_gcs]
6468

0 commit comments

Comments
 (0)