保存 Google Cloud Speech API 操作(作业)对象以稍后检索结果 [英] Save Google Cloud Speech API operation(job) object to retrieve results later

查看:53
本文介绍了保存 Google Cloud Speech API 操作(作业)对象以稍后检索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将 Google Cloud Speech Api 与 ruby​​ 客户端 (v0.22.2) 一起使用.

I'm struggling to use the Google Cloud Speech Api with the ruby client (v0.22.2).

我可以执行长时间运行的作业,如果我使用,我可以获得结果

I can execute long running jobs and can get results if I use

job.wait_until_done!

但这会锁定服务器很长一段时间.

but this locks up a server for what can be a long period of time.

根据 API 文档,我真正需要的是操作名称(id).

According to the API docs, all I really need is the operation name(id).

有没有办法根据操作名称创建作业对象并以这种方式检索它?我似乎无法创建一个功能性的新作业对象,例如使用来自 @grpc_op

Is there any way of creating a job object from the operation name and retrieving it that way? I can't seem to create a functional new job object such as to use the id from @grpc_op

我想做的是:

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 所需的对象复杂得多的对象.有什么我在这里遗漏的技巧吗?

Really hoping that makes sense to somebody. Struggling quite a bit with google's ruby clients on the basis that everything seems to be translated into objects which are much more complex than the ones required to use the API. Is there some trick that I'm missing here?

推荐答案

您可以将此功能修补到您正在使用的版本,但我建议升级到 google-cloud-speech 0.24.0 或更高版本.对于那些更新的版本,您可以使用 Operation#idProject#operation 来完成此操作.

You can monkey-patch this functionality to the version you are using, but I would advise upgrading to google-cloud-speech 0.24.0 or later. With those more current versions you can use Operation#id and Project#operation to accomplish this.

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

更新由于您无法升级,您可以使用 GoogleCloudPlatform/google-cloud-ruby#1214:

Update Since you can't upgrade, you can monkey-patch this functionality to an older-version using the workaround described in GoogleCloudPlatform/google-cloud-ruby#1214:

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

这篇关于保存 Google Cloud Speech API 操作(作业)对象以稍后检索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆