google-cloud-speech 0.29.0 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +2 -1
  3. data/LICENSE +1 -1
  4. data/README.md +69 -43
  5. data/lib/google/cloud/speech.rb +94 -252
  6. data/lib/google/cloud/speech/v1.rb +11 -1
  7. data/lib/google/cloud/speech/v1/cloud_speech_services_pb.rb +1 -1
  8. data/lib/google/cloud/speech/{version.rb → v1/credentials.rb} +12 -2
  9. data/lib/google/cloud/speech/v1/doc/google/cloud/speech/v1/cloud_speech.rb +2 -2
  10. data/lib/google/cloud/speech/v1/doc/google/longrunning/operations.rb +92 -0
  11. data/lib/google/cloud/speech/v1/doc/google/protobuf/any.rb +1 -1
  12. data/lib/google/cloud/speech/v1/doc/google/protobuf/duration.rb +1 -1
  13. data/lib/google/cloud/speech/v1/doc/google/rpc/status.rb +1 -1
  14. data/lib/google/cloud/speech/v1/doc/overview.rb +1 -1
  15. data/lib/google/cloud/speech/v1/helpers.rb +93 -0
  16. data/lib/google/cloud/speech/v1/speech_client.rb +26 -49
  17. data/lib/google/cloud/speech/v1/speech_client_config.json +5 -5
  18. data/lib/google/cloud/speech/v1/stream.rb +614 -0
  19. data/lib/google/cloud/speech/v1p1beta1.rb +126 -0
  20. data/lib/google/cloud/speech/v1p1beta1/cloud_speech_pb.rb +175 -0
  21. data/lib/google/cloud/speech/v1p1beta1/cloud_speech_services_pb.rb +54 -0
  22. data/lib/google/cloud/speech/v1p1beta1/credentials.rb +32 -0
  23. data/lib/google/cloud/speech/v1p1beta1/doc/google/cloud/speech/v1p1beta1/cloud_speech.rb +625 -0
  24. data/lib/google/cloud/speech/v1p1beta1/doc/google/longrunning/operations.rb +92 -0
  25. data/lib/google/cloud/speech/v1p1beta1/doc/google/protobuf/any.rb +124 -0
  26. data/lib/google/cloud/speech/v1p1beta1/doc/google/protobuf/duration.rb +90 -0
  27. data/lib/google/cloud/speech/v1p1beta1/doc/google/rpc/status.rb +83 -0
  28. data/lib/google/cloud/speech/v1p1beta1/doc/overview.rb +73 -0
  29. data/lib/google/cloud/speech/v1p1beta1/helpers.rb +93 -0
  30. data/lib/google/cloud/speech/v1p1beta1/speech_client.rb +322 -0
  31. data/lib/google/cloud/speech/v1p1beta1/speech_client_config.json +41 -0
  32. data/lib/google/cloud/speech/v1p1beta1/stream.rb +614 -0
  33. metadata +29 -120
  34. data/lib/google-cloud-speech.rb +0 -142
  35. data/lib/google/cloud/speech/audio.rb +0 -330
  36. data/lib/google/cloud/speech/convert.rb +0 -46
  37. data/lib/google/cloud/speech/credentials.rb +0 -57
  38. data/lib/google/cloud/speech/operation.rb +0 -262
  39. data/lib/google/cloud/speech/project.rb +0 -651
  40. data/lib/google/cloud/speech/result.rb +0 -240
  41. data/lib/google/cloud/speech/service.rb +0 -121
  42. data/lib/google/cloud/speech/stream.rb +0 -564
@@ -1,4 +1,4 @@
1
- # Copyright 2017 Google LLC
1
+ # Copyright 2018 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
  require "google/cloud/speech/v1/speech_client"
16
+ require "google/cloud/speech/v1/helpers"
16
17
 
17
18
  module Google
18
19
  module Cloud
@@ -93,11 +94,18 @@ module Google
93
94
  # or the specified config is missing data points.
94
95
  # @param timeout [Numeric]
95
96
  # The default timeout, in seconds, for calls made through this client.
97
+ # @param metadata [Hash]
98
+ # Default metadata to be sent with each request. This can be overridden on a per call basis.
99
+ # @param exception_transformer [Proc]
100
+ # An optional proc that intercepts any exceptions raised during an API call to inject
101
+ # custom error handling.
96
102
  def self.new \
97
103
  credentials: nil,
98
104
  scopes: nil,
99
105
  client_config: nil,
100
106
  timeout: nil,
107
+ metadata: nil,
108
+ exception_transformer: nil,
101
109
  lib_name: nil,
102
110
  lib_version: nil
103
111
  kwargs = {
@@ -105,6 +113,8 @@ module Google
105
113
  scopes: scopes,
106
114
  client_config: client_config,
107
115
  timeout: timeout,
116
+ metadata: metadata,
117
+ exception_transformer: exception_transformer,
108
118
  lib_name: lib_name,
109
119
  lib_version: lib_version
110
120
  }.select { |_, v| v != nil }
@@ -7,7 +7,7 @@
7
7
  # you may not use this file except in compliance with the License.
8
8
  # You may obtain a copy of the License at
9
9
  #
10
- # https://www.apache.org/licenses/LICENSE-2.0
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
11
  #
12
12
  # Unless required by applicable law or agreed to in writing, software
13
13
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,4 +1,4 @@
1
- # Copyright 2016 Google LLC
1
+ # Copyright 2018 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -12,11 +12,21 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ require "googleauth"
15
16
 
16
17
  module Google
17
18
  module Cloud
18
19
  module Speech
19
- VERSION = "0.29.0".freeze
20
+ module V1
21
+ class Credentials < Google::Auth::Credentials
22
+ SCOPE = [
23
+ "https://www.googleapis.com/auth/cloud-platform"
24
+ ].freeze
25
+ PATH_ENV_VARS = %w(SPEECH_KEYFILE GOOGLE_CLOUD_KEYFILE GCLOUD_KEYFILE)
26
+ JSON_ENV_VARS = %w(SPEECH_KEYFILE_JSON GOOGLE_CLOUD_KEYFILE_JSON GCLOUD_KEYFILE_JSON)
27
+ DEFAULT_PATHS = ["~/.config/gcloud/application_default_credentials.json"]
28
+ end
29
+ end
20
30
  end
21
31
  end
22
32
  end
@@ -1,4 +1,4 @@
1
- # Copyright 2017 Google LLC
1
+ # Copyright 2018 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@ module Google
20
20
  #
21
21
  # | Class | Description |
22
22
  # | ----- | ----------- |
23
- # | [SpeechClient][] | Google Cloud Speech API. |
23
+ # | [SpeechClient][] | Service that implements Google Cloud Speech API. |
24
24
  # | [Data Types][] | Data types for Google::Cloud::Speech::V1 |
25
25
  #
26
26
  # [SpeechClient]: https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-speech/latest/google/cloud/speech/v1/speechclient
@@ -0,0 +1,92 @@
1
+ # Copyright 2018 Google LLC
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
+ # https://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
+ module Google
16
+ module Longrunning
17
+ # This resource represents a long-running operation that is the result of a
18
+ # network API call.
19
+ # @!attribute [rw] name
20
+ # @return [String]
21
+ # The server-assigned name, which is only unique within the same service that
22
+ # originally returns it. If you use the default HTTP mapping, the
23
+ # +name+ should have the format of +operations/some/unique/name+.
24
+ # @!attribute [rw] metadata
25
+ # @return [Google::Protobuf::Any]
26
+ # Service-specific metadata associated with the operation. It typically
27
+ # contains progress information and common metadata such as create time.
28
+ # Some services might not provide such metadata. Any method that returns a
29
+ # long-running operation should document the metadata type, if any.
30
+ # @!attribute [rw] done
31
+ # @return [true, false]
32
+ # If the value is +false+, it means the operation is still in progress.
33
+ # If true, the operation is completed, and either +error+ or +response+ is
34
+ # available.
35
+ # @!attribute [rw] error
36
+ # @return [Google::Rpc::Status]
37
+ # The error result of the operation in case of failure or cancellation.
38
+ # @!attribute [rw] response
39
+ # @return [Google::Protobuf::Any]
40
+ # The normal response of the operation in case of success. If the original
41
+ # method returns no data on success, such as +Delete+, the response is
42
+ # +google.protobuf.Empty+. If the original method is standard
43
+ # +Get+/+Create+/+Update+, the response should be the resource. For other
44
+ # methods, the response should have the type +XxxResponse+, where +Xxx+
45
+ # is the original method name. For example, if the original method name
46
+ # is +TakeSnapshot()+, the inferred response type is
47
+ # +TakeSnapshotResponse+.
48
+ class Operation; end
49
+
50
+ # The request message for {Google::Longrunning::Operations::GetOperation Operations::GetOperation}.
51
+ # @!attribute [rw] name
52
+ # @return [String]
53
+ # The name of the operation resource.
54
+ class GetOperationRequest; end
55
+
56
+ # The request message for {Google::Longrunning::Operations::ListOperations Operations::ListOperations}.
57
+ # @!attribute [rw] name
58
+ # @return [String]
59
+ # The name of the operation collection.
60
+ # @!attribute [rw] filter
61
+ # @return [String]
62
+ # The standard list filter.
63
+ # @!attribute [rw] page_size
64
+ # @return [Integer]
65
+ # The standard list page size.
66
+ # @!attribute [rw] page_token
67
+ # @return [String]
68
+ # The standard list page token.
69
+ class ListOperationsRequest; end
70
+
71
+ # The response message for {Google::Longrunning::Operations::ListOperations Operations::ListOperations}.
72
+ # @!attribute [rw] operations
73
+ # @return [Array<Google::Longrunning::Operation>]
74
+ # A list of operations that matches the specified filter in the request.
75
+ # @!attribute [rw] next_page_token
76
+ # @return [String]
77
+ # The standard List next-page token.
78
+ class ListOperationsResponse; end
79
+
80
+ # The request message for {Google::Longrunning::Operations::CancelOperation Operations::CancelOperation}.
81
+ # @!attribute [rw] name
82
+ # @return [String]
83
+ # The name of the operation resource to be cancelled.
84
+ class CancelOperationRequest; end
85
+
86
+ # The request message for {Google::Longrunning::Operations::DeleteOperation Operations::DeleteOperation}.
87
+ # @!attribute [rw] name
88
+ # @return [String]
89
+ # The name of the operation resource to be deleted.
90
+ class DeleteOperationRequest; end
91
+ end
92
+ end
@@ -1,4 +1,4 @@
1
- # Copyright 2017 Google LLC
1
+ # Copyright 2018 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright 2017 Google LLC
1
+ # Copyright 2018 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright 2017 Google LLC
1
+ # Copyright 2018 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright 2017 Google LLC
1
+ # Copyright 2018 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -0,0 +1,93 @@
1
+ # Copyright 2018 Google LLC
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
+ # https://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
+ require "google/cloud/speech/v1/speech_client"
16
+ require "google/cloud/speech/v1/stream"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Speech
21
+ module V1
22
+ class SpeechClient
23
+ # Performs bidirectional streaming speech recognition: receive results
24
+ # while sending audio. This method is only available via the gRPC API
25
+ # (not REST).
26
+ #
27
+ # @param [Google::Cloud::Speech::V1::StreamingRecognitionConfig,
28
+ # Hash] streaming_config
29
+ # Provides information to the recognizer that specifies how to
30
+ # process the request.
31
+ # A hash of the same form as
32
+ # +Google::Cloud::Speech::V1::StreamingRecognitionConfig+
33
+ # can also be provided.
34
+ # @param [Google::Gax::CallOptions] options
35
+ # Overrides the default settings for this call, e.g, timeout,
36
+ # retries, etc.
37
+ # @return [Google::Cloud::Speech::V1::Stream]
38
+ # An object that streams the requests and responses.
39
+ #
40
+ # @raise [Google::Gax::GaxError] if the RPC is aborted.
41
+ #
42
+ # @example
43
+ # require "google/cloud/speech"
44
+ #
45
+ # speech_client = Google::Cloud::Speech.new
46
+ # streaming_config = {
47
+ # config: {
48
+ # encoding: :linear16,
49
+ # language_code: "en-US",
50
+ # sample_rate_hertz: 16000
51
+ # }
52
+ # }
53
+ # stream = speech_client.streaming_recognize(streaming_config)
54
+ #
55
+ # # Stream 5 seconds of audio from the microphone
56
+ # # Actual implementation of microphone input varies by platform
57
+ # 5.times do
58
+ # stream.send MicrophoneInput.read(32000)
59
+ # end
60
+ #
61
+ # stream.stop
62
+ # stream.wait_until_complete!
63
+ #
64
+ # results = stream.results
65
+ # result = results.first.alternatives.first
66
+ # result.transcript #=> "how old is the Brooklyn Bridge"
67
+ # result.confidence #=> 0.9826789498329163
68
+ #
69
+ def streaming_recognize streaming_config, options: nil
70
+ if streaming_config.is_a?(::Hash) &&
71
+ streaming_config[:config] &&
72
+ streaming_config[:config][:encoding]
73
+ streaming_config[:config][:encoding] =
74
+ streaming_config[:config][:encoding].upcase
75
+ end
76
+ V1::Stream.new(
77
+ streaming_config,
78
+ proc do |reqs|
79
+ request_protos = reqs.lazy.map do |req|
80
+ Google::Gax.to_proto(
81
+ req,
82
+ Google::Cloud::Speech::V1::StreamingRecognizeRequest
83
+ )
84
+ end
85
+ @streaming_recognize.call(request_protos, options)
86
+ end
87
+ )
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -1,4 +1,4 @@
1
- # Copyright 2017 Google LLC
1
+ # Copyright 2018 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -18,9 +18,6 @@
18
18
  # and updates to that file get reflected here through a refresh process.
19
19
  # For the short term, the refresh process will only be runnable by Google
20
20
  # engineers.
21
- #
22
- # The only allowed edits are to method and file documentation. A 3-way
23
- # merge preserves those additions if the generated source changes.
24
21
 
25
22
  require "json"
26
23
  require "pathname"
@@ -30,7 +27,7 @@ require "google/gax/operation"
30
27
  require "google/longrunning/operations_client"
31
28
 
32
29
  require "google/cloud/speech/v1/cloud_speech_pb"
33
- require "google/cloud/speech/credentials"
30
+ require "google/cloud/speech/v1/credentials"
34
31
 
35
32
  module Google
36
33
  module Cloud
@@ -58,7 +55,7 @@ module Google
58
55
  ].freeze
59
56
 
60
57
  class OperationsClient < Google::Longrunning::OperationsClient
61
- SERVICE_ADDRESS = SERVICE_ADDRESS
58
+ self::SERVICE_ADDRESS = SpeechClient::SERVICE_ADDRESS
62
59
  end
63
60
 
64
61
  # @param credentials [Google::Auth::Credentials, String, Hash, GRPC::Core::Channel, GRPC::Core::ChannelCredentials, Proc]
@@ -85,11 +82,18 @@ module Google
85
82
  # or the specified config is missing data points.
86
83
  # @param timeout [Numeric]
87
84
  # The default timeout, in seconds, for calls made through this client.
85
+ # @param metadata [Hash]
86
+ # Default metadata to be sent with each request. This can be overridden on a per call basis.
87
+ # @param exception_transformer [Proc]
88
+ # An optional proc that intercepts any exceptions raised during an API call to inject
89
+ # custom error handling.
88
90
  def initialize \
89
91
  credentials: nil,
90
92
  scopes: ALL_SCOPES,
91
93
  client_config: {},
92
94
  timeout: DEFAULT_TIMEOUT,
95
+ metadata: nil,
96
+ exception_transformer: nil,
93
97
  lib_name: nil,
94
98
  lib_version: ""
95
99
  # These require statements are intentionally placed here to initialize
@@ -98,7 +102,7 @@ module Google
98
102
  require "google/gax/grpc"
99
103
  require "google/cloud/speech/v1/cloud_speech_services_pb"
100
104
 
101
- credentials ||= Google::Cloud::Speech::Credentials.default
105
+ credentials ||= Google::Cloud::Speech::V1::Credentials.default
102
106
 
103
107
  @operations_client = OperationsClient.new(
104
108
  credentials: credentials,
@@ -110,7 +114,7 @@ module Google
110
114
  )
111
115
 
112
116
  if credentials.is_a?(String) || credentials.is_a?(Hash)
113
- updater_proc = Google::Cloud::Speech::Credentials.new(credentials).updater_proc
117
+ updater_proc = Google::Cloud::Speech::V1::Credentials.new(credentials).updater_proc
114
118
  end
115
119
  if credentials.is_a?(GRPC::Core::Channel)
116
120
  channel = credentials
@@ -134,6 +138,7 @@ module Google
134
138
  google_api_client.freeze
135
139
 
136
140
  headers = { :"x-goog-api-client" => google_api_client }
141
+ headers.merge!(metadata) unless metadata.nil?
137
142
  client_config_file = Pathname.new(__dir__).join(
138
143
  "speech_client_config.json"
139
144
  )
@@ -145,7 +150,7 @@ module Google
145
150
  Google::Gax::Grpc::STATUS_CODE_NAMES,
146
151
  timeout,
147
152
  errors: Google::Gax::Grpc::API_ERRORS,
148
- kwargs: headers
153
+ metadata: headers
149
154
  )
150
155
  end
151
156
 
@@ -164,15 +169,18 @@ module Google
164
169
 
165
170
  @recognize = Google::Gax.create_api_call(
166
171
  @speech_stub.method(:recognize),
167
- defaults["recognize"]
172
+ defaults["recognize"],
173
+ exception_transformer: exception_transformer
168
174
  )
169
175
  @long_running_recognize = Google::Gax.create_api_call(
170
176
  @speech_stub.method(:long_running_recognize),
171
- defaults["long_running_recognize"]
177
+ defaults["long_running_recognize"],
178
+ exception_transformer: exception_transformer
172
179
  )
173
180
  @streaming_recognize = Google::Gax.create_api_call(
174
181
  @speech_stub.method(:streaming_recognize),
175
- defaults["streaming_recognize"]
182
+ defaults["streaming_recognize"],
183
+ exception_transformer: exception_transformer
176
184
  )
177
185
  end
178
186
 
@@ -193,6 +201,9 @@ module Google
193
201
  # @param options [Google::Gax::CallOptions]
194
202
  # Overrides the default settings for this call, e.g, timeout,
195
203
  # retries, etc.
204
+ # @yield [result, operation] Access the result along with the RPC operation
205
+ # @yieldparam result [Google::Cloud::Speech::V1::RecognizeResponse]
206
+ # @yieldparam operation [GRPC::ActiveCall::Operation]
196
207
  # @return [Google::Cloud::Speech::V1::RecognizeResponse]
197
208
  # @raise [Google::Gax::GaxError] if the RPC is aborted.
198
209
  # @example
@@ -214,13 +225,14 @@ module Google
214
225
  def recognize \
215
226
  config,
216
227
  audio,
217
- options: nil
228
+ options: nil,
229
+ &block
218
230
  req = {
219
231
  config: config,
220
232
  audio: audio
221
233
  }.delete_if { |_, v| v.nil? }
222
234
  req = Google::Gax::to_proto(req, Google::Cloud::Speech::V1::RecognizeRequest)
223
- @recognize.call(req, options)
235
+ @recognize.call(req, options, &block)
224
236
  end
225
237
 
226
238
  # Performs asynchronous speech recognition: receive results via the
@@ -303,41 +315,6 @@ module Google
303
315
  operation.on_done { |operation| yield(operation) } if block_given?
304
316
  operation
305
317
  end
306
-
307
- # Performs bidirectional streaming speech recognition: receive results while
308
- # sending audio. This method is only available via the gRPC API (not REST).
309
- #
310
- # @param reqs [Enumerable<Google::Cloud::Speech::V1::StreamingRecognizeRequest>]
311
- # The input requests.
312
- # @param options [Google::Gax::CallOptions]
313
- # Overrides the default settings for this call, e.g, timeout,
314
- # retries, etc.
315
- # @return [Enumerable<Google::Cloud::Speech::V1::StreamingRecognizeResponse>]
316
- # An enumerable of Google::Cloud::Speech::V1::StreamingRecognizeResponse instances.
317
- #
318
- # @raise [Google::Gax::GaxError] if the RPC is aborted.
319
- #
320
- # @note
321
- # EXPERIMENTAL:
322
- # Streaming requests are still undergoing review.
323
- # This method interface might change in the future.
324
- #
325
- # @example
326
- # require "google/cloud/speech/v1"
327
- #
328
- # speech_client = Google::Cloud::Speech::V1.new
329
- # request = {}
330
- # requests = [request]
331
- # speech_client.streaming_recognize(requests).each do |element|
332
- # # Process element.
333
- # end
334
-
335
- def streaming_recognize reqs, options: nil
336
- request_protos = reqs.lazy.map do |req|
337
- Google::Gax::to_proto(req, Google::Cloud::Speech::V1::StreamingRecognizeRequest)
338
- end
339
- @streaming_recognize.call(request_protos, options)
340
- end
341
318
  end
342
319
  end
343
320
  end