eleven_rb 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +42 -0
- data/README.md +146 -12
- data/lib/eleven_rb/callbacks.rb +25 -1
- data/lib/eleven_rb/client.rb +10 -0
- data/lib/eleven_rb/configuration.rb +7 -3
- data/lib/eleven_rb/http/client.rb +86 -70
- data/lib/eleven_rb/http/realtime_transcription.rb +88 -0
- data/lib/eleven_rb/model_capabilities.rb +103 -0
- data/lib/eleven_rb/objects/audio.rb +17 -5
- data/lib/eleven_rb/objects/cost_info.rb +3 -0
- data/lib/eleven_rb/objects/model.rb +10 -0
- data/lib/eleven_rb/objects/voice_settings.rb +35 -0
- data/lib/eleven_rb/resources/audio_effects.rb +11 -0
- data/lib/eleven_rb/resources/base.rb +62 -0
- data/lib/eleven_rb/resources/models.rb +32 -6
- data/lib/eleven_rb/resources/music.rb +93 -124
- data/lib/eleven_rb/resources/sound_effects.rb +8 -13
- data/lib/eleven_rb/resources/speech_to_speech.rb +21 -9
- data/lib/eleven_rb/resources/speech_to_text.rb +37 -0
- data/lib/eleven_rb/resources/text_to_dialogue.rb +143 -30
- data/lib/eleven_rb/resources/text_to_speech.rb +182 -57
- data/lib/eleven_rb/resources/voices.rb +16 -3
- data/lib/eleven_rb/version.rb +1 -1
- data/lib/eleven_rb.rb +4 -0
- metadata +47 -1
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'openssl'
|
|
4
|
+
require 'socket'
|
|
5
|
+
require 'timeout'
|
|
6
|
+
require 'websocket/driver'
|
|
7
|
+
|
|
8
|
+
module ElevenRb
|
|
9
|
+
module HTTP
|
|
10
|
+
# A synchronous full-duplex Scribe connection. Read transcript events with
|
|
11
|
+
# #read_event and write PCM audio with #send_audio from a separate producer.
|
|
12
|
+
class RealtimeTranscription
|
|
13
|
+
attr_reader :url
|
|
14
|
+
|
|
15
|
+
def initialize(config, query)
|
|
16
|
+
uri = URI("#{config.base_url.delete_suffix('/')}/speech-to-text/realtime")
|
|
17
|
+
@timeout = config.timeout
|
|
18
|
+
@write_lock = Mutex.new
|
|
19
|
+
@events = []
|
|
20
|
+
@socket = Socket.tcp(uri.host, uri.port, connect_timeout: config.open_timeout)
|
|
21
|
+
Timeout.timeout(config.open_timeout, Errors::TimeoutError) do
|
|
22
|
+
start_tls(uri.host) if uri.scheme == 'https'
|
|
23
|
+
uri.query = URI.encode_www_form(query.compact)
|
|
24
|
+
@url = uri.to_s.sub(/\Ahttp/, 'ws')
|
|
25
|
+
start_driver(config.api_key)
|
|
26
|
+
read_frame until @driver.state == :open
|
|
27
|
+
end
|
|
28
|
+
rescue StandardError
|
|
29
|
+
@socket&.close
|
|
30
|
+
raise
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Transport method used by websocket-driver for data and control frames.
|
|
34
|
+
def write(bytes)
|
|
35
|
+
@write_lock.synchronize { @socket.write(bytes) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def send_audio(bytes, sample_rate: 16_000, commit: false, previous_text: nil)
|
|
39
|
+
frame = { 'message_type' => 'input_audio_chunk', 'audio_base_64' => Base64.strict_encode64(bytes),
|
|
40
|
+
'sample_rate' => sample_rate, 'commit' => commit, 'previous_text' => previous_text }.compact
|
|
41
|
+
raise IOError, 'Scribe session is closed' unless @driver.text(JSON.generate(frame))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def read_event
|
|
45
|
+
while @events.empty?
|
|
46
|
+
return if @driver.state == :closed
|
|
47
|
+
|
|
48
|
+
Timeout.timeout(@timeout, Errors::TimeoutError) { read_frame }
|
|
49
|
+
end
|
|
50
|
+
@events.shift
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def close
|
|
54
|
+
return if @socket.closed?
|
|
55
|
+
|
|
56
|
+
@driver.close
|
|
57
|
+
ensure
|
|
58
|
+
@socket.close unless @socket.closed?
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
def start_driver(api_key)
|
|
64
|
+
@driver = WebSocket::Driver.client(self)
|
|
65
|
+
@driver.set_header('xi-api-key', api_key)
|
|
66
|
+
@driver.on(:message) { |event| @events << JSON.parse(event.data) }
|
|
67
|
+
@driver.on(:error) { |error| raise Errors::ConnectionError, error.message }
|
|
68
|
+
@driver.start
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def start_tls(host)
|
|
72
|
+
context = OpenSSL::SSL::SSLContext.new
|
|
73
|
+
context.set_params
|
|
74
|
+
ssl = OpenSSL::SSL::SSLSocket.new(@socket, context)
|
|
75
|
+
ssl.sync_close = true
|
|
76
|
+
ssl.hostname = host
|
|
77
|
+
@socket = ssl
|
|
78
|
+
ssl.connect
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def read_frame
|
|
82
|
+
@driver.parse(@socket.readpartial(16_384))
|
|
83
|
+
rescue EOFError
|
|
84
|
+
raise Errors::ConnectionError, 'Scribe connection closed without a WebSocket close frame'
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ElevenRb
|
|
4
|
+
# What each ElevenLabs model family accepts, keyed by model-id family.
|
|
5
|
+
#
|
|
6
|
+
# The API silently ignores voice settings a model does not honour (Eleven v4
|
|
7
|
+
# accepts `speed`, `style` and `use_speaker_boost` and does nothing with them),
|
|
8
|
+
# so the gem uses this table to send only the settings that take effect, to
|
|
9
|
+
# cap text length per model, and to answer feature questions (audio tags,
|
|
10
|
+
# SSML `<break>` tags, request continuity via previous_text / next_text).
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
# ElevenRb::ModelCapabilities.supported_voice_settings('eleven_v4')
|
|
14
|
+
# # => [:stability, :similarity_boost, :clone_strength]
|
|
15
|
+
# ElevenRb::ModelCapabilities.max_text_length('eleven_flash_v2_5') # => 40_000
|
|
16
|
+
# ElevenRb::ModelCapabilities.supports?('eleven_v4', :audio_tags) # => true
|
|
17
|
+
module ModelCapabilities
|
|
18
|
+
# Capability record for one model family
|
|
19
|
+
Capabilities = Struct.new(:voice_settings, :max_text_length, :ssml_break, :audio_tags, :continuity,
|
|
20
|
+
keyword_init: true) do
|
|
21
|
+
def ssml_break? = ssml_break
|
|
22
|
+
def audio_tags? = audio_tags
|
|
23
|
+
def continuity? = continuity
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
ALL_VOICE_SETTINGS = %i[stability similarity_boost style use_speaker_boost speed].freeze
|
|
27
|
+
|
|
28
|
+
def self.build(voice_settings:, max_text_length:, ssml_break:, audio_tags:, continuity:)
|
|
29
|
+
Capabilities.new(
|
|
30
|
+
voice_settings: voice_settings.freeze,
|
|
31
|
+
max_text_length: max_text_length,
|
|
32
|
+
ssml_break: ssml_break,
|
|
33
|
+
audio_tags: audio_tags,
|
|
34
|
+
continuity: continuity
|
|
35
|
+
).freeze
|
|
36
|
+
end
|
|
37
|
+
private_class_method :build
|
|
38
|
+
|
|
39
|
+
V4 = build(voice_settings: %i[stability similarity_boost clone_strength], max_text_length: 100_000,
|
|
40
|
+
ssml_break: false, audio_tags: true, continuity: true)
|
|
41
|
+
V3_CONVERSATIONAL = build(voice_settings: %i[stability similarity_boost use_speaker_boost], max_text_length: 5_000,
|
|
42
|
+
ssml_break: false, audio_tags: true, continuity: true)
|
|
43
|
+
V3 = build(voice_settings: %i[stability similarity_boost], max_text_length: 5_000,
|
|
44
|
+
ssml_break: false, audio_tags: true, continuity: true)
|
|
45
|
+
V2_5_FAST = build(voice_settings: ALL_VOICE_SETTINGS, max_text_length: 40_000,
|
|
46
|
+
ssml_break: true, audio_tags: false, continuity: true)
|
|
47
|
+
V2_FAST = build(voice_settings: ALL_VOICE_SETTINGS, max_text_length: 30_000,
|
|
48
|
+
ssml_break: true, audio_tags: false, continuity: true)
|
|
49
|
+
DEFAULT = build(voice_settings: ALL_VOICE_SETTINGS, max_text_length: 10_000,
|
|
50
|
+
ssml_break: true, audio_tags: false, continuity: true)
|
|
51
|
+
|
|
52
|
+
# Ordered [matcher, capabilities] pairs; the first match wins.
|
|
53
|
+
FAMILIES = [
|
|
54
|
+
[/\Aeleven_v4/, V4],
|
|
55
|
+
[/\Aeleven_v3_conversational/, V3_CONVERSATIONAL],
|
|
56
|
+
[/\Aeleven_v3/, V3],
|
|
57
|
+
[/\Aeleven_(flash|turbo)_v2_5/, V2_5_FAST],
|
|
58
|
+
[/\Aeleven_(flash|turbo)_v2/, V2_FAST]
|
|
59
|
+
].freeze
|
|
60
|
+
|
|
61
|
+
FEATURES = %i[audio_tags ssml_break continuity].freeze
|
|
62
|
+
|
|
63
|
+
module_function
|
|
64
|
+
|
|
65
|
+
# Capabilities for a model id (unknown ids get the permissive default)
|
|
66
|
+
#
|
|
67
|
+
# @param model_id [String, Symbol, nil]
|
|
68
|
+
# @return [Capabilities] frozen
|
|
69
|
+
def for(model_id)
|
|
70
|
+
id = model_id.to_s
|
|
71
|
+
FAMILIES.each { |matcher, caps| return caps if matcher.match?(id) }
|
|
72
|
+
DEFAULT
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Voice settings keys the model honours
|
|
76
|
+
#
|
|
77
|
+
# @param model_id [String]
|
|
78
|
+
# @return [Array<Symbol>]
|
|
79
|
+
def supported_voice_settings(model_id)
|
|
80
|
+
self.for(model_id).voice_settings
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Maximum characters per request for the model
|
|
84
|
+
#
|
|
85
|
+
# @param model_id [String]
|
|
86
|
+
# @return [Integer]
|
|
87
|
+
def max_text_length(model_id)
|
|
88
|
+
self.for(model_id).max_text_length
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Whether the model supports a feature
|
|
92
|
+
#
|
|
93
|
+
# @param model_id [String]
|
|
94
|
+
# @param feature [Symbol] :audio_tags, :ssml_break or :continuity
|
|
95
|
+
# @return [Boolean]
|
|
96
|
+
def supports?(model_id, feature)
|
|
97
|
+
feature = feature.to_sym
|
|
98
|
+
raise ArgumentError, "Unknown feature #{feature.inspect} (expected one of #{FEATURES.join(', ')})" unless FEATURES.include?(feature)
|
|
99
|
+
|
|
100
|
+
self.for(model_id).public_send(feature) ? true : false
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -6,7 +6,8 @@ module ElevenRb
|
|
|
6
6
|
module Objects
|
|
7
7
|
# Represents generated audio data
|
|
8
8
|
class Audio
|
|
9
|
-
attr_reader :data, :format, :voice_id, :text, :model_id
|
|
9
|
+
attr_reader :data, :format, :voice_id, :text, :model_id,
|
|
10
|
+
:request_id, :character_cost, :dropped_settings, :song_id
|
|
10
11
|
|
|
11
12
|
# Initialize audio object
|
|
12
13
|
#
|
|
@@ -15,12 +16,21 @@ module ElevenRb
|
|
|
15
16
|
# @param voice_id [String] the voice ID used
|
|
16
17
|
# @param text [String] the text that was converted
|
|
17
18
|
# @param model_id [String, nil] the model ID used
|
|
18
|
-
|
|
19
|
+
# @param request_id [String, nil] the API's `request-id` response header (usable as
|
|
20
|
+
# previous_request_ids / next_request_ids on a later request)
|
|
21
|
+
# @param character_cost [Integer, nil] the API's `character-cost` response header
|
|
22
|
+
# @param dropped_settings [Array<Symbol>] voice settings the gem dropped because the model ignores them
|
|
23
|
+
def initialize(data:, format:, voice_id:, text:, model_id: nil, request_id: nil, character_cost: nil,
|
|
24
|
+
dropped_settings: [], song_id: nil)
|
|
25
|
+
@song_id = song_id
|
|
19
26
|
@data = data
|
|
20
27
|
@format = format
|
|
21
28
|
@voice_id = voice_id
|
|
22
29
|
@text = text
|
|
23
30
|
@model_id = model_id
|
|
31
|
+
@request_id = request_id
|
|
32
|
+
@character_cost = character_cost
|
|
33
|
+
@dropped_settings = Array(dropped_settings).dup.freeze
|
|
24
34
|
end
|
|
25
35
|
|
|
26
36
|
# Save audio to a file
|
|
@@ -58,11 +68,12 @@ module ElevenRb
|
|
|
58
68
|
# @return [String]
|
|
59
69
|
def content_type
|
|
60
70
|
case format
|
|
71
|
+
when 'zip' then 'application/zip'
|
|
61
72
|
when /mp3/
|
|
62
73
|
'audio/mpeg'
|
|
63
74
|
when /pcm/
|
|
64
75
|
'audio/pcm'
|
|
65
|
-
when /ogg/
|
|
76
|
+
when /ogg|opus/
|
|
66
77
|
'audio/ogg'
|
|
67
78
|
when /wav/
|
|
68
79
|
'audio/wav'
|
|
@@ -78,11 +89,12 @@ module ElevenRb
|
|
|
78
89
|
# @return [String]
|
|
79
90
|
def extension
|
|
80
91
|
case format
|
|
92
|
+
when 'zip' then 'zip'
|
|
81
93
|
when /mp3/
|
|
82
94
|
'mp3'
|
|
83
95
|
when /pcm/
|
|
84
96
|
'pcm'
|
|
85
|
-
when /ogg/
|
|
97
|
+
when /ogg|opus/
|
|
86
98
|
'ogg'
|
|
87
99
|
when /wav/
|
|
88
100
|
'wav'
|
|
@@ -97,7 +109,7 @@ module ElevenRb
|
|
|
97
109
|
#
|
|
98
110
|
# @return [Integer]
|
|
99
111
|
def character_count
|
|
100
|
-
text
|
|
112
|
+
text&.length || 0
|
|
101
113
|
end
|
|
102
114
|
|
|
103
115
|
# Check if data is present
|
|
@@ -13,6 +13,9 @@ module ElevenRb
|
|
|
13
13
|
'eleven_multilingual_v1' => 0.30,
|
|
14
14
|
'eleven_multilingual_v2' => 0.30,
|
|
15
15
|
'eleven_v3' => 0.30,
|
|
16
|
+
'eleven_v3_conversational' => 0.15,
|
|
17
|
+
'eleven_v4' => 0.30,
|
|
18
|
+
'eleven_v4_turbo' => 0.15,
|
|
16
19
|
'eleven_turbo_v2' => 0.18,
|
|
17
20
|
'eleven_turbo_v2_5' => 0.18,
|
|
18
21
|
'eleven_english_sts_v2' => 0.30,
|
|
@@ -18,6 +18,16 @@ module ElevenRb
|
|
|
18
18
|
attribute :max_characters_request_free_user
|
|
19
19
|
attribute :max_characters_request_subscribed_user
|
|
20
20
|
attribute :concurrency_group
|
|
21
|
+
attribute :model_rates
|
|
22
|
+
attribute :maximum_text_length_per_request
|
|
23
|
+
attribute :requires_alpha_access, type: :boolean
|
|
24
|
+
|
|
25
|
+
# Voice settings keys this model honours (from ModelCapabilities)
|
|
26
|
+
#
|
|
27
|
+
# @return [Array<Symbol>]
|
|
28
|
+
def supported_voice_settings
|
|
29
|
+
ModelCapabilities.supported_voice_settings(model_id)
|
|
30
|
+
end
|
|
21
31
|
|
|
22
32
|
# Check if this model supports a given language
|
|
23
33
|
#
|
|
@@ -6,6 +6,7 @@ module ElevenRb
|
|
|
6
6
|
class VoiceSettings < Base
|
|
7
7
|
attribute :stability
|
|
8
8
|
attribute :similarity_boost
|
|
9
|
+
attribute :clone_strength
|
|
9
10
|
attribute :style
|
|
10
11
|
attribute :use_speaker_boost, type: :boolean
|
|
11
12
|
|
|
@@ -25,6 +26,39 @@ module ElevenRb
|
|
|
25
26
|
from_response(DEFAULTS.merge(overrides))
|
|
26
27
|
end
|
|
27
28
|
|
|
29
|
+
# Voice-setting keys the capability table knows about. Only these can be
|
|
30
|
+
# dropped for a model; any other key is passed through untouched so a
|
|
31
|
+
# future API field is never swallowed.
|
|
32
|
+
KNOWN_KEYS = (ModelCapabilities::ALL_VOICE_SETTINGS + [:clone_strength]).freeze
|
|
33
|
+
|
|
34
|
+
# Build the voice_settings hash a model actually honours
|
|
35
|
+
#
|
|
36
|
+
# Starts from DEFAULTS filtered to the model's supported keys and merges the
|
|
37
|
+
# overrides (keys symbolized). Known keys the model does not support are
|
|
38
|
+
# dropped; unknown keys pass through after the supported ones, in the
|
|
39
|
+
# caller's order; nil values are removed. Only non-nil override keys count
|
|
40
|
+
# as dropped: defaults the model does not take are filtered silently.
|
|
41
|
+
#
|
|
42
|
+
# @example
|
|
43
|
+
# VoiceSettings.for_model('eleven_multilingual_v2')
|
|
44
|
+
# # => [{ stability: 0.5, similarity_boost: 0.75, style: 0.0, use_speaker_boost: true }, []]
|
|
45
|
+
# VoiceSettings.for_model('eleven_v4', speed: 1.1)
|
|
46
|
+
# # => [{ stability: 0.5, similarity_boost: 0.75 }, [:speed]]
|
|
47
|
+
#
|
|
48
|
+
# @param model_id [String] the model ID
|
|
49
|
+
# @param overrides [Hash] caller settings (String or Symbol keys)
|
|
50
|
+
# @return [Array(Hash, Array<Symbol>)] the settings hash and the dropped override keys
|
|
51
|
+
def self.for_model(model_id, overrides = {})
|
|
52
|
+
supported = ModelCapabilities.supported_voice_settings(model_id)
|
|
53
|
+
requested = (overrides || {}).to_h.transform_keys(&:to_sym)
|
|
54
|
+
|
|
55
|
+
dropped = requested.compact.keys & (KNOWN_KEYS - supported)
|
|
56
|
+
unknown = requested.except(*KNOWN_KEYS)
|
|
57
|
+
settings = DEFAULTS.slice(*supported).merge(requested.slice(*supported)).merge(unknown).compact
|
|
58
|
+
|
|
59
|
+
[settings, dropped]
|
|
60
|
+
end
|
|
61
|
+
|
|
28
62
|
# Convert to hash suitable for API request
|
|
29
63
|
#
|
|
30
64
|
# @return [Hash]
|
|
@@ -33,6 +67,7 @@ module ElevenRb
|
|
|
33
67
|
stability: stability,
|
|
34
68
|
similarity_boost: similarity_boost,
|
|
35
69
|
style: style,
|
|
70
|
+
clone_strength: clone_strength,
|
|
36
71
|
use_speaker_boost: use_speaker_boost
|
|
37
72
|
}.compact
|
|
38
73
|
end
|
|
@@ -51,6 +51,24 @@ module ElevenRb
|
|
|
51
51
|
http_client.post(path, body, response_type: :binary)
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
+
# Make a JSON POST request and also return the response headers
|
|
55
|
+
#
|
|
56
|
+
# @param path [String]
|
|
57
|
+
# @param body [Hash]
|
|
58
|
+
# @return [Hash] `{ body: Hash, headers: Hash<String, String> }`
|
|
59
|
+
def post_with_meta(path, body = {})
|
|
60
|
+
http_client.post(path, body, response_type: :json, with_meta: true)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Make a binary POST request and also return the response headers
|
|
64
|
+
#
|
|
65
|
+
# @param path [String]
|
|
66
|
+
# @param body [Hash]
|
|
67
|
+
# @return [Hash] `{ body: String, headers: Hash<String, String> }`
|
|
68
|
+
def post_binary_with_meta(path, body = {})
|
|
69
|
+
http_client.post(path, body, response_type: :binary, with_meta: true)
|
|
70
|
+
end
|
|
71
|
+
|
|
54
72
|
# Make a streaming POST request
|
|
55
73
|
#
|
|
56
74
|
# @param path [String]
|
|
@@ -60,6 +78,22 @@ module ElevenRb
|
|
|
60
78
|
http_client.post_stream(path, body, &block)
|
|
61
79
|
end
|
|
62
80
|
|
|
81
|
+
# Each JSON line is a complete audio/alignment event. HTTP chunks may split any line.
|
|
82
|
+
def stream_timestamps(path, body, &block)
|
|
83
|
+
buffer = +''.b
|
|
84
|
+
headers = post_stream(path, body) do |chunk|
|
|
85
|
+
buffer << chunk.b
|
|
86
|
+
while (ending = buffer.index("\n"))
|
|
87
|
+
line = buffer.slice!(0, ending + 1)
|
|
88
|
+
block.call(JSON.parse(line)) unless line.strip.empty?
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
block.call(JSON.parse(buffer)) unless buffer.strip.empty?
|
|
92
|
+
headers
|
|
93
|
+
rescue JSON::ParserError => e
|
|
94
|
+
raise Errors::APIError, "Invalid timestamp stream: #{e.message}"
|
|
95
|
+
end
|
|
96
|
+
|
|
63
97
|
# Make a multipart POST request
|
|
64
98
|
#
|
|
65
99
|
# @param path [String]
|
|
@@ -70,6 +104,34 @@ module ElevenRb
|
|
|
70
104
|
http_client.post_multipart(path, params, response_type: response_type)
|
|
71
105
|
end
|
|
72
106
|
|
|
107
|
+
def with_uploads(inputs)
|
|
108
|
+
opened = []
|
|
109
|
+
files = inputs.map do |input|
|
|
110
|
+
next input if input.respond_to?(:read)
|
|
111
|
+
|
|
112
|
+
raise Errors::ValidationError, "File not found: #{input}" unless File.file?(input)
|
|
113
|
+
|
|
114
|
+
file = File.open(input, 'rb')
|
|
115
|
+
opened << file
|
|
116
|
+
file
|
|
117
|
+
end
|
|
118
|
+
yield files
|
|
119
|
+
ensure
|
|
120
|
+
opened.each(&:close)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def notify_media_usage(audio, text, model_id, seconds, headers)
|
|
124
|
+
# The provider's credit charge depends on the account and endpoint.
|
|
125
|
+
# Preserve the old callback keys and expose the provider's units separately.
|
|
126
|
+
cost_info = {
|
|
127
|
+
character_count: text&.length || 0, voice_id: nil, model_id: model_id,
|
|
128
|
+
estimated_cost: nil, cost_per_character: nil, requested_duration_seconds: seconds,
|
|
129
|
+
credits: Integer(headers['character-cost'], exception: false)
|
|
130
|
+
}
|
|
131
|
+
http_client.config.trigger(:on_audio_generated, audio: audio, voice_id: nil, text: text,
|
|
132
|
+
cost_info: cost_info, request_id: headers['request-id'])
|
|
133
|
+
end
|
|
134
|
+
|
|
73
135
|
# Validate presence of a value
|
|
74
136
|
#
|
|
75
137
|
# @param value [Object]
|
|
@@ -9,23 +9,36 @@ module ElevenRb
|
|
|
9
9
|
#
|
|
10
10
|
# @example Find multilingual models
|
|
11
11
|
# client.models.multilingual
|
|
12
|
+
#
|
|
13
|
+
# @example Find one model
|
|
14
|
+
# client.models.find('eleven_v4')
|
|
12
15
|
class Models < Base
|
|
13
16
|
# List all available models
|
|
14
17
|
#
|
|
15
18
|
# @return [Array<Objects::Model>]
|
|
16
19
|
def list
|
|
17
|
-
|
|
20
|
+
# Call the HTTP client directly: #get below is the model lookup (kept for
|
|
21
|
+
# compatibility) and shadows Base#get, which made this method recurse.
|
|
22
|
+
response = http_client.get('/models')
|
|
18
23
|
response.map { |m| Objects::Model.from_response(m) }
|
|
19
24
|
end
|
|
20
25
|
|
|
21
|
-
#
|
|
26
|
+
# Find a specific model by ID
|
|
22
27
|
#
|
|
23
28
|
# @param model_id [String] the model ID
|
|
24
29
|
# @return [Objects::Model, nil]
|
|
25
|
-
def
|
|
30
|
+
def find(model_id)
|
|
26
31
|
list.find { |m| m.model_id == model_id }
|
|
27
32
|
end
|
|
28
33
|
|
|
34
|
+
# Alias of {#find}, kept for backwards compatibility
|
|
35
|
+
#
|
|
36
|
+
# @param model_id [String] the model ID
|
|
37
|
+
# @return [Objects::Model, nil]
|
|
38
|
+
def get(model_id)
|
|
39
|
+
find(model_id)
|
|
40
|
+
end
|
|
41
|
+
|
|
29
42
|
# Get all multilingual models
|
|
30
43
|
#
|
|
31
44
|
# @return [Array<Objects::Model>]
|
|
@@ -51,14 +64,20 @@ module ElevenRb
|
|
|
51
64
|
#
|
|
52
65
|
# @return [Objects::Model, nil]
|
|
53
66
|
def default
|
|
54
|
-
|
|
67
|
+
default_from(list)
|
|
55
68
|
end
|
|
56
69
|
|
|
57
|
-
# Get the latest/most capable model
|
|
70
|
+
# Get the latest/most capable model available to the account:
|
|
71
|
+
# eleven_v4, else eleven_v3, else {#default} (one /models request)
|
|
58
72
|
#
|
|
59
73
|
# @return [Objects::Model, nil]
|
|
60
74
|
def latest
|
|
61
|
-
|
|
75
|
+
models = list
|
|
76
|
+
%w[eleven_v4 eleven_v3].each do |model_id|
|
|
77
|
+
model = models.find { |m| m.model_id == model_id }
|
|
78
|
+
return model if model
|
|
79
|
+
end
|
|
80
|
+
default_from(models)
|
|
62
81
|
end
|
|
63
82
|
|
|
64
83
|
# Get model IDs as array
|
|
@@ -67,6 +86,13 @@ module ElevenRb
|
|
|
67
86
|
def ids
|
|
68
87
|
list.map(&:model_id)
|
|
69
88
|
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
# eleven_multilingual_v2, else the first TTS-capable model, from an already-fetched list
|
|
93
|
+
def default_from(models)
|
|
94
|
+
models.find { |m| m.model_id == 'eleven_multilingual_v2' } || models.find(&:can_do_text_to_speech)
|
|
95
|
+
end
|
|
70
96
|
end
|
|
71
97
|
end
|
|
72
98
|
end
|