Skip to content

Commit cda20d0

Browse files
committed
enhancement: Automatically fetch captions when extracting
1 parent 9dd2e9d commit cda20d0

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

apps/cf/lib/llms/statements_creator.ex

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,13 @@ defmodule CF.LLMs.StatementsCreator do
3030
Create statements from a video that has captions using LLMs
3131
"""
3232
def process_video!(video_id) do
33-
DB.Schema.Video
34-
|> join(:inner, [v], vc in DB.Schema.VideoCaption, on: v.id == vc.video_id)
35-
|> where([v, vc], v.id == ^video_id)
36-
|> order_by([v, vc], desc: vc.inserted_at)
37-
|> limit(1)
38-
|> select([v, vc], {v, vc})
39-
|> DB.Repo.one()
40-
|> case do
33+
video = DB.Repo.get(DB.Schema.Video, video_id)
34+
35+
case fetch_or_download_captions(video) do
4136
nil ->
42-
raise "Video or captions not found"
37+
raise "Video captions not found"
4338

44-
{video, video_caption} ->
39+
video_caption ->
4540
video_caption.parsed
4641
|> chunk_captions()
4742
|> Enum.map(fn captions ->
@@ -56,6 +51,23 @@ defmodule CF.LLMs.StatementsCreator do
5651
end
5752
end
5853

54+
defp fetch_or_download_captions(video) do
55+
case DB.Schema.VideoCaption
56+
|> where([vc], vc.video_id == ^video.id)
57+
|> order_by(desc: :inserted_at)
58+
|> limit(1)
59+
|> DB.Repo.one() do
60+
nil ->
61+
case CF.Videos.download_captions(video) do
62+
{:ok, video_caption} -> video_caption
63+
_ -> nil
64+
end
65+
66+
video_caption ->
67+
video_caption
68+
end
69+
end
70+
5971
@doc """
6072
Chunk captions everytime we reach the max caption length
6173
"""

apps/cf/lib/videos/videos.ex

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ defmodule CF.Videos do
166166

167167
@doc """
168168
Download and store captions for a video.
169-
Returns captions if success or {:error, reason} if something bad happend.
170-
171-
Usage:
172-
iex> download_captions(video)
169+
Returns captions if success or {:error, reason} if something bad happened.
173170
"""
174171
def download_captions(video = %Video{}) do
175172
# Try to fetch new captions
@@ -194,9 +191,6 @@ defmodule CF.Videos do
194191
end
195192

196193
{:error, :not_found}
197-
198-
result ->
199-
result
200194
end
201195
end
202196

0 commit comments

Comments
 (0)