@@ -122,11 +122,20 @@ def zero_shot_classification(
122
122
model_name: The name of the zero-shot model from Hugging Face Hub.
123
123
name: Name of the scorer.
124
124
"""
125
- if not _TRANSFORMERS_AVAILABLE :
126
- warn_at_user_stacklevel(_TRANSFORMERS_ERROR_MSG , UserWarning )
125
+ transformers_error_msg = (
126
+ " Hugging Face transformers dependency is not installed. "
127
+ " Please install with: pip install transformers torch"
128
+ )
129
+
130
+ try :
131
+ from transformers import ( # type: ignore [ attr -defined , import -not -found , unused -ignore ]
132
+ pipeline,
133
+ )
134
+ except ImportError :
135
+ warn_at_user_stacklevel(transformers_error_msg, UserWarning )
127
136
128
137
def disabled_evaluate (_ : t.Any) -> Metric:
129
- return Metric(value = 0.0 , attributes = {" error" : _TRANSFORMERS_ERROR_MSG })
138
+ return Metric(value = 0.0 , attributes = {" error" : transformers_error_msg })
130
139
131
140
return Scorer.from_callable(disabled_evaluate, name = name)
132
141
@@ -816,7 +825,7 @@ def detect_harm_with_openai(
816
825
* ,
817
826
api_key : str | None = None ,
818
827
model : t.Literal[" text-moderation-stable" , " text-moderation-latest" ] = " text-moderation-stable" ,
819
- client : openai.AsyncOpenAI | None = None ,
828
+ client : " openai.AsyncOpenAI | None" = None ,
820
829
name : str = " openai_harm" ,
821
830
) -> " Scorer[t.Any]" :
822
831
"""
@@ -837,6 +846,7 @@ def detect_harm_with_openai(
837
846
model: The moderation model to use.
838
847
name: Name of the scorer.
839
848
"""
849
+ import openai
840
850
841
851
async def evaluate (data : t.Any) -> Metric:
842
852
text = str (data)
@@ -1800,12 +1810,18 @@ def detect_pii_with_presidio(
1800
1810
invert: Invert the score (1.0 for no PII, 0.0 for PII detected).
1801
1811
name: Name of the scorer.
1802
1812
"""
1813
+ presidio_import_error_msg = (
1814
+ " Presidio dependencies are not installed. "
1815
+ " Please install them with: pip install presidio-analyzer presidio-anonymizer 'spacy[en_core_web_lg]'"
1816
+ )
1803
1817
1804
- if not _PRESIDIO_AVAILABLE :
1805
- warn_at_user_stacklevel(_PRESIDIO_ERROR_MSG , UserWarning )
1818
+ try :
1819
+ import presidio_analyzer # type: ignore [ import -not -found , unused -ignore ] # noqa: F401
1820
+ except ImportError :
1821
+ warn_at_user_stacklevel(presidio_import_error_msg, UserWarning )
1806
1822
1807
1823
def disabled_evaluate (_ : t.Any) -> Metric:
1808
- return Metric(value = 0.0 , attributes = {" error" : _PRESIDIO_ERROR_MSG })
1824
+ return Metric(value = 0.0 , attributes = {" error" : presidio_import_error_msg })
1809
1825
1810
1826
return Scorer.from_callable(disabled_evaluate, name = name)
1811
1827
0 commit comments