RuntimeError: Failed to import transformers.models.bert.modeling_tf_bert because of the following error (look up to see its traceback):
module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'
sentiment_analysis = pipeline("sentiment-analysis", model="ProsusAI/finbert")
@app.route('/news', methods=['POST'])
def analyze_news_sentiment():
data = request.json
news_text = data.get('news_text')
# Perform sentiment analysis
result = sentiment_analysis(news_text)
return jsonify(sentiment=result[0]['label'])
如何解决这个错误
似乎是版本兼容性问题。
我会尝试将 python 版本固定到 3.10 和 tensorflow/keras 以寻求稳定性。
conda create -n my_env python=3.10
conda activate my_env
python -m pip install tensorflow==2.15.0 keras==3.5.0
并尝试使用此设置运行您的程序