Skip to content

Commit 2cee8e4

Browse files
nitsanshaiNitsan Shainicain
authored
docs(samples): add quickstart code sample for STT V2 (#451)
Co-authored-by: Nitsan Shai <[email protected]> Co-authored-by: nicain <[email protected]>
1 parent ccac700 commit 2cee8e4

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

speech/snippets/quickstart_v2.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2022 Google Inc.
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+
# [START speech_quickstart_v2]
17+
import io
18+
19+
from google.cloud.speech_v2 import SpeechClient
20+
from google.cloud.speech_v2.types import cloud_speech
21+
22+
23+
def quickstart_v2(project_id, recognizer_id, audio_file):
24+
# Instantiates a client
25+
client = SpeechClient()
26+
27+
request = cloud_speech.CreateRecognizerRequest(
28+
parent=f"projects/{project_id}/locations/global",
29+
recognizer_id=recognizer_id,
30+
recognizer=cloud_speech.Recognizer(
31+
language_codes=["en-US"], model="latest_long"
32+
),
33+
)
34+
35+
# Creates a Recognizer
36+
operation = client.create_recognizer(request=request)
37+
recognizer = operation.result()
38+
39+
# Reads a file as bytes
40+
with io.open(audio_file, "rb") as f:
41+
content = f.read()
42+
43+
config = cloud_speech.RecognitionConfig(auto_decoding_config={})
44+
45+
request = cloud_speech.RecognizeRequest(
46+
recognizer=recognizer.name, config=config, content=content
47+
)
48+
49+
# Transcribes the audio into text
50+
response = client.recognize(request=request)
51+
52+
for result in response.results:
53+
print("Transcript: {}".format(result.alternatives[0].transcript))
54+
55+
return response
56+
57+
58+
# [END speech_quickstart_v2]
59+
60+
61+
if __name__ == "__main__":
62+
quickstart_v2()

speech/snippets/quickstart_v2_test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2022, 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 os
15+
import re
16+
from uuid import uuid4
17+
18+
from google.cloud.speech_v2 import SpeechClient
19+
from google.cloud.speech_v2.types import cloud_speech
20+
21+
import quickstart_v2
22+
23+
RESOURCES = os.path.join(os.path.dirname(__file__), "resources")
24+
25+
26+
def delete_recognizer(name):
27+
client = SpeechClient()
28+
request = cloud_speech.DeleteRecognizerRequest(name=name)
29+
client.delete_recognizer(request=request)
30+
31+
32+
def test_quickstart_v2(capsys):
33+
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
34+
35+
recognizer_id = "recognizer-" + str(uuid4())
36+
response = quickstart_v2.quickstart_v2(
37+
project_id, recognizer_id, os.path.join(RESOURCES, "audio.wav")
38+
)
39+
40+
assert re.search(
41+
r"how old is the Brooklyn Bridge",
42+
response.results[0].alternatives[0].transcript,
43+
re.DOTALL | re.I,
44+
)
45+
46+
delete_recognizer(
47+
f"projects/{project_id}/locations/global/recognizers/{recognizer_id}"
48+
)

speech/snippets/resources/audio.wav

56.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)