RuntimeError: Failed to import transformers.models.roberta.modeling_tf_roberta because of the following error (look up to see its traceback): No module named 'keras.engine'

Hi,

I wrote very small code :
from transformers import pipeline
model_id = “cardiffnlp/twitter-roberta-base-sentiment-latest”
sentiment_pipe = pipeline(“sentiment-analysis”, model=model_id)
print(sentiment_pipe(‘We hope you dont hate it’))
ERROR - RuntimeError: Failed to import transformers.models.roberta.modeling_tf_roberta because of the following error (look up to see its traceback):
No module named ‘keras.engine’
I tried to install tensorflow == 2.10 but its not present

Issue here:
File ~\AppData\Local\anaconda3\Lib\site-packages\transformers\modeling_tf_utils.py:69
68 from keras import backend as K
—> 69 from keras.engine import data_adapter
70 from keras.engine.keras_tensor import KerasTensor

ModuleNotFoundError: No module named ‘keras.engine’

Solutions: change the line 69 and 70 to ‘from tensorflow.python.keras.engine import data_adapter’ and ‘from tensorflow.python.keras.engine.keras_tensor import KerasTensor’

Thanks alot for the help

https://discuss.huggingface.co/t/cant-find-keras-engine/77743/3https://discuss.huggingface.co/t/cant-find-keras-engine/77743/3

Guys but the “facebook/roberta-hate-speech-dynabench-r4-target” is a Pytorch model, isn’t it?

I get to make it work with pytorch specifying the framework like this:


sentiment_pipe = pipeline("sentiment-analysis", 
                          model=toxicity_model_name, #toxicity_model,tokenizer=toxicity_tokenizer,
                          framework="pt",
                          device=device)

But latter I get the same error from the evaluator. Any ideas?

I think you have a problem importing the keras.engine module. Try installing tensorflow version 2.10 if you can’t find it. Also make sure that you have all the dependencies and environment configured correctly to work with the transformers library.

Thanks, I solve it modifying an internal transformer library file that was importing de tensorflow/keras model in a different Way.

Still I continue with the doubt because as you can see there is a parameter for the framework filled with pytorch and also All the Notebook what’s working with pytorch even the other Transformers models from hugging face are working also in pytorch so why is this One trying to import or using this module in tensorflow?