Class: ElevenlabsClient::SpeechToText

Inherits:
Object
  • Object
show all
Defined in:
lib/elevenlabs_client/endpoints/speech_to_text.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ SpeechToText

Returns a new instance of SpeechToText.



5
6
7
# File 'lib/elevenlabs_client/endpoints/speech_to_text.rb', line 5

def initialize(client)
  @client = client
end

Instance Method Details

#create(model_id, **options) ⇒ Hash Also known as: transcribe

POST /v1/speech-to-text Transcribe an audio or video file Documentation: https://elevenlabs.io/docs/api-reference/speech-to-text

Parameters:

  • model_id (String)

    The ID of the model to use for transcription

  • options (Hash)

    Optional parameters

Options Hash (**options):

  • :file (IO, File)

    The file to transcribe (required if no cloud_storage_url)

  • :filename (String)

    Original filename (required if file provided)

  • :cloud_storage_url (String)

    HTTPS URL of file to transcribe (required if no file)

  • :enable_logging (Boolean)

    Enable logging (default: true)

  • :language_code (String)

    ISO-639-1 or ISO-639-3 language code

  • :tag_audio_events (Boolean)

    Tag audio events like (laughter) (default: true)

  • :num_speakers (Integer)

    Maximum number of speakers (1-32)

  • :timestamps_granularity (String)

    Timestamp granularity ("none", "word", "character")

  • :diarize (Boolean)

    Annotate which speaker is talking (default: false)

  • :diarization_threshold (Float)

    Diarization threshold (0.1-0.4)

  • :additional_formats (Array)

    Additional export formats

  • :file_format (String)

    Input file format ("pcm_s16le_16" or "other")

  • :webhook (Boolean)

    Send result to webhook (default: false)

  • :webhook_id (String)

    Specific webhook ID

  • :temperature (Float)

    Randomness control (0.0-2.0)

  • :seed (Integer)

    Deterministic sampling seed (0-2147483647)

  • :use_multi_channel (Boolean)

    Multi-channel processing (default: false)

  • :webhook_metadata (String, Hash)

    Metadata for webhook

Returns:

  • (Hash)

    Transcription result or webhook response



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/elevenlabs_client/endpoints/speech_to_text.rb', line 34

def create(model_id, **options)
  endpoint = "/v1/speech-to-text"
  
  # Build query parameters
  query_params = {}
  query_params[:enable_logging] = options[:enable_logging] unless options[:enable_logging].nil?
  
  # Add query parameters to endpoint if any exist
  if query_params.any?
    query_string = query_params.map { |k, v| "#{k}=#{v}" }.join("&")
    endpoint += "?#{query_string}"
  end

  # Build multipart payload
  payload = {
    model_id: model_id
  }
  
  # Add file or cloud storage URL (exactly one is required)
  if options[:file] && options[:filename]
    payload[:file] = @client.file_part(options[:file], options[:filename])
  elsif options[:cloud_storage_url]
    payload[:cloud_storage_url] = options[:cloud_storage_url]
  else
    raise ArgumentError, "Either :file with :filename or :cloud_storage_url must be provided"
  end
  
  # Add optional form parameters
  payload[:language_code] = options[:language_code] if options[:language_code]
  payload[:tag_audio_events] = options[:tag_audio_events] unless options[:tag_audio_events].nil?
  payload[:num_speakers] = options[:num_speakers] if options[:num_speakers]
  payload[:timestamps_granularity] = options[:timestamps_granularity] if options[:timestamps_granularity]
  payload[:diarize] = options[:diarize] unless options[:diarize].nil?
  payload[:diarization_threshold] = options[:diarization_threshold] if options[:diarization_threshold]
  payload[:additional_formats] = options[:additional_formats] if options[:additional_formats]
  payload[:file_format] = options[:file_format] if options[:file_format]
  payload[:webhook] = options[:webhook] unless options[:webhook].nil?
  payload[:webhook_id] = options[:webhook_id] if options[:webhook_id]
  payload[:temperature] = options[:temperature] if options[:temperature]
  payload[:seed] = options[:seed] if options[:seed]
  payload[:use_multi_channel] = options[:use_multi_channel] unless options[:use_multi_channel].nil?
  
  # Handle webhook_metadata (can be string or hash)
  if options[:webhook_metadata]
    if options[:webhook_metadata].is_a?(Hash)
      payload[:webhook_metadata] = options[:webhook_metadata].to_json
    else
      payload[:webhook_metadata] = options[:webhook_metadata]
    end
  end

  @client.post_multipart(endpoint, payload)
end

#delete_transcript(transcription_id) ⇒ Hash Also known as: delete_transcription, remove_transcript

DELETE /v1/speech-to-text/transcripts/:transcription_id Delete a previously generated transcript by its ID Documentation: https://elevenlabs.io/docs/api-reference/speech-to-text/delete-transcript

Parameters:

  • transcription_id (String)

    The unique ID of the transcript to delete

Returns:

  • (Hash)

    Delete confirmation response



105
106
107
108
# File 'lib/elevenlabs_client/endpoints/speech_to_text.rb', line 105

def delete_transcript(transcription_id)
  endpoint = "/v1/speech-to-text/transcripts/#{transcription_id}"
  @client.delete(endpoint)
end

#get_transcript(transcription_id) ⇒ Hash Also known as: get_transcription, retrieve_transcript

GET /v1/speech-to-text/transcripts/:transcription_id Retrieve a previously generated transcript by its ID Documentation: https://elevenlabs.io/docs/api-reference/speech-to-text/get-transcript

Parameters:

  • transcription_id (String)

    The unique ID of the transcript to retrieve

Returns:

  • (Hash)

    The transcript data



94
95
96
97
# File 'lib/elevenlabs_client/endpoints/speech_to_text.rb', line 94

def get_transcript(transcription_id)
  endpoint = "/v1/speech-to-text/transcripts/#{transcription_id}"
  @client.get(endpoint)
end