takanezawa commited on
Commit
4af82f4
·
1 Parent(s): a03b9b6

スパースベクトル化に対応する

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. handler.py +20 -12
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ test*
handler.py CHANGED
@@ -1,15 +1,14 @@
1
  from typing import Dict, List, Any
2
- from optimum.onnxruntime import ORTModelForSequenceClassification
3
- from transformers import pipeline, AutoTokenizer
 
4
 
5
 
6
  class EndpointHandler():
7
- def __init__(self, path="./onnx"):
8
  # load the optimized model
9
- model = ORTModelForSequenceClassification.from_pretrained(path)
10
- tokenizer = AutoTokenizer.from_pretrained(path)
11
- # create inference pipeline
12
- self.pipeline = pipeline("text-classification", model=model, tokenizer=tokenizer)
13
 
14
 
15
  def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
@@ -24,11 +23,20 @@ class EndpointHandler():
24
  """
25
  inputs = data.pop("inputs", data)
26
  parameters = data.pop("parameters", None)
 
 
 
 
 
 
 
 
 
27
 
28
  # pass inputs with all kwargs in data
29
- if parameters is not None:
30
- prediction = self.pipeline(inputs, **parameters)
31
- else:
32
- prediction = self.pipeline(inputs)
33
  # postprocess the prediction
34
- return prediction
 
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
 
6
 
7
  class EndpointHandler():
8
+ def __init__(self, path="./"):
9
  # load the optimized model
10
+ # モデルの準備
11
+ model = BGEM3FlagModel("./", use_fp16=False)
 
 
12
 
13
 
14
  def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
 
23
  """
24
  inputs = data.pop("inputs", data)
25
  parameters = data.pop("parameters", None)
26
+
27
+ sparse_embs = []
28
+
29
+ result = self.model.encode(inputs, return_dense=False, return_sparse=True)
30
+ sparse_vectors = result["lexical_weights"]
31
+ for sparse_vector in sparse_vectors:
32
+ sparse_values = [value for value in sparse_vector.values()]
33
+ sparse_dimensions = [int(key) for key in sparse_vector.keys()]
34
+ sparse_embs.append((sparse_values, sparse_dimensions))
35
 
36
  # pass inputs with all kwargs in data
37
+ # if parameters is not None:
38
+ # prediction = self.pipeline(inputs, **parameters)
39
+ # else:
40
+ # prediction = self.pipeline(inputs)
41
  # postprocess the prediction
42
+ return sparse_vectors