Update handler.py
Browse files- handler.py +21 -0
handler.py
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def __call__(self, inputs):
|
| 2 |
try:
|
| 3 |
inputs_dict = json.loads(inputs)
|
|
@@ -29,3 +49,4 @@ def __call__(self, inputs):
|
|
| 29 |
|
| 30 |
except Exception as e:
|
| 31 |
return json.dumps({"error": str(e)})
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
class EndpointHandler:
|
| 6 |
+
def __init__(self, model_dir):
|
| 7 |
+
self.model_dir = model_dir
|
| 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 |
def __call__(self, inputs):
|
| 22 |
try:
|
| 23 |
inputs_dict = json.loads(inputs)
|
|
|
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
return json.dumps({"error": str(e)})
|
| 52 |
+
|