解决ValueError: You need to pass a valid `token` or login by using `huggingface……问题

Posted 沉迷单车的追风少年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决ValueError: You need to pass a valid `token` or login by using `huggingface……问题相关的知识,希望对你有一定的参考价值。

项目场景:

基于hugging face的NLP任务,MLM场景下微调BertModel。


问题描述

原报错代码:

from transformers import TrainingArguments

training_args = TrainingArguments(
    output_dir=f"model_name-finetuned-imdb",
    overwrite_output_dir=True,
    evaluation_strategy="epoch",
    learning_rate=2e-5,
    weight_decay=0.01,
    per_device_train_batch_size=64,
    per_device_eval_batch_size=64,
    push_to_hub=True,
    # push_to_hub=False,
    fp16=True,
    logging_steps=logging_steps,
)

或者登录时候显示错误:

from huggingface_hub import notebook_login

notebook_login()

这一段代码中参数push_to_hub=True表示会把自己训练好的模型共享到hugging face社区。

原因分析:

如果是第一次使用hugging face的用户,没有注册,没有设置自己的token,所以没有办法共享到hugging face社区。

简单的方法是讲push_to_hub=False,在后面的逻辑上都去掉上传的逻辑,这样就不会报错了。

下面讲一下如何在hugging face社区上设置自己的token。


解决方案:

1、打开官网:Hugging Face – The AI community building the future.

如果没有注册按照提示注册即可。

2、进入setting页面

 3、点击new token,新建自己的token,并复制。

注意不要复制Organizations API Tokens,复制User Access Tokens!

4、打开自己的服务器,执行:

pip install huggingface_hub
huggingface-cli login

执行成功会显示:

huggingface-cli login

        _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
        _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
        _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
        _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
        _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

        To login, `huggingface_hub` now requires a token generated from https://huggingface.co/settings/tokens .
        (Deprecated, will be removed in v0.3.0) To login with username and password instead, interrupt with Ctrl+C.

Token:
Login successful
Your token has been saved to /root/.huggingface/token

注意:如果执行失败,很可能是自己的token复制错误,此时会报错:

huggingface-cli login

        _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
        _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
        _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
        _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
        _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

        To login, `huggingface_hub` now requires a token generated from https://huggingface.co/settings/tokens .
        (Deprecated, will be removed in v0.3.0) To login with username and password instead, interrupt with Ctrl+C.

Token:
Traceback (most recent call last):
  File "/root/anaconda3/envs/dpm-pc-gen/bin/huggingface-cli", line 8, in <module>
    sys.exit(main())
  File "/root/anaconda3/envs/dpm-pc-gen/lib/python3.7/site-packages/huggingface_hub/commands/huggingface_cli.py", line 41, in main
    service.run()
  File "/root/anaconda3/envs/dpm-pc-gen/lib/python3.7/site-packages/huggingface_hub/commands/user.py", line 177, in run
    _login(self._api, token=token)
  File "/root/anaconda3/envs/dpm-pc-gen/lib/python3.7/site-packages/huggingface_hub/commands/user.py", line 396, in _login
    token, name = hf_api._validate_or_retrieve_token(token)
  File "/root/anaconda3/envs/dpm-pc-gen/lib/python3.7/site-packages/huggingface_hub/hf_api.py", line 681, in _validate_or_retrieve_token
    raise ValueError("You must use your personal account token.")
ValueError: You must use your personal account token.

仔细检查自己的复制是否有误!!

以上是关于解决ValueError: You need to pass a valid `token` or login by using `huggingface……问题的主要内容,如果未能解决你的问题,请参考以下文章

You need to run build with JDK or have tools.jar问题解决

You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法

you need to be root to perform this command

Git切换分支时报错:you need to resolve your current index first

关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法

You need to call a different part of POI to process this data (eg XSSF instead of HSSF)的解决方法