Class: Cohere::Transcribe::Runtime::WordPipeline::ConcreteRedecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/cohere/transcribe/runtime/word_pipeline.rb

Overview

Re-decodes one alignment candidate with the concrete backend recorded during ASR/checkpointing. It rejects automatic fallback and sample drift, which makes the second decode reproducible and source-consistent.

Instance Method Summary collapse

Constructor Details

#initialize(decoder:, sample_rate:, memory_byte_limit:, validate: nil, skip: nil) ⇒ ConcreteRedecoder

Returns a new instance of ConcreteRedecoder.

Raises:

  • (ArgumentError)


307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/cohere/transcribe/runtime/word_pipeline.rb', line 307

def initialize(decoder:, sample_rate:, memory_byte_limit:, validate: nil, skip: nil)
  @decoder = decoder
  @sample_rate = Integer(sample_rate)
  @memory_byte_limit = Integer(memory_byte_limit)
  raise ArgumentError, "sample_rate must be positive" unless @sample_rate.positive?
  raise ArgumentError, "memory_byte_limit must be positive" unless @memory_byte_limit.positive?
  raise ArgumentError, "validate must respond to call" if validate && !validate.respond_to?(:call)
  raise ArgumentError, "skip must respond to call" if skip && !skip.respond_to?(:call)

  @validate = validate
  @skip = skip
end

Instance Method Details

#call(work) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/cohere/transcribe/runtime/word_pipeline.rb', line 320

def call(work)
  return nil if @skip&.call(work)

  @validate&.call(work)
  return nil unless work.audio_required

  backend = work.decode_backend
  if backend.nil? || backend.empty? || backend == "auto"
    raise TranscriptionRuntimeError,
          "Cannot re-decode #{work.entry.path} for word alignment without a concrete ASR backend"
  end

  decoded = @decoder.decode(
    work.entry.path,
    backend: backend,
    sample_rate: @sample_rate,
    max_decoded_bytes: @memory_byte_limit
  )
  if decoded.backend != backend
    raise TranscriptionRuntimeError,
          "Alignment decoder backend changed for #{work.entry.path}: " \
          "#{backend} -> #{decoded.backend}"
  end
  unless decoded.samples.length == work.expected_sample_count
    raise TranscriptionRuntimeError,
          "Decoded sample count changed between ASR and alignment for #{work.entry.path}: " \
          "#{decoded.samples.length} != #{work.expected_sample_count}"
  end

  @validate&.call(work)
  decoded
end

#cancelObject



353
354
355
356
357
358
359
# File 'lib/cohere/transcribe/runtime/word_pipeline.rb', line 353

def cancel
  Audio::FFmpegNative.cancel_active! if
    defined?(Audio::FFmpegNative) && Audio::FFmpegNative.respond_to?(:cancel_active!)
  nil
rescue StandardError
  nil
end