from huggingface_hub import snapshot_download
import time
import logging
# 設(shè)置日志配置
logging.basicConfig(level=logging.INFO)
max_retries = 15 # 最大重試次數(shù)
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 # 默認(rèn)情況下不強(qiáng)制重新下載,除非設(shè)置為 True
)
logging.info("下載成功")
break
except Exception as e:
# 捕獲其他異常
retries += 1
logging.error(f"第 {retries} 次嘗試失敗油额,其他錯(cuò)誤: {e}")
if retries < max_retries:
time.sleep(2) # 等待 2 秒后重試
else:
logging.critical("下載失敗讲坎,超過(guò)最大重試次數(shù)")
有異常處理
from huggingface_hub import snapshot_download
import time
import logging
import requests
# 設(shè)置日志配置
logging.basicConfig(level=logging.INFO)
max_retries = 15 # 最大重試次數(shù)
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 # 默認(rèn)情況下不強(qiáng)制重新下載束凑,除非設(shè)置為 True
)
logging.info("下載成功")
break
except requests.exceptions.RequestException as e:
# 捕獲請(qǐng)求相關(guān)的錯(cuò)誤
retries += 1
logging.error(f"第 {retries} 次嘗試失敗,請(qǐng)求錯(cuò)誤: {e}")
except Exception as e:
# 捕獲其他異常
retries += 1
logging.error(f"第 {retries} 次嘗試失敗蹄殃,其他錯(cuò)誤: {e}")
if retries < max_retries:
time.sleep(2) # 等待 2 秒后重試
else:
logging.critical("下載失敗挽铁,超過(guò)最大重試次數(shù)")