Module: OpenAI::Whisper

Included in:
OpenAIBot
Defined in:
lib/open_ai/whisper.rb

Instance Method Summary collapse

Instance Method Details

#ogg_to_mp3(file) ⇒ Object



47
48
49
50
51
52
# File 'lib/open_ai/whisper.rb', line 47

def ogg_to_mp3(file)
  ogg = file.original_filename
  mp3 = ogg.sub(/og.\z/, "mp3")
  `ffmpeg -i ./#{ogg} -acodec libmp3lame ./#{mp3} -y 2>/dev/null`
  { file: File.open("./#{mp3}", "rb"), names: [ogg, mp3] }
end

#send_whisper_error(text) ⇒ Object



43
44
45
# File 'lib/open_ai/whisper.rb', line 43

def send_whisper_error(text)
  reply_code(text)
end

#send_whisper_request(file) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/open_ai/whisper.rb', line 54

def send_whisper_request(file)
  open_ai.audio.transcribe(
    parameters: {
      model: "whisper-1",
      file: file
    }
  )
end

#send_whisper_response(text) ⇒ Object



39
40
41
# File 'lib/open_ai/whisper.rb', line 39

def send_whisper_response(text)
  reply(text)
end

#transcribeObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/open_ai/whisper.rb', line 5

def transcribe
  if !allowed_chat?
    if @command == "/transcribe"
      reply(chat_not_allowed_message) if chat_not_allowed_message
    end

    return
  end

  attempt(3) do
    voice =
      if @command == "/transcribe"
        @replies_to&.voice
      else
        @msg.voice
      end

    return unless voice

    send_chat_action(:typing)

    file = ogg_to_mp3(download_file(voice))
    response = send_whisper_request(file[:file])

    if response["error"]
      send_whisper_error(response["error"])
    else
      send_whisper_response(response["text"])
    end
  ensure
    FileUtils.rm_rf(file[:names]) if file
  end
end