Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 0294a9a

Browse files
gguussbusunkim96
authored andcommitted
Add cloud-client samples for speech [(#775)](GoogleCloudPlatform/python-docs-samples#775)
1 parent e20f4b7 commit 0294a9a

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

samples/snippets/README.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,56 @@ To run this sample:
8282
$ python quickstart.py
8383
8484
85+
Transcribe
86+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
87+
88+
89+
90+
To run this sample:
91+
92+
.. code-block:: bash
93+
94+
$ python transcribe.py
95+
96+
usage: transcribe.py [-h] speech_file
97+
98+
Google Cloud Speech API sample application using the REST API for batch
99+
processing.
100+
101+
Example usage: python translate.py resources/audio.raw
102+
103+
positional arguments:
104+
speech_file Full path of audio file to be recognized
105+
106+
optional arguments:
107+
-h, --help show this help message and exit
108+
109+
110+
Transcribe async
111+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
112+
113+
114+
115+
To run this sample:
116+
117+
.. code-block:: bash
118+
119+
$ python transcribe_async.py
120+
121+
usage: transcribe_async.py [-h] speech_file
122+
123+
Google Cloud Speech API sample application using the REST API for async
124+
batch processing.
125+
126+
Example usage: python transcribe_async.py resources/audio.raw
127+
128+
positional arguments:
129+
speech_file Full path of audio file to be recognized
130+
131+
optional arguments:
132+
-h, --help show this help message and exit
133+
134+
85135
86136
87137
The client library

samples/snippets/README.rst.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@ setup:
1616
samples:
1717
- name: Quickstart
1818
file: quickstart.py
19+
- name: Transcribe
20+
file: transcribe.py
21+
show_help: true
22+
- name: Transcribe async
23+
file: transcribe_async.py
24+
show_help: true
1925

2026
cloud_client_library: true

samples/snippets/resources/audio2.raw

156 KB
Binary file not shown.

samples/snippets/transcribe.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Google Inc. All Rights Reserved.
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+
"""Google Cloud Speech API sample application using the REST API for batch
18+
processing.
19+
20+
Example usage: python translate.py resources/audio.raw
21+
"""
22+
23+
# [START import_libraries]
24+
import argparse
25+
import io
26+
# [END import_libraries]
27+
28+
29+
def main(speech_file):
30+
"""Transcribe the given audio file.
31+
32+
Args:
33+
speech_file: the name of the audio file.
34+
"""
35+
# [START authenticating]
36+
# Application default credentials provided by env variable
37+
# GOOGLE_APPLICATION_CREDENTIALS
38+
from google.cloud import speech
39+
speech_client = speech.Client()
40+
# [END authenticating]
41+
42+
# [START construct_request]
43+
# Loads the audio into memory
44+
with io.open(speech_file, 'rb') as audio_file:
45+
content = audio_file.read()
46+
audio_sample = speech_client.sample(
47+
content,
48+
source_uri=None,
49+
encoding='LINEAR16',
50+
sample_rate=16000)
51+
# [END construct_request]
52+
53+
# [START send_request]
54+
alternatives = speech_client.speech_api.sync_recognize(audio_sample)
55+
for alternative in alternatives:
56+
print('Transcript: {}'.format(alternative.transcript))
57+
# [END send_request]
58+
59+
60+
# [START run_application]
61+
if __name__ == '__main__':
62+
parser = argparse.ArgumentParser(
63+
description=__doc__,
64+
formatter_class=argparse.RawDescriptionHelpFormatter)
65+
parser.add_argument(
66+
'speech_file', help='Full path of audio file to be recognized')
67+
args = parser.parse_args()
68+
main(args.speech_file)
69+
# [END run_application]

samples/snippets/transcribe_async.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Google Inc. All Rights Reserved.
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+
"""Google Cloud Speech API sample application using the REST API for async
18+
batch processing.
19+
20+
Example usage: python transcribe_async.py resources/audio.raw
21+
"""
22+
23+
# [START import_libraries]
24+
import argparse
25+
import io
26+
import time
27+
# [END import_libraries]
28+
29+
30+
def main(speech_file):
31+
"""Transcribe the given audio file asynchronously.
32+
33+
Args:
34+
speech_file: the name of the audio file.
35+
"""
36+
# [START authenticating]
37+
# Application default credentials provided by env variable
38+
# GOOGLE_APPLICATION_CREDENTIALS
39+
from google.cloud import speech
40+
speech_client = speech.Client()
41+
# [END authenticating]
42+
43+
# [START construct_request]
44+
# Loads the audio into memory
45+
with io.open(speech_file, 'rb') as audio_file:
46+
content = audio_file.read()
47+
audio_sample = speech_client.sample(
48+
content,
49+
source_uri=None,
50+
encoding='LINEAR16',
51+
sample_rate=16000)
52+
# [END construct_request]
53+
54+
# [START send_request]
55+
operation = speech_client.speech_api.async_recognize(audio_sample)
56+
57+
retry_count = 100
58+
while retry_count > 0 and not operation.complete:
59+
retry_count -= 1
60+
time.sleep(2)
61+
operation.poll()
62+
63+
if not operation.complete:
64+
print("Operation not complete and retry limit reached.")
65+
return
66+
67+
alternatives = operation.results
68+
for alternative in alternatives:
69+
print('Transcript: {}'.format(alternative.transcript))
70+
print('Confidence: {}'.format(alternative.confidence))
71+
# [END send_request]
72+
73+
74+
# [START run_application]
75+
if __name__ == '__main__':
76+
parser = argparse.ArgumentParser(
77+
description=__doc__,
78+
formatter_class=argparse.RawDescriptionHelpFormatter)
79+
parser.add_argument(
80+
'speech_file', help='Full path of audio file to be recognized')
81+
args = parser.parse_args()
82+
main(args.speech_file)
83+
# [END run_application]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import re
15+
16+
from transcribe_async import main
17+
18+
19+
def test_main(resource, capsys):
20+
main(resource('audio.raw'))
21+
out, err = capsys.readouterr()
22+
23+
assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)

samples/snippets/transcribe_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import re
15+
16+
from transcribe import main
17+
18+
19+
def test_main(resource, capsys):
20+
main(resource('audio.raw'))
21+
out, err = capsys.readouterr()
22+
23+
assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)

0 commit comments

Comments
 (0)