Class: ActiveIntelligence::ASR::AwsAdapter

Inherits:
Adapter show all
Includes:
Concerns::Aws, Concerns::S3
Defined in:
lib/active_intelligence/asr/aws_adapter.rb

Defined Under Namespace

Classes: Diarizer, TranscriptionFailed

Instance Attribute Summary

Attributes inherited from ActiveIntelligence::Adapter

#settings

Instance Method Summary collapse

Methods inherited from Adapter

#logger

Methods inherited from ActiveIntelligence::Adapter

#initialize

Constructor Details

This class inherits a constructor from ActiveIntelligence::Adapter

Instance Method Details

#clientObject

private



32
33
34
35
36
37
38
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 32

def client
  @client ||= Aws::TranscribeService::Client.new(
    region: settings[:region],
    access_key_id: settings[:access_key_id],
    secret_access_key: settings[:secret_access_key]
  )
end

#diarize(path, options = {}) ⇒ Object



24
25
26
27
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 24

def diarize(path, options = {})
  json = transcribe(path, options.merge(raw: true))
  return Diarizer.new(json).lines.join("\n")
end

#download(info) ⇒ Object



40
41
42
43
44
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 40

def download(info)
  url = info.transcription_job.transcript.transcript_file_uri
  transcript = s3_download(key(url))
  return JSON.parse(transcript, symbolize_names: true)
end

#format(path) ⇒ Object



46
47
48
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 46

def format(path)
  File.extname(path).delete_prefix('.')
end

#info(job) ⇒ Object



50
51
52
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 50

def info(job)
  client.get_transcription_job(transcription_job_name: job)
end

#key(path) ⇒ Object



54
55
56
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 54

def key(path)
  File.basename(path)
end

#parameters(key, options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 58

def parameters(key, options)
  now = Time.now.to_i
  format = options.delete(:format) # TODO: This ain't good

  parameters = {
    transcription_job_name: "transcribe-#{now}",
    language_code: settings[:language_code] || 'en-US',
    media_format: format || format(key),
    media: { media_file_uri: s3_url(key) },
    output_bucket_name: settings[:bucket],
    output_key: s3_path("transcribe-#{now}.json")
  }

  return default_parameters.deep_merge(parameters).deep_merge(options)
end

#start(key, options) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 74

def start(key, options)
  parameters = parameters(key, options)
  client.start_transcription_job(parameters)

  job = parameters[:transcription_job_name]
  logger.debug("AwsAdapter#start: job=#{job}")

  return job
end

#status(info) ⇒ Object



84
85
86
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 84

def status(info)
  info.transcription_job.transcription_job_status
end

#transcribe(path, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 13

def transcribe(path, options = {})
  raw = options.delete(:raw)

  key = upload(path)
  job = start(key, options)
  info = wait(job)
  json = download(info)

  return raw ? json : json[:results][:transcripts].first[:transcript]
end

#upload(path) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 88

def upload(path)
  key = key(path)
  s3_upload(path, key)
  logger.debug("AwsAdapter#upload: path=#{path}, key=#{key}")

  return key
end

#wait(job) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/active_intelligence/asr/aws_adapter.rb', line 96

def wait(job)
  loop do
    info = info(job)
    logger.debug("AwsAdapter#wait: job=#{job}, status=#{status(info)}")

    case status(info)
      when 'COMPLETED' then return info
      when 'FAILED' then raise TranscriptionFailed, info
    end

    sleep settings[:sleep] || 60
  end
end