semhash 0.3.1__tar.gz → 0.3.2__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 (37) hide show
  1. {semhash-0.3.1 → semhash-0.3.2}/.gitignore +0 -2
  2. {semhash-0.3.1 → semhash-0.3.2}/PKG-INFO +32 -3
  3. {semhash-0.3.1 → semhash-0.3.2}/README.md +30 -1
  4. {semhash-0.3.1 → semhash-0.3.2}/pyproject.toml +1 -1
  5. {semhash-0.3.1 → semhash-0.3.2}/semhash/index.py +8 -2
  6. {semhash-0.3.1 → semhash-0.3.2}/semhash/semhash.py +7 -2
  7. {semhash-0.3.1 → semhash-0.3.2}/semhash/version.py +1 -1
  8. {semhash-0.3.1 → semhash-0.3.2}/semhash.egg-info/PKG-INFO +32 -3
  9. {semhash-0.3.1 → semhash-0.3.2}/semhash.egg-info/requires.txt +1 -1
  10. {semhash-0.3.1 → semhash-0.3.2}/uv.lock +812 -811
  11. {semhash-0.3.1 → semhash-0.3.2}/.github/workflows/ci.yaml +0 -0
  12. {semhash-0.3.1 → semhash-0.3.2}/.pre-commit-config.yaml +0 -0
  13. {semhash-0.3.1 → semhash-0.3.2}/LICENSE +0 -0
  14. {semhash-0.3.1 → semhash-0.3.2}/Makefile +0 -0
  15. {semhash-0.3.1 → semhash-0.3.2}/assets/images/semhash_logo.png +0 -0
  16. {semhash-0.3.1 → semhash-0.3.2}/assets/images/semhash_logo_v2.png +0 -0
  17. {semhash-0.3.1 → semhash-0.3.2}/benchmarks/__init__.py +0 -0
  18. {semhash-0.3.1 → semhash-0.3.2}/benchmarks/data.py +0 -0
  19. {semhash-0.3.1 → semhash-0.3.2}/benchmarks/results/train_benchmark_results.json +0 -0
  20. {semhash-0.3.1 → semhash-0.3.2}/benchmarks/results/train_test_benchmark_results.json +0 -0
  21. {semhash-0.3.1 → semhash-0.3.2}/benchmarks/run_benchmarks.py +0 -0
  22. {semhash-0.3.1 → semhash-0.3.2}/semhash/__init__.py +0 -0
  23. {semhash-0.3.1 → semhash-0.3.2}/semhash/datamodels.py +0 -0
  24. {semhash-0.3.1 → semhash-0.3.2}/semhash/records.py +0 -0
  25. {semhash-0.3.1 → semhash-0.3.2}/semhash/utils.py +0 -0
  26. {semhash-0.3.1 → semhash-0.3.2}/semhash.egg-info/SOURCES.txt +0 -0
  27. {semhash-0.3.1 → semhash-0.3.2}/semhash.egg-info/dependency_links.txt +0 -0
  28. {semhash-0.3.1 → semhash-0.3.2}/semhash.egg-info/top_level.txt +0 -0
  29. {semhash-0.3.1 → semhash-0.3.2}/setup.cfg +0 -0
  30. {semhash-0.3.1 → semhash-0.3.2}/tests/conftest.py +0 -0
  31. {semhash-0.3.1 → semhash-0.3.2}/tests/data/test_model/README.md +0 -0
  32. {semhash-0.3.1 → semhash-0.3.2}/tests/data/test_model/config.json +0 -0
  33. {semhash-0.3.1 → semhash-0.3.2}/tests/data/test_model/model.safetensors +0 -0
  34. {semhash-0.3.1 → semhash-0.3.2}/tests/data/test_model/modules.json +0 -0
  35. {semhash-0.3.1 → semhash-0.3.2}/tests/data/test_model/tokenizer.json +0 -0
  36. {semhash-0.3.1 → semhash-0.3.2}/tests/test_datamodels.py +0 -0
  37. {semhash-0.3.1 → semhash-0.3.2}/tests/test_semhash.py +0 -0
@@ -163,5 +163,3 @@ cython_debug/
163
163
  # and can be added to the global gitignore or merged into this file. For a more nuclear
164
164
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
165
165
  #.idea/
166
-
167
- .DS_Store
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: semhash
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: Fast Semantic Text Deduplication & Filtering
5
5
  Author-email: Thomas van Dongen <thomas123@live.nl>, Stéphan Tulkens <stephantul@gmail.com>
6
6
  License: MIT License
@@ -43,7 +43,7 @@ Classifier: Natural Language :: English
43
43
  Requires-Python: >=3.9
44
44
  Description-Content-Type: text/markdown
45
45
  Requires-Dist: model2vec>=0.3.4
46
- Requires-Dist: vicinity[usearch]>=0.4.0
46
+ Requires-Dist: vicinity[usearch]>=0.4.3
47
47
  Requires-Dist: frozendict
48
48
  Provides-Extra: dev
49
49
  Requires-Dist: black; extra == "dev"
@@ -374,6 +374,35 @@ deduplicated_texts = semhash.self_deduplicate()
374
374
 
375
375
  </details>
376
376
 
377
+
378
+
379
+
380
+ <details>
381
+ <summary> Using custom ANN backends </summary>
382
+ <br>
383
+
384
+ The following code snippet shows how to use a custom ANN backend and custom args with SemHash:
385
+
386
+ ```python
387
+ from datasets import load_dataset
388
+ from semhash import SemHash
389
+ from vicinity import Backend
390
+
391
+ # Load a dataset to deduplicate
392
+ texts = load_dataset("ag_news", split="train")["text"]
393
+
394
+ # Initialize a SemHash with the model and custom ann backend and custom args
395
+ semhash = SemHash.from_records(records=texts, ann_backend=Backend.FAISS, nlist=50)
396
+
397
+ # Deduplicate the texts
398
+ deduplicated_texts = semhash.self_deduplicate()
399
+ ```
400
+
401
+ For the full list of supported ANN backends and args, see the [Vicinity docs](https://github.com/MinishLab/vicinity/tree/main?tab=readme-ov-file#supported-backends).
402
+
403
+ </details>
404
+
405
+
377
406
  <details>
378
407
  <summary> Using Pandas DataFrames </summary>
379
408
  <br>
@@ -473,7 +502,7 @@ To run the benchmarks yourself, you can use the following command (assuming you
473
502
  ```bash
474
503
  python -m benchmarks.run_benchmarks
475
504
  ```
476
- Optionally, the datasets can be updated in the [datasets.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/datasets.py) file.
505
+ Optionally, the datasets can be updated in the [data.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/data.py) file.
477
506
 
478
507
  ## License
479
508
 
@@ -318,6 +318,35 @@ deduplicated_texts = semhash.self_deduplicate()
318
318
 
319
319
  </details>
320
320
 
321
+
322
+
323
+
324
+ <details>
325
+ <summary> Using custom ANN backends </summary>
326
+ <br>
327
+
328
+ The following code snippet shows how to use a custom ANN backend and custom args with SemHash:
329
+
330
+ ```python
331
+ from datasets import load_dataset
332
+ from semhash import SemHash
333
+ from vicinity import Backend
334
+
335
+ # Load a dataset to deduplicate
336
+ texts = load_dataset("ag_news", split="train")["text"]
337
+
338
+ # Initialize a SemHash with the model and custom ann backend and custom args
339
+ semhash = SemHash.from_records(records=texts, ann_backend=Backend.FAISS, nlist=50)
340
+
341
+ # Deduplicate the texts
342
+ deduplicated_texts = semhash.self_deduplicate()
343
+ ```
344
+
345
+ For the full list of supported ANN backends and args, see the [Vicinity docs](https://github.com/MinishLab/vicinity/tree/main?tab=readme-ov-file#supported-backends).
346
+
347
+ </details>
348
+
349
+
321
350
  <details>
322
351
  <summary> Using Pandas DataFrames </summary>
323
352
  <br>
@@ -417,7 +446,7 @@ To run the benchmarks yourself, you can use the following command (assuming you
417
446
  ```bash
418
447
  python -m benchmarks.run_benchmarks
419
448
  ```
420
- Optionally, the datasets can be updated in the [datasets.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/datasets.py) file.
449
+ Optionally, the datasets can be updated in the [data.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/data.py) file.
421
450
 
422
451
  ## License
423
452
 
@@ -24,7 +24,7 @@ classifiers = [
24
24
 
25
25
  dependencies = [
26
26
  "model2vec>=0.3.4",
27
- "vicinity[usearch]>=0.4.0",
27
+ "vicinity[usearch]>=0.4.3",
28
28
  "frozendict",
29
29
  ]
30
30
 
@@ -1,5 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
+ from typing import Any
4
+
3
5
  import numpy as np
4
6
  from vicinity import Backend
5
7
  from vicinity.backends import AbstractBackend, get_backend_class
@@ -27,17 +29,21 @@ class Index:
27
29
  self.vectors = vectors
28
30
 
29
31
  @classmethod
30
- def from_vectors_and_items(cls, vectors: np.ndarray, items: list[DictItem], backend_type: Backend) -> Index:
32
+ def from_vectors_and_items(
33
+ cls, vectors: np.ndarray, items: list[DictItem], backend_type: Backend | str, **kwargs: Any
34
+ ) -> Index:
31
35
  """
32
36
  Load the index from vectors and items.
33
37
 
34
38
  :param vectors: The vectors of the items.
35
39
  :param items: The items in the index.
36
40
  :param backend_type: The type of backend to use.
41
+ :param **kwargs: Additional arguments to pass to the backend.
37
42
  :return: The index.
38
43
  """
39
44
  backend_class = get_backend_class(backend_type)
40
- backend = backend_class.from_vectors(vectors)
45
+ arguments = backend_class.argument_class(**kwargs)
46
+ backend = backend_class.from_vectors(vectors, **arguments.dict())
41
47
 
42
48
  return cls(vectors, items, backend)
43
49
 
@@ -2,7 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  from collections import defaultdict
4
4
  from math import ceil
5
- from typing import Generic, Literal, Sequence
5
+ from typing import Any, Generic, Literal, Sequence
6
6
 
7
7
  import numpy as np
8
8
  from frozendict import frozendict
@@ -102,6 +102,8 @@ class SemHash(Generic[Record]):
102
102
  columns: Sequence[str] | None = None,
103
103
  use_ann: bool = True,
104
104
  model: Encoder | None = None,
105
+ ann_backend: Backend | str = Backend.USEARCH,
106
+ **kwargs: Any,
105
107
  ) -> SemHash:
106
108
  """
107
109
  Initialize a SemHash instance from records.
@@ -112,6 +114,8 @@ class SemHash(Generic[Record]):
112
114
  :param columns: Columns to featurize if records are dictionaries.
113
115
  :param use_ann: Whether to use approximate nearest neighbors (True) or basic search (False). Default is True.
114
116
  :param model: (Optional) An Encoder model. If None, the default model is used (minishlab/potion-base-8M).
117
+ :param ann_backend: (Optional) The ANN backend to use if use_ann is True. Defaults to Backend.USEARCH.
118
+ :param **kwargs: Any additional keyword arguments to pass to the Vicinity index.
115
119
  :return: A SemHash instance with a fitted vicinity index.
116
120
  :raises ValueError: If columns are not provided for dictionary records.
117
121
  """
@@ -151,11 +155,12 @@ class SemHash(Generic[Record]):
151
155
  embeddings = cls._featurize(deduplicated_records, columns, model)
152
156
 
153
157
  # Build the Vicinity index
154
- backend = Backend.USEARCH if use_ann else Backend.BASIC
158
+ backend = ann_backend if use_ann else Backend.BASIC
155
159
  index = Index.from_vectors_and_items(
156
160
  vectors=embeddings,
157
161
  items=items,
158
162
  backend_type=backend,
163
+ **kwargs,
159
164
  )
160
165
 
161
166
  return cls(index=index, columns=columns, model=model, was_string=was_string)
@@ -1,2 +1,2 @@
1
- __version_triple__ = (0, 3, 1)
1
+ __version_triple__ = (0, 3, 2)
2
2
  __version__ = ".".join(map(str, __version_triple__))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: semhash
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: Fast Semantic Text Deduplication & Filtering
5
5
  Author-email: Thomas van Dongen <thomas123@live.nl>, Stéphan Tulkens <stephantul@gmail.com>
6
6
  License: MIT License
@@ -43,7 +43,7 @@ Classifier: Natural Language :: English
43
43
  Requires-Python: >=3.9
44
44
  Description-Content-Type: text/markdown
45
45
  Requires-Dist: model2vec>=0.3.4
46
- Requires-Dist: vicinity[usearch]>=0.4.0
46
+ Requires-Dist: vicinity[usearch]>=0.4.3
47
47
  Requires-Dist: frozendict
48
48
  Provides-Extra: dev
49
49
  Requires-Dist: black; extra == "dev"
@@ -374,6 +374,35 @@ deduplicated_texts = semhash.self_deduplicate()
374
374
 
375
375
  </details>
376
376
 
377
+
378
+
379
+
380
+ <details>
381
+ <summary> Using custom ANN backends </summary>
382
+ <br>
383
+
384
+ The following code snippet shows how to use a custom ANN backend and custom args with SemHash:
385
+
386
+ ```python
387
+ from datasets import load_dataset
388
+ from semhash import SemHash
389
+ from vicinity import Backend
390
+
391
+ # Load a dataset to deduplicate
392
+ texts = load_dataset("ag_news", split="train")["text"]
393
+
394
+ # Initialize a SemHash with the model and custom ann backend and custom args
395
+ semhash = SemHash.from_records(records=texts, ann_backend=Backend.FAISS, nlist=50)
396
+
397
+ # Deduplicate the texts
398
+ deduplicated_texts = semhash.self_deduplicate()
399
+ ```
400
+
401
+ For the full list of supported ANN backends and args, see the [Vicinity docs](https://github.com/MinishLab/vicinity/tree/main?tab=readme-ov-file#supported-backends).
402
+
403
+ </details>
404
+
405
+
377
406
  <details>
378
407
  <summary> Using Pandas DataFrames </summary>
379
408
  <br>
@@ -473,7 +502,7 @@ To run the benchmarks yourself, you can use the following command (assuming you
473
502
  ```bash
474
503
  python -m benchmarks.run_benchmarks
475
504
  ```
476
- Optionally, the datasets can be updated in the [datasets.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/datasets.py) file.
505
+ Optionally, the datasets can be updated in the [data.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/data.py) file.
477
506
 
478
507
  ## License
479
508
 
@@ -1,5 +1,5 @@
1
1
  model2vec>=0.3.4
2
- vicinity[usearch]>=0.4.0
2
+ vicinity[usearch]>=0.4.3
3
3
  frozendict
4
4
 
5
5
  [dev]