news 2026/7/10 14:12:06

Hugging Face 使用笔记

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Hugging Face 使用笔记

1.HuggingFace简介

Hugging Face Hub 和 Github 类似,都是Hub(社区)。Hugging Face可以说的上是机器学习界的Github。Hugging Face为用户提供了以下主要功能:

Hugging Face在NLP领域最出名,其提供的模型大多都是基于Transformer的。为了易用性,Hugging Face还为用户提供了以下几个项目:

2. 注册与登陆

这里不用多说,使用邮箱注册,邮箱验证,然后登陆

3. 获取token

点击头像->Settings

Access Tokens->New token

关于这个类型的定义,有如下说明:

选择一下这些权限

4. 配置token

有三种方法

4.1. 使用代码登陆

把下面的代码写在一个脚本中,然后运行并输入token(注意:不要以交互式的方式逐行运行)

from huggingface_hub import login login()

这段代码会把token写入到配置文件中

4.2. 使用命令登录

huggingface-cli login

这条命令会把token写入到配置文件中

4.3. 修改配置文件

把token粘贴到该文件中

~/.cache/huggingface/token

5. 下载数据

5.1. 使用命令下载

这里会给你命令,按命令执行即可

注意:有时下载时间可能很久(多达几十小时),而且没有进度条。

5.2. 使用代码下载

import os from huggingface_hub import snapshot_download print('downloading entire files...') # 注意,这种方式仍然保存在cache_dir中 snapshot_download(repo_id="szymanowiczs/splatter-image-v1", repo_type="dataset", local_dir="/home/xxx/Downloads", local_dir_use_symlinks=False, resume_download=True, token='hf_***')

6. 下载预训练模型

6.1. 使用命令下载

注意:有时下载时间可能很久(多达几十小时),而且没有进度条。

6.2. 使用代码下载

6.2.1. 使用snapshot_download下载

import os from huggingface_hub import snapshot_download # 使用cache_dir参数,将模型/数据集保存到指定“本地路径” snapshot_download(repo_id="szymanowiczs/splatter-image-v1", repo_type=None, cache_dir="/home/xxx/Downloads", local_dir_use_symlinks=False, resume_download=True, token='hf_***')

6.2.2. 使用hf_hub_download下载

from huggingface_hub import hf_hub_download model_path = hf_hub_download(repo_id="szymanowiczs/splatter-image-multi-category-v1", filename="model_latest.pth")

7. 使用问题记录

7.1. 下载失败

7.1.1. 现象

'(MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /bert-base-uncased/resolve/main/vocab.txt (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7f1320354880>, 'Connection to huggingface.co timed out. (connect timeout=10)'))"), '(Request ID: 625af900-631f-4614-9358-30364ecacefe)')' thrown while requesting HEAD https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt
'(MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /bert-base-uncased/resolve/main/added_tokens.json (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7f1320354d60>, 'Connection to huggingface.co timed out. (connect timeout=10)'))"), '(Request ID: 1679a995-7441-4afe-a685-9a7bd6da9f2a)')' thrown while requesting HEAD https://huggingface.co/bert-base-uncased/resolve/main/added_tokens.json
'(MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /bert-base-uncased/resolve/main/special_tokens_map.json (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7f13202fb250>, 'Connection to huggingface.co timed out. (connect timeout=10)'))"), '(Request ID: 9af5b73e-5230-45d7-8886-5d37d38f09a8)')' thrown while requesting HEAD https://huggingface.co/bert-base-uncased/resolve/main/special_tokens_map.json
'(MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /bert-base-uncased/resolve/main/tokenizer_config.json (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7f13202fb730>, 'Connection to huggingface.co timed out. (connect timeout=10)'))"), '(Request ID: 12136040-d033-4099-821c-dcb80fb50018)')' thrown while requesting HEAD https://huggingface.co/bert-base-uncased/resolve/main/tokenizer_config.json
Traceback (most recent call last):
File "/tmp/pycharm_project_494/Zilean-Classifier/main.py", line 48, in <module>
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
File "/root/miniconda3/envs/DL/lib/python3.8/site-packages/transformers/tokenization_utils_base.py", line 1838, in from_pretrained
raise EnvironmentError(
OSError: Can't load tokenizer for 'bert-base-uncased'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'bert-base-uncased' is the correct path to a directory containing all relevant files for a BertTokenizer tokenizer.

7.1.2. 原因分析

造成这种错误的原因主要是因为你的服务器没有办法连接huggingface的原因,你可以直接在你的服务器上尝试能否直接ping

ping huggingface.co

那我的机器就是没有数据传输过来,当然前提是你自己的服务器一定要有网络连接(可以尝试ping www.baidu.com来检测自己机器是否有网络)。

7.1.3. 解决方法

使用另一台拥有网络条件的电脑下载,例如云服务器或其它操作系统的电脑

from transformers import BertModel, BertTokenizer # 使用bert-large-uncased model = BertModel.from_pretrained('bert-large-uncased') tokenizer = BertTokenizer.from_pretrained('bert-large-uncased')

此时你的机器上会出现如下图片:

找到本地下载好的模型文件

~/.cache/huggingface/hub/models--bert-base-uncased

上传文件到服务器上
将本地文件上传到服务器的下面路径中

~/.cache/huggingface/hub/models--bert-base-uncased

就可以运行你的代码了,但是这里运行的时候有个小问题,就是你运行时候仍然会报错说无法下载这些文件,请耐心等待,你的代码会正常运行

如果你不想出现之前上面还显示出错的问题,那么修改之前的加载方法,之前的加载方法为:

config = BertConfig.from_pretrained(model_name)

修改为

# 指定本地bert模型路径 bert_model_dir = "/path/to/bert/model" config = transformers.BertConfig.from_pretrained(bert_model_dir)

参考文献

Hugging Face快速入门(重点讲解模型(Transformers)和数据集部分(Datasets))_huggingface-CSDN博客

报错解决MaxRetryError(“HTTPSConnectionPool(host=‘huggingface.co‘, port=443):xxx“)_oserror: can't load tokenizer for 'bert-base-uncas-CSDN博客

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/10 14:04:41

鼠鬚管输入法:发现macOS上最纯粹的输入自由之旅

鼠鬚管输入法&#xff1a;发现macOS上最纯粹的输入自由之旅 【免费下载链接】squirrel 【鼠鬚管】Rime for macOS 项目地址: https://gitcode.com/gh_mirrors/squ/squirrel 想象一下&#xff0c;当你每天在macOS上处理文档、编写代码或聊天时&#xff0c;输入法不再是一…

作者头像 李华
网站建设 2026/7/10 13:56:27

使用VideosApi开发微信视频号,微信号二次开发

使用 VideosApi 开发微信视频号和微信号二次开发 微信视频号作为微信平台上的重要产品之一&#xff0c;为用户提供了分享、创作和观看视频内容的功能。而使用 VideosApi 进行二次开发&#xff0c;可以为微信视频号添加更多自定义的功能和特性&#xff0c;让用户的体验更加丰富…

作者头像 李华
网站建设 2026/7/10 13:55:00

CharacterPickerView完全指南:10分钟掌握Android仿iOS滚轮选择器

CharacterPickerView完全指南&#xff1a;10分钟掌握Android仿iOS滚轮选择器 【免费下载链接】CharacterPickerView 项目地址: https://gitcode.com/gh_mirrors/ch/CharacterPickerView 想要为你的Android应用添加iOS风格的滚轮选择器吗&#xff1f;CharacterPickerVie…

作者头像 李华
网站建设 2026/7/10 13:53:18

Windows文件资源管理器终极美化指南:ExplorerBlurMica深度解析

Windows文件资源管理器终极美化指南&#xff1a;ExplorerBlurMica深度解析 【免费下载链接】ExplorerBlurMica Add background Blur effect or Acrylic (Mica for win11) effect to explorer for win10 and win11 项目地址: https://gitcode.com/gh_mirrors/ex/ExplorerBlurM…

作者头像 李华