Class: Cohere::Transcribe::Runtime::Engine
- Inherits:
-
Object
- Object
- Cohere::Transcribe::Runtime::Engine
- Defined in:
- lib/cohere/transcribe/runtime/engine.rb
Overview
Reusable, serialized Ruby execution engine. Heavy model conversion and native session creation remain lazy until at least one speech segment actually needs inference.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(options, progress: nil, model_provider: nil, decoder: Audio::Decoder, silero_factory: nil, aligner_factory: nil) ⇒ Engine
constructor
A new instance of Engine.
- #transcribe(audio, raise_on_error: false, runtime_import_seconds: 0.0, serialization_wait_seconds: 0.0) ⇒ Object
Constructor Details
#initialize(options, progress: nil, model_provider: nil, decoder: Audio::Decoder, silero_factory: nil, aligner_factory: nil) ⇒ Engine
Returns a new instance of Engine.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cohere/transcribe/runtime/engine.rb', line 96 def initialize(, progress: nil, model_provider: nil, decoder: Audio::Decoder, silero_factory: nil, aligner_factory: nil) @options = @progress = progress @model_provider = model_provider || ModelProvider.new @decoder = decoder @silero_factory = silero_factory || ->(**keywords) { VAD::Silero.new(**keywords) } @aligner_factory = aligner_factory || ->(**keywords) { Alignment::Aligner.new(**keywords) } @gate = Mutex.new @progress_gate = Mutex.new @closed = false @active = false @identity = nil @resources = ModelResources.new @native_session = nil @aligner = nil @preparation_sileros = [] @preparation_silero_gate = Mutex.new end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
94 95 96 |
# File 'lib/cohere/transcribe/runtime/engine.rb', line 94 def @options end |
Instance Method Details
#close ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/cohere/transcribe/runtime/engine.rb', line 177 def close if Thread.current.thread_variable_get(THREAD_PROGRESS_KEY) || Thread.current.thread_variable_get(THREAD_ACTIVE_KEY) raise TranscriberBusyError, "Cannot close a Transcriber while transcription is active" end @gate.synchronize do return if @closed raise TranscriberBusyError, "Cannot close a Transcriber while its transcription is active" if @active begin @resources.close ensure @native_session = nil evict_aligner end @preparation_sileros.clear @closed = true end nil end |
#transcribe(audio, raise_on_error: false, runtime_import_seconds: 0.0, serialization_wait_seconds: 0.0) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/cohere/transcribe/runtime/engine.rb', line 116 def transcribe(audio, raise_on_error: false, runtime_import_seconds: 0.0, serialization_wait_seconds: 0.0) if Thread.current.thread_variable_get(THREAD_PROGRESS_KEY) raise TranscriberBusyError, "Reentrant transcription from a progress callback is not supported" end if Thread.current.thread_variable_get(THREAD_ACTIVE_KEY) raise TranscriberBusyError, "Reentrant transcription in one process is not supported" end started = monotonic wait_started = monotonic @gate.synchronize do measurements = Measurements.new( runtime_import_seconds: runtime_import_seconds, serialization_wait_seconds: serialization_wait_seconds + monotonic - wait_started ) raise TranscriberClosedError, "This Transcriber has been closed" if @closed raise TranscriberBusyError, "Reentrant transcription with one Transcriber is not supported" if @active @active = true begin process_wait = monotonic PROCESS_GATE.synchronize do measurements.serialization_wait_seconds += monotonic - process_wait previous_active = Thread.current.thread_variable_get(THREAD_ACTIVE_KEY) Thread.current.thread_variable_set(THREAD_ACTIVE_KEY, true) run = execute(audio, measurements, started) raise BatchTranscriptionError, run if raise_on_error && !run.ok? run ensure Thread.current.thread_variable_set(THREAD_ACTIVE_KEY, previous_active) end rescue ProgressCallbackError, BatchTranscriptionError, TranscriptionConfigurationError, TranscriptionInputError, TranscriberBusyError, TranscriberClosedError raise rescue FatalRuntimeError => e evict_inference_sessions raise TranscriptionRuntimeError, e. rescue TranscriptionRuntimeError evict_inference_sessions raise rescue Interrupt evict_inference_sessions raise rescue SystemExit => e evict_inference_sessions detail = e..to_s detail = "Transcription setup failed" if detail.empty? || detail == "SystemExit" raise TranscriptionRuntimeError, detail rescue ScriptError => e evict_inference_sessions raise TranscriptionRuntimeError, "#{e.class}: #{e.}" rescue StandardError => e evict_inference_sessions raise TranscriptionRuntimeError, "#{e.class}: #{e.}" ensure @active = false end end end |