Update app.py
Browse files
app.py
CHANGED
|
@@ -174,8 +174,30 @@ def recommend_books(user_id, top_n):
|
|
| 174 |
user_id = int(user_id)
|
| 175 |
|
| 176 |
if user_id not in ratings_df['user_id'].unique():
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
rated_books = set(ratings_df[ratings_df['user_id'] == user_id]['book_id'])
|
| 181 |
unseen_books = set(features_df['book_id']) - rated_books
|
|
|
|
| 174 |
user_id = int(user_id)
|
| 175 |
|
| 176 |
if user_id not in ratings_df['user_id'].unique():
|
| 177 |
+
|
| 178 |
+
# Cold start fallback → Top-N highest-rated books
|
| 179 |
+
top_books = (
|
| 180 |
+
ratings_df.groupby('book_id')['rating']
|
| 181 |
+
.mean()
|
| 182 |
+
.reset_index()
|
| 183 |
+
.sort_values(by='rating', ascending=False)
|
| 184 |
+
.head(top_n)
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
recommendations = []
|
| 188 |
+
for _, row in top_books.iterrows():
|
| 189 |
+
book_id = row['book_id']
|
| 190 |
+
avg_rating = row['rating']
|
| 191 |
+
book_info = features_df.loc[features_df['book_id'] == book_id]
|
| 192 |
+
if book_info.empty:
|
| 193 |
+
continue
|
| 194 |
+
title = book_info['title'].values[0]
|
| 195 |
+
image_url = book_info['image_url'].values[0] if 'image_url' in book_info else ""
|
| 196 |
+
recommendations.append(
|
| 197 |
+
(f"<img src='{image_url}' width='60'>", title, round(avg_rating, 3))
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
return pd.DataFrame(recommendations, columns=["Book Cover", "Book Title", "Predicted
|
| 201 |
|
| 202 |
rated_books = set(ratings_df[ratings_df['user_id'] == user_id]['book_id'])
|
| 203 |
unseen_books = set(features_df['book_id']) - rated_books
|