forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
168 lines (135 loc) · 6.95 KB
/
Copy pathclient.py
File metadata and controls
168 lines (135 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Basic client for Google Cloud Speech API."""
from google.cloud import client as client_module
from google.cloud.speech.connection import Connection
class Encoding(object):
"""Audio encoding types."""
LINEAR16 = 'LINEAR16'
"""LINEAR16 encoding type."""
FLAC = 'FLAC'
"""FLAC encoding type."""
MULAW = 'MULAW'
"""MULAW encoding type."""
AMR = 'AMR'
"""AMR encoding type."""
AMR_WB = 'AMR_WB'
"""AMR_WB encoding type."""
class Client(client_module.Client):
"""Client to bundle configuration needed for API requests.
:type project: str
:param project: the project which the client acts on behalf of. Will be
passed when creating a dataset / job. If not passed,
falls back to the default inferred from the environment.
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for the connection
owned by this client. If not passed (and if no ``http``
object is passed), falls back to the default inferred
from the environment.
:type http: :class:`httplib2.Http` or class that defines ``request()``.
:param http: An optional HTTP object to make requests. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
"""
_connection_class = Connection
def syncrecognize(self, content, uri, encoding, sample_rate,
language_code=None, max_alternatives=None,
profanity_filter=None,
speech_context=None):
"""Synchronous Speech Recognition.
.. _syncrecognize: https://cloud.google.com/speech/reference/\
rest/v1beta1/speech/syncrecognize
See `syncrecognize`_.
:type content: str
:param content: The content string containing the audio
data bytes encoded as specified in RecognitionConfig.
This is a base64-encoded string
:type uri: str
:param uri: URI that points to a file that contains audio
data bytes as specified in RecognitionConfig.
Currently, only Google Cloud Storage URIs are
supported, which must be specified in the following
format: gs://bucket_name/object_name
:type encoding: str
:param encoding: encoding of audio data sent in all RecognitionAudio
messages, can be one of: :attr:`~.Encoding.LINEAR16`,
:attr:`~.Encoding.FLAC`, :attr:`~.Encoding.MULAW`,
:attr:`~.Encoding.AMR`, :attr:`~.Encoding.AMR_WB`
:type sample_rate: int
:param sample_rate: Sample rate in Hertz of the audio data sent in all
RecognitionAudio messages. Valid values are: 8000-
48000. 16000 is optimal. For best results, set the
sampling rate of the audio source to 16000 Hz.
If that's not possible, use the native sample rate
of the audio source (instead of re-sampling).
:type language_code: str
:param language_code: (Optional) The language of the supplied audio as
BCP-47 language tag. Example: "en-GB".
If omitted, defaults to "en-US".
:type max_alternatives: int
:param max_alternatives: (Optional) Maximum number of recognition
hypotheses to be returned. The server may
return fewer than maxAlternatives.
Valid values are 0-30. A value of 0 or 1
will return a maximum of 1. Defaults to 1
:type profanity_filter: bool
:param profanity_filter: If True, the server will attempt to filter
out profanities, replacing all but the
initialcharacter in each filtered word with
asterisks, e.g. "f***". If False or omitted,
profanities won't be filtered out.
:type speech_context: list
:param speech_context: A list of strings (max 50) containing words and
phrases "hints" so that the speech recognition
is more likely to recognize them. This can be
used to improve the accuracy for specific words
and phrases. This can also be used to add new
words to the vocabulary of the recognizer.
:rtype: list
:returns: A list of tuples. One tuple for each alternative. Each tuple
contains a transcript text and a confidence value (between
0.0 and 1.0)
"""
if (content is None) and (uri is None):
message = 'content and uri cannot be both equal to None'
raise ValueError(message)
if (content is not None) and (uri is not None):
message = 'content and uri cannot be both different from None'
raise ValueError(message)
if content is not None:
audio = {'content': content}
else:
audio = {'uri': uri}
required_params = [('encoding', encoding), ('sampleRate', sample_rate)]
for param_name, param in required_params:
if param is None:
message = '%r cannot be None' % (param_name)
raise ValueError(message)
config = dict(required_params)
for param_name, param in [
('languageCode', language_code),
('maxAlternatives', max_alternatives),
('profanityFilter', profanity_filter)]:
if param is not None:
config[param_name] = param
if speech_context is not None:
config["speechContext"] = {"phrases": speech_context}
data = {
'audio': audio,
'config': config
}
api_response = self.connection.api_request(
method='POST', path='syncrecognize', data=data)
return api_response['results'][0]['alternatives']