Commit
·
473c7e6
1
Parent(s):
ae99f99
予測時間を計測できるように
Browse files- __pycache__/handler.cpython-311.pyc +0 -0
- handler.py +16 -7
__pycache__/handler.cpython-311.pyc
CHANGED
Binary files a/__pycache__/handler.cpython-311.pyc and b/__pycache__/handler.cpython-311.pyc differ
|
|
handler.py
CHANGED
@@ -2,7 +2,7 @@ from typing import Dict, List, Any
|
|
2 |
# from optimum.onnxruntime import ORTModelForSequenceClassification
|
3 |
# from transformers import pipeline, AutoTokenizer
|
4 |
from FlagEmbedding import BGEM3FlagModel
|
5 |
-
|
6 |
|
7 |
class EndpointHandler():
|
8 |
def __init__(self, path="."):
|
@@ -24,12 +24,21 @@ class EndpointHandler():
|
|
24 |
inputs = data.pop("inputs", data)
|
25 |
parameters = data.pop("parameters", None)
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
# print(result)
|
29 |
-
dense_vectors = result["dense_vecs"]
|
30 |
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
# defaultdict(<class 'int'>, {'6': 0.09546, '192661': 0.3323})
|
34 |
|
35 |
# pass inputs with all kwargs in data
|
@@ -40,13 +49,13 @@ class EndpointHandler():
|
|
40 |
# postprocess the prediction
|
41 |
|
42 |
# レスポンスをの型をkey=str, value=floatのdictにする。なお、numpy.float16はjsonに変換できないので、floatに変換する。
|
43 |
-
|
44 |
|
45 |
# レスポンスの型をnumpy.ndarrayから、通常のarrayに変更する
|
46 |
-
dense_vectors = dense_vectors.tolist()
|
47 |
|
48 |
return [
|
49 |
[
|
50 |
-
{ "outputs":
|
51 |
]
|
52 |
]
|
|
|
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="."):
|
|
|
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)
|
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
|
|
|
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 |
]
|