遇到这个问题的人可能不多,国内外找了半天,没找到能解决我问题的答案(搜索能力有限)。自己尝试了一些方法,现在终于解决了。可以直接看我最后的解决方法。


报错信息:

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.huggingface transformers的版本太老。

可以通过下面的命令进行更新,我这情况不适用。

pip install -U transformers
pip install -U huggingface_hub

最终解决方法:

我这里的问题是python的版本问题,我从python3.6.3升级到python3.9.13就不报错了。

我用的anaconda,直接新建了一个python3.9.13的环境,使用conda install -c huggingface transformers装好后,直接运行demo就不报错了。


最后免费赠送测试代码一份:

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}]

会提示你没有选模型,就给你用个默认的模型。

最后的结果:第一句是positive的,第二句是negative的,整挺好。


Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐