|
77 | 77 | from collections.abc import Sequence |
78 | 78 | from dataclasses import dataclass |
79 | 79 | from enum import Enum |
80 | | -from typing import TYPE_CHECKING, Any, Final |
| 80 | +from typing import Any, Final |
81 | 81 |
|
| 82 | +from presidio_analyzer import AnalyzerEngine, RecognizerResult |
| 83 | +from presidio_analyzer.nlp_engine import NlpEngineProvider |
82 | 84 | from pydantic import BaseModel, ConfigDict, Field |
83 | 85 |
|
84 | 86 | from guardrails.registry import default_spec_registry |
|
89 | 91 |
|
90 | 92 | logger = logging.getLogger(__name__) |
91 | 93 |
|
92 | | -if TYPE_CHECKING: |
93 | | - from presidio_analyzer import AnalyzerEngine, AnalyzerResult |
94 | | - |
95 | 94 |
|
96 | 95 | @functools.lru_cache(maxsize=1) |
97 | 96 | def _get_analyzer_engine() -> AnalyzerEngine: |
98 | 97 | """Return a cached, configured Presidio AnalyzerEngine instance. |
99 | 98 |
|
100 | 99 | Returns: |
101 | 100 | AnalyzerEngine: Initialized Presidio analyzer engine. |
102 | | -
|
103 | | - Raises: |
104 | | - ImportError: If required Presidio packages are not installed. |
105 | 101 | """ |
106 | | - try: |
107 | | - from presidio_analyzer import AnalyzerEngine |
108 | | - from presidio_analyzer.nlp_engine import NlpEngineProvider |
109 | | - except ImportError as e: |
110 | | - logger.error("Failed to import Presidio analyzer: %s", e) |
111 | | - raise ImportError("Presidio analyzer package is required") from e |
112 | | - |
113 | 102 | # Define a smaller NLP configuration |
114 | 103 | sm_nlp_config: Final[dict[str, Any]] = { |
115 | 104 | "nlp_engine_name": "spacy", |
@@ -226,11 +215,11 @@ class PiiDetectionResult: |
226 | 215 |
|
227 | 216 | Attributes: |
228 | 217 | mapping (dict[str, list[str]]): Mapping from entity type to list of detected strings. |
229 | | - analyzer_results (Sequence[AnalyzerResult]): Raw analyzer results for position information. |
| 218 | + analyzer_results (Sequence[RecognizerResult]): Raw analyzer results for position information. |
230 | 219 | """ |
231 | 220 |
|
232 | 221 | mapping: dict[str, list[str]] |
233 | | - analyzer_results: Sequence[AnalyzerResult] |
| 222 | + analyzer_results: Sequence[RecognizerResult] |
234 | 223 |
|
235 | 224 | def to_dict(self) -> dict[str, list[str]]: |
236 | 225 | """Convert the result to a dictionary. |
|
0 commit comments