Instructions to use clfegg/collaborate_base_recommend with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use clfegg/collaborate_base_recommend with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import CollaborativeRecommender model = CollaborativeRecommender.from_pretrained("clfegg/collaborate_base_recommend", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from clfegg/collaborate_base_recommend: direct link, hf CLI and curl.
- Browser
- Download file 2.23 kB
-
https://huggingface.co/clfegg/collaborate_base_recommend/resolve/main/handler.py
- Command line
-
hf download hf://clfegg/collaborate_base_recommend/handler.py
-
curl -L -o handler.py https://huggingface.co/clfegg/collaborate_base_recommend/resolve/main/handler.py
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 |