[Video] Testing Google Gemini Audio Capabilities

• 5 min read

Gemini with audio

Video

Code

Mix.install([
  {:req, "~> 0.4.14"},
  {:kino, "~> 0.12.0"}
])

Form

form =
  Kino.Control.form(
    [
      prompt: Kino.Input.textarea("Prompt"),
      audio: Kino.Input.audio("Audio", format: :wav)
    ],
    submit: "Submit"
  )

frame = Kino.Frame.new()

Kino.listen(form, fn %{data: %{prompt: prompt, audio: audio}} ->
  Kino.Frame.clear(frame)

  %{file_ref: file_ref} = audio
  file_path = Kino.Input.file_path(file_ref)

  Gemini.chat_streaming(prompt, file_path)
  |> Stream.each(&Kino.Frame.append(frame, Kino.Text.new(&1)))
  |> Stream.run()
end)

Kino.Layout.grid([form, frame])
defmodule Gemini do
  def chat_streaming(prompt, file_path) do
    pid = self()

    file_contents = File.read!(file_path)
    base64 = Base.encode64(file_contents)

    gemini_api_key = System.get_env("LB_GEMINI_API_KEY")

    Stream.resource(
      fn ->
        Task.async(fn ->
          Req.post!(
            "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:streamGenerateContent?key=#{gemini_api_key}&alt=sse",
            receive_timeout: :infinity,
            headers: [
              {"content-type", "application/json"}
            ],
            json: %{
              contents: [
                %{
                  role: "user",
                  parts: [
                    %{
                      text: prompt
                    },
                    %{
                      inlineData: %{
                        mimeType: "audio/wav",
                        data: base64
                      }
                    }
                  ]
                }
              ]
            },
            into: fn {:data, data}, {req, resp} ->
              chunks =
                data
                |> String.split("\n")
                |> Enum.filter(fn line ->
                  String.starts_with?(line, "data: {")
                end)
                |> Enum.map(fn line ->
                  line
                  |> String.replace_prefix("data: ", "")
                  |> Jason.decode!()
                  |> extract_text()
                end)

              for chunk <- chunks do
                send(pid, chunk)
              end

              {:cont, {req, resp}}
            end
          )

          send(pid, :done)
        end)
      end,
      fn task ->
        receive do
          :done ->
            {:halt, task}

          data ->
            {[data], task}
        after
          15_000 ->
            {:halt, task}
        end
      end,
      fn task -> Task.await(task, 120_000) end
    )
  end

  def extract_text(map) do
    map["candidates"]
    |> List.first()
    |> get_in(["content", "parts"])
    |> Enum.map(& &1["text"])
    |> Enum.join()
  end
end
1 cheers
Now look what you've done 🌋
Stop clicking and run for your life! 😱
Uh oh, I don't think the system can't handle it! 🔥
Stop it, you're too kind 😄
Thanks for the love! ❤️
Thanks, glad you enjoyed it! Care to share?
Hacker News Reddit

Hey 👋, thanks for reading!

I am currently looking for my next job.

If you know of someplace that might be a good fit for me, I'd really appreciate an intro: mail@samrat.me

Recommended Posts ✍🏻

See All »
• 6 min read
TIL: Sum Types With `instructor_ex`
Read Post »
• 1 min read
TIL: File Uploads Using the Req Elixir Library
Read Post »
• 1 min read
TIL: Creating `sentence-transformers` Embedding...
Read Post »