Skip to content

Commit

Permalink
enhancement: Automatically fetch captions when extracting
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Sep 15, 2024
1 parent 9dd2e9d commit cda20d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
32 changes: 22 additions & 10 deletions apps/cf/lib/llms/statements_creator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,13 @@ defmodule CF.LLMs.StatementsCreator do
Create statements from a video that has captions using LLMs
"""
def process_video!(video_id) do
DB.Schema.Video
|> join(:inner, [v], vc in DB.Schema.VideoCaption, on: v.id == vc.video_id)
|> where([v, vc], v.id == ^video_id)
|> order_by([v, vc], desc: vc.inserted_at)
|> limit(1)
|> select([v, vc], {v, vc})
|> DB.Repo.one()
|> case do
video = DB.Repo.get(DB.Schema.Video, video_id)

case fetch_or_download_captions(video) do
nil ->
raise "Video or captions not found"
raise "Video captions not found"

{video, video_caption} ->
video_caption ->
video_caption.parsed
|> chunk_captions()
|> Enum.map(fn captions ->
Expand All @@ -56,6 +51,23 @@ defmodule CF.LLMs.StatementsCreator do
end
end

defp fetch_or_download_captions(video) do
case DB.Schema.VideoCaption
|> where([vc], vc.video_id == ^video.id)
|> order_by(desc: :inserted_at)
|> limit(1)
|> DB.Repo.one() do
nil ->
case CF.Videos.download_captions(video) do
{:ok, video_caption} -> video_caption
_ -> nil
end

video_caption ->
video_caption
end
end

@doc """
Chunk captions everytime we reach the max caption length
"""
Expand Down
8 changes: 1 addition & 7 deletions apps/cf/lib/videos/videos.ex
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ defmodule CF.Videos do

@doc """
Download and store captions for a video.
Returns captions if success or {:error, reason} if something bad happend.
Usage:
iex> download_captions(video)
Returns captions if success or {:error, reason} if something bad happened.
"""
def download_captions(video = %Video{}) do
# Try to fetch new captions
Expand All @@ -194,9 +191,6 @@ defmodule CF.Videos do
end

{:error, :not_found}

result ->
result
end
end

Expand Down

0 comments on commit cda20d0

Please sign in to comment.