Spaces:
Sleeping
Sleeping
File size: 11,859 Bytes
07f113d | 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | import os
import math
import random
import asyncio
import logging
from collections import defaultdict
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
from typing import List, Dict
import numpy as np
from dotenv import load_dotenv
from supabase import create_client
from sentence_transformers import SentenceTransformer
import pdb
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
# Configuration
SEED = 42
random.seed(SEED)
np.random.seed(SEED)
TOP_K = 75
HISTORY_WINDOW = timedelta(days=1000)
TIMEZONE = ZoneInfo("UTC")
# Global variables
supabase_client = None
user_interactions = defaultdict(set)
post_features = {}
post_metadata = {}
if not os.path.exists('/tmp/cache'):
os.makedirs('/tmp/cache')
sentence_model = SentenceTransformer(
'all-MiniLM-L6-v2', cache_folder='/tmp/cache')
SUPABASE_URL = os.getenv('supabaseUrl')
SUPABASE_KEY = os.getenv('supabaseAnonKey')
def get_supabase_client():
global supabase_client
if supabase_client is None:
supabase_client = create_client(SUPABASE_URL, SUPABASE_KEY)
return supabase_client
def parse_datetime(dt_str: str) -> datetime:
"""Parse ISO datetime string and ensure correct microsecond precision."""
try:
if '.' in dt_str:
date_part, time_part = dt_str.split('T')
time_part, tz_part = time_part.split('+')
if '.' in time_part:
time_without_micro, micro = time_part.split('.')
micro = micro.ljust(6, '0')
time_part = f"{time_without_micro}.{micro}"
dt_str = f"{date_part}T{time_part}+00:00"
return datetime.fromisoformat(dt_str).astimezone(TIMEZONE)
except Exception as e:
logger.error(f"Error parsing datetime: {dt_str} - {str(e)}")
raise
def decay_weight(interaction_time: datetime, current_time: datetime, decay_rate: float = 0.0001) -> float:
"""
Compute an exponential decay weight for an interaction based on its age.
The decay_rate can be tuned to control how fast interactions lose weight.
"""
time_diff = (current_time - interaction_time).total_seconds()
return math.exp(-decay_rate * time_diff)
def normalize_profile(profile: np.ndarray) -> np.ndarray:
norm = np.linalg.norm(profile)
return profile / norm if norm > 0 else profile
def compute_score(sim_score: float, popularity: float, freshness: float) -> float:
"""
Compute a non-linear recommendation score.
- sim_score is squared to emphasize strong similarities.
- Popularity is log-transformed.
- Freshness is combined linearly.
"""
return 0.6 * (sim_score ** 2) + 0.3 * np.log1p(popularity) + 0.1 * freshness
class Recommender:
def __init__(self, like_weight=1.0, comment_weight=0.5, comment_like_weight=0.3,
reply_weight=0.5, reply_like_weight=0.3):
self.user_profiles = defaultdict(lambda: np.zeros(384))
self.post_popularity = defaultdict(float)
self.last_update = datetime.now(TIMEZONE) - HISTORY_WINDOW
# Parameterized weights
self.like_weight = like_weight
self.comment_weight = comment_weight
self.comment_like_weight = comment_like_weight
self.reply_weight = reply_weight
self.reply_like_weight = reply_like_weight
async def fetch_all_rows(self, table_name: str, columns: str, last_update: datetime, post_id_not_null: bool):
"""Fetch all rows from a table using pagination."""
supabase = get_supabase_client()
page_size = 1000
page = 0
all_data = []
while True:
if post_id_not_null:
response = await asyncio.to_thread(
supabase.table(table_name)
.select(columns)
.gt('created_at', last_update.isoformat())
.not_.is_("post_id", None)
.range(page * page_size, (page + 1) * page_size - 1)
.execute
)
else:
response = await asyncio.to_thread(
supabase.table(table_name)
.select(columns)
.gt('created_at', last_update.isoformat())
.range(page * page_size, (page + 1) * page_size - 1)
.execute
)
if not response.data:
break
all_data.extend(response.data)
page += 1
return all_data
async def update_data(self):
# Current time for decay calculation
current_time = datetime.now(TIMEZONE)
# Fetch all interaction data since last update
likes = await self.fetch_all_rows('likes', 'user_id, post_id, created_at', self.last_update, True)
comments = await self.fetch_all_rows('comments', 'id, user_id, post_id, created_at, comment', self.last_update, True)
commentlikes = await self.fetch_all_rows('commentlikes', 'user_id, author_id, post_id, created_at, comment_id', self.last_update, True)
replies = await self.fetch_all_rows('replies', 'user_id, to, post_id, created_at, comment, comment_id, reply_id', self.last_update, True)
replylikes = await self.fetch_all_rows('replylikes', 'user_id, author_id, post_id, created_at, reply_id', self.last_update, True)
# Process likes with time decay
for like in likes:
user_id = like['user_id']
post_id = like['post_id']
interaction_time = parse_datetime(like['created_at'])
weight = decay_weight(interaction_time, current_time)
user_interactions[user_id].add(post_id)
self.post_popularity[post_id] += self.like_weight * weight
if post_id in post_features:
self.user_profiles[user_id] += post_features[post_id] * \
self.like_weight * weight
# Process comments with time decay
for comment in comments:
user_id = comment['user_id']
post_id = comment['post_id']
interaction_time = parse_datetime(comment['created_at'])
weight = decay_weight(interaction_time, current_time)
user_interactions[user_id].add(post_id)
self.post_popularity[post_id] += self.comment_weight * weight
if post_id in post_features:
self.user_profiles[user_id] += post_features[post_id] * \
self.comment_weight * weight
# Process comment likes with time decay
for clike in commentlikes:
user_id = clike['user_id'] # User who liked the comment
post_id = clike['post_id']
interaction_time = parse_datetime(clike['created_at'])
weight = decay_weight(interaction_time, current_time)
user_interactions[user_id].add(post_id)
self.post_popularity[post_id] += self.comment_like_weight * weight
if post_id in post_features:
self.user_profiles[user_id] += post_features[post_id] * \
self.comment_like_weight * weight
# Process replies with time decay
for reply in replies:
user_id = reply['user_id']
post_id = reply['post_id']
interaction_time = parse_datetime(reply['created_at'])
weight = decay_weight(interaction_time, current_time)
user_interactions[user_id].add(post_id)
self.post_popularity[post_id] += self.reply_weight * weight
if post_id in post_features:
self.user_profiles[user_id] += post_features[post_id] * \
self.reply_weight * weight
# Process reply likes with time decay
for rlike in replylikes:
user_id = rlike['user_id']
post_id = rlike['post_id']
interaction_time = parse_datetime(rlike['created_at'])
weight = decay_weight(interaction_time, current_time)
user_interactions[user_id].add(post_id)
self.post_popularity[post_id] += self.reply_like_weight * weight
if post_id in post_features:
self.user_profiles[user_id] += post_features[post_id] * \
self.reply_like_weight * weight
# OPTIONAL: Process negative feedback if available
# for negative in negative_interactions:
# user_id = negative['user_id']
# post_id = negative['post_id']
# interaction_time = parse_datetime(negative['created_at'])
# weight = decay_weight(interaction_time, current_time)
# user_interactions[user_id].add(post_id)
# self.post_popularity[post_id] -= some_negative_weight * weight
# if post_id in post_features:
# self.user_profiles[user_id] -= post_features[post_id] * some_negative_weight * weight
# Normalize user profiles after processing interactions
for user_id in self.user_profiles:
self.user_profiles[user_id] = normalize_profile(
self.user_profiles[user_id])
# Fetch and update post features
posts = await self.fetch_all_rows('posts', '*', self.last_update, False)
post_texts, post_ids = [], []
for post in posts:
post_id = post['id']
text = f"{post.get('movie_name', '')} {post.get('content', '')}".strip(
)
post_texts.append(text)
post_ids.append(post_id)
post['created_at'] = parse_datetime(post['created_at'])
post['type'] = 'post'
post_metadata[post_id] = post
if post_texts:
embeddings = sentence_model.encode(
post_texts, show_progress_bar=False, convert_to_numpy=True)
for post_id, embedding in zip(post_ids, embeddings):
post_features[post_id] = embedding / np.linalg.norm(embedding)
self.last_update = datetime.now(TIMEZONE)
total_interactions = len(likes) + len(comments) + \
len(commentlikes) + len(replies) + len(replylikes)
logger.info(
f"Data updated: {len(posts)} posts, {total_interactions} interactions")
def get_recommendations(self, user_id: str) -> List[Dict]:
user_profile = self.user_profiles[user_id]
seen_posts = user_interactions[user_id]
scores = {}
now = datetime.now(TIMEZONE)
for post_id, feature in post_features.items():
if post_id in seen_posts:
continue
sim_score = np.dot(user_profile, feature) if np.any(
user_profile) else 0
time_diff = now - post_metadata[post_id]['created_at']
# Freshness is defined as an exponential decay based on weeks old
freshness = math.exp(-time_diff.days / 7.0)
score = compute_score(
sim_score, self.post_popularity[post_id], freshness)
# Adding a small random noise for exploration
scores[post_id] = score + random.uniform(-0.1, 0.1)
top_posts = sorted(
scores.items(), key=lambda x: x[1], reverse=True)[:TOP_K]
results = [post_metadata[post_id] for post_id, _ in top_posts]
random.shuffle(results)
return results
recommender = Recommender()
async def main():
# pdb.set_trace()
user_id = "d7411324-c8ea-42cb-ae59-7b8cbc61594c"
await recommender.update_data()
recommendations = recommender.get_recommendations(user_id)
print(f"Recommendations for user {user_id}:")
for post in recommendations:
print(
f"- Post {post['id']}: Movie: {post.get('movie_name', '')} Caption: {post.get('content', '')}")
if __name__ == "__main__":
asyncio.run(main())
|