错误如下:
stable-diffusion-webui/venv/lib/python3.10/site-packages/huggingface_hub/utils/_headers.py", line 117, in build_hf_headers
token_to_send = get_token_to_send(token)
File "/home/oem/Project/stable-diffusion-webui/venv/lib/python3.10/site-packages/huggingface_hub/utils/_headers.py", line 144, in get_token_to_send
cached_token = HfFolder().get_token()
File "/home/oem/Project/stable-diffusion-webui/venv/lib/python3.10/site-packages/huggingface_hub/utils/_hf_folder.py", line 60, in get_token
cls._copy_to_new_path_and_warn()
File "/home/oem/Project/stable-diffusion-webui/venv/lib/python3.10/site-packages/huggingface_hub/utils/_hf_folder.py", line 90, in _copy_to_new_path_and_warn
if cls._old_path_token.exists() and not cls.path_token.exists():
File "/home/oem/anaconda3/envs/stablediff/lib/python3.10/pathlib.py", line 1288, in exists
self.stat()
File "/home/oem/anaconda3/envs/stablediff/lib/python3.10/pathlib.py", line 1095, in stat
return self._accessor.stat(self, follow_symlinks=follow_symlinks)
PermissionError: [Errno 13] Permission denied: '/root/.huggingface/token'
因为当前目录权限问题,所以打算修改环境变量,这个环境变量名称找了半天全网也没多少提到,直到看到了官方文档:
https://huggingface.co/docs/huggingface_hub/package_reference/environment_variables
所以在Python代码中添加:
import os
os.environ['HUGGING_FACE_HUB_TOKEN'] = 'xxx' # 可以随意读写的路径即可,如
# os.environ['HUGGING_FACE_HUB_TOKEN'] = str(os.getcwd()) + '/transformers'
搞定!