Dineflow commited on
Commit
a38f2ca
·
verified ·
1 Parent(s): d265657

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +13 -4
handler.py CHANGED
@@ -8,18 +8,29 @@ class EndpointHandler:
8
  self.vectorizer = joblib.load(os.path.join(model_dir, 'vectorizer.joblib'))
9
  self.model = joblib.load(os.path.join(model_dir, 'logistic_classifier.joblib'))
10
 
 
 
 
 
 
 
 
 
 
 
11
  # Verify that the tokenizer configuration is correct
12
  with open(os.path.join(model_dir, "tokenizer.json"), "r") as file:
13
  tokenizer_config = json.load(file)
14
  if tokenizer_config['tokenizer'] != 'split':
15
  raise ValueError("Tokenizer configuration does not match the expected tokenizer.")
16
 
 
 
17
  def predict_rating(self, review):
18
  review_tfidf = self.vectorizer.transform([review])
19
  predicted_rating = self.model.predict(review_tfidf)[0]
20
  return int(predicted_rating)
21
 
22
- # __call__ method should be part of the class
23
  def __call__(self, inputs):
24
  try:
25
  # Parse the input JSON string
@@ -41,16 +52,13 @@ class EndpointHandler:
41
  if not isinstance(review, str) or not review.strip():
42
  return json.dumps({"error": "Review must be a non-empty string."})
43
 
44
- # Perform prediction
45
  predicted_rating = self.predict_rating(review)
46
 
47
- # Prepare response
48
  response = {
49
  "review": review,
50
  "predicted_rating": predicted_rating
51
  }
52
 
53
- # Return JSON response
54
  return json.dumps(response)
55
 
56
  except json.JSONDecodeError:
@@ -58,3 +66,4 @@ class EndpointHandler:
58
 
59
  except Exception as e:
60
  return json.dumps({"error": str(e)})
 
 
8
  self.vectorizer = joblib.load(os.path.join(model_dir, 'vectorizer.joblib'))
9
  self.model = joblib.load(os.path.join(model_dir, 'logistic_classifier.joblib'))
10
 
11
+ # Check if the vectorizer is fitted
12
+ if not hasattr(self.vectorizer, 'vocabulary_'):
13
+ raise ValueError("The vectorizer is not fitted. Ensure the vectorizer is trained and saved correctly.")
14
+
15
+ # Check if the model is fitted
16
+ if not hasattr(self.model, 'classes_'):
17
+ raise ValueError("The model is not fitted. Ensure the model is trained and saved correctly.")
18
+
19
+ print("Vectorizer and model loaded successfully.")
20
+
21
  # Verify that the tokenizer configuration is correct
22
  with open(os.path.join(model_dir, "tokenizer.json"), "r") as file:
23
  tokenizer_config = json.load(file)
24
  if tokenizer_config['tokenizer'] != 'split':
25
  raise ValueError("Tokenizer configuration does not match the expected tokenizer.")
26
 
27
+ print("Tokenizer configuration verified.")
28
+
29
  def predict_rating(self, review):
30
  review_tfidf = self.vectorizer.transform([review])
31
  predicted_rating = self.model.predict(review_tfidf)[0]
32
  return int(predicted_rating)
33
 
 
34
  def __call__(self, inputs):
35
  try:
36
  # Parse the input JSON string
 
52
  if not isinstance(review, str) or not review.strip():
53
  return json.dumps({"error": "Review must be a non-empty string."})
54
 
 
55
  predicted_rating = self.predict_rating(review)
56
 
 
57
  response = {
58
  "review": review,
59
  "predicted_rating": predicted_rating
60
  }
61
 
 
62
  return json.dumps(response)
63
 
64
  except json.JSONDecodeError:
 
66
 
67
  except Exception as e:
68
  return json.dumps({"error": str(e)})
69
+