While i was trying to perform sentiment analysis using transformers, then i parsed the pipeline function. Unfortunately, I ran into a runtime error: Failed to import transformers.models.distilbert.modeling_tf_distilbert because of the following error (look up to see its traceback): cannot import name 'type_spec_registry' from 'tensorflow.python.framework'
This is my code:`
from transformers import pipeline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score, f1_score, confusion_matrix
#Basic usage classifier = pipeline('sentiment-analysis')
This was the error i got;
ImportError Traceback (most recent call last)
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\transformers\utils\import_utils.py:1099, in _LazyModule._get_module(self, module_name)
1098 try:
-> 1099 return importlib.import_module("." + module_name, self.__name__)
1100 except Exception as e:
File ~\anaconda3\envs\Asiwajuflow\lib\importlib\__init__.py:127, in import_module(name, package)
126 level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)
File <frozen importlib._bootstrap>:1030, in _gcd_import(name, package, level)
File <frozen importlib._bootstrap>:1007, in _find_and_load(name, import_)
File <frozen importlib._bootstrap>:986, in _find_and_load_unlocked(name, import_)
File <frozen importlib._bootstrap>:680, in _load_unlocked(spec)
File <frozen importlib._bootstrap_external>:850, in exec_module(self, module)
File <frozen importlib._bootstrap>:228, in _call_with_frames_removed(f, *args, **kwds)
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\transformers\models\distilbert\modeling_tf_distilbert.py:28
26 import tensorflow as tf
---> 28 from ...activations_tf import get_tf_activation
29 from ...modeling_tf_outputs import (
30 TFBaseModelOutput,
31 TFMaskedLMOutput,
(...)
35 TFTokenClassifierOutput,
36 )
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\transformers\activations_tf.py:107
105 return tf.keras.activations.gelu(x, approximate=True)
--> 107 gelu = tf.keras.activations.gelu
108 gelu_new = approximate_gelu_wrap
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\tensorflow\python\util\lazy_loader.py:58, in LazyLoader.__getattr__(self, item)
57 def __getattr__(self, item):
---> 58 module = self._load()
59 return getattr(module, item)
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\tensorflow\python\util\lazy_loader.py:41, in LazyLoader._load(self)
40 # Import the target module and insert it into the parent's namespace
---> 41 module = importlib.import_module(self.__name__)
42 self._parent_module_globals[self._local_name] = module
File ~\anaconda3\envs\Asiwajuflow\lib\importlib\__init__.py:127, in import_module(name, package)
126 level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\__init__.py:20
15 """Implementation of the Keras API, the high-level API of TensorFlow.
16
17 Detailed documentation and user guides are available at
18 [keras.io](https://keras.io).
19 """
---> 20 from keras import distribute
21 from keras import models
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\distribute\__init__.py:18
15 """Keras' Distribution Strategy library."""
---> 18 from keras.distribute import sidecar_evaluator
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\distribute\sidecar_evaluator.py:22
21 from tensorflow.python.util import deprecation
---> 22 from keras.optimizers.optimizer_experimental import (
23 optimizer as optimizer_experimental,
24 )
25 from tensorflow.python.util.tf_export import keras_export
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\optimizers\__init__.py:25
24 # Imports needed for deserialization.
---> 25 from keras import backend
26 from keras.optimizers.legacy import adadelta as adadelta_legacy
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\backend\__init__.py:3
1 """AUTOGENERATED. DO NOT EDIT."""
----> 3 from keras.backend import experimental
4 from keras.src.backend import abs
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\backend\experimental\__init__.py:3
1 """AUTOGENERATED. DO NOT EDIT."""
----> 3 from keras.src.backend import disable_tf_random_generator
4 from keras.src.backend import enable_tf_random_generator
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\src\__init__.py:21
20 from keras.src import distribute
---> 21 from keras.src import models
22 from keras.src.engine.input_layer import Input
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\src\models\__init__.py:18
15 """Keras models API."""
---> 18 from keras.src.engine.functional import Functional
19 from keras.src.engine.sequential import Sequential
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\src\engine\functional.py:25
23 import tensorflow.compat.v2 as tf
---> 25 from keras.src import backend
26 from keras.src.dtensor import layout_map as layout_map_lib
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\src\backend.py:35
34 from keras.src.dtensor import dtensor_api as dtensor
---> 35 from keras.src.engine import keras_tensor
36 from keras.src.utils import control_flow_util
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\src\engine\keras_tensor.py:19
17 import tensorflow.compat.v2 as tf
---> 19 from keras.src.utils import object_identity
21 # isort: off
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\src\utils\__init__.py:53
52 # Preprocessing utils
---> 53 from keras.src.utils.feature_space import FeatureSpace
55 # Internal
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\src\utils\feature_space.py:20
19 from keras.src import backend
---> 20 from keras.src.engine import base_layer
21 from keras.src.saving import saving_lib
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\src\engine\base_layer.py:39
38 from keras.src.engine import keras_tensor
---> 39 from keras.src.engine import node as node_module
40 from keras.src.mixed_precision import autocast_variable
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\src\engine\node.py:28
27 from keras.src.engine import base_layer_utils
---> 28 from keras.src.saving.legacy.saved_model import json_utils
29 from keras.src.utils import tf_utils
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\keras\src\saving\legacy\saved_model\json_utils.py:38
37 # isort: off
---> 38 from tensorflow.python.framework import type_spec_registry
40 _EXTENSION_TYPE_SPEC = "_EXTENSION_TYPE_SPEC"
ImportError: cannot import name 'type_spec_registry' from 'tensorflow.python.framework' (C:\Users\hp\anaconda3\envs\Asiwajuflow\lib\site-packages\tensorflow\python\framework\__init__.py)
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
Cell In[9], line 2
1 #Basic usage
----> 2 classifier = pipeline('sentiment-analysis')
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\transformers\pipelines\__init__.py:788, in pipeline(task, model, config, tokenizer, feature_extractor, image_processor, framework, revision, use_fast, use_auth_token, device, device_map, torch_dtype, trust_remote_code, model_kwargs, pipeline_class, **kwargs)
786 if isinstance(model, str) or framework is None:
787 model_classes = {"tf": targeted_task["tf"], "pt": targeted_task["pt"]}
--> 788 framework, model = infer_framework_load_model(
789 model,
790 model_classes=model_classes,
791 config=config,
792 framework=framework,
793 task=task,
794 **hub_kwargs,
795 **model_kwargs,
796 )
798 model_config = model.config
799 hub_kwargs["_commit_hash"] = model.config._commit_hash
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\transformers\pipelines\base.py:245, in infer_framework_load_model(model, config, model_classes, task, framework, **model_kwargs)
243 classes.append(_class)
244 if look_tf:
--> 245 _class = getattr(transformers_module, f"TF{architecture}", None)
246 if _class is not None:
247 classes.append(_class)
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\transformers\utils\import_utils.py:1090, in _LazyModule.__getattr__(self, name)
1088 elif name in self._class_to_module.keys():
1089 module = self._get_module(self._class_to_module[name])
-> 1090 value = getattr(module, name)
1091 else:
1092 raise AttributeError(f"module {self.__name__} has no attribute {name}")
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\transformers\utils\import_utils.py:1089, in _LazyModule.__getattr__(self, name)
1087 value = self._get_module(name)
1088 elif name in self._class_to_module.keys():
-> 1089 module = self._get_module(self._class_to_module[name])
1090 value = getattr(module, name)
1091 else:
File ~\anaconda3\envs\Asiwajuflow\lib\site-packages\transformers\utils\import_utils.py:1101, in _LazyModule._get_module(self, module_name)
1099 return importlib.import_module("." + module_name, self.__name__)
1100 except Exception as e:
-> 1101 raise RuntimeError(
1102 f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its"
1103 f" traceback):\n{e}"
1104 ) from e
RuntimeError: Failed to import transformers.models.distilbert.modeling_tf_distilbert because of the following error (look up to see its traceback):
cannot import name 'type_spec_registry' from 'tensorflow.python.framework' (C:\Users\hp\anaconda3\envs\Asiwajuflow\lib\site-packages\tensorflow\python\framework\__init__.py)