@@ -122,11 +122,20 @@ def zero_shot_classification(
122122 model_name: The name of the zero-shot model from Hugging Face Hub.
123123 name: Name of the scorer.
124124 """
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 )
127136
128137 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 })
130139
131140 return Scorer.from_callable(disabled_evaluate, name = name)
132141
@@ -816,7 +825,7 @@ def detect_harm_with_openai(
816825 * ,
817826 api_key : str | None = None ,
818827 model : t.Literal[" text-moderation-stable" , " text-moderation-latest" ] = " text-moderation-stable" ,
819- client : openai.AsyncOpenAI | None = None ,
828+ client : " openai.AsyncOpenAI | None" = None ,
820829 name : str = " openai_harm" ,
821830) -> " Scorer[t.Any]" :
822831 """
@@ -837,6 +846,7 @@ def detect_harm_with_openai(
837846 model: The moderation model to use.
838847 name: Name of the scorer.
839848 """
849+ import openai
840850
841851 async def evaluate (data : t.Any) -> Metric:
842852 text = str (data)
@@ -1800,12 +1810,18 @@ def detect_pii_with_presidio(
18001810 invert: Invert the score (1.0 for no PII, 0.0 for PII detected).
18011811 name: Name of the scorer.
18021812 """
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+ )
18031817
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 )
18061822
18071823 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 })
18091825
18101826 return Scorer.from_callable(disabled_evaluate, name = name)
18111827
0 commit comments