File size: 1,329 Bytes
f1941ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# copy from https://github.com/huggingface/api-inference-community/blob/main/docker_images/sentence_transformers/app/pipelines/sentence_similarity.py

import os
from typing import Dict, List, Union

from sentence_transformers import SentenceTransformer, util


class PreTrainedPipeline:
    def __init__(
        self,
        model_id: str,
    ):
        self.model = SentenceTransformer(
            model_id, use_auth_token=os.getenv("HF_API_TOKEN")
        )

    def __call__(self, inputs: Dict[str, Union[str, List[str]]]) -> List[float]:
        """
        Args:
            inputs (:obj:`dict`):
                a dictionary containing two keys, 'source_sentence' mapping
                to the sentence that will be compared against all the others,
                and 'sentences', mapping to a list of strings to which the
                source will be compared.
        Return:
            A :obj:`list` of floats: Cosine similarity between `source_sentence` and each sentence from `sentences`.
        """
        embeddings1 = self.model.encode(
            inputs["source_sentence"], convert_to_tensor=True
        )
        embeddings2 = self.model.encode(inputs["sentences"], convert_to_tensor=True)
        similarities = util.pytorch_cos_sim(embeddings1, embeddings2).tolist()[0]
        return similarities