Module: Legion::LLM::API::Namespaces::OpenAI::Audio::Translations

Extended by:
Legion::Logging::Helper, Sinatra::Extension
Defined in:
lib/legion/llm/api/namespaces/openai/audio/translations.rb

Class Method Summary collapse

Class Method Details

.capable_provider_available?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/llm/api/namespaces/openai/audio/translations.rb', line 25

def self.capable_provider_available?
  instances = begin
    Legion::LLM::Call::Registry.all_instances
  rescue StandardError
    []
  end
  instances.any? do |entry|
    caps = entry[:capabilities] || entry['capabilities'] || []
    syms = caps.map(&:to_sym)
    syms.include?(:audio_translation) || syms.include?(:audio_transcription) || syms.include?(:speech_to_text)
  end
rescue StandardError => e
  log.warn("[llm][api][openai][audio][translations] action=capability_check error=#{e.message}")
  false
end

.extract_translation_text(pipeline_response) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/legion/llm/api/namespaces/openai/audio/translations.rb', line 52

def self.extract_translation_text(pipeline_response)
  raw_msg = pipeline_response.message
  case raw_msg
  when Hash
    msg = raw_msg.respond_to?(:transform_keys) ? raw_msg.transform_keys(&:to_sym) : raw_msg
    (msg[:content] || msg[:text] || '').to_s
  when String
    raw_msg
  else
    raw_msg.respond_to?(:content) ? raw_msg.content.to_s : raw_msg.to_s
  end
end

.format_srt(text) ⇒ Object



65
66
67
# File 'lib/legion/llm/api/namespaces/openai/audio/translations.rb', line 65

def self.format_srt(text)
  "1\n00:00:00,000 --> 00:00:30,000\n#{text}\n\n"
end

.format_vtt(text) ⇒ Object



69
70
71
# File 'lib/legion/llm/api/namespaces/openai/audio/translations.rb', line 69

def self.format_vtt(text)
  "WEBVTT\n\n00:00:00.000 --> 00:00:30.000\n#{text}\n\n"
end

.read_file_param(file_param) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/legion/llm/api/namespaces/openai/audio/translations.rb', line 41

def self.read_file_param(file_param)
  return '' if file_param.nil? || file_param.to_s.empty?

  if file_param.is_a?(Hash)
    tf = file_param[:tempfile] || file_param['tempfile']
    return Base64.strict_encode64(tf.read) if tf.respond_to?(:read)
  end

  file_param.to_s
end