一、【问题描述】
最近打算在huggingface搞个模型玩玩,把官网的代码跑起会报这个错误。刚好记录一下解决方法
二、【解决方法】
本质是没有终端代理的问题,两个解决思路,配置一个隧道通信(代理),或者把huggingface的文件下载到本地再运行
1. 挂一个代理
需要提前有一个远端的代理服务器,或者本地设置好的一个端口
- 按下 win+r ,输入cmd,打开 Windows 终端。
- 输入
-
set http_proxy=http://127.0.0.1:port set https_proxy=http://127.0.0.1:port # 其中 port 是代理服务器的端口号。
-
- 如果代理服务器需要用户名和密码
-
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故障,优先考虑网络问题,防火墙这种。