Dineflow commited on
Commit
d2e96db
·
verified ·
1 Parent(s): ae2af9c

This is final!

Browse files
Files changed (1) hide show
  1. handler.py +20 -6
handler.py CHANGED
@@ -1,6 +1,7 @@
1
  import joblib
2
  import os
3
  import json
 
4
 
5
  class EndpointHandler:
6
  def __init__(self, model_dir):
@@ -15,23 +16,36 @@ class EndpointHandler:
15
 
16
  def __call__(self, inputs):
17
  try:
18
- # Parse the input string to a dictionary
19
- data = json.loads(inputs)
20
- review = data.get("inputs")
 
 
 
 
 
 
 
 
21
 
22
  if not review:
23
- return json.dumps({"error": "No review provided"})
24
 
25
  # Call the prediction method
26
  predicted_rating = self.predict_rating(review)
27
-
28
  # Prepare the response
29
  response = {
30
  "review": review,
31
  "predicted_rating": int(predicted_rating)
32
  }
33
  return json.dumps(response)
34
-
35
  except json.JSONDecodeError:
36
  return json.dumps({"error": "Invalid JSON format"})
37
 
 
 
 
 
 
 
1
  import joblib
2
  import os
3
  import json
4
+ import re
5
 
6
  class EndpointHandler:
7
  def __init__(self, model_dir):
 
16
 
17
  def __call__(self, inputs):
18
  try:
19
+ # Ensure inputs is a JSON string
20
+ if not isinstance(inputs, str):
21
+ return json.dumps({"error": "Input must be a JSON string"})
22
+
23
+ # Using regex to extract the value of "inputs" from the JSON string
24
+ match = re.search(r'\"inputs\"\s*:\s*\"(.*?)\"', inputs)
25
+
26
+ if not match:
27
+ return json.dumps({"error": "No valid review provided or incorrect format"})
28
+
29
+ review = match.group(1)
30
 
31
  if not review:
32
+ return json.dumps({"error": "No valid review provided"})
33
 
34
  # Call the prediction method
35
  predicted_rating = self.predict_rating(review)
36
+
37
  # Prepare the response
38
  response = {
39
  "review": review,
40
  "predicted_rating": int(predicted_rating)
41
  }
42
  return json.dumps(response)
43
+
44
  except json.JSONDecodeError:
45
  return json.dumps({"error": "Invalid JSON format"})
46
 
47
+ except Exception as e:
48
+ # General exception handling
49
+ return json.dumps({"error": str(e)})
50
+
51
+