fix download retry bug, add get_cache_dir to hut/util

This commit is contained in:
mulin.lyh
2024-06-04 16:18:52 +08:00
parent 7b18f99d03
commit 339ebab9dd
2 changed files with 19 additions and 3 deletions

View File

@@ -224,10 +224,10 @@ def download_part_with_retry(params):
with open(part_file_name, 'rb') as f:
partial_length = f.seek(0, io.SEEK_END)
progress.update(partial_length)
start = start + partial_length
if start > end:
download_start = start + partial_length
if download_start > end:
break # this part is download completed.
get_headers['Range'] = 'bytes=%s-%s' % (start, end)
get_headers['Range'] = 'bytes=%s-%s' % (download_start, end)
with open(part_file_name, 'ab+') as f:
r = requests.get(
url,

View File

@@ -3,6 +3,7 @@
import hashlib
import os
from datetime import datetime
from pathlib import Path
from typing import Optional
import requests
@@ -28,6 +29,21 @@ def model_id_to_group_owner_name(model_id):
return group_or_owner, name
def get_cache_dir(model_id: Optional[str] = None):
"""cache dir precedence:
function parameter > environment > ~/.cache/modelscope/hub
Args:
model_id (str, optional): The model id.
Returns:
str: the model_id dir if model_id not None, otherwise cache root dir.
"""
default_cache_dir = Path.home().joinpath('.cache', 'modelscope')
base_path = os.getenv('MODELSCOPE_CACHE',
os.path.join(default_cache_dir, 'hub'))
return base_path if model_id is None else os.path.join(
base_path, model_id + '/')
def get_release_datetime():
if MODELSCOPE_SDK_DEBUG in os.environ:
rt = int(round(datetime.now().timestamp()))