Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39ac99c

Browse files
committed
Add Speech beta samples
1 parent 3892e9a commit 39ac99c

File tree

3 files changed

+315
-0
lines changed

3 files changed

+315
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Google LLC
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+
# https://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+
# DO NOT EDIT! This is a generated sample ("Request", "speech_adaptation_beta")
18+
19+
# To install the latest published package dependency, execute the following:
20+
# pip install google-cloud-speech
21+
22+
# sample-metadata
23+
# title:
24+
# description: Performs synchronous speech recognition with speech adaptation.
25+
# usage: python3 samples/v1p1beta1/speech_adaptation_beta.py [--sample_rate_hertz 44100] [--language_code "en-US"] [--phrase "Brooklyn Bridge"] [--boost 20] [--uri_path "gs://cloud-samples-data/speech/brooklyn_bridge.mp3"]
26+
import sys
27+
28+
# [START speech_adaptation_beta]
29+
30+
from google.cloud import speech_v1p1beta1
31+
from google.cloud.speech_v1p1beta1 import enums
32+
import six
33+
34+
35+
def sample_recognize(sample_rate_hertz, language_code, phrase, boost, uri_path):
36+
"""
37+
Performs synchronous speech recognition with speech adaptation.
38+
39+
Args:
40+
sample_rate_hertz Sample rate in Hertz of the audio data sent in all
41+
`RecognitionAudio` messages. Valid values are: 8000-48000.
42+
language_code The language of the supplied audio.
43+
phrase Phrase "hints" help Speech-to-Text API recognize the specified phrases
44+
from your audio data.
45+
boost Positive value will increase the probability that a specific phrase will
46+
be recognized over other similar sounding phrases.
47+
uri_path Path to the audio file stored on GCS.
48+
"""
49+
# [START speech_adaptation_beta_core]
50+
51+
client = speech_v1p1beta1.SpeechClient()
52+
53+
# sample_rate_hertz = 44100
54+
# language_code = 'en-US'
55+
# phrase = 'Brooklyn Bridge'
56+
# boost = 20
57+
# uri_path = 'gs://cloud-samples-data/speech/brooklyn_bridge.mp3'
58+
59+
if isinstance(language_code, six.binary_type):
60+
language_code = language_code.decode("utf-8")
61+
if isinstance(phrase, six.binary_type):
62+
phrase = phrase.decode("utf-8")
63+
64+
if isinstance(uri_path, six.binary_type):
65+
uri_path = uri_path.decode("utf-8")
66+
encoding = enums.RecognitionConfig.AudioEncoding.MP3
67+
phrases = [phrase]
68+
speech_contexts_element = {"phrases": phrases, "boost": boost}
69+
speech_contexts = [speech_contexts_element]
70+
config = {
71+
"encoding": encoding,
72+
"sample_rate_hertz": sample_rate_hertz,
73+
"language_code": language_code,
74+
"speech_contexts": speech_contexts,
75+
}
76+
audio = {"uri": uri_path}
77+
78+
response = client.recognize(config, audio)
79+
for result in response.results:
80+
# First alternative is the most probable result
81+
alternative = result.alternatives[0]
82+
print("Transcript: {}".format(alternative.transcript))
83+
84+
# [END speech_adaptation_beta_core]
85+
86+
87+
# [END speech_adaptation_beta]
88+
89+
90+
def main():
91+
import argparse
92+
93+
parser = argparse.ArgumentParser()
94+
parser.add_argument("--sample_rate_hertz", type=int, default=44100)
95+
parser.add_argument("--language_code", type=str, default="en-US")
96+
parser.add_argument("--phrase", type=str, default="Brooklyn Bridge")
97+
parser.add_argument("--boost", type=float, default=20)
98+
parser.add_argument(
99+
"--uri_path",
100+
type=str,
101+
default="gs://cloud-samples-data/speech/brooklyn_bridge.mp3",
102+
)
103+
args = parser.parse_args()
104+
105+
sample_recognize(
106+
args.sample_rate_hertz,
107+
args.language_code,
108+
args.phrase,
109+
args.boost,
110+
args.uri_path,
111+
)
112+
113+
114+
if __name__ == "__main__":
115+
main()
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Google LLC
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+
# https://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+
# DO NOT EDIT! This is a generated sample ("Request", "speech_contexts_classes_beta")
18+
19+
# To install the latest published package dependency, execute the following:
20+
# pip install google-cloud-speech
21+
22+
# sample-metadata
23+
# title:
24+
# description: Performs synchronous speech recognition with static context classes.
25+
# usage: python3 samples/v1p1beta1/speech_contexts_classes_beta.py [--sample_rate_hertz 24000] [--language_code "en-US"] [--phrase "$TIME"] [--uri_path "gs://cloud-samples-data/speech/time.mp3"]
26+
import sys
27+
28+
# [START speech_contexts_classes_beta]
29+
30+
from google.cloud import speech_v1p1beta1
31+
from google.cloud.speech_v1p1beta1 import enums
32+
import six
33+
34+
35+
def sample_recognize(sample_rate_hertz, language_code, phrase, uri_path):
36+
"""
37+
Performs synchronous speech recognition with static context classes.
38+
39+
Args:
40+
sample_rate_hertz Sample rate in Hertz of the audio data sent in all
41+
`RecognitionAudio` messages. Valid values are: 8000-48000.
42+
language_code The language of the supplied audio.
43+
phrase Phrase "hints" help Speech-to-Text API recognize the specified phrases
44+
from your audio data. In this sample we are using a static class phrase
45+
($TIME). Classes represent groups of words that represent common concepts that
46+
occur in natural language. We recommend checking out the docs page for more
47+
info on static classes.
48+
uri_path Path to the audio file stored on GCS.
49+
"""
50+
# [START speech_contexts_classes_beta_core]
51+
52+
client = speech_v1p1beta1.SpeechClient()
53+
54+
# sample_rate_hertz = 24000
55+
# language_code = 'en-US'
56+
# phrase = '$TIME'
57+
# uri_path = 'gs://cloud-samples-data/speech/time.mp3'
58+
59+
if isinstance(language_code, six.binary_type):
60+
language_code = language_code.decode("utf-8")
61+
if isinstance(phrase, six.binary_type):
62+
phrase = phrase.decode("utf-8")
63+
if isinstance(uri_path, six.binary_type):
64+
uri_path = uri_path.decode("utf-8")
65+
encoding = enums.RecognitionConfig.AudioEncoding.MP3
66+
phrases = [phrase]
67+
speech_contexts_element = {"phrases": phrases}
68+
speech_contexts = [speech_contexts_element]
69+
config = {
70+
"encoding": encoding,
71+
"sample_rate_hertz": sample_rate_hertz,
72+
"language_code": language_code,
73+
"speech_contexts": speech_contexts,
74+
}
75+
audio = {"uri": uri_path}
76+
77+
response = client.recognize(config, audio)
78+
for result in response.results:
79+
# First alternative is the most probable result
80+
alternative = result.alternatives[0]
81+
print("Transcript: {}".format(alternative.transcript))
82+
83+
# [END speech_contexts_classes_beta_core]
84+
85+
86+
# [END speech_contexts_classes_beta]
87+
88+
89+
def main():
90+
import argparse
91+
92+
parser = argparse.ArgumentParser()
93+
parser.add_argument("--sample_rate_hertz", type=int, default=24000)
94+
parser.add_argument("--language_code", type=str, default="en-US")
95+
parser.add_argument("--phrase", type=str, default="$TIME")
96+
parser.add_argument(
97+
"--uri_path", type=str, default="gs://cloud-samples-data/speech/time.mp3"
98+
)
99+
args = parser.parse_args()
100+
101+
sample_recognize(
102+
args.sample_rate_hertz, args.language_code, args.phrase, args.uri_path
103+
)
104+
105+
106+
if __name__ == "__main__":
107+
main()
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Google LLC
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+
# https://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+
# DO NOT EDIT! This is a generated sample ("Request", "speech_quickstart_beta")
18+
19+
# To install the latest published package dependency, execute the following:
20+
# pip install google-cloud-speech
21+
22+
# sample-metadata
23+
# title:
24+
# description: Performs synchronous speech recognition on an audio file.
25+
# usage: python3 samples/v1p1beta1/speech_quickstart_beta.py [--sample_rate_hertz 44100] [--language_code "en-US"] [--uri_path "gs://cloud-samples-data/speech/brooklyn_bridge.mp3"]
26+
import sys
27+
28+
# [START speech_quickstart_beta]
29+
30+
from google.cloud import speech_v1p1beta1
31+
from google.cloud.speech_v1p1beta1 import enums
32+
import six
33+
34+
35+
def sample_recognize(sample_rate_hertz, language_code, uri_path):
36+
"""
37+
Performs synchronous speech recognition on an audio file.
38+
39+
Args:
40+
sample_rate_hertz Sample rate in Hertz of the audio data sent in all
41+
`RecognitionAudio` messages. Valid values are: 8000-48000.
42+
language_code The language of the supplied audio.
43+
uri_path Path to the audio file stored on GCS.
44+
"""
45+
# [START speech_quickstart_beta_core]
46+
47+
client = speech_v1p1beta1.SpeechClient()
48+
49+
# sample_rate_hertz = 44100
50+
# language_code = 'en-US'
51+
# uri_path = 'gs://cloud-samples-data/speech/brooklyn_bridge.mp3'
52+
53+
if isinstance(language_code, six.binary_type):
54+
language_code = language_code.decode("utf-8")
55+
if isinstance(uri_path, six.binary_type):
56+
uri_path = uri_path.decode("utf-8")
57+
encoding = enums.RecognitionConfig.AudioEncoding.MP3
58+
config = {
59+
"encoding": encoding,
60+
"sample_rate_hertz": sample_rate_hertz,
61+
"language_code": language_code,
62+
}
63+
audio = {"uri": uri_path}
64+
65+
response = client.recognize(config, audio)
66+
for result in response.results:
67+
transcript = result.alternatives[0].transcript
68+
print("Transcript: {}".format(transcript))
69+
70+
# [END speech_quickstart_beta_core]
71+
72+
73+
# [END speech_quickstart_beta]
74+
75+
76+
def main():
77+
import argparse
78+
79+
parser = argparse.ArgumentParser()
80+
parser.add_argument("--sample_rate_hertz", type=int, default=44100)
81+
parser.add_argument("--language_code", type=str, default="en-US")
82+
parser.add_argument(
83+
"--uri_path",
84+
type=str,
85+
default="gs://cloud-samples-data/speech/brooklyn_bridge.mp3",
86+
)
87+
args = parser.parse_args()
88+
89+
sample_recognize(args.sample_rate_hertz, args.language_code, args.uri_path)
90+
91+
92+
if __name__ == "__main__":
93+
main()

0 commit comments

Comments
 (0)