Skip to content

Commit 94c5fab

Browse files
authored
Merge pull request #478 from CaptainFact/staging
Release
2 parents ff39336 + 7b6e999 commit 94c5fab

File tree

4 files changed

+49
-36
lines changed

4 files changed

+49
-36
lines changed

apps/cf/lib/llms/statements_creator.ex

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,38 @@ 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+
video_caption = fetch_or_download_captions(video)
35+
36+
if video_caption != nil do
37+
video_caption.parsed
38+
|> chunk_captions()
39+
|> Enum.map(fn captions ->
40+
video
41+
|> get_llm_suggested_statements(captions)
42+
|> filter_known_statements(video)
43+
|> create_statements_from_inputs(video)
44+
|> broadcast_statements(video)
45+
46+
Process.sleep(500)
47+
end)
48+
end
49+
end
50+
51+
defp fetch_or_download_captions(video) do
52+
case DB.Schema.VideoCaption
53+
|> where([vc], vc.video_id == ^video.id)
54+
|> order_by(desc: :inserted_at)
55+
|> limit(1)
56+
|> DB.Repo.one() do
4157
nil ->
42-
raise "Video or captions not found"
43-
44-
{video, video_caption} ->
45-
video_caption.parsed
46-
|> chunk_captions()
47-
|> Enum.map(fn captions ->
48-
video
49-
|> get_llm_suggested_statements(captions)
50-
|> filter_known_statements(video)
51-
|> create_statements_from_inputs(video)
52-
|> broadcast_statements(video)
53-
54-
Process.sleep(500)
55-
end)
58+
case CF.Videos.download_captions(video) do
59+
{:ok, video_caption} -> video_caption
60+
_ -> nil
61+
end
62+
63+
video_caption ->
64+
video_caption
5665
end
5766
end
5867

apps/cf/lib/llms/templates/statements_extractor_user_prompt.eex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"title": "<%= video.id %>"
55
},
66
"captions": <%= captions |> Enum.map(fn caption -> %{
7-
"start": floor(caption["start"]),
8-
"text": String.trim(caption["text"])
7+
start: floor(caption["start"]),
8+
text: String.trim(caption["text"])
99
} end) |> Jason.encode! %>
1010
}
1111
```

apps/cf/lib/videos/videos.ex

Lines changed: 15 additions & 8 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
@@ -181,6 +178,19 @@ defmodule CF.Videos do
181178
captions_base
182179
|> VideoCaption.changeset(Map.merge(captions, %{video_id: video.id}))
183180
|> Repo.insert_or_update()
181+
|> case do
182+
# The Atoms become strings when saving/loading from the DB, let's make things consistent
183+
{:error, changeset} ->
184+
{:error, changeset}
185+
186+
{:ok, _video_caption} ->
187+
video
188+
|> get_existing_captions()
189+
|> case do
190+
nil -> {:error, :not_found}
191+
existing -> {:ok, existing}
192+
end
193+
end
184194

185195
# If no Youtube caption found, insert a dummy entry in DB to prevent retrying for 30 days
186196
{:error, :not_found} ->
@@ -194,16 +204,13 @@ defmodule CF.Videos do
194204
end
195205

196206
{:error, :not_found}
197-
198-
result ->
199-
result
200207
end
201208
end
202209

203210
defp get_existing_captions(video) do
204211
VideoCaption
205212
|> where([vc], vc.video_id == ^video.id)
206-
|> order_by(desc: :inserted_at)
213+
|> order_by(desc: :updated_at)
207214
|> limit(1)
208215
|> Repo.one()
209216
end

apps/cf_jobs/lib/jobs/download_captions.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ defmodule CF.Jobs.DownloadCaptions do
55
import Ecto.Query
66

77
alias DB.Repo
8-
alias DB.Schema.UserAction
98
alias DB.Schema.Video
109
alias DB.Schema.VideoCaption
1110
alias DB.Schema.UsersActionsReport
1211

13-
alias CF.Jobs.ReportManager
14-
1512
@name :download_captions
1613
@analyser_id UsersActionsReport.analyser_id(@name)
1714

@@ -55,7 +52,7 @@ defmodule CF.Jobs.DownloadCaptions do
5552
on: captions.video_id == v.id,
5653
where:
5754
is_nil(captions.id) or
58-
captions.inserted_at < ^DateTime.add(DateTime.utc_now(), -30 * 24 * 60 * 60, :second),
55+
captions.updated_at < ^DateTime.add(DateTime.utc_now(), -30 * 24 * 60 * 60, :second),
5956
group_by: v.id,
6057
order_by: [desc: v.inserted_at]
6158
)

0 commit comments

Comments
 (0)