HuggingFace レポート ImportError: 名前 'HfApi' をインポートできません

この問題に遭遇した人は多くないかもしれません. 国内外で長い間検索した後、私の問題に対する答えを見つけることができませんでした (検索機能は限られています) . 自分でいくつかのことを試しましたが、今では最終的に解決されています。私の最終的な解決策を直接見ることができます。


エラーメッセージ:

Traceback (most recent call last):
  File "D:/Longer/working/Trading/TradingFrame/others/huggingface/huggingface_test.py", line 3, in <module>
    from transformers import pipeline
  File "D:\anaconda3\envs\python3633\lib\site-packages\transformers\__init__.py", line 43, in <module>
    from . import dependency_versions_check
  File "D:\anaconda3\envs\python3633\lib\site-packages\transformers\dependency_versions_check.py", line 36, in <module>
    from .file_utils import is_tokenizers_available
  File "D:\anaconda3\envs\python3633\lib\site-packages\transformers\file_utils.py", line 51, in <module>
    from huggingface_hub import HfApi, HfFolder, Repository
ImportError: cannot import name 'HfApi'

オンライン方式:

1. huggingface と Transformer のバージョンが一致しません。

しかし、私はコマンドを使って自分でインストールしました。そのような問題はないはずです:

conda install -c huggingface transformers

2.ハグフェイストランスフォーマーのバージョンが古すぎる。

次のコマンドで更新できますが、これは私には当てはまりません。

pip install -U transformers
pip install -U huggingface_hub

最終的な回避策:

ここでの問題は python のバージョンです. python3.6.3 から python3.9.13 にアップグレードしてもエラーは報告されません.

直接使用した anaconda で python3.9.13 環境を作成し、 conda install -c huggingface translators でインストールした後、エラーを報告せずに直接デモを実行しました。


最後に、テスト コードを無料で提供します。

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
res = classifier(["We are very happy to show you the Transformers library.", 'Oh, no.'])
print(res)

出力:

No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).
Using a pipeline without specifying a model name and revision in production is not recommended.
[{'label': 'POSITIVE', 'score': 0.9997994303703308}, {'label': 'NEGATIVE', 'score': 0.9975526928901672}]

モデルを選択していないことを示すプロンプトが表示され、デフォルトのモデルが使用されます。

最終結果: 最初の文は肯定的で、2 番目の文は否定的です。これはかなり良い結果です。


おすすめ

転載: blog.csdn.net/BeiErGeLaiDe/article/details/129085992