Save Google Cloud Speech API operation(job) object to retrieve results later

☆樱花仙子☆ 提交于 2019-12-02 10:03:50

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

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

Ok. Have a very ugly way of solving the issue.

Get the id of the Operation from the job object

operation_id = job.grpc.grpc_op.name

Get an access token to manually use the 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!

Make an api call to retrieve the operation details.

This will return with a "done" => true parameter, once results are in and will display the results. If "done" => true isn't there then you'll have to poll again later until it is.

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

There must be a better way of doing that. Seems such an obvious use case for the speech API.

Anyone from google in the house who can explain a much simpler/cleaner way of doing it?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!