【问题标题】:Save Google Cloud Speech API operation(job) object to retrieve results later保存 Google Cloud Speech API 操作(作业)对象以稍后检索结果
【发布时间】:2017-08-31 11:21:31
【问题描述】:

我正在努力将 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

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

【问题讨论】:

    标签: ruby google-cloud-speech


    【解决方案1】:

    您可以将此功能修补到您正在使用的版本,但我建议升级到google-cloud-speech 0.24.0 或更高版本。对于那些更新的版本,您可以使用 Operation#idProject#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 中描述的解决方法将此功能猴子修补到旧版本:

    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 api 客户端,这与我正在做的其他一些事情不兼容(我们通过 api 与驱动器和 gmail 集成)没有全部返工。也就是说,在绝大多数情况下,这显然是正确的做法。
    • 已更新以显示如何将功能修补到旧版本。
    • 在这里遇到了几个问题。首先,猴子补丁使用'speech'而不定义它,这会导致未定义的方法错误。 NameError: undefined local variable or method 'speech' for #<:cloud::speech::project:0x007fe77f6ad060> 其次,如果你解决了这个问题,from_grpc 方法只接受该版本库中的一个参数(id)。所以你不能将 speech.service 对象传递给它。 ArgumentError:来自 .../vendor/bundle/gems/google-cloud-speech-0.22.2/lib/google/cloud/speech/job.rb:151:in 的参数数量错误(给定 2,预期 1) from_grpc'
    【解决方案2】:

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

    从job对象中获取Operation的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 似乎是一个如此明显的用例。

    谷歌的任何人都可以解释一种更简单/更清洁的方法吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多