File size: 1,030 Bytes
31d0d85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, List, Any
import pickle
import numpy as np
import pandas as pd
class EndpointHandler:
    def __init__(self, path=""):
        # Load the serialized model
        with open(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

        """
        # Get inputs
        user_id = data.pop("user_id", None)
        k = data.pop("k", 10)  # Default to 10 if not provided

        if user_id is None:
            return [{"error": "user_id is required"}]

        # Run prediction
        try:
            recommended_books = self.model.predict(user_id, k=k)
            return [{"recommended_books": recommended_books.tolist()}]
        except Exception as e:
            return [{"error": str(e)}]