Class: Cohere::Transcribe::Transcriber
- Inherits:
-
Object
- Object
- Cohere::Transcribe::Transcriber
- Defined in:
- lib/cohere/transcribe/api.rb,
sig/cohere/transcribe.rbs
Overview
Reusable, serialized transcription session with lazy model loading.
Instance Attribute Summary collapse
-
#options ⇒ TranscriptionOptions
readonly
Returns the value of attribute options.
-
#progress ⇒ progress_callback?
readonly
Returns the value of attribute progress.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ nil
- #closed? ⇒ Boolean
-
#initialize(positional_options = OPTIONS_UNSET, options: OPTIONS_UNSET, progress: nil, engine_factory: nil) ⇒ Transcriber
constructor
A new instance of Transcriber.
- #transcribe(audio, raise_on_error: false) ⇒ TranscriptionRun
Constructor Details
#initialize(options, progress:) ⇒ void #initialize(options:, progress:) ⇒ void
Returns a new instance of Transcriber.
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( = OPTIONS_UNSET, options: OPTIONS_UNSET, progress: nil, engine_factory: nil) positional_given = !.equal?(OPTIONS_UNSET) keyword_given = !.equal?(OPTIONS_UNSET) raise ArgumentError, "options must be supplied either positionally or by keyword, not both" if positional_given && keyword_given = keyword_given ? : = nil if .equal?(OPTIONS_UNSET) @options = .nil? ? TranscriptionOptions.new : 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
#options ⇒ TranscriptionOptions (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/cohere/transcribe/api.rb', line 15 def @options end |
#progress ⇒ progress_callback? (readonly)
Returns the value of attribute progress.
15 16 17 |
# File 'lib/cohere/transcribe/api.rb', line 15 def progress @progress end |
Class Method Details
.open(options, progress:) ⇒ Transcriber .open ⇒ void .open(options:, progress:) ⇒ Transcriber .open ⇒ void
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
#close ⇒ 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
82 83 84 |
# File 'lib/cohere/transcribe/api.rb', line 82 def closed? @closed end |
#transcribe(audio, raise_on_error: false) ⇒ TranscriptionRun
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 |