浏览器下载huggingface网络连接超时,使用镜像源教程

镜像源网址:

https://hf-mirror.com/

可以:

1.安装依赖:

pip install -U huggingface_hub

2.设置环境变量

(linux)

export HF_ENDPOINT=https://hf-mirror.com

(windows)

$env:HF_ENDPOINT = "https://hf-mirror.com"

3.下载模型指令(替换掉模型gpt和地址)

huggingface-cli download --resume-download gpt2 --local-dir gpt2

下载数据集指令

huggingface-cli download --repo-type dataset --resume-download wikitext --local-dir wikitext

详细操作见网址

下载模型到本地之后,可以通过代码使用本地模型:

复制代码
from transformers import AutoTokenizer, AutoModel

# 使用绝对路径或相对路径
local_model_path = "your_path"

# 或者如果代码文件在同一目录下,可以用相对路径
# local_model_path = "./roberta-base-finetuned-dianping-chinese"

# 加载模型和分词器
tokenizer = AutoTokenizer.from_pretrained(local_model_path)
model = AutoModel.from_pretrained(local_model_path)

print("✅ 模型加载成功!")
print(f"模型类型: {type(model)}")
print(f"分词器: {tokenizer}")

实操:

以下为jupyter nootbook三个cell

复制代码
# Cell 1: 设置镜像
import os
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
print("镜像已设置为: https://hf-mirror.com")

# Cell 2: 下载模型(带进度显示)
!huggingface-cli download hfl/rbt3 --local-dir ./hfl_rbt3

# Cell 3: 验证下载完成
import glob
files = glob.glob("./hfl_rbt3/*")
print(f"下载了 {len(files)} 个文件:")
for f in files:
    print(f"  - {f}")
相关推荐
曲幽10 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时14 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿16 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780511 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee1 天前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay1 天前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤1 天前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean1 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek