-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathclient.py
More file actions
370 lines (296 loc) · 10.2 KB
/
client.py
File metadata and controls
370 lines (296 loc) · 10.2 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""
Client module for DataFog.
Provides CLI commands for scanning images and text using DataFog's OCR and PII detection capabilities.
"""
import asyncio
import logging
from typing import List
import typer
from .config import OperationType, get_config
from .main import DataFog
from .models.anonymizer import Anonymizer, AnonymizerType, HashType
from .models.spacy_nlp import SpacyAnnotator
app = typer.Typer()
@app.command()
def scan_image(
image_urls: List[str] = typer.Argument(
None, help="List of image URLs or file paths to extract text from"
),
operations: str = typer.Option("scan", help="Operation to perform"),
):
"""
Scan images for text and PII.
Extracts text from images using OCR, then detects PII entities.
Handles both remote URLs and local file paths.
Args:
image_urls: List of image URLs or file paths
operations: Pipeline operations to run (default: scan)
Prints results or exits with error on failure.
"""
if not image_urls:
typer.echo("No image URLs or file paths provided. Please provide at least one.")
raise typer.Exit(code=1)
logging.basicConfig(level=logging.INFO)
# Convert comma-separated string operations to a list of OperationType objects
operation_list = [OperationType(op.strip()) for op in operations.split(",")]
ocr_client = DataFog(operations=operation_list)
try:
results = asyncio.run(ocr_client.run_ocr_pipeline(image_urls=image_urls))
typer.echo(f"OCR Pipeline Results: {results}")
try:
from .telemetry import track_function_call
track_function_call(
function_name="scan_image",
module="datafog.client",
source="cli",
batch_size=len(image_urls),
)
except Exception:
pass
except Exception as e:
logging.exception("Error in run_ocr_pipeline")
try:
from .telemetry import track_error
track_error("scan_image", type(e).__name__, source="cli")
except Exception:
pass
typer.echo(f"Error: {str(e)}", err=True)
raise typer.Exit(code=1)
@app.command()
def scan_text(
str_list: List[str] = typer.Argument(
None, help="List of texts to extract text from"
),
operations: str = typer.Option("scan", help="Operation to perform"),
):
"""
Scan texts for PII.
Detects PII entities in a list of input texts.
Args:
str_list: List of texts to analyze
operations: Pipeline operations to run (default: scan)
Prints results or exits with error on failure.
"""
if not str_list:
typer.echo("No texts provided.")
raise typer.Exit(code=1)
logging.basicConfig(level=logging.INFO)
# Convert comma-separated string operations to a list of OperationType objects
operation_list = [OperationType(op.strip()) for op in operations.split(",")]
text_client = DataFog(operations=operation_list)
try:
results = text_client.run_text_pipeline_sync(str_list=str_list)
typer.echo(f"Text Pipeline Results: {results}")
try:
from .telemetry import track_function_call
track_function_call(
function_name="scan_text",
module="datafog.client",
source="cli",
batch_size=len(str_list),
operations=[op.value for op in operation_list],
)
except Exception:
pass
except Exception as e:
logging.exception("Text pipeline error")
try:
from .telemetry import track_error
track_error("scan_text", type(e).__name__, source="cli")
except Exception:
pass
typer.echo(f"Error: {str(e)}", err=True)
raise typer.Exit(code=1)
@app.command()
def health():
"""
Check DataFog service health.
Prints a message indicating that DataFog is running.
"""
typer.echo("DataFog is running.")
@app.command()
def show_config():
"""
Show current configuration.
Prints the current DataFog configuration.
"""
typer.echo(get_config())
@app.command()
def download_model(
model_name: str = typer.Argument(..., help="Model to download"),
engine: str = typer.Option("spacy", help="Engine type (spacy, gliner)"),
):
"""
Download a model for specified engine.
Examples:
spaCy: datafog download-model en_core_web_sm --engine spacy
GLiNER: datafog download-model urchade/gliner_multi_pii-v1 --engine gliner
"""
if engine == "spacy":
SpacyAnnotator.download_model(model_name)
typer.echo(f"SpaCy model {model_name} downloaded successfully.")
elif engine == "gliner":
try:
from datafog.processing.text_processing.gliner_annotator import (
GLiNERAnnotator,
)
GLiNERAnnotator.download_model(model_name)
typer.echo(f"GLiNER model {model_name} downloaded and cached successfully.")
except ImportError:
typer.echo(
"GLiNER not available. Install with: pip install datafog[nlp-advanced]"
)
raise typer.Exit(code=1)
except Exception as e:
typer.echo(f"Error downloading GLiNER model {model_name}: {str(e)}")
raise typer.Exit(code=1)
else:
typer.echo(f"Unknown engine: {engine}. Supported engines: spacy, gliner")
raise typer.Exit(code=1)
@app.command()
def show_spacy_model_directory(
model_name: str = typer.Argument(None, help="Model to check")
):
"""
Show the directory path for a spaCy model.
Args:
model_name: Name of the model to check.
Prints the directory path of the specified model.
"""
if not model_name:
typer.echo("No model name provided to check.")
raise typer.Exit(code=1)
annotator = SpacyAnnotator(model_name)
typer.echo(annotator.show_model_path())
@app.command()
def list_spacy_models():
"""
List available spaCy models.
Prints a list of all available spaCy models.
"""
annotator = SpacyAnnotator()
typer.echo(annotator.list_models())
@app.command()
def list_models(
engine: str = typer.Option(
"spacy", help="Engine to list models for (spacy, gliner)"
)
):
"""
List available models for specified engine.
Examples:
datafog list-models --engine spacy
datafog list-models --engine gliner
"""
if engine == "spacy":
annotator = SpacyAnnotator()
typer.echo("Available spaCy models:")
typer.echo(annotator.list_models())
elif engine == "gliner":
typer.echo("Popular GLiNER models:")
models = [
"urchade/gliner_base (recommended starting point)",
"urchade/gliner_multi_pii-v1 (specialized for PII detection)",
"urchade/gliner_large-v2 (higher accuracy)",
"knowledgator/modern-gliner-bi-large-v1.0 (4x faster, modern)",
"urchade/gliner_medium-v2.1 (balanced size/performance)",
]
for model in models:
typer.echo(f" • {model}")
typer.echo("\nSee more at: https://huggingface.co/models?search=gliner")
else:
typer.echo(f"Unknown engine: {engine}. Supported engines: spacy, gliner")
raise typer.Exit(code=1)
@app.command()
def list_entities():
"""
List available entities.
Prints a list of all available entities that can be recognized.
"""
annotator = SpacyAnnotator()
typer.echo(annotator.list_entities())
@app.command()
def redact_text(text: str = typer.Argument(None, help="Text to redact")):
"""
Redact PII in text.
Args:
text: Text to redact.
Prints the redacted text.
"""
if not text:
typer.echo("No text provided to redact.")
raise typer.Exit(code=1)
annotator = SpacyAnnotator()
anonymizer = Anonymizer(anonymizer_type=AnonymizerType.REDACT)
annotations = annotator.annotate_text(text)
result = anonymizer.anonymize(text, annotations)
typer.echo(result.anonymized_text)
try:
from .telemetry import track_function_call
track_function_call(
function_name="redact_text",
module="datafog.client",
source="cli",
method="redact",
)
except Exception:
pass
@app.command()
def replace_text(text: str = typer.Argument(None, help="Text to replace PII")):
"""
Replace PII in text with anonymized values.
Args:
text: Text to replace PII.
Prints the text with PII replaced.
"""
if not text:
typer.echo("No text provided to replace PII.")
raise typer.Exit(code=1)
annotator = SpacyAnnotator()
anonymizer = Anonymizer(anonymizer_type=AnonymizerType.REPLACE)
annotations = annotator.annotate_text(text)
result = anonymizer.anonymize(text, annotations)
typer.echo(result.anonymized_text)
try:
from .telemetry import track_function_call
track_function_call(
function_name="replace_text",
module="datafog.client",
source="cli",
method="replace",
)
except Exception:
pass
@app.command()
def hash_text(
text: str = typer.Argument(None, help="Text to hash PII"),
hash_type: HashType = typer.Option(HashType.SHA256, help="Hash algorithm to use"),
):
"""
Choose from SHA256, MD5, or SHA3-256 algorithms to hash detected PII in text.
Args:
text: Text to hash PII.
hash_type: Hash algorithm to use.
Prints the text with PII hashed.
"""
if not text:
typer.echo("No text provided to hash.")
raise typer.Exit(code=1)
annotator = SpacyAnnotator()
anonymizer = Anonymizer(anonymizer_type=AnonymizerType.HASH, hash_type=hash_type)
annotations = annotator.annotate_text(text)
result = anonymizer.anonymize(text, annotations)
typer.echo(result.anonymized_text)
try:
from .telemetry import track_function_call
track_function_call(
function_name="hash_text",
module="datafog.client",
source="cli",
method="hash",
hash_type=hash_type.value,
)
except Exception:
pass
if __name__ == "__main__":
app()