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

Method _process_model_path

modelscope/hub/utils/aigc.py:187–275  ·  view source on GitHub ↗

Process model_path to extract weight information

(self)

Source from the content-addressed store, hash-verified

185 f'Recommended values: {supported_tags}. ')
186
187 def _process_model_path(self):
188 """Process model_path to extract weight information"""
189 from modelscope.utils.file_utils import compute_file_hash
190
191 # Expand user path
192 self.model_path = os.path.expanduser(self.model_path)
193
194 if not os.path.exists(self.model_path):
195 raise ValueError(f'Model path does not exist: {self.model_path}')
196
197 if os.path.isfile(self.model_path):
198 target_file = self.model_path
199 logger.info('Using file: %s', os.path.basename(target_file))
200 elif os.path.isdir(self.model_path):
201 # Validate top-level directory: it must not be empty; and if it has files,
202 # they must not be only the common placeholder files
203 top_entries = os.listdir(self.model_path)
204 if len(top_entries) == 0:
205 raise ValueError(
206 f'Directory is empty: {self.model_path}. '
207 f'Please place at least one model file at the top level (e.g., .safetensors/.pth/.bin).'
208 )
209
210 top_files = [
211 name for name in top_entries
212 if os.path.isfile(os.path.join(self.model_path, name))
213 ]
214 placeholder_names = {
215 '.gitattributes', 'configuration.json', 'readme.md'
216 }
217 if top_files:
218 normalized = {name.lower() for name in top_files}
219 if normalized.issubset(placeholder_names):
220 raise ValueError(
221 'Top-level directory contains only [.gitattributes, configuration.json, README.md]. '
222 'Please place additional model files at the top level (e.g., .safetensors/.pth/.bin).'
223 )
224
225 # Priority order for metadata file: safetensors -> pth -> bin -> first file
226 file_extensions = ['.safetensors', '.pth', '.bin']
227 target_file = None
228
229 for ext in file_extensions:
230 files = glob.glob(os.path.join(self.model_path, f'*{ext}'))
231 if files:
232 target_file = files[0]
233 logger.info(f'Found {ext} file: %s',
234 os.path.basename(target_file))
235 if len(files) > 1:
236 logger.warning(
237 f'Multiple {ext} files found, using: %s for metadata',
238 os.path.basename(target_file))
239 logger.info(f'Other {ext} files: %s',
240 [os.path.basename(f) for f in files[1:]])
241 break
242
243 # If no preferred files found, use the first available file
244 if not target_file:

Callers 1

__init__Method · 0.95

Calls 3

compute_file_hashFunction · 0.90
infoMethod · 0.80
existsMethod · 0.45

Tested by

no test coverage detected