Skip to content

Commit 17bf9d6

Browse files
jmdobrydandhlee
authored andcommitted
Add quickstart sample for the Cloud Speech API. [(#658)](#658)
1 parent 88b530c commit 17bf9d6

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

speech/snippets/README.rst.in

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Speech API
5+
short_name: Cloud Speech API
6+
url: https://cloud.google.com/speech/docs/
7+
description: >
8+
The `Google Cloud Speech API`_ enables easy integration of Google speech
9+
recognition technologies into developer applications. Send audio and receive
10+
a text transcription from the Cloud Speech API service.
11+
12+
setup:
13+
- auth
14+
- install_deps
15+
16+
samples:
17+
- name: Quickstart
18+
file: quickstart.py
19+
20+
cloud_client_library: true

speech/snippets/quickstart.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 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+
18+
def run_quickstart():
19+
# [START speech_quickstart]
20+
import io
21+
import os
22+
23+
# Imports the Google Cloud client library
24+
from google.cloud import speech
25+
26+
# Instantiates a client
27+
speech_client = speech.Client()
28+
29+
# The name of the audio file to transcribe
30+
file_name = os.path.join(
31+
os.path.dirname(__file__),
32+
'resources',
33+
'audio.raw')
34+
35+
# Loads the audio into memory
36+
with io.open(file_name, 'rb') as audio_file:
37+
content = audio_file.read()
38+
audio_sample = speech_client.sample(
39+
content,
40+
source_uri=None,
41+
encoding='LINEAR16',
42+
sample_rate=16000)
43+
44+
# Detects speech in the audio file
45+
alternatives = speech_client.speech_api.sync_recognize(audio_sample)
46+
47+
for alternative in alternatives:
48+
print('Transcript: {}'.format(alternative.transcript))
49+
# [END speech_quickstart]
50+
51+
52+
if __name__ == '__main__':
53+
run_quickstart()

speech/snippets/quickstart_test.py

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

speech/snippets/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-speech==0.21.0

speech/snippets/resources/audio.raw

56.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)