我正在努力将 Google Cloud Speech Api 与 ruby 客户端 (v0.22.2) 一起使用。
我可以执行长时间运行的作业,并且如果使用的话可以获得结果
job.wait_until_done!
但这会导致服务器锁定很长一段时间。
根据 API 文档,我真正需要的只是操作名称(id)。
有没有办法从操作名称创建作业对象并以这种方式检索它?
我似乎无法创建一个功能性的新作业对象,例如使用 @grpc_op 中的 id
我想做的是这样的:
speech = Google::Cloud::Speech.new(auth_credentials)
job = speech.recognize_job file, options
saved_job = job.to_json #Or some element of that object such that I can retrieve it.
Later, I want to do something like....
job_object = Google::Cloud::Speech::Job.new(saved_job)
job.reload!
job.done?
job.results
真的希望这对某人有意义。
在使用 google 的 ruby 客户端时遇到了很大的困难,因为所有内容似乎都被转换为比使用 API 所需的对象复杂得多的对象。
我在这里缺少一些技巧吗?
您可以将此功能猴子修补到您正在使用的版本,但我建议升级到谷歌云语音 0.24.0 https://github.com/GoogleCloudPlatform/google-cloud-ruby/releases/tag/google-cloud-speech%2Fv0.24.0或稍后。对于那些更新的版本,您可以使用Operation#id https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-speech/latest/google/cloud/speech/operation?method=id-instance and Project#operation https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-speech/latest/google/cloud/speech/project?method=operation-instance来实现这一点。
require "google/cloud/speech"
speech = Google::Cloud::Speech.new
audio = speech.audio "path/to/audio.raw",
encoding: :linear16,
language: "en-US",
sample_rate: 16000
op = audio.process
# get the operation's id
id = op.id #=> "1234567890"
# construct a new operation object from the id
op2 = speech.operation id
# verify the jobs are the same
op.id == op2.id #=> true
op2.done? #=> false
op2.wait_until_done!
op2.done? #=> true
results = op2.results
Update由于您无法升级,因此您可以使用中描述的解决方法将此功能猴子修补到旧版本GoogleCloudPlatform/google-cloud-ruby#1214 https://github.com/GoogleCloudPlatform/google-cloud-ruby/issues/1214#issuecomment-278823829:
require "google/cloud/speech"
# Add monkey-patches
module Google
Module Cloud
Module Speech
class Job
def id
@grpc.name
end
end
class Project
def job id
Job.from_grpc(OpenStruct.new(name: id), speech.service).refresh!
end
end
end
end
end
# Use the new monkey-patched methods
speech = Google::Cloud::Speech.new
audio = speech.audio "path/to/audio.raw",
encoding: :linear16,
language: "en-US",
sample_rate: 16000
job = audio.recognize_job
# get the job's id
id = job.id #=> "1234567890"
# construct a new operation object from the id
job2 = speech.job id
# verify the jobs are the same
job.id == job2.id #=> true
job2.done? #=> false
job2.wait_until_done!
job2.done? #=> true
results = job2.results
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)