Class: OmniAI::Google::Transcribe::Gemini

Inherits:
Object
  • Object
show all
Includes:
OmniAI::Google::TranscribeHelpers
Defined in:
lib/omniai/google/transcribe/gemini.rb

Overview

Transcribes with a Gemini transcribe model (e.g. gemini-3.5-transcribe). Unlike Chirp (Cloud Speech-to-Text), these are served by Vertex generateContent with the audio as the only part.

Usage:

client.transcribe("interview.wav", model: "gemini-3.5-transcribe", diarization: true)

Constant Summary collapse

INLINE_LIMIT =

bytes; larger audio goes through GCS

10_000_000
SIGNATURES =

Leading bytes of the audio containers Gemini accepts. generateContent requires a mimeType.

{
  "RIFF" => "audio/wav",
  "ID3" => "audio/mpeg",
  "\xFF\xFB".b => "audio/mpeg",
  "\xFF\xF3".b => "audio/mpeg",
  "\xFF\xF2".b => "audio/mpeg",
  "fLaC" => "audio/flac",
  "OggS" => "audio/ogg",
  "\x1A\x45\xDF\xA3".b => "audio/webm",
  "FORM" => "audio/aiff",
}.freeze
EXTENSIONS =
{
  ".wav" => "audio/wav", ".mp3" => "audio/mpeg", ".flac" => "audio/flac", ".ogg" => "audio/ogg",
  ".opus" => "audio/ogg", ".webm" => "audio/webm", ".m4a" => "audio/mp4", ".mp4" => "audio/mp4",
  ".aac" => "audio/aac", ".aif" => "audio/aiff", ".aiff" => "audio/aiff",
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, client:, model:, language: nil, format: nil, diarization: false, word_timestamps: false, vocabulary: nil) ⇒ Gemini

Returns a new instance of Gemini.

Parameters:

  • io (String, Pathname, IO) —

    a file path, IO or gs:// URI

  • client (OmniAI::Google::Client) —

    must use Vertex credentials

  • model (String)
  • language (String, Array<String>, nil) (defaults to: nil) —

    optional - auto-detected when omitted

  • format (String, nil) (defaults to: nil) —

    optional - verbose_json adds per-utterance segments

  • diarization (Boolean) (defaults to: false) —

    optional - speaker labels per segment (experimental)

  • word_timestamps (Boolean) (defaults to: false) —

    optional - word times (experimental; can drop speech, see README)

  • vocabulary (Array<String>, nil) (defaults to: nil) —

    optional - custom vocabulary (up to 1,000 terms)



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/omniai/google/transcribe/gemini.rb', line 54

def initialize(io, client:, model:, language: nil, format: nil, diarization: false, word_timestamps: false,
  vocabulary: nil)
  @io = io
  @client = client.for_model(model)
  @model = model
  @language = language
  @format = format || OmniAI::Transcribe::Format::JSON
  @diarization = diarization
  @word_timestamps = word_timestamps
  @vocabulary = vocabulary
end

Class Method Details

.model?(model) ⇒ Boolean

Parameters:

  • model (String)

Returns:

  • (Boolean)


38
39
40
# File 'lib/omniai/google/transcribe/gemini.rb', line 38

def self.model?(model)
  String(model).start_with?("gemini-")
end

.process! ⇒ Object



42
43
44
# File 'lib/omniai/google/transcribe/gemini.rb', line 42

def self.process!(...)
  new(...).process!
end

Instance Method Details

#process! ⇒ OmniAI::Transcribe::Transcription

Returns:

  • (OmniAI::Transcribe::Transcription)

Raises:

  • (OmniAI::HTTPError, OmniAI::Error)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/omniai/google/transcribe/gemini.rb', line 69

def process!
  raise ArgumentError, "#{@model} requires Vertex credentials" unless @client.credentials?

  response = request!
  raise HTTPError, response unless response.status.ok?

  OmniAI::Transcribe::Transcription.parse(model: @model, format: @format, data: parse(response.parse))
ensure
  cleanup_gcs_file(@uploaded_gcs_uri) if @uploaded_gcs_uri
  @uploaded_gcs_uri = nil
end