|
from typing import Dict, List, Any |
|
import pickle |
|
import os |
|
import __main__ |
|
import numpy as np |
|
|
|
class CollaborativeRecommender: |
|
def __init__(self, algo, trainset): |
|
self.algo = algo |
|
self.trainset = trainset |
|
|
|
def predict(self, user_id, k=10): |
|
try: |
|
|
|
inner_user_id = self.trainset.to_inner_uid(user_id) |
|
except ValueError: |
|
|
|
return None |
|
|
|
|
|
user_books = set(self.trainset.ur[inner_user_id]) |
|
all_books = set(self.trainset.all_items()) |
|
unseen_books = all_books - user_books |
|
|
|
predictions = [self.algo.predict(self.trainset.to_raw_uid(inner_user_id), self.trainset.to_raw_iid(book_id)) for book_id in unseen_books] |
|
|
|
top_predictions = sorted(predictions, key=lambda x: x.est, reverse=True)[:k] |
|
top_books = [pred.iid for pred in top_predictions] |
|
return top_books |
|
|
|
__main__.CollaborativeRecommender = CollaborativeRecommender |
|
|
|
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]]: |
|
|
|
inputs = data.get('inputs', {}) |
|
|
|
|
|
if isinstance(inputs, str): |
|
inputs = {'user_id': inputs} |
|
|
|
user_id = inputs.get('user_id') |
|
k = inputs.get('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}] |
|
except Exception as e: |
|
return [{"error": str(e)}] |
|
|
|
def load_model(model_path): |
|
handler = EndpointHandler(model_path) |
|
return handler |