Class: OmniAI::Speak

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/speak.rb

Overview

An abstract class that provides a consistent interface for processing speak requests.

Usage:

class OmniAI::OpenAI::Speak < OmniAI::Speak
module Model
  WHISPER_1 = "whisper-1"
end

protected

# @return [Hash]
def payload
  raise NotImplementedError, "#{self.class.name}#payload undefined"
end

# @return [String]
def path
  raise NotImplementedError, "#{self.class.name}#path undefined"
end
end

File.open('audio.wav', 'wb') do |file|
client.speak('Sally sells seashells by the seashore.', format: OmniAI::Speak::Format::WAV) do |chunk|
  file << chunk
end
end

Defined Under Namespace

Modules: Format

Constant Summary collapse

DEFAULT_FORMAT =
Format::AAC

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, client:, model:, voice:, speed: nil, format: DEFAULT_FORMAT) ⇒ Speak

Returns a new instance of Speak.

Parameters:

  • client (OmniAI::Client)

    required

  • input (String)

    required

  • model (String)

    required

  • voice (String)

    required

  • speed (Float) (defaults to: nil)

    optional

  • format (String) (defaults to: DEFAULT_FORMAT)

    optional (default "aac"):

    • "aac"
    • "mp3"
    • "flac"
    • "opus"
    • "pcm"
    • "wav"


77
78
79
80
81
82
83
84
# File 'lib/omniai/speak.rb', line 77

def initialize(input, client:, model:, voice:, speed: nil, format: DEFAULT_FORMAT)
  @input = input
  @client = client
  @model = model
  @voice = voice
  @speed = speed
  @format = format
end

Class Method Details

.process!(input, client:, model:, voice:, speed: nil, format: DEFAULT_FORMAT) {|chunk| ... } ⇒ Tempfile

Parameters:

  • client (OmniAI::Client)

    required

  • input (String)

    required

  • model (String)

    required

  • voice (String)

    required

  • speed (Float) (defaults to: nil)

    optional

  • format (String) (defaults to: DEFAULT_FORMAT)

    optional (default "aac"):

    • "aac"
    • "mp3"
    • "flac"
    • "opus"
    • "pcm"
    • "wav"

Yields:

  • (chunk)

Returns:

  • (Tempfile)

Raises:



61
62
63
# File 'lib/omniai/speak.rb', line 61

def self.process!(input, client:, model:, voice:, speed: nil, format: DEFAULT_FORMAT, &)
  new(input, client:, model:, voice:, speed:, format:).process!(&)
end

Instance Method Details

#process! {|chunk| ... } ⇒ Tempfile

Yields:

  • (chunk)

Returns:

  • (Tempfile)

Raises:



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/omniai/speak.rb', line 91

def process!(&block)
  response = request!

  raise HTTPError, response.flush unless response.status.ok?

  if block
    stream!(response:, &block)
  else
    fetch!(response:)
  end
end