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
|