model2vec 0.3.4__tar.gz → 0.3.6__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.
Files changed (53) hide show
  1. {model2vec-0.3.4 → model2vec-0.3.6}/PKG-INFO +2 -2
  2. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec/distill/distillation.py +13 -6
  3. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec/hf_utils.py +23 -26
  4. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec/model.py +22 -1
  5. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec/version.py +1 -1
  6. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec.egg-info/PKG-INFO +2 -2
  7. {model2vec-0.3.4 → model2vec-0.3.6}/uv.lock +15 -15
  8. {model2vec-0.3.4 → model2vec-0.3.6}/.github/workflows/ci.yaml +0 -0
  9. {model2vec-0.3.4 → model2vec-0.3.6}/.gitignore +0 -0
  10. {model2vec-0.3.4 → model2vec-0.3.6}/.pre-commit-config.yaml +0 -0
  11. {model2vec-0.3.4 → model2vec-0.3.6}/LICENSE +0 -0
  12. {model2vec-0.3.4 → model2vec-0.3.6}/Makefile +0 -0
  13. {model2vec-0.3.4 → model2vec-0.3.6}/README.md +0 -0
  14. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/logo.png +0 -0
  15. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/logo_v2.png +0 -0
  16. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/model2vec_model_diagram.png +0 -0
  17. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/model2vec_model_diagram_transparant_dark.png +0 -0
  18. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/model2vec_model_diagram_transparant_light.png +0 -0
  19. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/sentences_per_second_vs_average_score.png +0 -0
  20. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/speed_vs_accuracy.png +0 -0
  21. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/speed_vs_accuracy_v2.png +0 -0
  22. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/speed_vs_accuracy_v3.png +0 -0
  23. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/speed_vs_accuracy_v4.png +0 -0
  24. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/speed_vs_mteb_score.png +0 -0
  25. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/speed_vs_mteb_score_v2.png +0 -0
  26. {model2vec-0.3.4 → model2vec-0.3.6}/assets/images/tutorial_ezlo.png +0 -0
  27. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec/__init__.py +0 -0
  28. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec/distill/__init__.py +0 -0
  29. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec/distill/inference.py +0 -0
  30. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec/distill/tokenizer.py +0 -0
  31. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec/distill/utils.py +0 -0
  32. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec/model_card_template.md +0 -0
  33. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec/utils.py +0 -0
  34. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec.egg-info/SOURCES.txt +0 -0
  35. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec.egg-info/dependency_links.txt +0 -0
  36. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec.egg-info/requires.txt +0 -0
  37. {model2vec-0.3.4 → model2vec-0.3.6}/model2vec.egg-info/top_level.txt +0 -0
  38. {model2vec-0.3.4 → model2vec-0.3.6}/pyproject.toml +0 -0
  39. {model2vec-0.3.4 → model2vec-0.3.6}/results/README.md +0 -0
  40. {model2vec-0.3.4 → model2vec-0.3.6}/scripts/export_to_onnx.py +0 -0
  41. {model2vec-0.3.4 → model2vec-0.3.6}/setup.cfg +0 -0
  42. {model2vec-0.3.4 → model2vec-0.3.6}/tests/__init__.py +0 -0
  43. {model2vec-0.3.4 → model2vec-0.3.6}/tests/conftest.py +0 -0
  44. {model2vec-0.3.4 → model2vec-0.3.6}/tests/data/test_tokenizer/special_tokens_map.json +0 -0
  45. {model2vec-0.3.4 → model2vec-0.3.6}/tests/data/test_tokenizer/tokenizer.json +0 -0
  46. {model2vec-0.3.4 → model2vec-0.3.6}/tests/data/test_tokenizer/tokenizer_config.json +0 -0
  47. {model2vec-0.3.4 → model2vec-0.3.6}/tests/test_distillation.py +0 -0
  48. {model2vec-0.3.4 → model2vec-0.3.6}/tests/test_model.py +0 -0
  49. {model2vec-0.3.4 → model2vec-0.3.6}/tests/test_utils.py +0 -0
  50. {model2vec-0.3.4 → model2vec-0.3.6}/tutorials/README.md +0 -0
  51. {model2vec-0.3.4 → model2vec-0.3.6}/tutorials/recipe_search.ipynb +0 -0
  52. {model2vec-0.3.4 → model2vec-0.3.6}/tutorials/semantic_chunking.ipynb +0 -0
  53. {model2vec-0.3.4 → model2vec-0.3.6}/tutorials/semantic_deduplication.ipynb +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: model2vec
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary: The Fastest State-of-the-Art Static Embeddings in the World
5
5
  Author-email: Stéphan Tulkens <stephantul@gmail.com>, Thomas van Dongen <thomas123@live.nl>
6
6
  License: MIT License
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import logging
4
+ import os
4
5
  import re
5
6
  from typing import Literal, Union
6
7
 
@@ -141,13 +142,19 @@ def distill_from_model(
141
142
  "hidden_dim": embeddings.shape[1],
142
143
  "seq_length": 1000000, # Set this to a high value since we don't have a sequence length limit.
143
144
  }
144
- # Get the language from the model card
145
- try:
146
- info = model_info(model_name)
147
- language = info.cardData.get("language")
148
- except RepositoryNotFoundError:
149
- logger.info("No model info found for model. Setting `language` to None.")
145
+
146
+ if os.path.exists(model_name):
147
+ # Using a local model. Get the model name from the path.
148
+ model_name = os.path.basename(model_name)
150
149
  language = None
150
+ else:
151
+ # Get the language from the model card.
152
+ try:
153
+ info = model_info(model_name)
154
+ language = info.cardData.get("language", None)
155
+ except RepositoryNotFoundError:
156
+ logger.info("No model info found for the model. Setting language to None.")
157
+ language = None
151
158
 
152
159
  return StaticModel(
153
160
  vectors=embeddings, tokenizer=new_tokenizer, config=config, base_model_name=model_name, language=language
@@ -84,7 +84,7 @@ def _create_model_card(
84
84
 
85
85
 
86
86
  def load_pretrained(
87
- folder_or_repo_path: str | Path, token: str | None = None
87
+ folder_or_repo_path: str | Path, token: str | None = None, from_sentence_transformers: bool = False
88
88
  ) -> tuple[np.ndarray, Tokenizer, dict[str, Any], dict[str, Any]]:
89
89
  """
90
90
  Loads a pretrained model from a folder.
@@ -93,26 +93,31 @@ def load_pretrained(
93
93
  - If this is a local path, we will load from the local path.
94
94
  - If the local path is not found, we will attempt to load from the huggingface hub.
95
95
  :param token: The huggingface token to use.
96
+ :param from_sentence_transformers: Whether to load the model from a sentence transformers model.
96
97
  :raises: FileNotFoundError if the folder exists, but the file does not exist locally.
97
98
  :return: The embeddings, tokenizer, config, and metadata.
98
99
 
99
100
  """
101
+ if from_sentence_transformers:
102
+ model_file = "0_StaticEmbedding/model.safetensors"
103
+ tokenizer_file = "0_StaticEmbedding/tokenizer.json"
104
+ config_name = "config_sentence_transformers.json"
105
+ else:
106
+ model_file = "model.safetensors"
107
+ tokenizer_file = "tokenizer.json"
108
+ config_name = "config.json"
109
+
100
110
  folder_or_repo_path = Path(folder_or_repo_path)
101
111
  if folder_or_repo_path.exists():
102
- embeddings_path = folder_or_repo_path / "model.safetensors"
112
+ embeddings_path = folder_or_repo_path / model_file
103
113
  if not embeddings_path.exists():
104
- old_embeddings_path = folder_or_repo_path / "embeddings.safetensors"
105
- if old_embeddings_path.exists():
106
- logger.warning("Old embeddings file found. Please rename to `model.safetensors` and re-save.")
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"
114
+ raise FileNotFoundError(f"Embeddings file does not exist in {folder_or_repo_path}")
115
+
116
+ config_path = folder_or_repo_path / config_name
112
117
  if not config_path.exists():
113
118
  raise FileNotFoundError(f"Config file does not exist in {folder_or_repo_path}")
114
119
 
115
- tokenizer_path = folder_or_repo_path / "tokenizer.json"
120
+ tokenizer_path = folder_or_repo_path / tokenizer_file
116
121
  if not tokenizer_path.exists():
117
122
  raise FileNotFoundError(f"Tokenizer file does not exist in {folder_or_repo_path}")
118
123
 
@@ -122,18 +127,7 @@ def load_pretrained(
122
127
 
123
128
  else:
124
129
  logger.info("Folder does not exist locally, attempting to use huggingface hub.")
125
- try:
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
130
+ embeddings_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(), model_file, token=token)
137
131
 
138
132
  try:
139
133
  readme_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(), "README.md", token=token)
@@ -142,11 +136,14 @@ def load_pretrained(
142
136
  logger.info("No README found in the model folder. No model card loaded.")
143
137
  metadata = {}
144
138
 
145
- config_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(), "config.json", token=token)
146
- tokenizer_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(), "tokenizer.json", token=token)
139
+ config_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(), config_name, token=token)
140
+ tokenizer_path = huggingface_hub.hf_hub_download(folder_or_repo_path.as_posix(), tokenizer_file, token=token)
147
141
 
148
142
  opened_tensor_file = cast(SafeOpenProtocol, safetensors.safe_open(embeddings_path, framework="numpy"))
149
- embeddings = opened_tensor_file.get_tensor("embeddings")
143
+ if from_sentence_transformers:
144
+ embeddings = opened_tensor_file.get_tensor("embedding.weight")
145
+ else:
146
+ embeddings = opened_tensor_file.get_tensor("embeddings")
150
147
 
151
148
  tokenizer: Tokenizer = Tokenizer.from_file(str(tokenizer_path))
152
149
  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, 4)
1
+ __version_triple__ = (0, 3, 6)
2
2
  __version__ = ".".join(map(str, __version_triple__))
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: model2vec
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary: The Fastest State-of-the-Art Static Embeddings in the World
5
5
  Author-email: Stéphan Tulkens <stephantul@gmail.com>, Thomas van Dongen <thomas123@live.nl>
6
6
  License: MIT License
@@ -160,7 +160,7 @@ name = "click"
160
160
  version = "8.1.8"
161
161
  source = { registry = "https://pypi.org/simple" }
162
162
  dependencies = [
163
- { name = "colorama", marker = "platform_system == 'Windows'" },
163
+ { name = "colorama", marker = "sys_platform == 'win32'" },
164
164
  ]
165
165
  sdist = { url = "https://files.pythonhosted.org/packages/b9/2e/0090cbf739cee7d23781ad4b89a9894a41538e4fcf4c31dcdd705b78eb8b/click-8.1.8.tar.gz", hash = "sha256:ed53c9d8990d83c2a27deae68e4ee337473f6330c040a31d4225c9574d16096a", size = 226593 }
166
166
  wheels = [
@@ -536,7 +536,7 @@ wheels = [
536
536
 
537
537
  [[package]]
538
538
  name = "model2vec"
539
- version = "0.3.3"
539
+ version = "0.3.5"
540
540
  source = { editable = "." }
541
541
  dependencies = [
542
542
  { name = "jinja2" },
@@ -1627,19 +1627,19 @@ dependencies = [
1627
1627
  { name = "jinja2" },
1628
1628
  { name = "networkx", version = "3.2.1", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.10'" },
1629
1629
  { name = "networkx", version = "3.4.2", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.10'" },
1630
- { name = "nvidia-cublas-cu12", marker = "platform_machine == 'x86_64' and platform_system == 'Linux'" },
1631
- { name = "nvidia-cuda-cupti-cu12", marker = "platform_machine == 'x86_64' and platform_system == 'Linux'" },
1632
- { name = "nvidia-cuda-nvrtc-cu12", marker = "platform_machine == 'x86_64' and platform_system == 'Linux'" },
1633
- { name = "nvidia-cuda-runtime-cu12", marker = "platform_machine == 'x86_64' and platform_system == 'Linux'" },
1634
- { name = "nvidia-cudnn-cu12", marker = "platform_machine == 'x86_64' and platform_system == 'Linux'" },
1635
- { name = "nvidia-cufft-cu12", marker = "platform_machine == 'x86_64' and platform_system == 'Linux'" },
1636
- { name = "nvidia-curand-cu12", marker = "platform_machine == 'x86_64' and platform_system == 'Linux'" },
1637
- { name = "nvidia-cusolver-cu12", marker = "platform_machine == 'x86_64' and platform_system == 'Linux'" },
1638
- { name = "nvidia-cusparse-cu12", marker = "platform_machine == 'x86_64' and platform_system == 'Linux'" },
1639
- { name = "nvidia-nccl-cu12", marker = "platform_machine == 'x86_64' and platform_system == 'Linux'" },
1640
- { name = "nvidia-nvtx-cu12", marker = "platform_machine == 'x86_64' and platform_system == 'Linux'" },
1630
+ { name = "nvidia-cublas-cu12", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
1631
+ { name = "nvidia-cuda-cupti-cu12", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
1632
+ { name = "nvidia-cuda-nvrtc-cu12", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
1633
+ { name = "nvidia-cuda-runtime-cu12", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
1634
+ { name = "nvidia-cudnn-cu12", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
1635
+ { name = "nvidia-cufft-cu12", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
1636
+ { name = "nvidia-curand-cu12", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
1637
+ { name = "nvidia-cusolver-cu12", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
1638
+ { name = "nvidia-cusparse-cu12", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
1639
+ { name = "nvidia-nccl-cu12", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
1640
+ { name = "nvidia-nvtx-cu12", marker = "platform_machine == 'x86_64' and sys_platform == 'linux'" },
1641
1641
  { name = "sympy" },
1642
- { name = "triton", marker = "python_full_version < '3.13' and platform_machine == 'x86_64' and platform_system == 'Linux'" },
1642
+ { name = "triton", marker = "python_full_version < '3.13' and platform_machine == 'x86_64' and sys_platform == 'linux'" },
1643
1643
  { name = "typing-extensions" },
1644
1644
  ]
1645
1645
  wheels = [
@@ -1666,7 +1666,7 @@ name = "tqdm"
1666
1666
  version = "4.67.1"
1667
1667
  source = { registry = "https://pypi.org/simple" }
1668
1668
  dependencies = [
1669
- { name = "colorama", marker = "platform_system == 'Windows'" },
1669
+ { name = "colorama", marker = "sys_platform == 'win32'" },
1670
1670
  ]
1671
1671
  sdist = { url = "https://files.pythonhosted.org/packages/a8/4b/29b4ef32e036bb34e4ab51796dd745cdba7ed47ad142a9f4a1eb8e0c744d/tqdm-4.67.1.tar.gz", hash = "sha256:f8aef9c52c08c13a65f30ea34f4e5aac3fd1a34959879d7e59e63027286627f2", size = 169737 }
1672
1672
  wheels = [
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