File size: 1,524 Bytes
c0c068a ec39970 c0c068a ec39970 c0c068a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
from transformers import pipeline
import numpy as np
class EndpointHandler():
def __init__(self, path=""):
self.pipeline = pipeline("sentence-embeddings",model=path)
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
data args:
inputs (:obj: `str`)
date (:obj: `str`)
Return:
A :obj:`list` | `dict`: will be serialized and returned
"""
def normalize_vectors(vectors):
"""
Normalize a single vector or a list of vectors.
Parameters:
vectors (Union[np.ndarray, List[np.ndarray]]): Input vector or list of vectors.
Returns:
Union[np.ndarray, List[np.ndarray]]: Normalized vector or list of normalized vectors.
"""
if isinstance(vectors, np.ndarray):
# If it's a single vector, normalize it
return vectors / np.linalg.norm(vectors)
elif isinstance(vectors, list):
# If it's a list of vectors, normalize each vector in the list
return [vector / np.linalg.norm(vector) for vector in vectors]
else:
raise ValueError("Input must be a numpy array or a list of numpy arrays.")
inputs = data.pop("inputs",data)
prediction = self.pipeline(inputs)
normalized_prediction = normalize_vectors(prediction)
return {"embeddings": normalized_prediction}
|