Skip to content

Commit

Permalink
fix: put HFTokenStreamingHandler in a lazy_import block (#7403)
Browse files Browse the repository at this point in the history
* put HFTokenStreamingHandler in a lazy_import block

* fix pylint
  • Loading branch information
anakin87 committed Mar 22, 2024
1 parent c789f90 commit d594445
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 6 additions & 2 deletions haystack/components/generators/hugging_face_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
deserialize_secrets_inplace,
serialize_callable,
)
from haystack.utils.hf import HFTokenStreamingHandler, deserialize_hf_model_kwargs, serialize_hf_model_kwargs
from haystack.utils.hf import deserialize_hf_model_kwargs, serialize_hf_model_kwargs

logger = logging.getLogger(__name__)

Expand All @@ -19,7 +19,11 @@
with LazyImport(message="Run 'pip install transformers[torch]'") as transformers_import:
from transformers import StoppingCriteriaList, pipeline

from haystack.utils.hf import StopWordsCriteria, resolve_hf_pipeline_kwargs # pylint: disable=ungrouped-imports
from haystack.utils.hf import ( # pylint: disable=ungrouped-imports
HFTokenStreamingHandler,
StopWordsCriteria,
resolve_hf_pipeline_kwargs,
)


@component
Expand Down
3 changes: 1 addition & 2 deletions haystack/utils/hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ def check_generation_params(kwargs: Optional[Dict[str, Any]], additional_accepte
with LazyImport(message="Run 'pip install transformers[torch]'") as torch_and_transformers_import:
from transformers import PreTrainedTokenizer, PreTrainedTokenizerFast, StoppingCriteria, TextStreamer

transformers_import.check()
torch_import.check()
torch_and_transformers_import.check()

class StopWordsCriteria(StoppingCriteria):
"""
Expand Down

0 comments on commit d594445

Please sign in to comment.