| from typing import Dict, List, Any | |
| from sentence_transformers import SentenceTransformer | |
| import pandas as pd | |
| import numpy as np | |
| import os | |
| class EndpointHandler: | |
| def __init__(self, path=""): | |
| self.model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") | |
| self.embeddings = np.load(os.path.join(path, "embeddings.npy")) | |
| self.spotify = pd.read_csv(os.path.join(path, "spotify.csv")) | |
| def __call__(self, data: Dict[str, Any]) -> List[float]: | |
| """ | |
| data args: | |
| inputs (:obj: `str` | `PIL.Image` | `np.array`) | |
| kwargs | |
| Return: | |
| A :obj:`list` | `dict`: will be serialized and returned | |
| """ | |
| input_embedding = self.model.encode(data["inputs"]) | |
| cos_score = self.embeddings @ input_embedding | |
| top_10 = cos_score.argsort()[-10:][::-1] | |
| return self.spotify.iloc[top_10].to_dict(orient="records") | |