Class: Zuno::Providers::ElevenLabs

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

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.elevenlabs.io".freeze
SPEECH_TO_TEXT_PATH =
"/v1/speech-to-text".freeze
DEFAULT_TIMEOUT =
120_000

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, timeout: DEFAULT_TIMEOUT, base_url: DEFAULT_BASE_URL) ⇒ ElevenLabs

Returns a new instance of ElevenLabs.

Raises:



1872
1873
1874
1875
1876
1877
1878
# File 'lib/zuno.rb', line 1872

def initialize(api_key: nil, timeout: DEFAULT_TIMEOUT, base_url: DEFAULT_BASE_URL)
  @api_key = api_key
  raise ProviderError, "ElevenLabs API key not configured" if @api_key.nil? || @api_key.to_s.empty?

  @timeout = timeout
  @base_url = base_url.to_s.empty? ? DEFAULT_BASE_URL : base_url.to_s
end

Instance Method Details

#transcribe(model_id:, file:, cloud_storage_url:, source_url:, options:) ⇒ Object



1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
# File 'lib/zuno.rb', line 1880

def transcribe(model_id:, file:, cloud_storage_url:, source_url:, options:)
  opened_file = nil
  body = build_body(
    model_id: model_id,
    file: file,
    cloud_storage_url: cloud_storage_url,
    source_url: source_url,
    options: options
  ) do |candidate|
    opened_file = candidate
  end

  response = Typhoeus.post(
    speech_to_text_url,
    headers: headers,
    body: body,
    multipart: true,
    timeout: @timeout
  )

  validate_response!(response)
  parsed = JSON.parse(response.body)
  raise ProviderError, "ElevenLabs returned invalid JSON" unless parsed.is_a?(Hash)

  parsed
rescue JSON::ParserError => e
  raise ProviderError, "Failed to parse ElevenLabs response: #{e.message}"
ensure
  opened_file.close if opened_file.is_a?(File) && !opened_file.closed?
end