File size: 1,025 Bytes
e74577e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, List, Any
from FlagEmbedding import BGEM3FlagModel

class EndpointHandler():
    def __init__(self, path=""):
        self.model = BGEM3FlagModel('BAAI/bge-m3',  use_fp16=True)

    def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
        """
       data args:
            inputs (:`list`: `str`)
            kwargs
      Return:
            A :obj:`list` | `dict`: will be serialized and returned
        """

        results = []
        inputs = data.pop("inputs",data)
        for i in inputs:
            output = self.model.encode(i, return_dense=False, return_sparse=True, return_colbert_vecs=False)
            results.append(self._toJsonSerialisableFormat(self.model.convert_id_to_token(output['lexical_weights'])))
        
        return results

    def _toJsonSerialisableFormat(self, model_output):
        # convert the numpy float16 to a Python float
        # so it can be serialised as JSON
        return {key:float(value) for (key,value) in model_output.items()}