Dineflow commited on
Commit
d08e49e
·
verified ·
1 Parent(s): 833f879

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +27 -21
handler.py CHANGED
@@ -18,35 +18,41 @@ class EndpointHandler:
18
  review_tfidf = self.vectorizer.transform([review])
19
  predicted_rating = self.model.predict(review_tfidf)[0]
20
  return int(predicted_rating)
21
- def __call__(self, inputs):
22
- try:
23
- inputs_dict = json.loads(inputs)
24
 
25
- if 'inputs' not in inputs_dict:
26
- return json.dumps({"error": "No valid inputs provided"})
 
 
 
27
 
28
- inputs_data = inputs_dict['inputs']
 
 
29
 
30
- if 'review' not in inputs_data:
31
- return json.dumps({"error": "No valid review provided"})
32
 
33
- review = inputs_data['review']
 
 
34
 
35
- if not review:
36
- return json.dumps({"error": "No valid review provided"})
37
 
38
- predicted_rating = self.predict_rating(review)
 
 
39
 
40
- response = {
41
- "review": review,
42
- "predicted_rating": predicted_rating
43
- }
44
 
45
- return json.dumps(response)
 
 
 
46
 
47
- except json.JSONDecodeError:
48
- return json.dumps({"error": "Invalid JSON format"})
49
 
50
- except Exception as e:
51
- return json.dumps({"error": str(e)})
 
 
 
52
 
 
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
26
+ inputs_dict = json.loads(inputs)
27
 
28
+ # Check if 'inputs' key exists
29
+ if 'inputs' not in inputs_dict:
30
+ return json.dumps({"error": "No 'inputs' key provided in the JSON input."})
31
 
32
+ inputs_data = inputs_dict['inputs']
 
33
 
34
+ # Check if 'review' key exists
35
+ if 'review' not in inputs_data:
36
+ return json.dumps({"error": "No 'review' key provided in the 'inputs' object."})
37
 
38
+ review = inputs_data['review']
 
39
 
40
+ # Validate that the review is a non-empty string
41
+ if not isinstance(review, str) or not review.strip():
42
+ return json.dumps({"error": "Review must be a non-empty string."})
43
 
44
+ predicted_rating = self.predict_rating(review)
 
 
 
45
 
46
+ response = {
47
+ "review": review,
48
+ "predicted_rating": predicted_rating
49
+ }
50
 
51
+ return json.dumps(response)
 
52
 
53
+ except json.JSONDecodeError:
54
+ return json.dumps({"error": "Invalid JSON format in input."})
55
+
56
+ except Exception as e:
57
+ return json.dumps({"error": str(e)})
58