clfegg's picture
Create handler.py
2a4d3c4 verified
raw
history blame contribute delete
No virus
2.23 kB
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