Revert "カスタムハンドラではなく、デフォルトでスパースに対応する"
Browse filesThis reverts commit 79e008e5730d9501b9a0c1fd955bfd6e8e5b0776.
- handler.py +61 -0
handler.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
# from optimum.onnxruntime import ORTModelForSequenceClassification
|
| 3 |
+
# from transformers import pipeline, AutoTokenizer
|
| 4 |
+
from FlagEmbedding import BGEM3FlagModel
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
class EndpointHandler():
|
| 8 |
+
def __init__(self, path="."):
|
| 9 |
+
# load the optimized model
|
| 10 |
+
# モデルの準備
|
| 11 |
+
self.model = BGEM3FlagModel(path, use_fp16=True)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
| 15 |
+
"""
|
| 16 |
+
Args:
|
| 17 |
+
data (:obj:):
|
| 18 |
+
includes the input data and the parameters for the inference.
|
| 19 |
+
Return:
|
| 20 |
+
A :obj:`list`:. The object returned should be a list of one list like [[{"label": 0.9939950108528137}]] containing :
|
| 21 |
+
- "label": A string representing what the label/class is. There can be multiple labels.
|
| 22 |
+
- "score": A score between 0 and 1 describing how confident the model is for this label/class.
|
| 23 |
+
"""
|
| 24 |
+
inputs = data.pop("inputs", data)
|
| 25 |
+
parameters = data.pop("parameters", None)
|
| 26 |
+
|
| 27 |
+
# encodeメソッドの実行前に時間を記録
|
| 28 |
+
start_time = time.time()
|
| 29 |
+
|
| 30 |
+
result = self.model.encode(inputs, return_dense=False, return_sparse=True, max_length=1024)
|
| 31 |
+
|
| 32 |
+
# encodeメソッドの実行後に時間を記録
|
| 33 |
+
end_time = time.time()
|
| 34 |
+
# print(result)
|
| 35 |
+
# dense_vectors = result["dense_vecs"]
|
| 36 |
+
|
| 37 |
+
# 経過時間を計算
|
| 38 |
+
elapsed_time = end_time - start_time
|
| 39 |
+
print(f"Encoding took {elapsed_time:.4f} seconds")
|
| 40 |
+
|
| 41 |
+
sparse_vectors = result["lexical_weights"]
|
| 42 |
+
# defaultdict(<class 'int'>, {'6': 0.09546, '192661': 0.3323})
|
| 43 |
+
|
| 44 |
+
# pass inputs with all kwargs in data
|
| 45 |
+
# if parameters is not None:
|
| 46 |
+
# prediction = self.pipeline(inputs, **parameters)
|
| 47 |
+
# else:
|
| 48 |
+
# prediction = self.pipeline(inputs)
|
| 49 |
+
# postprocess the prediction
|
| 50 |
+
|
| 51 |
+
# レスポンスをの型をkey=str, value=floatのdictにする。なお、numpy.float16はjsonに変換できないので、floatに変換する。
|
| 52 |
+
sparse_vectors = {str(k): float(v) for k, v in sparse_vectors.items()}
|
| 53 |
+
|
| 54 |
+
# レスポンスの型をnumpy.ndarrayから、通常のarrayに変更する
|
| 55 |
+
# dense_vectors = dense_vectors.tolist()
|
| 56 |
+
|
| 57 |
+
return [
|
| 58 |
+
[
|
| 59 |
+
{ "outputs": sparse_vectors}
|
| 60 |
+
]
|
| 61 |
+
]
|