模型下载超时We couldn‘t connect to ‘https://huggingface.co‘ to load this file …… TimeoutError: timed out

一、【问题描述】

最近打算在huggingface搞个模型玩玩,把官网的代码跑起会报这个错误。刚好记录一下解决方法

二、【解决方法】

本质是没有终端代理的问题,两个解决思路,配置一个隧道通信(代理),或者把huggingface的文件下载到本地再运行

1. 挂一个代理

需要提前有一个远端的代理服务器,或者本地设置好的一个端口

  1. 按下 win+r ,输入cmd,打开 Windows 终端。
  2. 输入 
    1. set http_proxy=http://127.0.0.1:port
      set https_proxy=http://127.0.0.1:port
      # 其中 port 是代理服务器的端口号。
  3. 如果代理服务器需要用户名和密码
    1. set http_proxy_user=username
      set http_proxy_pass=password
      # 其中 username 和 password 分别是你的用户名和密码

2. 下载到本地

以笔者最近玩的microsoft/Phi-3-mini-4k-instruct · Hugging Face 为例

改一下里面的一个变量,注释是原代码,区别就是加了一个路径作为变量

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline

torch.random.manual_seed(0)

''' original code
model = AutoModelForCausalLM.from_pretrained(
    "microsoft/Phi-3-mini-4k-instruct", 
    device_map="cuda", 
    torch_dtype="auto", 
    trust_remote_code=True, 
)
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
'''


local_model_path = r'C:\path\to\ur\dir\microsoft_Phi-3-mini-4k-instruct'
model = AutoModelForCausalLM.from_pretrained(
    local_model_path, 
    device_map="cpu", 
    torch_dtype="auto", 
    trust_remote_code=True, 
)
tokenizer = AutoTokenizer.from_pretrained(local_model_path)

messages = [
    {"role": "user", "content": "Can you provide ways to eat combinations of bananas and dragonfruits?"},
    {"role": "assistant", "content": "Sure! Here are some ways to eat bananas and dragonfruits together: 1. Banana and dragonfruit smoothie: Blend bananas and dragonfruits together with some milk and honey. 2. Banana and dragonfruit salad: Mix sliced bananas and dragonfruits together with some lemon juice and honey."},
    {"role": "user", "content": "What about solving an 2x + 3 = 7 equation?"},
]

pipe = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
)

generation_args = {
    "max_new_tokens": 500,
    "return_full_text": False,
    "temperature": 0.0,
    "do_sample": False,
}

output = pipe(messages, **generation_args)
print(output[0]['generated_text'])

三、【总结】

本文介绍了当网络不佳时访问huggingface回报超时故障的处理方法。一般timeout故障,优先考虑网络问题,防火墙这种。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值