Skip to content

Commit 0c7baa1

Browse files
committed
rewrite speech quickstart for v2
1 parent 5bf02d7 commit 0c7baa1

File tree

1 file changed

+57
-20
lines changed

1 file changed

+57
-20
lines changed

speech/quickstart.php

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,76 @@
1616
*/
1717

1818
# [START speech_quickstart]
19-
# Includes the autoloader for libraries installed with composer
19+
// Includes the autoloader for libraries installed with composer
2020
require __DIR__ . '/vendor/autoload.php';
2121

22-
# Imports the Google Cloud client library
22+
// Imports the Google Cloud client library
2323
use Google\Cloud\Speech\V2\Client\SpeechClient;
24-
use Google\Cloud\Speech\V2\ExplicitDecodingConfig\AudioEncoding;
25-
use Google\Cloud\Speech\V2\RecognitionAudio;
2624
use Google\Cloud\Speech\V2\RecognitionConfig;
25+
use Google\Cloud\Speech\V2\ExplicitDecodingConfig;
26+
use Google\Cloud\Speech\V2\Recognizer;
2727
use Google\Cloud\Speech\V2\RecognizeRequest;
28+
use Google\Cloud\Speech\V2\CreateRecognizerRequest;
29+
use Google\Cloud\Speech\V2\GetRecognizerRequest;
30+
use Google\ApiCore\ApiException;
2831

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

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

36-
# The audio file's encoding, sample rate and language
37-
$config = new RecognitionConfig([
38-
'encoding' => AudioEncoding::LINEAR16,
39-
'sample_rate_hertz' => 16000,
40-
'language_code' => 'en-US'
41-
]);
42-
43-
# Instantiates a client
40+
// Instantiates a client
4441
$client = new SpeechClient();
4542

46-
# Detects speech in the audio file
43+
// The name of the recognizer to create
44+
$recognizerName = $client->recognizerName($projectId, $location, $recognizerId);
45+
$getRecognizerRequest = (new GetRecognizerRequest())->setName($recognizerName);
46+
47+
try {
48+
$recognizer = $client->getRecognizer($getRecognizerRequest);
49+
} catch (ApiException $e) {
50+
if ($e->getStatus() === 'NOT_FOUND') {
51+
// If the recognizer does not exist, create it.
52+
53+
// Create an explicit decoding config because .raw files have no header.
54+
$explicitConfig = (new ExplicitDecodingConfig())
55+
->setEncoding(ExplicitDecodingConfig\AudioEncoding::LINEAR16)
56+
->setSampleRateHertz(16000)
57+
->setAudioChannelCount(1); // The brooklyn_bridge audio is single-channel (mono)
58+
59+
$config = (new RecognitionConfig())
60+
->setLanguageCodes(['en-US'])
61+
->setModel('long') // Or other models like 'telephony', 'medical_dictation'
62+
->setExplicitDecodingConfig($explicitConfig);
63+
64+
$recognizer = (new Recognizer())
65+
->setName($recognizerName)
66+
->setDefaultRecognitionConfig($config);
67+
68+
$createRecognizerRequest = (new CreateRecognizerRequest())
69+
->setParent($client->locationName($projectId, $location))
70+
->setRecognizer($recognizer)
71+
->setRecognizerId($recognizerId);
72+
$operation = $client->createRecognizer($createRecognizerRequest);
73+
74+
$operation->pollUntilComplete();
75+
$recognizer = $operation->getResult();
76+
printf('Created Recognizer: %s' . PHP_EOL, $recognizer->getName());
77+
} else {
78+
throw $e;
79+
}
80+
}
81+
82+
// Detects speech in the audio file
4783
$recognizeRequest = (new RecognizeRequest())
48-
->setRecognizer($config);
84+
->setRecognizer($recognizerName)
85+
->setUri($gcsUri);
4986
$response = $client->recognize($recognizeRequest);
5087

51-
# Print most likely transcription
88+
// Print most likely transcription
5289
foreach ($response->getResults() as $result) {
5390
$alternatives = $result->getAlternatives();
5491
$mostLikely = $alternatives[0];

0 commit comments

Comments
 (0)