Ahmed Mostafa commited on
Commit
7fcef06
Β·
1 Parent(s): 9e1c846

feat: implement recommendation API endpoints with personalized and general modes

Browse files
Files changed (1) hide show
  1. src/api/recommendation_routes.py +10 -17
src/api/recommendation_routes.py CHANGED
@@ -82,38 +82,31 @@ async def get_recommendations(
82
  limit: int = Query(default=5, ge=1, le=20),
83
  ) -> Dict[str, Any]:
84
 
85
- user_id = current_user.id # βœ… correct for your system
86
 
87
  if not user_id:
88
  raise HTTPException(status_code=401, detail="Invalid user")
89
 
90
  logger.info(f"🎯 Getting recommendations for user: {user_id}")
91
 
92
- interaction_count = 0
93
- is_personalized = False
94
-
95
  try:
96
- interaction_count = await recommendation_service.get_user_interaction_count(
97
- db=db,
98
- user_id=user_id,
99
- )
100
 
101
- is_personalized = interaction_count >= MIN_INTERACTIONS_FOR_PERSONALIZATION
102
 
103
  if is_personalized:
104
- logger.info(
105
- f"βœ… Personalized mode for {user_id} ({interaction_count} interactions)"
106
- )
 
107
  recommendations = await recommendation_service.get_recommendations_for_user(
108
  db=db,
109
  user_id=user_id,
110
  limit=limit,
111
  )
112
  else:
113
- logger.info(
114
- f"🌱 General mode for {user_id} "
115
- f"({interaction_count}/{MIN_INTERACTIONS_FOR_PERSONALIZATION})"
116
- )
117
  recommendations = GENERAL_VIDEOS[:limit]
118
 
119
  except Exception as e:
@@ -126,7 +119,7 @@ async def get_recommendations(
126
  return {
127
  "status": "success",
128
  "is_personalized": is_personalized,
129
- "interactions_count": interaction_count,
130
  "interactions_needed": MIN_INTERACTIONS_FOR_PERSONALIZATION,
131
  "count": len(recommendations),
132
  "recommendations": recommendations,
 
82
  limit: int = Query(default=5, ge=1, le=20),
83
  ) -> Dict[str, Any]:
84
 
85
+ user_id = current_user.id # βœ… your model uses "id"
86
 
87
  if not user_id:
88
  raise HTTPException(status_code=401, detail="Invalid user")
89
 
90
  logger.info(f"🎯 Getting recommendations for user: {user_id}")
91
 
 
 
 
92
  try:
93
+ # βœ… Use notesCount directly from user
94
+ notes_count = getattr(current_user, "notesCount", 0) or 0
 
 
95
 
96
+ is_personalized = notes_count >= MIN_INTERACTIONS_FOR_PERSONALIZATION
97
 
98
  if is_personalized:
99
+ logger.info(f"βœ… Personalized mode for {user_id} ({notes_count} notes)")
100
+
101
+ # ⚠️ for now you can still return general videos
102
+ # until you build real recommender
103
  recommendations = await recommendation_service.get_recommendations_for_user(
104
  db=db,
105
  user_id=user_id,
106
  limit=limit,
107
  )
108
  else:
109
+ logger.info(f"🌱 General mode for {user_id} ({notes_count}/{MIN_INTERACTIONS_FOR_PERSONALIZATION} notes)")
 
 
 
110
  recommendations = GENERAL_VIDEOS[:limit]
111
 
112
  except Exception as e:
 
119
  return {
120
  "status": "success",
121
  "is_personalized": is_personalized,
122
+ "interactions_count": notes_count, # βœ… mapped correctly
123
  "interactions_needed": MIN_INTERACTIONS_FOR_PERSONALIZATION,
124
  "count": len(recommendations),
125
  "recommendations": recommendations,