clfegg commited on
Commit
4649329
1 Parent(s): 1e87c85

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +12 -5
handler.py CHANGED
@@ -1,9 +1,9 @@
1
-
2
  from typing import Dict, List, Any
3
  import pickle
4
  import os
5
  import __main__
6
-
 
7
 
8
  class ContentBasedRecommender:
9
  def __init__(self, train_data):
@@ -16,8 +16,8 @@ class ContentBasedRecommender:
16
 
17
  return np.random.choice(recommended_books, size=min(k, len(recommended_books)), replace=False).tolist()
18
 
19
-
20
  __main__.ContentBasedRecommender = ContentBasedRecommender
 
21
  class EndpointHandler:
22
  def __init__(self, path=""):
23
  model_path = os.path.join(path, "model.pkl")
@@ -25,8 +25,15 @@ class EndpointHandler:
25
  self.model = pickle.load(f)
26
 
27
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
28
- user_id = data.pop("user_id", None)
29
- k = data.pop("k", 10) # Default to 10 if not provided
 
 
 
 
 
 
 
30
 
31
  if user_id is None:
32
  return [{"error": "user_id is required"}]
 
 
1
  from typing import Dict, List, Any
2
  import pickle
3
  import os
4
  import __main__
5
+ import numpy as np
6
+ import pandas as pd
7
 
8
  class ContentBasedRecommender:
9
  def __init__(self, train_data):
 
16
 
17
  return np.random.choice(recommended_books, size=min(k, len(recommended_books)), replace=False).tolist()
18
 
 
19
  __main__.ContentBasedRecommender = ContentBasedRecommender
20
+
21
  class EndpointHandler:
22
  def __init__(self, path=""):
23
  model_path = os.path.join(path, "model.pkl")
 
25
  self.model = pickle.load(f)
26
 
27
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
28
+ # Extract the 'inputs' from the data
29
+ inputs = data.get('inputs', {})
30
+
31
+ # If inputs is a string (for single user_id input), convert it to a dict
32
+ if isinstance(inputs, str):
33
+ inputs = {'user_id': inputs}
34
+
35
+ user_id = inputs.get('user_id')
36
+ k = inputs.get('k', 10) # Default to 10 if not provided
37
 
38
  if user_id is None:
39
  return [{"error": "user_id is required"}]