from huggingface_hub import snapshot_download
import time
import logging
# 设置日志配置
logging.basicConfig(level=logging.INFO)
max_retries = 15 # 最大重试次数
retries = 0
model_name = "h94/IP-Adapter"
local_name=r"E:\downloads\huggingface\ip_adapter"
while retries < max_retries:
try:
# 尝试下载
snapshot_download(
repo_id=model_name,
ignore_patterns=["*.bin"], # 忽略下载哪些文件
local_dir=local_name,
force_download=False # 默认情况下不强制重新下载,除非设置为 True
)
logging.info("下载成功")
break
except Exception as e:
# 捕获其他异常
retries += 1
logging.error(f"第 {retries} 次尝试失败,其他错误: {e}")
if retries < max_retries:
time.sleep(2) # 等待 2 秒后重试
else:
logging.critical("下载失败,超过最大重试次数")
有异常处理
from huggingface_hub import snapshot_download
import time
import logging
import requests
# 设置日志配置
logging.basicConfig(level=logging.INFO)
max_retries = 15 # 最大重试次数
retries = 0
model_name = "h94/IP-Adapter"
local_name=r"E:\downloads\huggingface\ip_adapter"
while retries < max_retries:
try:
# 尝试下载
snapshot_download(
repo_id=model_name,
ignore_patterns=["*.bin"], # 忽略下载哪些文件
local_dir=local_name,
force_download=False # 默认情况下不强制重新下载,除非设置为 True
)
logging.info("下载成功")
break
except requests.exceptions.RequestException as e:
# 捕获请求相关的错误
retries += 1
logging.error(f"第 {retries} 次尝试失败,请求错误: {e}")
except Exception as e:
# 捕获其他异常
retries += 1
logging.error(f"第 {retries} 次尝试失败,其他错误: {e}")
if retries < max_retries:
time.sleep(2) # 等待 2 秒后重试
else:
logging.critical("下载失败,超过最大重试次数")