model2vec 0.3.5__tar.gz → 0.3.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {model2vec-0.3.5 → model2vec-0.3.7}/PKG-INFO +1 -1
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec/hf_utils.py +32 -28
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec/model.py +22 -1
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec/version.py +1 -1
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec.egg-info/PKG-INFO +1 -1
- {model2vec-0.3.5 → model2vec-0.3.7}/tests/test_model.py +1 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/uv.lock +1 -1
- {model2vec-0.3.5 → model2vec-0.3.7}/.github/workflows/ci.yaml +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/.gitignore +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/.pre-commit-config.yaml +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/LICENSE +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/Makefile +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/README.md +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/logo.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/logo_v2.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/model2vec_model_diagram.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/model2vec_model_diagram_transparant_dark.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/model2vec_model_diagram_transparant_light.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/sentences_per_second_vs_average_score.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/speed_vs_accuracy.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/speed_vs_accuracy_v2.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/speed_vs_accuracy_v3.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/speed_vs_accuracy_v4.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/speed_vs_mteb_score.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/speed_vs_mteb_score_v2.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/assets/images/tutorial_ezlo.png +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec/__init__.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec/distill/__init__.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec/distill/distillation.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec/distill/inference.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec/distill/tokenizer.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec/distill/utils.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec/model_card_template.md +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec/utils.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec.egg-info/SOURCES.txt +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec.egg-info/dependency_links.txt +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec.egg-info/requires.txt +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/model2vec.egg-info/top_level.txt +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/pyproject.toml +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/results/README.md +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/scripts/export_to_onnx.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/setup.cfg +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/tests/__init__.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/tests/conftest.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/tests/data/test_tokenizer/special_tokens_map.json +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/tests/data/test_tokenizer/tokenizer.json +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/tests/data/test_tokenizer/tokenizer_config.json +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/tests/test_distillation.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/tests/test_utils.py +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/tutorials/README.md +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/tutorials/recipe_search.ipynb +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/tutorials/semantic_chunking.ipynb +0 -0
- {model2vec-0.3.5 → model2vec-0.3.7}/tutorials/semantic_deduplication.ipynb +0 -0
|
@@ -39,7 +39,14 @@ def save_pretrained(
|
|
|
39
39
|
folder_path.mkdir(exist_ok=True, parents=True)
|
|
40
40
|
save_file({"embeddings": embeddings}, folder_path / "model.safetensors")
|
|
41
41
|
tokenizer.save(str(folder_path / "tokenizer.json"))
|
|
42
|
-
json.dump(config, open(folder_path / "config.json", "w"))
|
|
42
|
+
json.dump(config, open(folder_path / "config.json", "w"), indent=4)
|
|
43
|
+
|
|
44
|
+
# Create modules.json
|
|
45
|
+
modules = [{"idx": 0, "name": "0", "path": ".", "type": "sentence_transformers.models.StaticEmbedding"}]
|
|
46
|
+
if config.get("normalize"):
|
|
47
|
+
# If normalize=True, add sentence_transformers.models.Normalize
|
|
48
|
+
modules.append({"idx": 1, "name": "1", "path": "1_Normalize", "type": "sentence_transformers.models.Normalize"})
|
|
49
|
+
json.dump(modules, open(folder_path / "modules.json", "w"), indent=4)
|
|
43
50
|
|
|
44
51
|
logger.info(f"Saved model to {folder_path}")
|
|
45
52
|
|
|
@@ -75,7 +82,7 @@ def _create_model_card(
|
|
|
75
82
|
base_model=base_model_name,
|
|
76
83
|
license=license,
|
|
77
84
|
language=language,
|
|
78
|
-
tags=["embeddings", "static-embeddings"],
|
|
85
|
+
tags=["embeddings", "static-embeddings", "sentence-transformers"],
|
|
79
86
|
library_name="model2vec",
|
|
80
87
|
**kwargs,
|
|
81
88
|
)
|
|
@@ -84,7 +91,7 @@ def _create_model_card(
|
|
|
84
91
|
|
|
85
92
|
|
|
86
93
|
def load_pretrained(
|
|
87
|
-
folder_or_repo_path: str | Path, token: str | None = None
|
|
94
|
+
folder_or_repo_path: str | Path, token: str | None = None, from_sentence_transformers: bool = False
|
|
88
95
|
) -> tuple[np.ndarray, Tokenizer, dict[str, Any], dict[str, Any]]:
|
|
89
96
|
"""
|
|
90
97
|
Loads a pretrained model from a folder.
|
|
@@ -93,26 +100,31 @@ def load_pretrained(
|
|
|
93
100
|
- If this is a local path, we will load from the local path.
|
|
94
101
|
- If the local path is not found, we will attempt to load from the huggingface hub.
|
|
95
102
|
:param token: The huggingface token to use.
|
|
103
|
+
:param from_sentence_transformers: Whether to load the model from a sentence transformers model.
|
|
96
104
|
:raises: FileNotFoundError if the folder exists, but the file does not exist locally.
|
|
97
105
|
:return: The embeddings, tokenizer, config, and metadata.
|
|
98
106
|
|
|
99
107
|
"""
|
|
108
|
+
if from_sentence_transformers:
|
|
109
|
+
model_file = "0_StaticEmbedding/model.safetensors"
|
|
110
|
+
tokenizer_file = "0_StaticEmbedding/tokenizer.json"
|
|
111
|
+
config_name = "config_sentence_transformers.json"
|
|
112
|
+
else:
|
|
113
|
+
model_file = "model.safetensors"
|
|
114
|
+
tokenizer_file = "tokenizer.json"
|
|
115
|
+
config_name = "config.json"
|
|
116
|
+
|
|
100
117
|
folder_or_repo_path = Path(folder_or_repo_path)
|
|
101
118
|
if folder_or_repo_path.exists():
|
|
102
|
-
embeddings_path = folder_or_repo_path /
|
|
119
|
+
embeddings_path = folder_or_repo_path / model_file
|
|
103
120
|
if not embeddings_path.exists():
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
embeddings_path = old_embeddings_path
|
|
108
|
-
else:
|
|
109
|
-
raise FileNotFoundError(f"Embeddings file does not exist in {folder_or_repo_path}")
|
|
110
|
-
|
|
111
|
-
config_path = folder_or_repo_path / "config.json"
|
|
121
|
+
raise FileNotFoundError(f"Embeddings file does not exist in {folder_or_repo_path}")
|
|
122
|
+
|
|
123
|
+
config_path = folder_or_repo_path / config_name
|
|
112
124
|
if not config_path.exists():
|
|
113
125
|
raise FileNotFoundError(f"Config file does not exist in {folder_or_repo_path}")
|
|
114
126
|
|
|
115
|
-
tokenizer_path = folder_or_repo_path /
|
|
127
|
+
tokenizer_path = folder_or_repo_path / tokenizer_file
|
|
116
128
|
if not tokenizer_path.exists():
|
|
117
129
|
raise FileNotFoundError(f"Tokenizer file does not exist in {folder_or_repo_path}")
|
|
118
130
|
|
|
@@ -122,18 +134,7 @@ def load_pretrained(
|
|
|
122
134
|
|
|
123
135
|
else:
|
|
124
136
|
logger.info("Folder does not exist locally, attempting to use huggingface hub.")
|
|
125
|
-
|
|
126
|
-
embeddings_path = huggingface_hub.hf_hub_download(
|
|
127
|
-
folder_or_repo_path.as_posix(), "model.safetensors", token=token
|
|
128
|
-
)
|
|
129
|
-
except huggingface_hub.utils.EntryNotFoundError as e:
|
|
130
|
-
try:
|
|
131
|
-
embeddings_path = huggingface_hub.hf_hub_download(
|
|
132
|
-
folder_or_repo_path.as_posix(), "embeddings.safetensors", token=token
|
|
133
|
-
)
|
|
134
|
-
except huggingface_hub.utils.EntryNotFoundError:
|
|
135
|
-
# Raise original exception.
|
|
136
|
-
raise e
|
|
137
|
+
embeddings_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(), model_file, token=token)
|
|
137
138
|
|
|
138
139
|
try:
|
|
139
140
|
readme_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(), "README.md", token=token)
|
|
@@ -142,11 +143,14 @@ def load_pretrained(
|
|
|
142
143
|
logger.info("No README found in the model folder. No model card loaded.")
|
|
143
144
|
metadata = {}
|
|
144
145
|
|
|
145
|
-
config_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(),
|
|
146
|
-
tokenizer_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(),
|
|
146
|
+
config_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(), config_name, token=token)
|
|
147
|
+
tokenizer_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(), tokenizer_file, token=token)
|
|
147
148
|
|
|
148
149
|
opened_tensor_file = cast(SafeOpenProtocol, safetensors.safe_open(embeddings_path, framework="numpy"))
|
|
149
|
-
|
|
150
|
+
if from_sentence_transformers:
|
|
151
|
+
embeddings = opened_tensor_file.get_tensor("embedding.weight")
|
|
152
|
+
else:
|
|
153
|
+
embeddings = opened_tensor_file.get_tensor("embeddings")
|
|
150
154
|
|
|
151
155
|
tokenizer: Tokenizer = Tokenizer.from_file(str(tokenizer_path))
|
|
152
156
|
config = json.load(open(config_path))
|
|
@@ -160,12 +160,33 @@ class StaticModel:
|
|
|
160
160
|
"""
|
|
161
161
|
from model2vec.hf_utils import load_pretrained
|
|
162
162
|
|
|
163
|
-
embeddings, tokenizer, config, metadata = load_pretrained(path, token=token)
|
|
163
|
+
embeddings, tokenizer, config, metadata = load_pretrained(path, token=token, from_sentence_transformers=False)
|
|
164
164
|
|
|
165
165
|
return cls(
|
|
166
166
|
embeddings, tokenizer, config, base_model_name=metadata.get("base_model"), language=metadata.get("language")
|
|
167
167
|
)
|
|
168
168
|
|
|
169
|
+
@classmethod
|
|
170
|
+
def from_sentence_transformers(
|
|
171
|
+
cls: type[StaticModel],
|
|
172
|
+
path: PathLike,
|
|
173
|
+
token: str | None = None,
|
|
174
|
+
) -> StaticModel:
|
|
175
|
+
"""
|
|
176
|
+
Load a StaticModel trained with sentence transformers from a local path or huggingface hub path.
|
|
177
|
+
|
|
178
|
+
NOTE: if you load a private model from the huggingface hub, you need to pass a token.
|
|
179
|
+
|
|
180
|
+
:param path: The path to load your static model from.
|
|
181
|
+
:param token: The huggingface token to use.
|
|
182
|
+
:return: A StaticModel
|
|
183
|
+
"""
|
|
184
|
+
from model2vec.hf_utils import load_pretrained
|
|
185
|
+
|
|
186
|
+
embeddings, tokenizer, config, _ = load_pretrained(path, token=token, from_sentence_transformers=True)
|
|
187
|
+
|
|
188
|
+
return cls(embeddings, tokenizer, config, base_model_name=None, language=None)
|
|
189
|
+
|
|
169
190
|
def encode_as_sequence(
|
|
170
191
|
self,
|
|
171
192
|
sentences: list[str] | str,
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
__version_triple__ = (0, 3,
|
|
1
|
+
__version_triple__ = (0, 3, 7)
|
|
2
2
|
__version__ = ".".join(map(str, __version_triple__))
|
|
@@ -161,6 +161,7 @@ def test_save_pretrained(
|
|
|
161
161
|
assert (save_path / "model.safetensors").exists()
|
|
162
162
|
assert (save_path / "tokenizer.json").exists()
|
|
163
163
|
assert (save_path / "config.json").exists()
|
|
164
|
+
assert (save_path / "modules.json").exists()
|
|
164
165
|
|
|
165
166
|
|
|
166
167
|
def test_load_pretrained(
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{model2vec-0.3.5 → model2vec-0.3.7}/assets/images/model2vec_model_diagram_transparant_dark.png
RENAMED
|
File without changes
|
{model2vec-0.3.5 → model2vec-0.3.7}/assets/images/model2vec_model_diagram_transparant_light.png
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|