|
from typing import Dict, List, Any |
|
import pickle |
|
import numpy as np |
|
import pandas as pd |
|
import os |
|
|
|
class ContentBasedRecommender: |
|
def __init__(self, train_data): |
|
self.train_data = train_data |
|
|
|
def predict(self, user_id, k=10): |
|
user_books = set(self.train_data[self.train_data['user_id'] == user_id]['book_id']) |
|
similar_books = set().union(*(self.train_data[self.train_data['book_id'] == book_id]['similar_books'].iloc[0] for book_id in user_books)) |
|
recommended_books = list(similar_books - user_books) |
|
|
|
return np.random.choice(recommended_books, size=k, replace=False) if len(recommended_books) >= k else recommended_books |
|
|
|
def evaluate(self, test_data, k=10): |
|
user_ids = test_data['user_id'].unique() |
|
hit_rate, ndcg_scores = [], [] |
|
|
|
for user_id in user_ids[:100]: |
|
true_books = set(test_data[test_data['user_id'] == user_id]['book_id']) |
|
pred_books = set(self.predict(user_id, k)) |
|
|
|
hits = len(true_books & pred_books) |
|
hit_rate.append(hits / min(k, len(true_books))) |
|
|
|
dcg = sum(1 / math.log2(rank + 2) for rank, book in enumerate(pred_books) if book in true_books) |
|
idcg = sum(1 / math.log2(i + 2) for i in range(min(k, len(true_books)))) |
|
ndcg = dcg / idcg if idcg > 0 else 0 |
|
ndcg_scores.append(ndcg) |
|
|
|
return np.mean(hit_rate), np.mean(ndcg_scores) |
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
model_path = os.path.join(path, "model.pkl") |
|
with open(model_path, 'rb') as f: |
|
self.model = pickle.load(f) |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
""" |
|
data args: |
|
user_id (:obj: `str` or `int`) |
|
k (:obj: `int`, optional) |
|
Return: |
|
A :obj:`list` of :obj:`dict`: will be serialized and returned |
|
""" |
|
user_id = data.pop("user_id", None) |
|
k = data.pop("k", 10) |
|
|
|
if user_id is None: |
|
return [{"error": "user_id is required"}] |
|
|
|
try: |
|
recommended_books = self.model.predict(user_id, k=k) |
|
return [{"recommended_books": recommended_books.tolist()}] |
|
except Exception as e: |
|
return [{"error": str(e)}] |
|
|
|
def load_model(model_path): |
|
handler = EndpointHandler(model_path) |
|
return handler |