Skip to content

Commit ff3d873

Browse files
feat: adds a new multi region sample (GoogleCloudPlatform#1246)
1 parent 52ff214 commit ff3d873

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

speech/src/multi_region_gcs.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
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+
# [START speech_transcribe_with_multi_region_gcs]
19+
# Includes the autoloader for libraries installed with composer
20+
require __DIR__ . '/../vendor/autoload.php';
21+
22+
# 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;
27+
28+
# The name of the audio file to transcribe
29+
$gcsURI = "gs://cloud-samples-data/speech/brooklyn_bridge.raw";
30+
31+
# set string as audio content
32+
$audio = (new RecognitionAudio())
33+
->setUri($gcsURI);
34+
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'
40+
]);
41+
42+
# Specify a new endpoint.
43+
$options = ['apiEndpoint' => 'eu-speech.googleapis.com'];
44+
45+
# Instantiates a client
46+
$client = new SpeechClient($options);
47+
48+
# Detects speech in the audio file
49+
$response = $client->recognize($config, $audio);
50+
51+
# Print most likely transcription
52+
foreach ($response->getResults() as $result) {
53+
$alternatives = $result->getAlternatives();
54+
$mostLikely = $alternatives[0];
55+
$transcript = $mostLikely->getTranscript();
56+
printf('Transcript: %s' . PHP_EOL, $transcript);
57+
}
58+
59+
$client->close();
60+
# [END speech_transcribe_with_multi_region_gcs]

speech/src/profanity_filter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# [START profanity_filter]
15+
# [START speech_profanity_filter]
1616

1717
# Includes the autoloader for libraries installed with composer
1818
require __DIR__ . '/../vendor/autoload.php';
@@ -64,4 +64,4 @@
6464

6565
$client->close();
6666

67-
# [END profanity_filter]
67+
# [END speech_profanity_filter]

speech/src/profanity_filter_gcs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# [START profanity_filter_gcs]
15+
# [START speech_profanity_filter_gcs]
1616
# Includes the autoloader for libraries installed with composer
1717
require __DIR__ . '/../vendor/autoload.php';
1818

@@ -63,4 +63,4 @@
6363

6464
$client->close();
6565

66-
# [END profanity_filter_gcs]
66+
# [END speech_profanity_filter_gcs]

speech/test/speechTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testTranscribe($command, $audioFile, $requireGrpc = false)
7676
if ($requireGrpc && !extension_loaded('grpc')) {
7777
self::markTestSkipped('Must enable grpc extension.');
7878
}
79-
if (!self::$bucketName && in_array($command, ['transcribe_gcs', 'transcribe_async_gcs', 'profanity_filter_gcs'])) {
79+
if (!self::$bucketName && '_gcs' == substr($command, -4)) {
8080
$this->requireEnv('GOOGLE_STORAGE_BUCKET');
8181
}
8282
$output = $this->runSnippet($command, [$audioFile]);
@@ -99,6 +99,7 @@ public function provideTranscribe()
9999
['transcribe_async_gcs', 'gs://' . self::$bucketName . '/speech/audio32KHz.raw'],
100100
['transcribe_async_words', __DIR__ . '/data/audio32KHz.raw'],
101101
['profanity_filter_gcs', 'gs://' . self::$bucketName . '/speech/audio32KHz.raw'],
102+
['multi_region_gcs', 'gs://' . self::$bucketName . '/speech/audio32KHz.raw'],
102103
['profanity_filter', __DIR__ . '/data/audio32KHz.raw'],
103104
['streaming_recognize', __DIR__ . '/data/audio32KHz.raw', true],
104105
];

0 commit comments

Comments
 (0)