Module: Cohere::Transcribe::Input

Defined in:
lib/cohere/transcribe/input.rb

Class Method Summary collapse

Class Method Details

.expand(audio, recursive: true) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cohere/transcribe/input.rb', line 48

def expand(audio, recursive: true)
  inputs = normalize(audio)
  seen = {}
  entries = inputs.flat_map do |raw|
    source = strict_realpath(raw)
    candidates = if source.file?
                   [[source, Pathname(source.basename.to_s)]]
                 elsif source.directory?
                   directory_candidates(source, recursive)
                 else
                   raise TranscriptionInputError, "Input is not a regular file or directory: #{source}"
                 end
    candidates.filter_map do |path, relative_path|
      canonical = strict_realpath(path)
      next if seen.key?(canonical.to_s)

      seen[canonical.to_s] = true
      InputEntry.new(path: canonical.freeze, relative_path: relative_path.freeze)
    end
  end
  raise TranscriptionInputError, "No audio files found in the supplied inputs." if entries.empty?

  entries.freeze
end

.normalize(audio) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cohere/transcribe/input.rb', line 16

def normalize(audio)
  sequence = audio.to_ary if !path_like?(audio) && audio.respond_to?(:to_ary)
  values = if path_like?(audio)
             [audio]
           elsif sequence.is_a?(Array)
             raise TranscriptionInputError, "audio must contain at least one path" if sequence.empty?

             sequence
           else
             raise TranscriptionInputError, "audio must be one path or an ordered sequence of paths"
           end

  values.each_with_index.map do |value, index|
    label = values.length == 1 && path_like?(audio) ? "audio" : "audio[#{index}]"
    raise TranscriptionInputError, "#{label} must be a string or path-like object" unless path_like?(value)

    text = value.is_a?(String) ? value : value.to_path
    raise TranscriptionInputError, "#{label} must resolve to a text path" unless text.is_a?(String)
    raise TranscriptionInputError, "#{label} must not be empty" if PythonText.blank?(text)

    text.dup.freeze
  rescue TypeError, ArgumentError, SystemCallError => e
    raise e if e.is_a?(TranscriptionInputError)

    raise TranscriptionInputError, "#{label} is not a valid text path: #{e.message}"
  end.freeze
rescue TypeError, ArgumentError, SystemCallError => e
  raise e if e.is_a?(TranscriptionInputError)

  raise TranscriptionInputError, "audio is not a valid ordered path sequence: #{e.message}"
end