Files

33 lines
802 B
Elixir

defmodule Whisper.Transcriber do
use GenServer
alias Whisper.SendToModel
def start_link(_) do
GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
end
def init(state) do
schedule()
{:ok, state}
end
def handle_info(:transcribe, state) do
for {ref, chunks} <- AudioBuffer.flush_all(), chunks != [] do
audio =
chunks
|> Enum.map(fn {_rate, bin} -> bin end)
|> IO.iodata_to_binary()
Task.start(fn ->
text = SendToModel.transcribe_binary(audio, Whisper.RealtimeModel.Serving)
Phoenix.PubSub.broadcast(Whisper.PubSub, "transcription", {:transcription_tiny, %{ref: ref, text: text}})
end)
end
schedule()
{:noreply, state}
end
defp schedule(), do: Process.send_after(self(), :transcribe, 300)
end