Update handler.py
Browse files- 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 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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 |
+
|
|
|
|
|
|
|
|
|