保存Google Cloud Speech API操作(作业)对象以便稍后检索结果

我正在努力使用ruby客户端(v0.22.2)使用Google Cloud Speech Api。

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

job.wait_until_done! 

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

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

有没有办法从操作名称创建作业对象并以这种方式检索它? 我似乎无法创建一个function新的作业对象,例如使用@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 

真的希望这对某人有意义。 谷歌的ruby客户端苦苦挣扎,因为一切似乎都被翻译成了比使用API​​所需的对象复杂得多的对象。 我在这里缺少一些技巧吗?

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

 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中描述的解决方法将此function修补为旧版本:

 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 

好。 有一个非常丑陋的方法来解决这个问题。

从作业对象获取操作的ID

 operation_id = job.grpc.grpc_op.name 

获取访问令牌以手动使用RestAPI

 json_key_io = StringIO.new(ENV["GOOGLE_CLOUD_SPEECH_JSON_KEY"]) authorisation = Google::Auth::ServiceAccountCredentials.make_creds( json_key_io:json_key_io, scope:"https://www.googleapis.com/auth/cloud-platform" ) token = authorisation.fetch_access_token! 

进行api调用以检索操作详细信息。

这将返回一个“done”=> true参数,一旦结果出现并将显示结果。 如果“done”=> true不存在,那么您将不得不再次轮询,直到它为止。

 HTTParty.get( "https://speech.googleapis.com/v1/operations/#{operation_id}", headers: {"Authorization" => "Bearer #{token['access_token']}"} ) 

必须有更好的方法来做到这一点。 似乎语音API有这么明显的用例。

任何来自谷歌的人都可以解释一个更简单/更清洁的方式吗?

Interesting Posts