File size: 2,228 Bytes
2a4d3c4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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:
# Convert raw user_id to inner user_id
inner_user_id = self.trainset.to_inner_uid(user_id)
except ValueError:
# User not found in trainset, return None
return None
# Get the list of books the user has interacted with
user_books = set(self.trainset.ur[inner_user_id])
all_books = set(self.trainset.all_items())
unseen_books = all_books - user_books
# Predict the ratings for unseen 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]
# Sort the predictions by estimated rating and return the top-k 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]]:
# Extract the 'inputs' from the data
inputs = data.get('inputs', {})
# If inputs is a string (for single user_id input), convert it to a dict
if isinstance(inputs, str):
inputs = {'user_id': inputs}
user_id = inputs.get('user_id')
k = inputs.get('k', 10) # Default to 10 if not provided
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 |