Skip to content

Commit 825006d

Browse files
nitsanshaiNitsan Shainicain
authored andcommitted
docs(samples): add create_recognizer code sample (#450)
Co-authored-by: Nitsan Shai <[email protected]> Co-authored-by: nicain <[email protected]>
1 parent 400d183 commit 825006d

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

speech/snippets/create_recognizer.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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_create_recognizer]
17+
from google.cloud.speech_v2 import SpeechClient
18+
from google.cloud.speech_v2.types import cloud_speech
19+
20+
21+
def create_recognizer(project_id, recognizer_id):
22+
# Instantiates a client
23+
client = SpeechClient()
24+
25+
request = cloud_speech.CreateRecognizerRequest(
26+
parent=f"projects/{project_id}/locations/global",
27+
recognizer_id=recognizer_id,
28+
recognizer=cloud_speech.Recognizer(
29+
language_codes=["en-US"], model="latest_long"
30+
),
31+
)
32+
33+
operation = client.create_recognizer(request=request)
34+
recognizer = operation.result()
35+
36+
print("Created Recognizer:", recognizer.name)
37+
return recognizer
38+
39+
40+
# [END speech_create_recognizer]
41+
42+
43+
if __name__ == "__main__":
44+
create_recognizer()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
from uuid import uuid4
16+
17+
from google.cloud.speech_v2 import SpeechClient
18+
from google.cloud.speech_v2.types import cloud_speech
19+
20+
import create_recognizer
21+
22+
23+
def delete_recognizer(name):
24+
client = SpeechClient()
25+
request = cloud_speech.DeleteRecognizerRequest(name=name)
26+
client.delete_recognizer(request=request)
27+
28+
29+
def test_create_recognizer(capsys):
30+
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
31+
32+
recognizer = create_recognizer.create_recognizer(
33+
project_id, "recognizer-" + str(uuid4())
34+
)
35+
delete_recognizer(recognizer.name)

0 commit comments

Comments
 (0)