Spaces:
Running
Running
File size: 6,658 Bytes
0eeeed2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | """
Shared pgvector query helpers.
Both hybrid_search.py and recommendations.py call into these utilities
so the raw cosine-similarity SQL is written exactly once.
"""
import logging
from typing import List, Optional, Tuple
from sqlalchemy import text
from sqlalchemy.orm import Session
logger = logging.getLogger(__name__)
def vector_to_str(embedding) -> str:
"""Convert a numpy / list embedding to the literal string pgvector expects."""
return "[" + ",".join(str(float(v)) for v in embedding) + "]"
# ---------------------------------------------------------------------------
# Single-column similarity (used by legacy callers / fallback)
# ---------------------------------------------------------------------------
def cosine_similarity_query(
db: Session,
embedding_str: str,
*,
vector_column: str = "caption_embedding",
limit: int = 20,
offset: int = 0,
status: str = "approved",
exclude_video_ids: Optional[List[int]] = None,
) -> List[Tuple[int, float]]:
"""Run a pgvector cosine-similarity search against media_analysis + videos.
Returns [(video_id, similarity_score), ...] ordered by similarity DESC.
Parameters
----------
embedding_str : str
Pre-formatted vector string, e.g. ``"[0.1,0.2,...]"``.
vector_column : str
Which pgvector column to compare against (default ``caption_embedding``).
exclude_video_ids : list[int], optional
Video IDs to exclude from results (e.g. already-liked videos).
"""
exclude_clause = ""
params: dict = {
"embedding": embedding_str,
"status": status,
"limit": limit,
"offset": offset,
}
if exclude_video_ids:
exclude_clause = "AND v.id NOT IN :exclude_ids"
params["exclude_ids"] = tuple(exclude_video_ids)
sql = text(f"""
SELECT ma.video_id,
(1 - (ma.{vector_column} <=> CAST(:embedding AS vector))) AS similarity
FROM media_analysis ma
JOIN videos v ON v.id = ma.video_id
WHERE ma.{vector_column} IS NOT NULL
AND v.status = :status
AND v.video_url IS NOT NULL
AND v.video_url != ''
AND ma.status = 'done'
{exclude_clause}
ORDER BY ma.{vector_column} <=> CAST(:embedding AS vector)
LIMIT :limit OFFSET :offset
""")
rows = db.execute(sql, params).fetchall()
return [(row[0], float(row[1])) for row in rows]
# ---------------------------------------------------------------------------
# Blended dual-column similarity (caption + visual)
# ---------------------------------------------------------------------------
def blended_cosine_similarity_query(
db: Session,
caption_embedding_str: Optional[str],
visual_embedding_str: Optional[str],
*,
caption_weight: float = 0.5,
visual_weight: float = 0.5,
limit: int = 20,
offset: int = 0,
status: str = "approved",
exclude_video_ids: Optional[List[int]] = None,
) -> List[Tuple[int, float]]:
"""Cosine similarity against both caption and visual embeddings, blended.
Returns [(video_id, blended_score), ...] ordered by blended_score DESC.
If a video has only one embedding type (the other is NULL), the NULL side
contributes 0 via COALESCE so the score degrades gracefully rather than
producing NULL.
Either embedding string can be ``None`` (e.g. no visual taste vector
computed). When one is None, the query falls back to single-column mode
for that side.
"""
exclude_clause = ""
params: dict = {
"caption_weight": caption_weight,
"visual_weight": visual_weight,
"status": status,
"limit": limit,
"offset": offset,
}
if exclude_video_ids:
exclude_clause = "AND v.id NOT IN :exclude_ids"
params["exclude_ids"] = tuple(exclude_video_ids)
# Build per-side COALESCE expressions
if caption_embedding_str is not None:
params["caption_vec"] = caption_embedding_str
caption_expr = "COALESCE(1 - (ma.caption_embedding <=> CAST(:caption_vec AS vector)), 0)"
else:
caption_expr = "0"
if visual_embedding_str is not None:
params["visual_vec"] = visual_embedding_str
visual_expr = "COALESCE(1 - (ma.visual_embedding <=> CAST(:visual_vec AS vector)), 0)"
else:
visual_expr = "0"
# At least one side must be active, otherwise there's nothing to score
if caption_embedding_str is None and visual_embedding_str is None:
return []
# We need at least one non-null embedding column to filter on
if caption_embedding_str is not None and visual_embedding_str is not None:
non_null_filter = "(ma.caption_embedding IS NOT NULL OR ma.visual_embedding IS NOT NULL)"
elif caption_embedding_str is not None:
non_null_filter = "ma.caption_embedding IS NOT NULL"
else:
non_null_filter = "ma.visual_embedding IS NOT NULL"
sql = text(f"""
SELECT ma.video_id,
:caption_weight * {caption_expr}
+ :visual_weight * {visual_expr} AS blended_sim
FROM media_analysis ma
JOIN videos v ON v.id = ma.video_id
WHERE {non_null_filter}
AND v.status = :status
AND v.video_url IS NOT NULL
AND v.video_url != ''
AND ma.status = 'done'
{exclude_clause}
ORDER BY blended_sim DESC
LIMIT :limit OFFSET :offset
""")
rows = db.execute(sql, params).fetchall()
return [(row[0], float(row[1])) for row in rows]
# ---------------------------------------------------------------------------
# Trending fallback (no embeddings required)
# ---------------------------------------------------------------------------
def trending_query(
db: Session,
*,
limit: int = 20,
offset: int = 0,
days: int = 7,
) -> List[Tuple[int, float]]:
"""Fallback query: most-liked videos from the last *days* days.
Returns [(video_id, score), ...] where score is normalised likes_count.
Used when a user has no embedding history (new user or no embeddings yet).
"""
sql = text("""
SELECT v.id, LEAST(v.likes_count, 1000)::float AS score
FROM videos v
WHERE v.status = 'approved'
AND v.video_url IS NOT NULL
AND v.video_url != ''
AND v.created_at >= NOW() - (:days || ' days')::interval
ORDER BY v.likes_count DESC, v.views DESC
LIMIT :limit OFFSET :offset
""")
rows = db.execute(sql, {"days": days, "limit": limit, "offset": offset}).fetchall()
return [(row[0], float(row[1])) for row in rows]
|