Update handler.py
Browse files- handler.py +21 -19
handler.py
CHANGED
@@ -13,27 +13,29 @@ class EndpointHandler():
|
|
13 |
Return:
|
14 |
A :obj:`list` | `dict`: will be serialized and returned
|
15 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
inputs = data.pop("inputs",data)
|
17 |
prediction = self.pipeline(inputs)
|
18 |
normalized_prediction = normalize_vectors(prediction)
|
19 |
|
20 |
-
return normalized_prediction
|
21 |
-
|
22 |
-
def normalize_vectors(vectors):
|
23 |
-
"""
|
24 |
-
Normalize a single vector or a list of vectors.
|
25 |
-
|
26 |
-
Parameters:
|
27 |
-
vectors (Union[np.ndarray, List[np.ndarray]]): Input vector or list of vectors.
|
28 |
|
29 |
-
Returns:
|
30 |
-
Union[np.ndarray, List[np.ndarray]]: Normalized vector or list of normalized vectors.
|
31 |
-
"""
|
32 |
-
if isinstance(vectors, np.ndarray):
|
33 |
-
# If it's a single vector, normalize it
|
34 |
-
return vectors / np.linalg.norm(vectors)
|
35 |
-
elif isinstance(vectors, list):
|
36 |
-
# If it's a list of vectors, normalize each vector in the list
|
37 |
-
return [vector / np.linalg.norm(vector) for vector in vectors]
|
38 |
-
else:
|
39 |
-
raise ValueError("Input must be a numpy array or a list of numpy arrays.")
|
|
|
13 |
Return:
|
14 |
A :obj:`list` | `dict`: will be serialized and returned
|
15 |
"""
|
16 |
+
|
17 |
+
def normalize_vectors(vectors):
|
18 |
+
"""
|
19 |
+
Normalize a single vector or a list of vectors.
|
20 |
+
|
21 |
+
Parameters:
|
22 |
+
vectors (Union[np.ndarray, List[np.ndarray]]): Input vector or list of vectors.
|
23 |
+
|
24 |
+
Returns:
|
25 |
+
Union[np.ndarray, List[np.ndarray]]: Normalized vector or list of normalized vectors.
|
26 |
+
"""
|
27 |
+
if isinstance(vectors, np.ndarray):
|
28 |
+
# If it's a single vector, normalize it
|
29 |
+
return vectors / np.linalg.norm(vectors)
|
30 |
+
elif isinstance(vectors, list):
|
31 |
+
# If it's a list of vectors, normalize each vector in the list
|
32 |
+
return [vector / np.linalg.norm(vector) for vector in vectors]
|
33 |
+
else:
|
34 |
+
raise ValueError("Input must be a numpy array or a list of numpy arrays.")
|
35 |
+
|
36 |
inputs = data.pop("inputs",data)
|
37 |
prediction = self.pipeline(inputs)
|
38 |
normalized_prediction = normalize_vectors(prediction)
|
39 |
|
40 |
+
return {"embeddings": normalized_prediction}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|