初始化项目,由ModelHub XC社区提供模型
Model: Blaxzter/LaBSE-sentence-embeddings Source: Original Platform
This commit is contained in:
44
.gitattributes
vendored
Normal file
44
.gitattributes
vendored
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bin filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.h5 filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tflite filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tar.gz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ot filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.onnx filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
||||||
|
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
||||||
|
model.safetensors filter=lfs diff=lfs merge=lfs -text
|
||||||
|
|
||||||
|
*.7z filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.arrow filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bin filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ftz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.gz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.joblib filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.model filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.npy filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.npz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.onnx filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.parquet filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pb filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pickle filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pkl filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pt filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pth filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.rar filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
||||||
|
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tflite filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tgz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.wasm filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.xz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.zip filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.zst filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
||||||
226
README.md
Normal file
226
README.md
Normal file
@@ -0,0 +1,226 @@
|
|||||||
|
---
|
||||||
|
language:
|
||||||
|
- af
|
||||||
|
- am
|
||||||
|
- ar
|
||||||
|
- as
|
||||||
|
- az
|
||||||
|
- be
|
||||||
|
- bg
|
||||||
|
- bn
|
||||||
|
- bo
|
||||||
|
- bs
|
||||||
|
- ca
|
||||||
|
- ceb
|
||||||
|
- co
|
||||||
|
- cs
|
||||||
|
- cy
|
||||||
|
- da
|
||||||
|
- de
|
||||||
|
- el
|
||||||
|
- en
|
||||||
|
- eo
|
||||||
|
- es
|
||||||
|
- et
|
||||||
|
- eu
|
||||||
|
- fa
|
||||||
|
- fi
|
||||||
|
- fr
|
||||||
|
- fy
|
||||||
|
- ga
|
||||||
|
- gd
|
||||||
|
- gl
|
||||||
|
- gu
|
||||||
|
- ha
|
||||||
|
- haw
|
||||||
|
- he
|
||||||
|
- hi
|
||||||
|
- hmn
|
||||||
|
- hr
|
||||||
|
- ht
|
||||||
|
- hu
|
||||||
|
- hy
|
||||||
|
- id
|
||||||
|
- ig
|
||||||
|
- is
|
||||||
|
- it
|
||||||
|
- ja
|
||||||
|
- jv
|
||||||
|
- ka
|
||||||
|
- kk
|
||||||
|
- km
|
||||||
|
- kn
|
||||||
|
- ko
|
||||||
|
- ku
|
||||||
|
- ky
|
||||||
|
- la
|
||||||
|
- lb
|
||||||
|
- lo
|
||||||
|
- lt
|
||||||
|
- lv
|
||||||
|
- mg
|
||||||
|
- mi
|
||||||
|
- mk
|
||||||
|
- ml
|
||||||
|
- mn
|
||||||
|
- mr
|
||||||
|
- ms
|
||||||
|
- mt
|
||||||
|
- my
|
||||||
|
- ne
|
||||||
|
- nl
|
||||||
|
- no
|
||||||
|
- ny
|
||||||
|
- or
|
||||||
|
- pa
|
||||||
|
- pl
|
||||||
|
- pt
|
||||||
|
- ro
|
||||||
|
- ru
|
||||||
|
- rw
|
||||||
|
- si
|
||||||
|
- sk
|
||||||
|
- sl
|
||||||
|
- sm
|
||||||
|
- sn
|
||||||
|
- so
|
||||||
|
- sq
|
||||||
|
- sr
|
||||||
|
- st
|
||||||
|
- su
|
||||||
|
- sv
|
||||||
|
- sw
|
||||||
|
- ta
|
||||||
|
- te
|
||||||
|
- tg
|
||||||
|
- th
|
||||||
|
- tk
|
||||||
|
- tl
|
||||||
|
- tr
|
||||||
|
- tt
|
||||||
|
- ug
|
||||||
|
- uk
|
||||||
|
- ur
|
||||||
|
- uz
|
||||||
|
- vi
|
||||||
|
- wo
|
||||||
|
- xh
|
||||||
|
- yi
|
||||||
|
- yo
|
||||||
|
- zh
|
||||||
|
- zu
|
||||||
|
tags:
|
||||||
|
- bert
|
||||||
|
- sentence_embedding
|
||||||
|
- multilingual
|
||||||
|
- google
|
||||||
|
- sentence-similarity
|
||||||
|
license: apache-2.0
|
||||||
|
datasets:
|
||||||
|
- CommonCrawl
|
||||||
|
- Wikipedia
|
||||||
|
---
|
||||||
|
|
||||||
|
Copy of setu4993/LaBSE that returns the sentence embeddings (pooler_output) and implements caching
|
||||||
|
|
||||||
|
Original Model Card:
|
||||||
|
|
||||||
|
# LaBSE
|
||||||
|
|
||||||
|
## Model description
|
||||||
|
|
||||||
|
Language-agnostic BERT Sentence Encoder (LaBSE) is a BERT-based model trained for sentence embedding for 109 languages. The pre-training process combines masked language modeling with translation language modeling. The model is useful for getting multilingual sentence embeddings and for bi-text retrieval.
|
||||||
|
|
||||||
|
- Model: [HuggingFace's model hub](https://huggingface.co/setu4993/LaBSE).
|
||||||
|
- Paper: [arXiv](https://arxiv.org/abs/2007.01852).
|
||||||
|
- Original model: [TensorFlow Hub](https://tfhub.dev/google/LaBSE/2).
|
||||||
|
- Blog post: [Google AI Blog](https://ai.googleblog.com/2020/08/language-agnostic-bert-sentence.html).
|
||||||
|
- Conversion from TensorFlow to PyTorch: [GitHub](https://github.com/setu4993/convert-labse-tf-pt).
|
||||||
|
|
||||||
|
This is migrated from the v2 model on the TF Hub, which uses dict-based input. The embeddings produced by both the versions of the model are [equivalent](https://github.com/setu4993/convert-labse-tf-pt/blob/ec3a019159a54ed6493181a64486c2808c01f216/tests/test_conversion.py#L31).
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Using the model:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import torch
|
||||||
|
from transformers import BertModel, BertTokenizerFast
|
||||||
|
|
||||||
|
|
||||||
|
tokenizer = BertTokenizerFast.from_pretrained("setu4993/LaBSE")
|
||||||
|
model = BertModel.from_pretrained("setu4993/LaBSE")
|
||||||
|
model = model.eval()
|
||||||
|
|
||||||
|
english_sentences = [
|
||||||
|
"dog",
|
||||||
|
"Puppies are nice.",
|
||||||
|
"I enjoy taking long walks along the beach with my dog.",
|
||||||
|
]
|
||||||
|
english_inputs = tokenizer(english_sentences, return_tensors="pt", padding=True)
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
english_outputs = model(**english_inputs)
|
||||||
|
```
|
||||||
|
|
||||||
|
To get the sentence embeddings, use the pooler output:
|
||||||
|
|
||||||
|
```python
|
||||||
|
english_embeddings = english_outputs.pooler_output
|
||||||
|
```
|
||||||
|
|
||||||
|
Output for other languages:
|
||||||
|
|
||||||
|
```python
|
||||||
|
italian_sentences = [
|
||||||
|
"cane",
|
||||||
|
"I cuccioli sono carini.",
|
||||||
|
"Mi piace fare lunghe passeggiate lungo la spiaggia con il mio cane.",
|
||||||
|
]
|
||||||
|
japanese_sentences = ["犬", "子犬はいいです", "私は犬と一緒にビーチを散歩するのが好きです"]
|
||||||
|
italian_inputs = tokenizer(italian_sentences, return_tensors="pt", padding=True)
|
||||||
|
japanese_inputs = tokenizer(japanese_sentences, return_tensors="pt", padding=True)
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
italian_outputs = model(**italian_inputs)
|
||||||
|
japanese_outputs = model(**japanese_inputs)
|
||||||
|
|
||||||
|
italian_embeddings = italian_outputs.pooler_output
|
||||||
|
japanese_embeddings = japanese_outputs.pooler_output
|
||||||
|
```
|
||||||
|
|
||||||
|
For similarity between sentences, an L2-norm is recommended before calculating the similarity:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import torch.nn.functional as F
|
||||||
|
|
||||||
|
|
||||||
|
def similarity(embeddings_1, embeddings_2):
|
||||||
|
normalized_embeddings_1 = F.normalize(embeddings_1, p=2)
|
||||||
|
normalized_embeddings_2 = F.normalize(embeddings_2, p=2)
|
||||||
|
return torch.matmul(
|
||||||
|
normalized_embeddings_1, normalized_embeddings_2.transpose(0, 1)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
print(similarity(english_embeddings, italian_embeddings))
|
||||||
|
print(similarity(english_embeddings, japanese_embeddings))
|
||||||
|
print(similarity(italian_embeddings, japanese_embeddings))
|
||||||
|
```
|
||||||
|
|
||||||
|
## Details
|
||||||
|
|
||||||
|
Details about data, training, evaluation and performance metrics are available in the [original paper](https://arxiv.org/abs/2007.01852).
|
||||||
|
|
||||||
|
### BibTeX entry and citation info
|
||||||
|
|
||||||
|
```bibtex
|
||||||
|
@misc{feng2020languageagnostic,
|
||||||
|
title={Language-agnostic BERT Sentence Embedding},
|
||||||
|
author={Fangxiaoyu Feng and Yinfei Yang and Daniel Cer and Naveen Arivazhagan and Wei Wang},
|
||||||
|
year={2020},
|
||||||
|
eprint={2007.01852},
|
||||||
|
archivePrefix={arXiv},
|
||||||
|
primaryClass={cs.CL}
|
||||||
|
}
|
||||||
|
```
|
||||||
23
config.json
Normal file
23
config.json
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"architectures": ["BertModel"],
|
||||||
|
"attention_probs_dropout_prob": 0.1,
|
||||||
|
"classifier_dropout": null,
|
||||||
|
"gradient_checkpointing": false,
|
||||||
|
"hidden_act": "gelu",
|
||||||
|
"hidden_dropout_prob": 0.1,
|
||||||
|
"hidden_size": 768,
|
||||||
|
"initializer_range": 0.02,
|
||||||
|
"intermediate_size": 3072,
|
||||||
|
"layer_norm_eps": 1e-12,
|
||||||
|
"max_position_embeddings": 512,
|
||||||
|
"model_type": "bert",
|
||||||
|
"num_attention_heads": 12,
|
||||||
|
"num_hidden_layers": 12,
|
||||||
|
"pad_token_id": 0,
|
||||||
|
"position_embedding_type": "absolute",
|
||||||
|
"torch_dtype": "float32",
|
||||||
|
"transformers_version": "4.25.1",
|
||||||
|
"type_vocab_size": 2,
|
||||||
|
"use_cache": true,
|
||||||
|
"vocab_size": 501153
|
||||||
|
}
|
||||||
3
flax_model.msgpack
Normal file
3
flax_model.msgpack
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:4cbe50771a6b147d2da0beb6da1d80908a706cec2e2e06a09873649ed183e884
|
||||||
|
size 1883714625
|
||||||
80
handler.py
Normal file
80
handler.py
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
from typing import Dict, List, Any
|
||||||
|
import time
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from transformers import BertModel, BertTokenizerFast
|
||||||
|
|
||||||
|
|
||||||
|
class EndpointHandler():
|
||||||
|
def __init__(self, path_to_model: str = ".", max_cache_entries: int = 10000):
|
||||||
|
# Preload all the elements you are going to need at inference.
|
||||||
|
# pseudo:
|
||||||
|
self.tokenizer = BertTokenizerFast.from_pretrained(path_to_model)
|
||||||
|
self.model = BertModel.from_pretrained(path_to_model)
|
||||||
|
self.model = self.model.eval()
|
||||||
|
self.cache = {}
|
||||||
|
self.last_cache_cleanup = time.time()
|
||||||
|
self.max_cache_entries = max_cache_entries
|
||||||
|
|
||||||
|
def _lookup_cache(self, inputs):
|
||||||
|
cached_results = {}
|
||||||
|
uncached_inputs = []
|
||||||
|
|
||||||
|
for index, input_string in enumerate(inputs):
|
||||||
|
if input_string in self.cache:
|
||||||
|
cached_results[index] = self.cache[input_string]["pooler_output"]
|
||||||
|
else:
|
||||||
|
uncached_inputs.append((index, input_string))
|
||||||
|
|
||||||
|
return uncached_inputs, cached_results
|
||||||
|
|
||||||
|
def _store_to_cache(self, index, result):
|
||||||
|
# Add timings to cache
|
||||||
|
self.cache[index] = {
|
||||||
|
"pooler_output": result,
|
||||||
|
"last_access": time.time()
|
||||||
|
}
|
||||||
|
|
||||||
|
def _cleanup_cache(self):
|
||||||
|
current_time = time.time()
|
||||||
|
if current_time - self.last_cache_cleanup > 60 and len(self.cache) > self.max_cache_entries:
|
||||||
|
# Sort the cache by last access time
|
||||||
|
sorted_cache = sorted(self.cache.items(), key = lambda x: x[1]["last_access"])
|
||||||
|
|
||||||
|
# Remove the oldest entries
|
||||||
|
for i in range(len(self.cache) - self.max_cache_entries):
|
||||||
|
del self.cache[sorted_cache[i][0]]
|
||||||
|
|
||||||
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
This method is called whenever a request is made to the endpoint.
|
||||||
|
:param data: { inputs [str]: list of strings to be encoded }
|
||||||
|
:return: A :obj:`list` | `dict`: will be serialized and returned
|
||||||
|
"""
|
||||||
|
inputs = data['inputs']
|
||||||
|
|
||||||
|
# Check cache for inputs
|
||||||
|
uncached_inputs, cached_results = self._lookup_cache(inputs)
|
||||||
|
|
||||||
|
output_results = {}
|
||||||
|
|
||||||
|
# Call model for uncached input
|
||||||
|
if len(uncached_inputs) != 0:
|
||||||
|
model_inputs = [input_string for _, input_string in uncached_inputs]
|
||||||
|
uncached_inputs_tokenized = self.tokenizer(model_inputs, return_tensors = "pt", padding = True)
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
uncached_output_tensor = self.model(**uncached_inputs_tokenized)
|
||||||
|
|
||||||
|
uncached_output_list = uncached_output_tensor.pooler_output.tolist()
|
||||||
|
|
||||||
|
# combine cached and uncached results
|
||||||
|
for (index, input_string), result in zip(uncached_inputs, uncached_output_list):
|
||||||
|
self._store_to_cache(input_string, result)
|
||||||
|
output_results[index] = result
|
||||||
|
|
||||||
|
self._cleanup_cache()
|
||||||
|
|
||||||
|
output_results.update(cached_results)
|
||||||
|
|
||||||
|
return [output_results[i] for i in range(len(inputs))]
|
||||||
3
model.safetensors
Normal file
3
model.safetensors
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:77d8e1f2dbab6eb5d3c261ce9d3dbf1e3c69e02938c95f934f94f42c22dfa31f
|
||||||
|
size 1883734344
|
||||||
3
pytorch_model.bin
Normal file
3
pytorch_model.bin
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:5f45b674a2e511b204647f9e2cb056da656eedaee063a5cef435777b5a5ca1bf
|
||||||
|
size 1883775789
|
||||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
transformers==4.28.1
|
||||||
7
special_tokens_map.json
Normal file
7
special_tokens_map.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"cls_token": "[CLS]",
|
||||||
|
"mask_token": "[MASK]",
|
||||||
|
"pad_token": "[PAD]",
|
||||||
|
"sep_token": "[SEP]",
|
||||||
|
"unk_token": "[UNK]"
|
||||||
|
}
|
||||||
3
tf_model.h5
Normal file
3
tf_model.h5
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:e971d0404bba02ce8ab4568bc9625a74e6fbe99c7cdc927d5f3095597a70c55d
|
||||||
|
size 1883974632
|
||||||
3
tokenizer.json
Normal file
3
tokenizer.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:5aab105881afc3a73d5c8445cdc5c0302b1c3efdecd71a1a34fa0cf4e5b7bf43
|
||||||
|
size 13631023
|
||||||
14
tokenizer_config.json
Normal file
14
tokenizer_config.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"cls_token": "[CLS]",
|
||||||
|
"do_basic_tokenize": true,
|
||||||
|
"do_lower_case": false,
|
||||||
|
"mask_token": "[MASK]",
|
||||||
|
"model_max_length": 512,
|
||||||
|
"never_split": null,
|
||||||
|
"pad_token": "[PAD]",
|
||||||
|
"sep_token": "[SEP]",
|
||||||
|
"strip_accents": null,
|
||||||
|
"tokenize_chinese_chars": true,
|
||||||
|
"tokenizer_class": "BertTokenizer",
|
||||||
|
"unk_token": "[UNK]"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user