2017-08-31 80 views
0

我很努力將Ruby雲語音API用於ruby客戶端(v0.22.2)。保存Google雲語音API操作(作業)對象以稍後檢索結果

我能夠執行長時間運行的工作,如果我用

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 

真的希望這對某人有意義。 基於一切似乎都被翻譯成比使用API​​所需的對象更復雜的對象的基礎上,與Google的Ruby客戶端苦苦掙扎。 有沒有我在這裏失蹤的一些詭計?

回答

1

您可以將此功能修改爲您正在使用的版本,但我會建議升級到或更高版本。有了這些更新的版本,您可以使用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 
+0

這看起來方便多了。 我的問題,對我來說可能是獨一無二的,升級是它會轉移到與我正在做的其他事情(我們通過api與驅動器和Gmail集成)不兼容的不同版本的Google API客戶端,而無需一整件重工。他說,這顯然是絕大多數情況下應該做的事情。 – Carpela

+0

已更新,以顯示如何將功能更新到舊版本。 – blowmage

+0

在這裏遇到一些問題。 首先,猴子補丁使用'語音'而沒有定義它,這導致了未定義的方法錯誤。 NameError:未定義的局部變量或方法'語音'爲# 其次,如果解決該問題,from_grpc方法只接受該版本中的一個參數圖書館。 因此,您無法將speech.service對象傳遞給它。 ArgumentError:錯誤的參數數量(給定2,預計1) from .../vendor/bundle/gems/google-cloud-speech-0.22.2/lib/google/cloud/speech/job.rb: 151:在'from_grpc' – Carpela

0

好的。有一個非常醜陋的方式來解決這個問題。

從作業對象

operation_id = job.grpc.grpc_op.name 

獲取操作的ID獲取一個訪問令牌手動使用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的用例。

任何人都可以從房子裏的谷歌誰能解釋一個更簡單/更清潔的方式嗎?