Class: Lara::AudioTranslator
- Inherits:
-
Object
- Object
- Lara::AudioTranslator
- Defined in:
- lib/lara/audio.rb
Constant Summary collapse
- ALLOWED_AUDIO_PARAMS =
%i[ id status target filename source created_at updated_at options translated_seconds total_seconds error_reason ].freeze
Instance Method Summary collapse
-
#download(id) ⇒ String
Download translated audio bytes.
-
#initialize(client, s3_client = S3Client.new) ⇒ AudioTranslator
constructor
A new instance of AudioTranslator.
-
#status(id) ⇒ Lara::Models::Audio
Fetch audio translation status.
-
#translate(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, no_trace: false, style: nil, voice_gender: nil) ⇒ String
Translates an audio file end-to-end.
-
#upload(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, no_trace: false, style: nil, voice_gender: nil) ⇒ Lara::Models::Audio
Uploads an audio file to S3 and creates a translation job.
Constructor Details
#initialize(client, s3_client = S3Client.new) ⇒ AudioTranslator
Returns a new instance of AudioTranslator.
19 20 21 22 23 |
# File 'lib/lara/audio.rb', line 19 def initialize(client, s3_client = S3Client.new) @client = client @s3 = s3_client @polling_interval = 2 end |
Instance Method Details
#download(id) ⇒ String
Download translated audio bytes.
63 64 65 66 |
# File 'lib/lara/audio.rb', line 63 def download(id) url = @client.get("/v2/audio/#{id}/download-url")["url"] @s3.download(url: url) end |
#status(id) ⇒ Lara::Models::Audio
Fetch audio translation status.
55 56 57 58 59 |
# File 'lib/lara/audio.rb', line 55 def status(id) response = @client.get("/v2/audio/#{id}") response_params = response.transform_keys(&:to_sym) Lara::Models::Audio.new(**filter_audio_params(response_params)) end |
#translate(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, no_trace: false, style: nil, voice_gender: nil) ⇒ String
Translates an audio file end-to-end
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/lara/audio.rb', line 70 def translate(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, no_trace: false, style: nil, voice_gender: nil) audio = upload(file_path: file_path, filename: filename, target: target, source: source, adapt_to: adapt_to, glossaries: glossaries, no_trace: no_trace, style: style, voice_gender: voice_gender) max_wait_time = 60 * 15 # 15 minutes start = Time.now loop do |_| current = status(audio.id) case current.status when Lara::Models::AudioStatus::TRANSLATED return download(current.id) when Lara::Models::AudioStatus::ERROR raise Lara::LaraApiError.new(500, "AudioError", current.error_reason || "Unknown error") end raise Timeout::Error if Time.now - start > max_wait_time sleep @polling_interval end end |
#upload(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, no_trace: false, style: nil, voice_gender: nil) ⇒ Lara::Models::Audio
Uploads an audio file to S3 and creates a translation job.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lara/audio.rb', line 27 def upload(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, no_trace: false, style: nil, voice_gender: nil) response_data = @client.get("/v2/audio/upload-url", params: { filename: filename }) url = response_data["url"] fields = response_data["fields"] @s3.upload(url: url, fields: fields, io: file_path) body = { s3key: fields["key"], target: target, source: source, adapt_to: adapt_to, glossaries: glossaries, style: style, voice_gender: voice_gender }.compact headers = {} headers["X-No-Trace"] = "true" if no_trace response = @client.post("/v2/audio/translate", body: body, headers: headers) response_params = response.transform_keys(&:to_sym) Lara::Models::Audio.new(**filter_audio_params(response_params)) end |