Class: Cohere::Transcribe::Transcriber

Inherits:
Object
  • Object
show all
Defined in:
lib/cohere/transcribe/api.rb,
sig/cohere/transcribe.rbs

Overview

Reusable, serialized transcription session with lazy model loading.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, progress:) ⇒ void #initialize(options:, progress:) ⇒ void

Returns a new instance of Transcriber.

Overloads:

  • #initialize(options, progress:) ⇒ void

    Parameters:

  • #initialize(options:, progress:) ⇒ void

    Parameters:

Raises:

  • (ArgumentError)


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
# File 'lib/cohere/transcribe/api.rb', line 17

def initialize(positional_options = OPTIONS_UNSET, options: OPTIONS_UNSET, progress: nil, engine_factory: nil)
  positional_given = !positional_options.equal?(OPTIONS_UNSET)
  keyword_given = !options.equal?(OPTIONS_UNSET)
  raise ArgumentError, "options must be supplied either positionally or by keyword, not both" if positional_given && keyword_given

  selected_options = keyword_given ? options : positional_options
  selected_options = nil if selected_options.equal?(OPTIONS_UNSET)
  @options = selected_options.nil? ? TranscriptionOptions.new : selected_options
  raise TypeError, "options must be a TranscriptionOptions instance" unless @options.is_a?(TranscriptionOptions)
  raise TypeError, "progress must be callable or nil" if !progress.nil? && !progress.respond_to?(:call)

  @progress = progress
  @engine_factory = if engine_factory.nil?
                      lambda do
                        Loader.load_runtime!
                        Runtime::Engine.new(@options, progress: @progress)
                      end
                    else
                      engine_factory
                    end
  @implementation = nil
  @lock = Mutex.new
  @closed = false
  @closing = false
end

Instance Attribute Details

#optionsTranscriptionOptions (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/cohere/transcribe/api.rb', line 15

def options
  @options
end

#progressprogress_callback? (readonly)

Returns the value of attribute progress.

Returns:

  • (progress_callback, nil)


15
16
17
# File 'lib/cohere/transcribe/api.rb', line 15

def progress
  @progress
end

Class Method Details

.open(options, progress:) ⇒ Transcriber .openvoid .open(options:, progress:) ⇒ Transcriber .openvoid

Overloads:



86
87
88
89
90
91
92
93
94
95
# File 'lib/cohere/transcribe/api.rb', line 86

def self.open(...)
  transcriber = new(...)
  return transcriber unless block_given?

  begin
    yield transcriber
  ensure
    transcriber.close
  end
end

Instance Method Details

#closenil

Returns:

  • (nil)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cohere/transcribe/api.rb', line 58

def close
  implementation = @lock.synchronize do
    return if @closed
    raise TranscriberBusyError, "This Transcriber is already being closed" if @closing

    @closing = true
    @implementation
  end
  begin
    implementation&.close
  rescue Exception
    @lock.synchronize do
      @closing = false
    end
    raise
  end
  @lock.synchronize do
    @closed = true
    @closing = false
    @implementation = nil if @implementation.equal?(implementation)
  end
  nil
end

#closed?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/cohere/transcribe/api.rb', line 82

def closed?
  @closed
end

#transcribe(audio, raise_on_error: false) ⇒ TranscriptionRun

Parameters:

  • audio (audio_input)
  • raise_on_error: (Boolean) (defaults to: false)

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cohere/transcribe/api.rb', line 43

def transcribe(audio, raise_on_error: false)
  started = monotonic
  normalized = Input.normalize(audio)
  implementation, import_seconds, wait_seconds = implementation()
  run = implementation.transcribe(
    normalized,
    raise_on_error: raise_on_error,
    runtime_import_seconds: import_seconds,
    serialization_wait_seconds: wait_seconds
  )
  with_elapsed(run, monotonic - started)
rescue BatchTranscriptionError => e
  raise BatchTranscriptionError.new(with_elapsed(e.run, monotonic - started))
end