MCPcopy Create free account
hub / github.com/modelscope/modelscope / ModelFileSystemCache

Class ModelFileSystemCache

modelscope/hub/utils/caching.py:161–390  ·  view source on GitHub ↗

Local cache file layout cache_root/owner/model_name/individual cached files and cache index file '.mcs' Save only one version for each file.

Source from the content-addressed store, hash-verified

159
160
161class ModelFileSystemCache(FileSystemCache):
162 """Local cache file layout
163 cache_root/owner/model_name/individual cached files and cache index file '.mcs'
164 Save only one version for each file.
165 """
166
167 def __init__(self, cache_root, owner=None, name=None):
168 """Put file to the cache
169 Args:
170 cache_root(`str`): The modelscope local cache root(default: ~/.cache/modelscope/hub)
171 owner(`str`): The model owner.
172 name('str'): The name of the model
173 Returns:
174 Raises:
175 None
176 <Tip>
177 model_id = {owner}/{name}
178 </Tip>
179 """
180 if owner is None or name is None:
181 # get model meta from
182 super().__init__(os.path.join(cache_root))
183 self.load_model_meta()
184 else:
185 super().__init__(os.path.join(cache_root, owner, name))
186 self.model_meta = {
187 FileSystemCache.MODEL_META_MODEL_ID: '%s/%s' % (owner, name)
188 }
189 self.save_model_meta()
190 self.cached_model_revision = self.load_model_version()
191
192 def load_model_meta(self):
193 meta_file_path = os.path.join(self.cache_root_location,
194 FileSystemCache.MODEL_META_FILE_NAME)
195 if os.path.exists(meta_file_path):
196 try:
197 with open(meta_file_path, 'rb') as f:
198 data = pickle.load(f)
199 if isinstance(data, dict):
200 self.model_meta = data
201 return
202 logger.warning('Model meta %s has unexpected type, resetting.',
203 meta_file_path)
204 except Exception as e:
205 logger.warning('Failed to load model meta %s: %s. Resetting.',
206 meta_file_path, e)
207 try:
208 os.replace(meta_file_path, meta_file_path + '.corrupted')
209 except OSError:
210 pass
211 self.model_meta = {FileSystemCache.MODEL_META_MODEL_ID: 'unknown'}
212
213 def load_model_version(self):
214 model_version_file_path = os.path.join(
215 self.cache_root_location, FileSystemCache.MODEL_VERSION_FILE_NAME)
216 if os.path.exists(model_version_file_path):
217 with open(model_version_file_path, 'r') as f:
218 return f.read().strip()

Callers 3

_scan_cached_repoFunction · 0.90
get_model_id_from_cacheFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…