MaartenGr / KeyBERT

Minimal keyword extraction with BERT
https://MaartenGr.github.io/KeyBERT/
MIT License
3.31k stars 337 forks source link

ImportError: cannot import name 'PartialState' from 'accelerate.state' (/opt/conda/lib/python3.10/site-packages/accelerate/state.py) #176

Closed vrunm closed 10 months ago

vrunm commented 12 months ago

While running this

from keybert import KeyBERT

doc = """
         Supervised learning is the machine learning task of learning a function that
         maps an input to an output based on example input-output pairs. It infers a
         function from labeled training data consisting of a set of training examples.
         In supervised learning, each example is a pair consisting of an input object
         (typically a vector) and a desired output value (also called the supervisory signal).
         A supervised learning algorithm analyzes the training data and produces an inferred function,
         which can be used for mapping new examples. An optimal scenario will allow for the
         algorithm to correctly determine the class labels for unseen instances. This requires
         the learning algorithm to generalize from the training data to unseen situations in a
         'reasonable' way (see inductive bias).
      """
kw_model = KeyBERT()
keywords = kw_model.extract_keywords(doc)

Got this error:

/opt/conda/lib/python3.10/site-packages/tensorflow_io/python/ops/__init__.py:98: UserWarning: unable to load libtensorflow_io_plugins.so: unable to open file: libtensorflow_io_plugins.so, from paths: ['/opt/conda/lib/python3.10/site-packages/tensorflow_io/python/ops/libtensorflow_io_plugins.so']
caused by: ['/opt/conda/lib/python3.10/site-packages/tensorflow_io/python/ops/libtensorflow_io_plugins.so: undefined symbol: _ZN3tsl6StatusC1EN10tensorflow5error4CodeESt17basic_string_viewIcSt11char_traitsIcEENS_14SourceLocationE']
  warnings.warn(f"unable to load libtensorflow_io_plugins.so: {e}")
/opt/conda/lib/python3.10/site-packages/tensorflow_io/python/ops/__init__.py:104: UserWarning: file system plugins are not loaded: unable to open file: libtensorflow_io.so, from paths: ['/opt/conda/lib/python3.10/site-packages/tensorflow_io/python/ops/libtensorflow_io.so']
caused by: ['/opt/conda/lib/python3.10/site-packages/tensorflow_io/python/ops/libtensorflow_io.so: undefined symbol: _ZTVN10tensorflow13GcsFileSystemE']
  warnings.warn(f"file system plugins are not loaded: {e}")

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[2], line 1
----> 1 from keybert import KeyBERT
      3 doc = """
      4          Supervised learning is the machine learning task of learning a function that
      5          maps an input to an output based on example input-output pairs. It infers a
   (...)
     13          'reasonable' way (see inductive bias).
     14       """
     15 kw_model = KeyBERT()

File /opt/conda/lib/python3.10/site-packages/keybert/__init__.py:1
----> 1 from keybert._model import KeyBERT
      3 __version__ = "0.7.0"

File /opt/conda/lib/python3.10/site-packages/keybert/_model.py:16
     14 from keybert._maxsum import max_sum_distance
     15 from keybert._highlight import highlight_document
---> 16 from keybert.backend._utils import select_backend
     19 class KeyBERT:
     20     """
     21     A minimal method for keyword extraction with BERT
     22 
   (...)
     36     </div>
     37     """

File /opt/conda/lib/python3.10/site-packages/keybert/backend/_utils.py:3
      1 from ._base import BaseEmbedder
      2 from ._sentencetransformers import SentenceTransformerBackend
----> 3 from ._hftransformers import HFTransformerBackend
      4 from transformers.pipelines import Pipeline
      7 def select_backend(embedding_model) -> BaseEmbedder:

File /opt/conda/lib/python3.10/site-packages/keybert/backend/_hftransformers.py:7
      5 from torch.utils.data import Dataset
      6 from sklearn.preprocessing import normalize
----> 7 from transformers.pipelines import Pipeline
      9 from keybert.backend import BaseEmbedder
     12 class HFTransformerBackend(BaseEmbedder):

File /opt/conda/lib/python3.10/site-packages/transformers/pipelines/__init__.py:44
     34 from ..tokenization_utils import PreTrainedTokenizer
     35 from ..utils import (
     36     HUGGINGFACE_CO_RESOLVE_ENDPOINT,
     37     is_kenlm_available,
   (...)
     42     logging,
     43 )
---> 44 from .audio_classification import AudioClassificationPipeline
     45 from .automatic_speech_recognition import AutomaticSpeechRecognitionPipeline
     46 from .base import (
     47     ArgumentHandler,
     48     CsvPipelineDataFormat,
   (...)
     56     infer_framework_load_model,
     57 )

File /opt/conda/lib/python3.10/site-packages/transformers/pipelines/audio_classification.py:21
     18 import requests
     20 from ..utils import add_end_docstrings, is_torch_available, logging
---> 21 from .base import PIPELINE_INIT_ARGS, Pipeline
     24 if is_torch_available():
     25     from ..models.auto.modeling_auto import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

File /opt/conda/lib/python3.10/site-packages/transformers/pipelines/base.py:35
     33 from ..feature_extraction_utils import PreTrainedFeatureExtractor
     34 from ..image_processing_utils import BaseImageProcessor
---> 35 from ..modelcard import ModelCard
     36 from ..models.auto.configuration_auto import AutoConfig
     37 from ..tokenization_utils import PreTrainedTokenizer

File /opt/conda/lib/python3.10/site-packages/transformers/modelcard.py:48
     31 from . import __version__
     32 from .models.auto.modeling_auto import (
     33     MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES,
     34     MODEL_FOR_CAUSAL_LM_MAPPING_NAMES,
   (...)
     46     MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMES,
     47 )
---> 48 from .training_args import ParallelMode
     49 from .utils import (
     50     MODEL_CARD_NAME,
     51     cached_file,
   (...)
     57     logging,
     58 )
     61 TASK_MAPPING = {
     62     "text-generation": MODEL_FOR_CAUSAL_LM_MAPPING_NAMES,
     63     "image-classification": MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES,
   (...)
     74     "zero-shot-image-classification": MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMES,
     75 }

File /opt/conda/lib/python3.10/site-packages/transformers/training_args.py:67
     64     import torch.distributed as dist
     66 if is_accelerate_available():
---> 67     from accelerate.state import AcceleratorState, PartialState
     68     from accelerate.utils import DistributedType
     70 if is_torch_tpu_available(check_device=False):

ImportError: cannot import name 'PartialState' from 'accelerate.state' (/opt/conda/lib/python3.10/site-packages/accelerate/state.py)
MaartenGr commented 12 months ago

I might be mistaken here but it might be a result of an outdated Kaggle notebook, which has happened to me before. It should be resolved with !pip install -U accelerate.