This commit is contained in:
Chris McCord 2023-03-13 11:37:59 -04:00
parent 0832300e4b
commit 38d5158773
8 changed files with 1 additions and 71 deletions

View file

@ -10,19 +10,7 @@ defmodule LiveBeats.Application do
LiveBeats.MediaLibrary.attach()
topologies = Application.get_env(:libcluster, :topologies) || []
{:ok, whisper} = Bumblebee.load_model({:hf, "openai/whisper-tiny"})
{:ok, featurizer} = Bumblebee.load_featurizer({:hf, "openai/whisper-tiny"})
{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "openai/whisper-tiny"})
children = [
{Nx.Serving,
serving:
Bumblebee.Audio.speech_to_text(whisper, featurizer, tokenizer,
max_new_tokens: 200,
defn_options: [batch_size: 10, compiler: EXLA]
),
name: WhisperServing,
batch_timeout: 100},
{Cluster.Supervisor, [topologies, [name: LiveBeats.ClusterSupervisor]]},
{Task.Supervisor, name: LiveBeats.TaskSupervisor},
# Start the Ecto repository

View file

@ -1,17 +0,0 @@
defmodule LiveBeats.Audio do
def speech_to_text(path, chunk_time \\ 15.0, func) when chunk_time <= 30.0 do
{:ok, stat} = LiveBeats.MP3Stat.parse(path)
Stream.iterate(0, &(&1 + chunk_time))
|> Enum.take_while(&(&1 < stat.duration))
|> Task.async_stream(
fn ss ->
args = ~w(-i #{path} -ac 1 -ar 16k -f f32le -ss #{ss} -t #{chunk_time} -v quiet -)
{data, 0} = System.cmd("ffmpeg", args)
{ss, Nx.Serving.batched_run(WhisperServing, Nx.from_binary(data, :f32))}
end,
timeout: :infinity, max_concurrency: 4
)
|> Enum.map(fn {:ok, {ss, %{results: [%{text: text}]}}} -> func.(ss, text) end)
end
end

View file

@ -22,8 +22,4 @@ defmodule LiveBeats.MediaLibrary.Events do
defmodule SongDeleted do
defstruct song: nil
end
defmodule SpeechToText do
defstruct song_id: nil, segment: nil
end
end

View file

@ -24,7 +24,6 @@ defmodule LiveBeats.MediaLibrary.Song do
field :position, :integer, default: 0
belongs_to :user, Accounts.User
belongs_to :genre, LiveBeats.MediaLibrary.Genre
embeds_many :speech_segments, LiveBeats.MediaLibrary.TextSegment
timestamps()
end

View file

@ -1,9 +0,0 @@
defmodule LiveBeats.MediaLibrary.TextSegment do
use Ecto.Schema
embedded_schema do
field :start_time, :float
field :text, :string
field :in_progress?, :boolean, virtual: true, default: false
end
end

View file

@ -155,7 +155,6 @@ defmodule LiveBeatsWeb.ProfileLive do
MediaLibrary.subscribe_to_profile(profile)
Accounts.subscribe(current_user.id)
Presence.subscribe(profile)
send(self(), :mounted)
end
active_song = MediaLibrary.get_current_active_song(profile)
@ -171,7 +170,6 @@ defmodule LiveBeatsWeb.ProfileLive do
profile: profile,
owns_profile?: MediaLibrary.owns_profile?(current_user, profile),
songs_count: Enum.count(songs),
mounted?: false
)
|> stream(:songs, songs)
|> stream(:speech_segments, speech_segments, dom_id: &"ss-#{trunc(&1.start_time)}")
@ -229,8 +227,6 @@ defmodule LiveBeatsWeb.ProfileLive do
end
end
def handle_info(:mounted, socket), do: {:noreply, assign(socket, :mounted?, true) }
def handle_info({LiveBeatsWeb.Presence, %{user_joined: presence}}, socket) do
{:noreply, assign_presence(socket, presence)}
end
@ -284,17 +280,6 @@ defmodule LiveBeatsWeb.ProfileLive do
end)}
end
def handle_info(
{MediaLibrary, %MediaLibrary.Events.SpeechToText{song_id: id, segment: segment}},
socket
) do
if socket.assigns.active_song_id == id do
{:noreply, stream_insert(socket, :speech_segments, segment)}
else
{:noreply, socket}
end
end
def handle_info({MediaLibrary, %MediaLibrary.Events.SongDeleted{song: song}}, socket) do
{:noreply,
socket

View file

@ -54,10 +54,7 @@ defmodule LiveBeats.MixProject do
{:heroicons, "~> 0.2.2"},
{:castore, "~> 0.1.13"},
{:tailwind, "~> 0.1"},
{:libcluster, "~> 3.3.1"},
{:bumblebee, github: "elixir-nx/bumblebee"},
{:exla, "~> 0.5.1"},
{:erlexec, "~> 2.0"}
{:libcluster, "~> 3.3.1"}
]
end

View file

@ -1,9 +0,0 @@
defmodule LiveBeats.Repo.Migrations.AddSpeechSegmentsToSongs do
use Ecto.Migration
def change do
alter table(:songs) do
add :speech_segments, {:array, :map}, null: false, default: []
end
end
end