piitag 0.1.0__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.
piitag/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ """Python interface for the Desert Ant Labs Redact PII detector."""
2
+
3
+ from .model import Item, Label, Options, Redact, Redaction
4
+
5
+ __all__ = ["Item", "Label", "Options", "Redact", "Redaction"]
piitag/_assets.py ADDED
@@ -0,0 +1,67 @@
1
+ """Resolve the pinned Redact model assets."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+ from pathlib import Path
7
+
8
+ from huggingface_hub import hf_hub_download
9
+
10
+ REPOSITORY = "desert-ant-labs/redact"
11
+ REVISION = "v0.4.0"
12
+
13
+
14
+ @dataclass(frozen=True)
15
+ class Assets:
16
+ """Paths to the files required by the Redact model."""
17
+
18
+ tokenizer: Path
19
+ labels: Path
20
+ tflite: Path
21
+
22
+ @property
23
+ def directory(self) -> Path:
24
+ """Return the common directory when all assets share one."""
25
+ return self.tokenizer.parent
26
+
27
+
28
+ ASSET_FILENAMES = {
29
+ "tokenizer": "redact_tokenizer.bin",
30
+ "labels": "labels.json",
31
+ "tflite": "redact.tflite",
32
+ }
33
+
34
+
35
+ def _from_directory(directory: Path) -> Assets:
36
+ paths = {name: directory / filename for name, filename in ASSET_FILENAMES.items()}
37
+ missing = [str(path) for path in paths.values() if not path.is_file()]
38
+ if missing:
39
+ raise FileNotFoundError(
40
+ "Redact model assets are missing from the supplied directory: "
41
+ + ", ".join(missing)
42
+ )
43
+ return Assets(**paths)
44
+
45
+
46
+ def resolve_assets(directory: str | Path | None = None) -> Assets:
47
+ """Resolve local assets or download the pinned files into the HF cache.
48
+
49
+ A supplied directory is strictly offline: it must already contain all
50
+ three files. Without a directory, ``huggingface_hub`` manages the cache.
51
+ """
52
+ if directory is not None:
53
+ return _from_directory(Path(directory))
54
+ downloaded = {
55
+ name: Path(
56
+ hf_hub_download(
57
+ repo_id=REPOSITORY,
58
+ filename=filename,
59
+ revision=REVISION,
60
+ )
61
+ )
62
+ for name, filename in ASSET_FILENAMES.items()
63
+ }
64
+ return Assets(**downloaded)
65
+
66
+
67
+ __all__ = ["ASSET_FILENAMES", "Assets", "resolve_assets"]