Skip to content

Commit a63a93e

Browse files
authored
Revert "removes async_recognize, speech v1 now requires gcs uri" (GoogleCloudPlatform#347)
This reverts commit a93570d.
1 parent 1362362 commit a63a93e

File tree

4 files changed

+73
-22
lines changed

4 files changed

+73
-22
lines changed

speech/api/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"files": [
1414
"src/functions/streaming_recognize.php",
15+
"src/functions/transcribe_async.php",
1516
"src/functions/transcribe_async_gcs.php",
1617
"src/functions/transcribe_sync.php",
1718
"src/functions/transcribe_sync_gcs.php"

speech/api/src/TranscribeCommand.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace Google\Cloud\Samples\Speech;
1919

20-
use LogicException;
2120
use Symfony\Component\Console\Command\Command;
2221
use Symfony\Component\Console\Input\InputArgument;
2322
use Symfony\Component\Console\Input\InputInterface;
@@ -109,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
109108
}
110109
} else {
111110
if ($input->getOption('async')) {
112-
throw new LogicException('Async requests require a GCS URI');
111+
transcribe_async($audioFile, $languageCode, $options);
113112
}
114113
if ($input->getOption('stream')) {
115114
$encodingInt = constant("google\cloud\speech\\v1\RecognitionConfig\AudioEncoding::$encoding");
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
/**
3+
* Copyright 2016 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+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/speech/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Speech;
25+
26+
use Exception;
27+
# [START transcribe_async]
28+
use Google\Cloud\Speech\SpeechClient;
29+
use Google\Cloud\Core\ExponentialBackoff;
30+
31+
/**
32+
* Transcribe an audio file using Google Cloud Speech API
33+
* Example:
34+
* ```
35+
* transcribe_async('/path/to/audiofile.wav');
36+
* ```.
37+
*
38+
* @param string $audioFile path to an audio file.
39+
* @param string $languageCode The language of the content to
40+
* be recognized. Accepts BCP-47 (e.g., `"en-US"`, `"es-ES"`).
41+
* @param array $options configuration options.
42+
*
43+
* @return string the text transcription
44+
*/
45+
function transcribe_async($audioFile, $languageCode = 'en-US', $options = [])
46+
{
47+
$speech = new SpeechClient([
48+
'languageCode' => $languageCode,
49+
]);
50+
$operation = $speech->beginRecognizeOperation(
51+
fopen($audioFile, 'r'),
52+
$options
53+
);
54+
$backoff = new ExponentialBackoff(10);
55+
$backoff->execute(function () use ($operation) {
56+
print('Waiting for operation to complete' . PHP_EOL);
57+
$operation->reload();
58+
if (!$operation->isComplete()) {
59+
throw new Exception('Job has not yet completed', 500);
60+
}
61+
});
62+
63+
if ($operation->isComplete()) {
64+
if (empty($results = $operation->results())) {
65+
$results = $operation->info();
66+
}
67+
print_r($results);
68+
}
69+
}
70+
# [END transcribe_async]

speech/api/test/TranscribeCommandTest.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,33 +60,14 @@ public function testTranscribe($audioFile, $encoding, $sampleRate, $options = []
6060
$this->expectOutputRegex("/how old is the Brooklyn Bridge/");
6161
}
6262

63-
/**
64-
* @expectedException \LogicException
65-
* @expectedExceptionMessage Async requests require a GCS URI
66-
*/
67-
public function testTranscribeAsyncThrowsException()
68-
{
69-
$application = new Application();
70-
$application->add(new TranscribeCommand());
71-
$commandTester = new CommandTester($application->get('transcribe'));
72-
$commandTester->execute(
73-
[
74-
'audio-file' => __DIR__ . '/data/audio32KHz.raw',
75-
'--encoding' => 'LINEAR16',
76-
'--sample-rate' => '32000',
77-
'--async' => true,
78-
],
79-
['interactive' => false]
80-
);
81-
}
82-
8363
public function provideTranscribe()
8464
{
8565
self::$bucketName = getenv('GOOGLE_BUCKET_NAME');
8666
return [
8767
[__DIR__ . '/data/audio32KHz.raw', 'LINEAR16', '32000'],
8868
[__DIR__ . '/data/audio32KHz.flac', 'FLAC', '32000'],
8969
[__DIR__ . '/data/audio32KHz.raw', 'LINEAR16', '32000', ['--stream' => true]],
70+
[__DIR__ . '/data/audio32KHz.raw', 'LINEAR16', '32000', ['--async' => true]],
9071
['gs://' . self::$bucketName . '/audio32KHz.raw', 'LINEAR16', '32000'],
9172
['gs://' . self::$bucketName . '/audio32KHz.raw', 'LINEAR16', '32000', ['--async' => true]],
9273
];

0 commit comments

Comments
 (0)