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

Method preupload_weights

modelscope/hub/utils/aigc.py:310–379  ·  view source on GitHub ↗

Pre-upload aigc model weights to the LFS server. Server may require the sha256 of weights to be registered before creation. This method streams the weight file so the sha gets registered. Args: cookies: Optional requests-style cookies (CookieJar/dict). If provid

(self,
                          *,
                          cookies: Optional[object] = None,
                          timeout: int = 300,
                          headers: Optional[dict] = None,
                          endpoint: Optional[str] = None)

Source from the content-addressed store, hash-verified

308 return False
309
310 def preupload_weights(self,
311 *,
312 cookies: Optional[object] = None,
313 timeout: int = 300,
314 headers: Optional[dict] = None,
315 endpoint: Optional[str] = None) -> None:
316 """Pre-upload aigc model weights to the LFS server.
317
318 Server may require the sha256 of weights to be registered before creation.
319 This method streams the weight file so the sha gets registered.
320
321 Args:
322 cookies: Optional requests-style cookies (CookieJar/dict). If provided, preferred.
323 timeout: Request timeout seconds.
324 headers: Optional headers.
325 """
326 endpoint = endpoint or get_endpoint()
327 endpoint_host: str = urlparse(endpoint.strip()).hostname.lstrip('www.')
328
329 # https://lfs.modelscope.cn or https://pre-lfs.modelscope.cn
330 base_url: str = f'{MODELSCOPE_URL_SCHEME}lfs.{endpoint_host}' if not endpoint_host.startswith('pre') \
331 else f'{MODELSCOPE_URL_SCHEME}pre-lfs.{endpoint_host.lstrip("pre.")}'
332
333 url: str = f'{base_url}/api/v1/models/aigc/weights'
334
335 file_path = getattr(self, 'target_file', None) or self.model_path
336 file_path = os.path.abspath(os.path.expanduser(file_path))
337 if not os.path.isfile(file_path):
338 raise ValueError(f'Pre-upload expects a file, got: {file_path}')
339
340 cookies = dict(cookies) if cookies else None
341 if cookies is None:
342 raise ValueError('Token does not exist, please login first.')
343
344 headers.update({'Cookie': f"m_session_id={cookies['m_session_id']}"})
345
346 file_size = os.path.getsize(file_path)
347
348 def read_in_chunks(file_object,
349 pbar,
350 chunk_size: int = 1 * 1024 * 1024):
351 while True:
352 ck = file_object.read(chunk_size)
353 if not ck:
354 break
355 pbar.update(len(ck))
356 yield ck
357
358 with tqdm(
359 total=file_size,
360 unit='B',
361 unit_scale=True,
362 dynamic_ncols=True,
363 desc='[Pre-uploading] ') as pbar:
364 with open(file_path, 'rb') as f:
365 r = requests.put(
366 url,
367 headers=headers,

Callers

nothing calls this directly

Calls 4

get_endpointFunction · 0.90
putMethod · 0.80
updateMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected