Skip to content

Commit c170866

Browse files
authored
Merge pull request #23 from aar0np/main
reworking recommended videos...again?
2 parents 1a0ab71 + d8df7d8 commit c170866

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

app/api/v1/endpoints/video_catalog.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,6 @@ async def get_related_videos_for_video(
366366
] = 5,
367367
):
368368
"""Return a list of videos related to the given video.
369-
370-
The underlying implementation is currently stubbed out and will return the
371-
latest videos (excluding the source video) with a random relevance score.
372369
"""
373370

374371
related_items = await recommendation_service.get_related_videos(

app/services/recommendation_service.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616
async def get_related_videos(
1717
video_id: VideoID, limit: int = 10
1818
) -> List[RecommendationItem]:
19-
"""Return a stubbed *related videos* list.
20-
21-
In a future iteration this will call into a real recommendation engine that
22-
analyses the content of the referenced video to find similar items. For the
23-
moment we simply return the latest videos (excluding the reference video)
24-
and assign each a random relevance score.
25-
"""
2619

2720
from opentelemetry import trace
2821
import time
@@ -42,18 +35,28 @@ async def get_related_videos(
4235
if target_video is None:
4336
return []
4437

45-
latest_summaries, _total = await video_service.list_latest_videos(
46-
page=1, page_size=limit + 5
38+
latest_summaries, _total = await video_service.get_recommended_videos(
39+
query_vector=target_video.content_features, page=1, page_size=limit + 5
4740
)
4841

4942
related_items: List[RecommendationItem] = []
43+
unique_video_names: List[str] = []
5044

5145
for summary in latest_summaries:
5246
if summary.videoId == video_id:
5347
# Skip the source video itself
5448
continue
49+
50+
if summary.title in unique_video_names:
51+
# Skip if we've already added this video to the list
52+
# ...sometimes we get duplicate rows
53+
continue
54+
5555
if len(related_items) >= limit:
5656
break
57+
58+
unique_video_names.append(summary.title)
59+
5760
related_items.append(
5861
RecommendationItem(
5962
videoId=summary.videoId,

app/services/video_service.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from __future__ import annotations
99

10+
from bdb import effective
1011
import re
1112
from datetime import datetime, timezone, timedelta
1213
import asyncio
@@ -529,13 +530,33 @@ async def list_latest_videos(
529530
source_table_name=VIDEOS_TABLE_NAME,
530531
)
531532

533+
async def get_recommended_videos(
534+
query_vector: Optional[List[float]],
535+
page: int,
536+
page_size: int,
537+
db_table: Optional[AstraDBCollection] = None,
538+
) -> Tuple[List[VideoSummary], int]:
539+
540+
if query_vector is None:
541+
return [], 0
542+
543+
return await list_videos_with_query(
544+
{},
545+
page,
546+
page_size=page_size,
547+
sort_options={"content_features": query_vector},
548+
db_table=db_table,
549+
source_table_name=VIDEOS_TABLE_NAME
550+
)
551+
532552

533553
async def list_videos_by_tag(
534554
tag: str,
535555
page: int,
536556
page_size: int,
537557
db_table: Optional[AstraDBCollection] = None,
538558
) -> Tuple[List[VideoSummary], int]:
559+
539560
query_filter = {
540561
"tags": {"$in": [tag]},
541562
}

0 commit comments

Comments
 (0)