-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path__init__.py
More file actions
294 lines (237 loc) · 8.49 KB
/
__init__.py
File metadata and controls
294 lines (237 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""
DataFog: Lightning-fast PII detection and anonymization library.
Core package provides regex-based PII detection with 190x performance advantage.
Optional extras available for advanced features:
- pip install datafog[nlp] - for spaCy integration
- pip install datafog[ocr] - for image/OCR processing
- pip install datafog[all] - for all features
"""
from .__about__ import __version__
# Core API functions - always available (lightweight)
from .core import anonymize_text, detect_pii, get_supported_entities, scan_text
# Essential models - always available
from .models.common import EntityTypes
# Conditional imports for better lightweight performance
def _lazy_import_core_models():
"""Lazy import of core models to reduce startup time."""
global AnnotationResult, AnnotatorRequest, AnonymizationResult
global Anonymizer, AnonymizerRequest, AnonymizerType
if "AnnotationResult" not in globals():
from .models.annotator import AnnotationResult, AnnotatorRequest
from .models.anonymizer import (
AnonymizationResult,
Anonymizer,
AnonymizerRequest,
AnonymizerType,
)
globals().update(
{
"AnnotationResult": AnnotationResult,
"AnnotatorRequest": AnnotatorRequest,
"AnonymizationResult": AnonymizationResult,
"Anonymizer": Anonymizer,
"AnonymizerRequest": AnonymizerRequest,
"AnonymizerType": AnonymizerType,
}
)
def _lazy_import_regex_annotator():
"""Lazy import of regex annotator to reduce startup time."""
global RegexAnnotator
if "RegexAnnotator" not in globals():
from .processing.text_processing.regex_annotator import RegexAnnotator
globals()["RegexAnnotator"] = RegexAnnotator
# Optional imports with graceful fallback
try:
from .client import app
except ImportError:
app = None
try:
from .main import DataFog, TextPIIAnnotator
except ImportError:
DataFog = None
TextPIIAnnotator = None
try:
from .services.text_service import TextService
except ImportError:
TextService = None
def __getattr__(name: str):
"""Handle lazy imports for better lightweight performance."""
# Lazy import core models when first accessed
if name in {
"AnnotationResult",
"AnnotatorRequest",
"AnonymizationResult",
"Anonymizer",
"AnonymizerRequest",
"AnonymizerType",
}:
_lazy_import_core_models()
return globals()[name]
# Lazy import regex annotator when first accessed
elif name == "RegexAnnotator":
_lazy_import_regex_annotator()
return globals()[name]
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
# Optional heavy features - only import if dependencies available
def _optional_import(name, module_path, extra_name):
"""Helper to import optional modules with helpful error messages."""
try:
module = __import__(module_path, fromlist=[name])
return getattr(module, name)
except ImportError:
def _missing_dependency(*args, **kwargs):
raise ImportError(
f"{name} requires additional dependencies. "
f"Install with: pip install datafog[{extra_name}]"
)
return _missing_dependency
# OCR/Image processing - requires 'ocr' extra
DonutProcessor = _optional_import(
"DonutProcessor", "datafog.processing.image_processing.donut_processor", "ocr"
)
PytesseractProcessor = _optional_import(
"PytesseractProcessor",
"datafog.processing.image_processing.pytesseract_processor",
"ocr",
)
ImageService = _optional_import("ImageService", "datafog.services.image_service", "ocr")
# NLP processing - requires 'nlp' extra
SpacyPIIAnnotator = _optional_import(
"SpacyPIIAnnotator", "datafog.processing.text_processing.spacy_pii_annotator", "nlp"
)
# Distributed processing - requires 'distributed' extra
SparkService = _optional_import(
"SparkService", "datafog.services.spark_service", "distributed"
)
# Simple API for core functionality (backward compatibility)
def detect(text: str) -> list:
"""
Detect PII in text using regex patterns.
Args:
text: Input text to scan for PII
Returns:
List of detected PII entities
Example:
>>> from datafog import detect
>>> detect("Contact john@example.com")
[{'type': 'EMAIL', 'value': 'john@example.com', 'start': 8, 'end': 24}]
"""
import time as _time
_start = _time.monotonic()
_lazy_import_regex_annotator()
annotator = RegexAnnotator()
# Use the structured output to get proper positions
_, result = annotator.annotate_with_spans(text)
# Convert to simple format, filtering out empty matches
entities = []
for span in result.spans:
if span.text.strip(): # Only include non-empty matches
entities.append(
{
"type": span.label,
"value": span.text,
"start": span.start,
"end": span.end,
}
)
try:
from .telemetry import (
_get_duration_bucket,
_get_text_length_bucket,
track_function_call,
)
_duration = (_time.monotonic() - _start) * 1000
entity_types = list({e["type"] for e in entities})
track_function_call(
function_name="detect",
module="datafog",
engine="regex",
text_length_bucket=_get_text_length_bucket(len(text)),
entity_count=len(entities),
entity_types_found=entity_types,
duration_ms_bucket=_get_duration_bucket(_duration),
)
except Exception:
pass
return entities
def process(text: str, anonymize: bool = False, method: str = "redact") -> dict:
"""
Process text to detect and optionally anonymize PII.
Args:
text: Input text to process
anonymize: Whether to anonymize detected PII
method: Anonymization method ('redact', 'replace', 'hash')
Returns:
Dictionary with original text, anonymized text (if requested), and findings
Example:
>>> from datafog import process
>>> process("Contact john@example.com", anonymize=True)
{
'original': 'Contact john@example.com',
'anonymized': 'Contact [EMAIL_REDACTED]',
'findings': [{'type': 'EMAIL', 'value': 'john@example.com', ...}]
}
"""
import time as _time
_start = _time.monotonic()
findings = detect(text)
result = {"original": text, "findings": findings}
if anonymize:
anonymized = text
# Simple anonymization - replace from end to start to preserve positions
for finding in sorted(findings, key=lambda x: x["start"], reverse=True):
start, end = finding["start"], finding["end"]
entity_type = finding["type"]
if method == "redact":
replacement = f"[{entity_type}_REDACTED]"
elif method == "replace":
replacement = f"[{entity_type}_XXXXX]"
elif method == "hash":
import hashlib
replacement = f"[{entity_type}_{hashlib.md5(finding['value'].encode()).hexdigest()[:8]}]"
else:
replacement = f"[{entity_type}]"
anonymized = anonymized[:start] + replacement + anonymized[end:]
result["anonymized"] = anonymized
try:
from .telemetry import _get_duration_bucket, track_function_call
_duration = (_time.monotonic() - _start) * 1000
track_function_call(
function_name="process",
module="datafog",
anonymize=anonymize,
method=method,
entity_count=len(findings),
duration_ms_bucket=_get_duration_bucket(_duration),
)
except Exception:
pass
return result
# Core exports
__all__ = [
"__version__",
"detect",
"process",
"detect_pii",
"anonymize_text",
"scan_text",
"get_supported_entities",
"AnnotationResult",
"AnnotatorRequest",
"AnonymizationResult",
"Anonymizer",
"AnonymizerRequest",
"AnonymizerType",
"EntityTypes",
"RegexAnnotator",
# Optional exports (may be None if dependencies missing)
"DataFog",
"TextPIIAnnotator",
"TextService",
"app",
"DonutProcessor",
"PytesseractProcessor",
"ImageService",
"SpacyPIIAnnotator",
"SparkService",
]