Dineflow commited on
Commit
8dafdc5
·
verified ·
1 Parent(s): 77fdc05

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +18 -24
handler.py CHANGED
@@ -1,5 +1,6 @@
1
  import joblib
2
  import os
 
3
 
4
  class EndpointHandler:
5
  def __init__(self, model_dir):
@@ -12,29 +13,22 @@ class EndpointHandler:
12
  predicted_rating = self.model.predict(review_tfidf)[0]
13
  return predicted_rating
14
 
15
- def map_rating_to_description(self, rating):
16
- if rating == 1:
17
- return "Poor"
18
- elif rating == 2:
19
- return "Unsatisfactory"
20
- elif rating == 3:
21
- return "Average"
22
- elif rating == 4:
23
- return "Satisfactory"
24
- elif rating == 5:
25
- return "Outstanding"
26
- else:
27
- return "Invalid rating"
28
-
29
  def __call__(self, inputs):
30
- review = inputs.get("review")
31
-
32
- if not review:
33
- return {"error": "No review provided"}
 
 
 
 
 
 
 
 
 
 
34
 
35
- predicted_rating = self.predict_rating(review)
36
- predicted_description = self.map_rating_to_description(predicted_rating)
37
- return {
38
- "review": review,
39
- "predicted_rating": int(predicted_rating)
40
- }
 
1
  import joblib
2
  import os
3
+ import json
4
 
5
  class EndpointHandler:
6
  def __init__(self, model_dir):
 
13
  predicted_rating = self.model.predict(review_tfidf)[0]
14
  return predicted_rating
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def __call__(self, inputs):
17
+ try:
18
+ data = json.loads(inputs)
19
+ review = data.get("review")
20
+
21
+ if not review:
22
+ return json.dumps({"error": "No review provided"})
23
+
24
+ predicted_rating = self.predict_rating(review)
25
+
26
+ response = {
27
+ "review": review,
28
+ "predicted_rating": int(predicted_rating)
29
+ }
30
+ return json.dumps(response)
31
 
32
+ except json.JSONDecodeError:
33
+ return json.dumps({"error": "Invalid JSON format"})
34
+