Spaces:
Running
Running
Ahmed Mostafa commited on
Commit Β·
7fcef06
1
Parent(s): 9e1c846
feat: implement recommendation API endpoints with personalized and general modes
Browse files- 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 # β
|
| 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 |
-
|
| 97 |
-
|
| 98 |
-
user_id=user_id,
|
| 99 |
-
)
|
| 100 |
|
| 101 |
-
is_personalized =
|
| 102 |
|
| 103 |
if is_personalized:
|
| 104 |
-
logger.info(
|
| 105 |
-
|
| 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":
|
| 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,
|