google-cloud-speech 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/google-cloud-speech.rb +117 -0
- data/lib/google/cloud/speech.rb +146 -0
- data/lib/google/cloud/speech/audio.rb +230 -0
- data/lib/google/cloud/speech/credentials.rb +32 -0
- data/lib/google/cloud/speech/job.rb +178 -0
- data/lib/google/cloud/speech/project.rb +435 -0
- data/lib/google/cloud/speech/result.rb +82 -0
- data/lib/google/cloud/speech/service.rb +107 -0
- data/lib/google/cloud/speech/v1beta1.rb +17 -0
- data/lib/google/cloud/speech/v1beta1/cloud_speech_pb.rb +116 -0
- data/lib/google/cloud/speech/v1beta1/cloud_speech_services_pb.rb +54 -0
- data/lib/google/cloud/speech/v1beta1/speech_api.rb +208 -0
- data/lib/google/cloud/speech/v1beta1/speech_client_config.json +43 -0
- data/lib/google/cloud/speech/version.rb +22 -0
- metadata +256 -0
@@ -0,0 +1,82 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
require "google/cloud/speech/v1beta1"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Speech
|
21
|
+
##
|
22
|
+
# # Result
|
23
|
+
#
|
24
|
+
# A speech recognition result corresponding to a portion of the audio.
|
25
|
+
#
|
26
|
+
# See {Project#recognize} and {Job#results}.
|
27
|
+
#
|
28
|
+
# @see https://cloud.google.com/speech/reference/rpc/google.cloud.speech.v1beta1#google.cloud.speech.v1beta1.SpeechRecognitionResult
|
29
|
+
# SpeechRecognitionResult
|
30
|
+
#
|
31
|
+
# @attr_reader [String] transcript Transcript text representing the words
|
32
|
+
# that the user spoke.
|
33
|
+
# @attr_reader [Float] confidence The confidence estimate between 0.0 and
|
34
|
+
# 1.0. A higher number means the system is more confident that the
|
35
|
+
# recognition is correct. This field is typically provided only for the
|
36
|
+
# top hypothesis. A value of 0.0 is a sentinel value indicating
|
37
|
+
# confidence was not set.
|
38
|
+
# @attr_reader [Array<Result>] alternatives Additional recognition
|
39
|
+
# hypotheses (up to the value specified in `max_alternatives`).
|
40
|
+
#
|
41
|
+
# @example
|
42
|
+
# require "google/cloud"
|
43
|
+
#
|
44
|
+
# gcloud = Google::Cloud.new
|
45
|
+
# speech = gcloud.speech
|
46
|
+
#
|
47
|
+
# audio = speech.audio "path/to/audio.raw",
|
48
|
+
# encoding: :raw, sample_rate: 16000
|
49
|
+
# results = audio.recognize
|
50
|
+
#
|
51
|
+
# result = results.first
|
52
|
+
# result.transcript #=> "how old is the Brooklyn Bridge"
|
53
|
+
# result.confidence #=> 88.15
|
54
|
+
# alternative = result.alternatives.first
|
55
|
+
# alternative.transcript #=> "how old is the Brooklyn brim"
|
56
|
+
# alternative.confidence #=> 22.39
|
57
|
+
#
|
58
|
+
class Result
|
59
|
+
attr_reader :transcript, :confidence, :alternatives
|
60
|
+
|
61
|
+
##
|
62
|
+
# @private Creates a new Results instance.
|
63
|
+
def initialize transcript, confidence, alternatives = []
|
64
|
+
@transcript = transcript
|
65
|
+
@confidence = confidence
|
66
|
+
@alternatives = alternatives
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# @private New Results from a SpeechRecognitionAlternative object.
|
71
|
+
def self.from_grpc grpc
|
72
|
+
head, *tail = *grpc.alternatives
|
73
|
+
return nil if head.nil?
|
74
|
+
alternatives = tail.map do |alt|
|
75
|
+
new alt.transcript, alt.confidence
|
76
|
+
end
|
77
|
+
new head.transcript, head.confidence, alternatives
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
require "google/cloud/errors"
|
17
|
+
require "google/cloud/speech/credentials"
|
18
|
+
require "google/cloud/speech/version"
|
19
|
+
require "google/cloud/speech/v1beta1"
|
20
|
+
|
21
|
+
module Google
|
22
|
+
module Cloud
|
23
|
+
module Speech
|
24
|
+
##
|
25
|
+
# @private Represents the gRPC Speech service, including all the API
|
26
|
+
# methods.
|
27
|
+
class Service
|
28
|
+
attr_accessor :project, :credentials, :host, :timeout, :client_config
|
29
|
+
|
30
|
+
##
|
31
|
+
# Creates a new Service instance.
|
32
|
+
def initialize project, credentials, host: nil, timeout: nil,
|
33
|
+
client_config: nil
|
34
|
+
@project = project
|
35
|
+
@credentials = credentials
|
36
|
+
@host = host || V1beta1::SpeechApi::SERVICE_ADDRESS
|
37
|
+
@timeout = timeout
|
38
|
+
@client_config = client_config || {}
|
39
|
+
end
|
40
|
+
|
41
|
+
def channel
|
42
|
+
GRPC::Core::Channel.new host, nil, chan_creds
|
43
|
+
end
|
44
|
+
|
45
|
+
def chan_creds
|
46
|
+
return credentials if insecure?
|
47
|
+
GRPC::Core::ChannelCredentials.new.compose \
|
48
|
+
GRPC::Core::CallCredentials.new credentials.client.updater_proc
|
49
|
+
end
|
50
|
+
|
51
|
+
def service
|
52
|
+
return mocked_service if mocked_service
|
53
|
+
@service ||= \
|
54
|
+
V1beta1::SpeechApi.new(
|
55
|
+
service_path: host,
|
56
|
+
channel: channel,
|
57
|
+
timeout: timeout,
|
58
|
+
client_config: client_config,
|
59
|
+
app_name: "google-cloud-speech",
|
60
|
+
app_version: Google::Cloud::Speech::VERSION)
|
61
|
+
end
|
62
|
+
attr_accessor :mocked_service
|
63
|
+
|
64
|
+
def ops
|
65
|
+
return mocked_ops if mocked_ops
|
66
|
+
@ops ||= begin
|
67
|
+
require "google/longrunning/operations_services_pb"
|
68
|
+
|
69
|
+
Google::Longrunning::Operations::Stub.new(
|
70
|
+
host, chan_creds, timeout: timeout)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
attr_accessor :mocked_ops
|
74
|
+
|
75
|
+
def insecure?
|
76
|
+
credentials == :this_channel_is_insecure
|
77
|
+
end
|
78
|
+
|
79
|
+
def recognize_sync audio, config
|
80
|
+
execute { service.sync_recognize config, audio }
|
81
|
+
end
|
82
|
+
|
83
|
+
def recognize_async audio, config
|
84
|
+
execute { service.async_recognize config, audio }
|
85
|
+
end
|
86
|
+
|
87
|
+
def get_op name
|
88
|
+
req = Google::Longrunning::GetOperationRequest.new name: name
|
89
|
+
execute { ops.get_operation req }
|
90
|
+
end
|
91
|
+
|
92
|
+
def inspect
|
93
|
+
"#{self.class}(#{@project})"
|
94
|
+
end
|
95
|
+
|
96
|
+
protected
|
97
|
+
|
98
|
+
def execute
|
99
|
+
require "grpc" # Ensure GRPC is loaded before rescuing exception
|
100
|
+
yield
|
101
|
+
rescue GRPC::BadStatus => e
|
102
|
+
raise Google::Cloud::Error.from_error(e)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
require "google/cloud/speech/v1beta1/speech_api"
|
16
|
+
# Load the protobufs so code can see these classes
|
17
|
+
require "google/cloud/speech/v1beta1/cloud_speech_pb"
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
2
|
+
# source: google/cloud/speech/v1beta1/cloud_speech.proto
|
3
|
+
|
4
|
+
require 'google/protobuf'
|
5
|
+
|
6
|
+
require 'google/api/annotations_pb'
|
7
|
+
require 'google/longrunning/operations_pb'
|
8
|
+
require 'google/protobuf/timestamp_pb'
|
9
|
+
require 'google/rpc/status_pb'
|
10
|
+
Google::Protobuf::DescriptorPool.generated_pool.build do
|
11
|
+
add_message "google.cloud.speech.v1beta1.SyncRecognizeRequest" do
|
12
|
+
optional :config, :message, 1, "google.cloud.speech.v1beta1.RecognitionConfig"
|
13
|
+
optional :audio, :message, 2, "google.cloud.speech.v1beta1.RecognitionAudio"
|
14
|
+
end
|
15
|
+
add_message "google.cloud.speech.v1beta1.AsyncRecognizeRequest" do
|
16
|
+
optional :config, :message, 1, "google.cloud.speech.v1beta1.RecognitionConfig"
|
17
|
+
optional :audio, :message, 2, "google.cloud.speech.v1beta1.RecognitionAudio"
|
18
|
+
end
|
19
|
+
add_message "google.cloud.speech.v1beta1.StreamingRecognizeRequest" do
|
20
|
+
oneof :streaming_request do
|
21
|
+
optional :streaming_config, :message, 1, "google.cloud.speech.v1beta1.StreamingRecognitionConfig"
|
22
|
+
optional :audio_content, :bytes, 2
|
23
|
+
end
|
24
|
+
end
|
25
|
+
add_message "google.cloud.speech.v1beta1.StreamingRecognitionConfig" do
|
26
|
+
optional :config, :message, 1, "google.cloud.speech.v1beta1.RecognitionConfig"
|
27
|
+
optional :single_utterance, :bool, 2
|
28
|
+
optional :interim_results, :bool, 3
|
29
|
+
end
|
30
|
+
add_message "google.cloud.speech.v1beta1.RecognitionConfig" do
|
31
|
+
optional :encoding, :enum, 1, "google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding"
|
32
|
+
optional :sample_rate, :int32, 2
|
33
|
+
optional :language_code, :string, 3
|
34
|
+
optional :max_alternatives, :int32, 4
|
35
|
+
optional :profanity_filter, :bool, 5
|
36
|
+
optional :speech_context, :message, 6, "google.cloud.speech.v1beta1.SpeechContext"
|
37
|
+
end
|
38
|
+
add_enum "google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding" do
|
39
|
+
value :ENCODING_UNSPECIFIED, 0
|
40
|
+
value :LINEAR16, 1
|
41
|
+
value :FLAC, 2
|
42
|
+
value :MULAW, 3
|
43
|
+
value :AMR, 4
|
44
|
+
value :AMR_WB, 5
|
45
|
+
end
|
46
|
+
add_message "google.cloud.speech.v1beta1.SpeechContext" do
|
47
|
+
repeated :phrases, :string, 1
|
48
|
+
end
|
49
|
+
add_message "google.cloud.speech.v1beta1.RecognitionAudio" do
|
50
|
+
oneof :audio_source do
|
51
|
+
optional :content, :bytes, 1
|
52
|
+
optional :uri, :string, 2
|
53
|
+
end
|
54
|
+
end
|
55
|
+
add_message "google.cloud.speech.v1beta1.SyncRecognizeResponse" do
|
56
|
+
repeated :results, :message, 2, "google.cloud.speech.v1beta1.SpeechRecognitionResult"
|
57
|
+
end
|
58
|
+
add_message "google.cloud.speech.v1beta1.AsyncRecognizeResponse" do
|
59
|
+
repeated :results, :message, 2, "google.cloud.speech.v1beta1.SpeechRecognitionResult"
|
60
|
+
end
|
61
|
+
add_message "google.cloud.speech.v1beta1.AsyncRecognizeMetadata" do
|
62
|
+
optional :progress_percent, :int32, 1
|
63
|
+
optional :start_time, :message, 2, "google.protobuf.Timestamp"
|
64
|
+
optional :last_update_time, :message, 3, "google.protobuf.Timestamp"
|
65
|
+
end
|
66
|
+
add_message "google.cloud.speech.v1beta1.StreamingRecognizeResponse" do
|
67
|
+
optional :error, :message, 1, "google.rpc.Status"
|
68
|
+
repeated :results, :message, 2, "google.cloud.speech.v1beta1.StreamingRecognitionResult"
|
69
|
+
optional :result_index, :int32, 3
|
70
|
+
optional :endpointer_type, :enum, 4, "google.cloud.speech.v1beta1.StreamingRecognizeResponse.EndpointerType"
|
71
|
+
end
|
72
|
+
add_enum "google.cloud.speech.v1beta1.StreamingRecognizeResponse.EndpointerType" do
|
73
|
+
value :ENDPOINTER_EVENT_UNSPECIFIED, 0
|
74
|
+
value :START_OF_SPEECH, 1
|
75
|
+
value :END_OF_SPEECH, 2
|
76
|
+
value :END_OF_AUDIO, 3
|
77
|
+
value :END_OF_UTTERANCE, 4
|
78
|
+
end
|
79
|
+
add_message "google.cloud.speech.v1beta1.StreamingRecognitionResult" do
|
80
|
+
repeated :alternatives, :message, 1, "google.cloud.speech.v1beta1.SpeechRecognitionAlternative"
|
81
|
+
optional :is_final, :bool, 2
|
82
|
+
optional :stability, :float, 3
|
83
|
+
end
|
84
|
+
add_message "google.cloud.speech.v1beta1.SpeechRecognitionResult" do
|
85
|
+
repeated :alternatives, :message, 1, "google.cloud.speech.v1beta1.SpeechRecognitionAlternative"
|
86
|
+
end
|
87
|
+
add_message "google.cloud.speech.v1beta1.SpeechRecognitionAlternative" do
|
88
|
+
optional :transcript, :string, 1
|
89
|
+
optional :confidence, :float, 2
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
module Google
|
94
|
+
module Cloud
|
95
|
+
module Speech
|
96
|
+
module V1beta1
|
97
|
+
SyncRecognizeRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.SyncRecognizeRequest").msgclass
|
98
|
+
AsyncRecognizeRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.AsyncRecognizeRequest").msgclass
|
99
|
+
StreamingRecognizeRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.StreamingRecognizeRequest").msgclass
|
100
|
+
StreamingRecognitionConfig = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.StreamingRecognitionConfig").msgclass
|
101
|
+
RecognitionConfig = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.RecognitionConfig").msgclass
|
102
|
+
RecognitionConfig::AudioEncoding = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding").enummodule
|
103
|
+
SpeechContext = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.SpeechContext").msgclass
|
104
|
+
RecognitionAudio = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.RecognitionAudio").msgclass
|
105
|
+
SyncRecognizeResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.SyncRecognizeResponse").msgclass
|
106
|
+
AsyncRecognizeResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.AsyncRecognizeResponse").msgclass
|
107
|
+
AsyncRecognizeMetadata = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.AsyncRecognizeMetadata").msgclass
|
108
|
+
StreamingRecognizeResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.StreamingRecognizeResponse").msgclass
|
109
|
+
StreamingRecognizeResponse::EndpointerType = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.StreamingRecognizeResponse.EndpointerType").enummodule
|
110
|
+
StreamingRecognitionResult = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.StreamingRecognitionResult").msgclass
|
111
|
+
SpeechRecognitionResult = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.SpeechRecognitionResult").msgclass
|
112
|
+
SpeechRecognitionAlternative = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.cloud.speech.v1beta1.SpeechRecognitionAlternative").msgclass
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
2
|
+
# Source: google/cloud/speech/v1beta1/cloud_speech.proto for package 'google.cloud.speech.v1beta1'
|
3
|
+
# Original file comments:
|
4
|
+
# Copyright 2016 Google Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'grpc'
|
20
|
+
require 'google/cloud/speech/v1beta1/cloud_speech_pb'
|
21
|
+
|
22
|
+
module Google
|
23
|
+
module Cloud
|
24
|
+
module Speech
|
25
|
+
module V1beta1
|
26
|
+
module Speech
|
27
|
+
# Service that implements Google Cloud Speech API.
|
28
|
+
class Service
|
29
|
+
|
30
|
+
include GRPC::GenericService
|
31
|
+
|
32
|
+
self.marshal_class_method = :encode
|
33
|
+
self.unmarshal_class_method = :decode
|
34
|
+
self.service_name = 'google.cloud.speech.v1beta1.Speech'
|
35
|
+
|
36
|
+
# Perform synchronous speech-recognition: receive results after all audio
|
37
|
+
# has been sent and processed.
|
38
|
+
rpc :SyncRecognize, SyncRecognizeRequest, SyncRecognizeResponse
|
39
|
+
# Perform asynchronous speech-recognition: receive results via the
|
40
|
+
# google.longrunning.Operations interface. Returns either an
|
41
|
+
# `Operation.error` or an `Operation.response` which contains
|
42
|
+
# an `AsyncRecognizeResponse` message.
|
43
|
+
rpc :AsyncRecognize, AsyncRecognizeRequest, Google::Longrunning::Operation
|
44
|
+
# Perform bidirectional streaming speech-recognition: receive results while
|
45
|
+
# sending audio. This method is only available via the gRPC API (not REST).
|
46
|
+
rpc :StreamingRecognize, stream(StreamingRecognizeRequest), stream(StreamingRecognizeResponse)
|
47
|
+
end
|
48
|
+
|
49
|
+
Stub = Service.rpc_stub_class
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,208 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
# EDITING INSTRUCTIONS
|
16
|
+
# This file was generated from the file
|
17
|
+
# https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto,
|
18
|
+
# and updates to that file get reflected here through a refresh process.
|
19
|
+
# For the short term, the refresh process will only be runnable by Google
|
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
|
+
|
25
|
+
require "json"
|
26
|
+
require "pathname"
|
27
|
+
|
28
|
+
require "google/gax"
|
29
|
+
|
30
|
+
module Google
|
31
|
+
module Cloud
|
32
|
+
module Speech
|
33
|
+
module V1beta1
|
34
|
+
# Service that implements Google Cloud Speech API.
|
35
|
+
#
|
36
|
+
# @!attribute [r] speech_stub
|
37
|
+
# @return [Google::Cloud::Speech::V1beta1::Speech::Stub]
|
38
|
+
class SpeechApi
|
39
|
+
attr_reader :speech_stub
|
40
|
+
|
41
|
+
# The default address of the service.
|
42
|
+
SERVICE_ADDRESS = "speech.googleapis.com".freeze
|
43
|
+
|
44
|
+
# The default port of the service.
|
45
|
+
DEFAULT_SERVICE_PORT = 443
|
46
|
+
|
47
|
+
CODE_GEN_NAME_VERSION = "gapic/0.1.0".freeze
|
48
|
+
|
49
|
+
DEFAULT_TIMEOUT = 30
|
50
|
+
|
51
|
+
# The scopes needed to make gRPC calls to all of the methods defined in
|
52
|
+
# this service.
|
53
|
+
ALL_SCOPES = [
|
54
|
+
"https://www.googleapis.com/auth/cloud-platform"
|
55
|
+
].freeze
|
56
|
+
|
57
|
+
# @param service_path [String]
|
58
|
+
# The domain name of the API remote host.
|
59
|
+
# @param port [Integer]
|
60
|
+
# The port on which to connect to the remote host.
|
61
|
+
# @param channel [Channel]
|
62
|
+
# A Channel object through which to make calls.
|
63
|
+
# @param chan_creds [Grpc::ChannelCredentials]
|
64
|
+
# A ChannelCredentials for the setting up the RPC client.
|
65
|
+
# @param client_config[Hash]
|
66
|
+
# A Hash for call options for each method. See
|
67
|
+
# Google::Gax#construct_settings for the structure of
|
68
|
+
# this data. Falls back to the default config if not specified
|
69
|
+
# or the specified config is missing data points.
|
70
|
+
# @param timeout [Numeric]
|
71
|
+
# The default timeout, in seconds, for calls made through this client.
|
72
|
+
# @param app_name [String]
|
73
|
+
# The codename of the calling service.
|
74
|
+
# @param app_version [String]
|
75
|
+
# The version of the calling service.
|
76
|
+
def initialize \
|
77
|
+
service_path: SERVICE_ADDRESS,
|
78
|
+
port: DEFAULT_SERVICE_PORT,
|
79
|
+
channel: nil,
|
80
|
+
chan_creds: nil,
|
81
|
+
scopes: ALL_SCOPES,
|
82
|
+
client_config: {},
|
83
|
+
timeout: DEFAULT_TIMEOUT,
|
84
|
+
app_name: "gax",
|
85
|
+
app_version: Google::Gax::VERSION
|
86
|
+
# These require statements are intentionally placed here to initialize
|
87
|
+
# the gRPC module only when it's required.
|
88
|
+
# See https://github.com/googleapis/toolkit/issues/446
|
89
|
+
require "google/gax/grpc"
|
90
|
+
require "google/cloud/speech/v1beta1/cloud_speech_services_pb"
|
91
|
+
|
92
|
+
google_api_client = "#{app_name}/#{app_version} " \
|
93
|
+
"#{CODE_GEN_NAME_VERSION} gax/#{Google::Gax::VERSION} " \
|
94
|
+
"ruby/#{RUBY_VERSION}".freeze
|
95
|
+
headers = { :"x-goog-api-client" => google_api_client }
|
96
|
+
client_config_file = Pathname.new(__dir__).join(
|
97
|
+
"speech_client_config.json"
|
98
|
+
)
|
99
|
+
defaults = client_config_file.open do |f|
|
100
|
+
Google::Gax.construct_settings(
|
101
|
+
"google.cloud.speech.v1beta1.Speech",
|
102
|
+
JSON.parse(f.read),
|
103
|
+
client_config,
|
104
|
+
Google::Gax::Grpc::STATUS_CODE_NAMES,
|
105
|
+
timeout,
|
106
|
+
errors: Google::Gax::Grpc::API_ERRORS,
|
107
|
+
kwargs: headers
|
108
|
+
)
|
109
|
+
end
|
110
|
+
@speech_stub = Google::Gax::Grpc.create_stub(
|
111
|
+
service_path,
|
112
|
+
port,
|
113
|
+
chan_creds: chan_creds,
|
114
|
+
channel: channel,
|
115
|
+
scopes: scopes,
|
116
|
+
&Google::Cloud::Speech::V1beta1::Speech::Stub.method(:new)
|
117
|
+
)
|
118
|
+
|
119
|
+
@sync_recognize = Google::Gax.create_api_call(
|
120
|
+
@speech_stub.method(:sync_recognize),
|
121
|
+
defaults["sync_recognize"]
|
122
|
+
)
|
123
|
+
@async_recognize = Google::Gax.create_api_call(
|
124
|
+
@speech_stub.method(:async_recognize),
|
125
|
+
defaults["async_recognize"]
|
126
|
+
)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Service calls
|
130
|
+
|
131
|
+
# Perform synchronous speech-recognition: receive results after all audio
|
132
|
+
# has been sent and processed.
|
133
|
+
#
|
134
|
+
# @param config [Google::Cloud::Speech::V1beta1::RecognitionConfig]
|
135
|
+
# [Required] The +config+ message provides information to the recognizer
|
136
|
+
# that specifies how to process the request.
|
137
|
+
# @param audio [Google::Cloud::Speech::V1beta1::RecognitionAudio]
|
138
|
+
# [Required] The audio data to be recognized.
|
139
|
+
# @param options [Google::Gax::CallOptions]
|
140
|
+
# Overrides the default settings for this call, e.g, timeout,
|
141
|
+
# retries, etc.
|
142
|
+
# @return [Google::Cloud::Speech::V1beta1::SyncRecognizeResponse]
|
143
|
+
# @raise [Google::Gax::GaxError] if the RPC is aborted.
|
144
|
+
# @example
|
145
|
+
# require "google/cloud/speech/v1beta1/speech_api"
|
146
|
+
#
|
147
|
+
# RecognitionAudio = Google::Cloud::Speech::V1beta1::RecognitionAudio
|
148
|
+
# RecognitionConfig = Google::Cloud::Speech::V1beta1::RecognitionConfig
|
149
|
+
# SpeechApi = Google::Cloud::Speech::V1beta1::SpeechApi
|
150
|
+
#
|
151
|
+
# speech_api = SpeechApi.new
|
152
|
+
# config = RecognitionConfig.new
|
153
|
+
# audio = RecognitionAudio.new
|
154
|
+
# response = speech_api.sync_recognize(config, audio)
|
155
|
+
|
156
|
+
def sync_recognize \
|
157
|
+
config,
|
158
|
+
audio,
|
159
|
+
options: nil
|
160
|
+
req = Google::Cloud::Speech::V1beta1::SyncRecognizeRequest.new(
|
161
|
+
config: config,
|
162
|
+
audio: audio
|
163
|
+
)
|
164
|
+
@sync_recognize.call(req, options)
|
165
|
+
end
|
166
|
+
|
167
|
+
# Perform asynchronous speech-recognition: receive results via the
|
168
|
+
# google.longrunning.Operations interface. Returns either an
|
169
|
+
# +Operation.error+ or an +Operation.response+ which contains
|
170
|
+
# an +AsyncRecognizeResponse+ message.
|
171
|
+
#
|
172
|
+
# @param config [Google::Cloud::Speech::V1beta1::RecognitionConfig]
|
173
|
+
# [Required] The +config+ message provides information to the recognizer
|
174
|
+
# that specifies how to process the request.
|
175
|
+
# @param audio [Google::Cloud::Speech::V1beta1::RecognitionAudio]
|
176
|
+
# [Required] The audio data to be recognized.
|
177
|
+
# @param options [Google::Gax::CallOptions]
|
178
|
+
# Overrides the default settings for this call, e.g, timeout,
|
179
|
+
# retries, etc.
|
180
|
+
# @return [Google::Longrunning::Operation]
|
181
|
+
# @raise [Google::Gax::GaxError] if the RPC is aborted.
|
182
|
+
# @example
|
183
|
+
# require "google/cloud/speech/v1beta1/speech_api"
|
184
|
+
#
|
185
|
+
# RecognitionAudio = Google::Cloud::Speech::V1beta1::RecognitionAudio
|
186
|
+
# RecognitionConfig = Google::Cloud::Speech::V1beta1::RecognitionConfig
|
187
|
+
# SpeechApi = Google::Cloud::Speech::V1beta1::SpeechApi
|
188
|
+
#
|
189
|
+
# speech_api = SpeechApi.new
|
190
|
+
# config = RecognitionConfig.new
|
191
|
+
# audio = RecognitionAudio.new
|
192
|
+
# response = speech_api.async_recognize(config, audio)
|
193
|
+
|
194
|
+
def async_recognize \
|
195
|
+
config,
|
196
|
+
audio,
|
197
|
+
options: nil
|
198
|
+
req = Google::Cloud::Speech::V1beta1::AsyncRecognizeRequest.new(
|
199
|
+
config: config,
|
200
|
+
audio: audio
|
201
|
+
)
|
202
|
+
@async_recognize.call(req, options)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|