model2vec 0.3.1__py3-none-any.whl → 0.3.3__py3-none-any.whl

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/utils.py CHANGED
@@ -26,17 +26,19 @@ _MODULE_MAP = (("scikit-learn", "sklearn"),)
26
26
 
27
27
  def get_package_extras(package: str, extra: str) -> Iterator[str]:
28
28
  """Get the extras of the package."""
29
- message = metadata(package)
29
+ try:
30
+ message = metadata(package)
31
+ except Exception as e:
32
+ raise ImportError(f"Could not retrieve metadata for package '{package}': {e}")
33
+
30
34
  all_packages = message.get_all("Requires-Dist") or []
31
35
  for package in all_packages:
32
36
  name, *rest = package.split(";", maxsplit=1)
33
- if not rest:
34
- continue
35
- _, found_extra = rest[0].split("==", maxsplit=1)
36
- # Strip off quotes
37
- found_extra = found_extra.strip(' "')
38
- if found_extra == extra:
39
- yield name
37
+ if rest:
38
+ # Extract and clean the extra requirement
39
+ found_extra = rest[0].split("==")[-1].strip(" \"'")
40
+ if found_extra == extra:
41
+ yield name.strip()
40
42
 
41
43
 
42
44
  def importable(module: str, extra: str) -> None:
model2vec/version.py CHANGED
@@ -1,2 +1,2 @@
1
- __version_triple__ = (0, 3, 1)
1
+ __version_triple__ = (0, 3, 3)
2
2
  __version__ = ".".join(map(str, __version_triple__))
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: model2vec
3
- Version: 0.3.1
4
- Summary: Distill a Small Static Model from any Sentence Transformer
3
+ Version: 0.3.3
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
7
7
 
@@ -43,11 +43,13 @@ Classifier: Natural Language :: English
43
43
  Requires-Python: >=3.9
44
44
  Description-Content-Type: text/markdown
45
45
  License-File: LICENSE
46
+ Requires-Dist: jinja2
46
47
  Requires-Dist: numpy
47
48
  Requires-Dist: rich
48
- Requires-Dist: tqdm
49
- Requires-Dist: tokenizers >=0.20
49
+ Requires-Dist: safetensors
50
50
  Requires-Dist: setuptools
51
+ Requires-Dist: tokenizers >=0.20
52
+ Requires-Dist: tqdm
51
53
  Provides-Extra: dev
52
54
  Requires-Dist: black ; extra == 'dev'
53
55
  Requires-Dist: ipython ; extra == 'dev'
@@ -60,6 +62,9 @@ Provides-Extra: distill
60
62
  Requires-Dist: torch ; extra == 'distill'
61
63
  Requires-Dist: transformers ; extra == 'distill'
62
64
  Requires-Dist: scikit-learn ; extra == 'distill'
65
+ Provides-Extra: onnx
66
+ Requires-Dist: onnx ; extra == 'onnx'
67
+ Requires-Dist: torch ; extra == 'onnx'
63
68
 
64
69
 
65
70
  <div align="center">
@@ -70,7 +75,7 @@ Requires-Dist: scikit-learn ; extra == 'distill'
70
75
  </div>
71
76
 
72
77
  <div align="center">
73
- <h2>Distill a Small Static Model from any Sentence Transformer</h2>
78
+ <h2>The Fastest State-of-the-Art Static Embeddings in the World</h2>
74
79
  </div>
75
80
 
76
81
  <div align="center">
@@ -102,7 +107,7 @@ Requires-Dist: scikit-learn ; extra == 'distill'
102
107
  <img src="assets/images/model2vec_model_diagram_transparant_light.png#gh-light-mode-only" width="90%">
103
108
  </div>
104
109
 
105
- Model2Vec is a technique to turn any sentence transformer into a really small static model, reducing model size by 15x and making the models up to 500x faster, with a small drop in performance. See our results [here](results/README.md), or dive in to see how it works.
110
+ Model2Vec is a technique to turn any sentence transformer into a really small static model, reducing model size by 15x and making the models up to 500x faster, with a small drop in performance. Our [best model](https://huggingface.co/minishlab/potion-base-8M) is the most performant static embedding model in the world. See our results [here](results/README.md), or dive in to see how it works.
106
111
 
107
112
 
108
113
  ## Updates & Announcements
@@ -117,6 +122,7 @@ Model2Vec is a technique to turn any sentence transformer into a really small st
117
122
  - [Distillation](#distillation)
118
123
  - [Inference](#inference)
119
124
  - [Evaluation](#evaluation)
125
+ - [Integrations](#integrations)
120
126
  - [Model List](#model-list)
121
127
  - [Results](#results)
122
128
  - [Related Work](#related-work)
@@ -419,6 +425,89 @@ print(make_leaderboard(task_scores))
419
425
  ```
420
426
  </details>
421
427
 
428
+ ### Integrations
429
+ <details>
430
+ <summary> Sentence Transformers </summary>
431
+ <br>
432
+
433
+ Model2Vec can be used directly in [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) using the `StaticEmbedding` module.
434
+
435
+ The following code snippet shows how to load a Model2Vec model into a Sentence Transformer model:
436
+ ```python
437
+ from sentence_transformers import SentenceTransformer
438
+ from sentence_transformers.models import StaticEmbedding
439
+
440
+ # Initialize a StaticEmbedding module
441
+ static_embedding = StaticEmbedding.from_model2vec("minishlab/potion-base-8M")
442
+ model = SentenceTransformer(modules=[static_embedding])
443
+ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
444
+ ```
445
+
446
+ The following code snippet shows how to distill a model directly into a Sentence Transformer model:
447
+
448
+ ```python
449
+ from sentence_transformers import SentenceTransformer
450
+ from sentence_transformers.models import StaticEmbedding
451
+
452
+ static_embedding = StaticEmbedding.from_distillation("BAAI/bge-base-en-v1.5", device="cpu", pca_dims=256)
453
+ model = SentenceTransformer(modules=[static_embedding])
454
+ embeddings = model.encode(["It's dangerous to go alone!", "It's a secret to everybody."])
455
+ ```
456
+
457
+ </details>
458
+
459
+
460
+ <details>
461
+ <summary> Transformers.js </summary>
462
+
463
+ <br>
464
+
465
+ To use a Model2Vec model in [transformers.js](https://github.com/huggingface/transformers.js), the following code snippet can be used as a starting point:
466
+
467
+ ```javascript
468
+ import { AutoModel, AutoTokenizer, Tensor } from '@huggingface/transformers';
469
+
470
+ const modelName = 'minishlab/potion-base-8M';
471
+
472
+ const modelConfig = {
473
+ config: { model_type: 'model2vec' },
474
+ dtype: 'fp32',
475
+ revision: 'refs/pr/1'
476
+ };
477
+ const tokenizerConfig = {
478
+ revision: 'refs/pr/2'
479
+ };
480
+
481
+ const model = await AutoModel.from_pretrained(modelName, modelConfig);
482
+ const tokenizer = await AutoTokenizer.from_pretrained(modelName, tokenizerConfig);
483
+
484
+ const texts = ['hello', 'hello world'];
485
+ const { input_ids } = await tokenizer(texts, { add_special_tokens: false, return_tensor: false });
486
+
487
+ const cumsum = arr => arr.reduce((acc, num, i) => [...acc, num + (acc[i - 1] || 0)], []);
488
+ const offsets = [0, ...cumsum(input_ids.slice(0, -1).map(x => x.length))];
489
+
490
+ const flattened_input_ids = input_ids.flat();
491
+ const modelInputs = {
492
+ input_ids: new Tensor('int64', flattened_input_ids, [flattened_input_ids.length]),
493
+ offsets: new Tensor('int64', offsets, [offsets.length])
494
+ };
495
+
496
+ const { embeddings } = await model(modelInputs);
497
+ console.log(embeddings.tolist()); // output matches python version
498
+ ```
499
+
500
+ Note that this requires that the Model2Vec has a `model.onnx` file and several required tokenizers file. To generate these for a model that does not have them yet, the following code snippet can be used:
501
+
502
+ ```bash
503
+ python scripts/export_to_onnx.py --model_path <path-to-a-model2vec-model> --save_path "<path-to-save-the-onnx-model>"
504
+ ```
505
+
506
+
507
+ <br>
508
+ </details>
509
+
510
+
422
511
  ## Model List
423
512
 
424
513
  We provide a number of models that can be used out of the box. These models are available on the [HuggingFace hub](https://huggingface.co/collections/minishlab/model2vec-base-models-66fd9dd9b7c3b3c0f25ca90e) and can be loaded using the `from_pretrained` method. The models are listed below.
@@ -2,15 +2,15 @@ model2vec/__init__.py,sha256=osue7o8CuO_1T9opJAWfnbg4SspGKx0cHuyWv8Tedfs,124
2
2
  model2vec/hf_utils.py,sha256=Z-_V2kYagBX7m9_--e2ZAGP4irfRpqIlBQgGwYswpoU,7772
3
3
  model2vec/model.py,sha256=DuzsHaTBf0fTsp0ovVPzFs3EzDqGnFd5bGC94vumYuU,12031
4
4
  model2vec/model_card_template.md,sha256=rQo4bE3VPXH3XB9OUCe7FCenhBc4wv6L1sNURVT4Sro,2747
5
- model2vec/utils.py,sha256=ID5TYV9dvcQccfqBbeVVAK66qfKm0tYf6O9xbISlcOQ,2577
6
- model2vec/version.py,sha256=fgJbJS8ResifrwglA284jEeq3crjuclMKhZfSCctVnQ,84
5
+ model2vec/utils.py,sha256=AyKPu7w7XO2DhJEBN8NrO53vZj5rZpbwWJNxjagCKIk,2684
6
+ model2vec/version.py,sha256=azyH_mT8eRQuggt3kh7T7FSoYvfnwcLHjURqaYWVK0Q,84
7
7
  model2vec/distill/__init__.py,sha256=KpFRhafXVceM1xhorJhXbd8OnX8dwOGcWMbXglZxd4U,330
8
8
  model2vec/distill/distillation.py,sha256=yDdIW4nqR5Vw4cUYL4p7hrJnIzZWO9fV9LiqZHe_z8s,11504
9
9
  model2vec/distill/inference.py,sha256=QgHAEkLoeq-jPyKnFTB6p6Ry-ZGZqonkZaEg2S7VTOY,6123
10
10
  model2vec/distill/tokenizer.py,sha256=xtVsMTxquqTufoP9z39C4Itg2LLcGViqeguz-M7y-u8,4293
11
11
  model2vec/distill/utils.py,sha256=lzGyvFUO8mUcz63wnTRimGhQCsXzqnQfG5vRRKDGxic,721
12
- model2vec-0.3.1.dist-info/LICENSE,sha256=sCFPFI7OrnOfkWIJ54z-FXEsS4EMi8nUS2wtHRYWqrY,1074
13
- model2vec-0.3.1.dist-info/METADATA,sha256=sSDg_zBOB90cmsCzw3KFz785oWC1jlgGbMddWkDQWh8,22614
14
- model2vec-0.3.1.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
15
- model2vec-0.3.1.dist-info/top_level.txt,sha256=cprhxJcbtkFiydp0BGvdCVNupTWg07-3p93jFStc78g,10
16
- model2vec-0.3.1.dist-info/RECORD,,
12
+ model2vec-0.3.3.dist-info/LICENSE,sha256=sCFPFI7OrnOfkWIJ54z-FXEsS4EMi8nUS2wtHRYWqrY,1074
13
+ model2vec-0.3.3.dist-info/METADATA,sha256=1kDQ4KrbGMom8ZKMx3b0DJ4XpUcJtkQsAmdEfZWPd0o,25800
14
+ model2vec-0.3.3.dist-info/WHEEL,sha256=R06PA3UVYHThwHvxuRWMqaGcr-PuniXahwjmQRFMEkY,91
15
+ model2vec-0.3.3.dist-info/top_level.txt,sha256=cprhxJcbtkFiydp0BGvdCVNupTWg07-3p93jFStc78g,10
16
+ model2vec-0.3.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.3.0)
2
+ Generator: setuptools (75.5.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5