Class: Lara::AudioTranslator

Inherits:
Object
  • Object
show all
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

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.

Returns:

  • (String)

    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

#get_translated_transcript(id) ⇒ Lara::Models::AudioTextResult

Retrieves the translated transcript JSON.



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/lara/audio.rb', line 125

def get_translated_transcript(id)
  response = @client.get("/v2/audio/#{id}/translated-transcript")
  Lara::Models::AudioTextResult.new(
    id: response["id"],
    source: response["source"],
    target: response["target"],
    filename: response["filename"],
    duration: response["duration"],
    text: response["text"],
    translation: response["translation"],
    segments: response["segments"]
  )
end

#status(id) ⇒ Lara::Models::Audio

Fetch audio translation status.

Returns:



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

Returns:

  • (String)

    translated audio bytes



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

#translate_transcript(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, no_trace: false, style: nil) ⇒ Lara::Models::AudioTextResult

Translates an audio transcript end-to-end



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/lara/audio.rb', line 141

def translate_transcript(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil,
                         no_trace: false, style: nil)
  audio = upload_for_transcription(file_path: file_path, filename: filename, target: target, source: source,
                                   adapt_to: adapt_to, glossaries: glossaries, no_trace: no_trace, style: style)

  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 get_translated_transcript(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.

Returns:



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

#upload_for_transcription(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, no_trace: false, style: nil) ⇒ Lara::Models::Audio

Uploads an audio file to S3 and creates a transcript translation job.

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/lara/audio.rb', line 98

def upload_for_transcription(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil,
                             no_trace: false, style: 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
  }.compact

  headers = {}
  headers["X-No-Trace"] = "true" if no_trace

  response = @client.post("/v2/audio/translate-transcript", body: body, headers: headers)
  response_params = response.transform_keys(&:to_sym)
  Lara::Models::Audio.new(**filter_audio_params(response_params))
end