Sentence Similarity
sentence-transformers
PyTorch
ONNX
xlm-roberta
feature-extraction
text-embeddings-inference
Instructions to use p0x0q-dev/bge-m3-sparse-experimental with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use p0x0q-dev/bge-m3-sparse-experimental with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("p0x0q-dev/bge-m3-sparse-experimental") sentences = [ "That is a happy person", "That is a happy dog", "That is a very happy person", "Today is a sunny day" ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [4, 4] - Notebooks
- Google Colab
- Kaggle
Commit ·
4af82f4
1
Parent(s): a03b9b6
スパースベクトル化に対応する
Browse files- .gitignore +1 -0
- 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="./
|
| 8 |
# load the optimized model
|
| 9 |
-
|
| 10 |
-
|
| 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 |
-
|
| 31 |
-
else:
|
| 32 |
-
|
| 33 |
# postprocess the prediction
|
| 34 |
-
return
|
|
|
|
| 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
|