From 39730f40fee921e5cda1859767319d78df286cfe Mon Sep 17 00:00:00 2001 From: Yingda Chen Date: Wed, 31 Aug 2022 16:28:31 +0800 Subject: [PATCH] [to #42322933] support get_cache_dir with model id --- modelscope/fileio/format/json.py | 3 ++- modelscope/hub/utils/utils.py | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/modelscope/fileio/format/json.py b/modelscope/fileio/format/json.py index f615366f..9979c023 100644 --- a/modelscope/fileio/format/json.py +++ b/modelscope/fileio/format/json.py @@ -1,5 +1,4 @@ # Copyright (c) Alibaba, Inc. and its affiliates. -import jsonplus import numpy as np from .base import FormatHandler @@ -25,11 +24,13 @@ class JsonHandler(FormatHandler): """Use jsonplus, serialization of Python types to JSON that "just works".""" def load(self, file): + import jsonplus return jsonplus.loads(file.read()) def dump(self, obj, file, **kwargs): file.write(self.dumps(obj, **kwargs)) def dumps(self, obj, **kwargs): + import jsonplus kwargs.setdefault('default', set_default) return jsonplus.dumps(obj, **kwargs) diff --git a/modelscope/hub/utils/utils.py b/modelscope/hub/utils/utils.py index 8faf8f1d..7e219d16 100644 --- a/modelscope/hub/utils/utils.py +++ b/modelscope/hub/utils/utils.py @@ -1,5 +1,6 @@ import hashlib import os +from typing import Optional from modelscope.hub.constants import (DEFAULT_MODELSCOPE_DATA_ENDPOINT, DEFAULT_MODELSCOPE_DOMAIN, @@ -23,14 +24,16 @@ def model_id_to_group_owner_name(model_id): return group_or_owner, name -def get_cache_dir(): +def get_cache_dir(model_id: Optional[str] = None): """ cache dir precedence: function parameter > enviroment > ~/.cache/modelscope/hub """ default_cache_dir = get_default_cache_dir() - return os.getenv('MODELSCOPE_CACHE', os.path.join(default_cache_dir, - 'hub')) + 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_endpoint():