HubApi

class swift.hub.api.HubApi(endpoint=None)[source]

Model hub api interface.

__init__(endpoint=None)[source]

The ModelScope HubApi。

Parameters:

endpoint (str, optional) – The modelscope server http|https address. Defaults to None.

login(access_token)[source]
Login with your SDK access token, which can be obtained from

https://www.modelscope.cn user center.

Parameters:

access_token (str) – user access token on modelscope.

Returns:

to authenticate yourself to ModelScope open-api git_token: token to access your git repository.

Return type:

cookies

Note

You only have to login once within 30 days.

create_model(model_id, visibility=5, license='Apache License 2.0', chinese_name=None, original_model_id='')[source]

Create model repo at ModelScope Hub.

Parameters:
  • model_id (str) – The model id

  • visibility (int, optional) – visibility of the model(1-private, 5-public), default 5.

  • license (str, optional) – license of the model, default none.

  • chinese_name (str, optional) – chinese name of the model.

  • original_model_id (str, optional) – the base model id which this model is trained from

Returns:

Name of the model created

Raises:
  • InvalidParameter – If model_id is invalid.

  • ValueError – If not login.

Return type:

str

Note

model_id = {owner}/{name}

delete_model(model_id)[source]

Delete model_id from ModelScope.

Parameters:

model_id (str) – The model id.

Raises:

ValueError – If not login.

Note

model_id = {owner}/{name}

get_model(model_id, revision=None)[source]

Get model information at ModelScope

Parameters:
  • model_id (str) – The model id.

  • revision (str optional) – revision of model.

Returns:

The model detail information.

Raises:

NotExistError – If the model is not exist, will throw NotExistError

Return type:

str

Note

model_id = {owner}/{name}

push_model(model_id, model_dir, visibility=5, license='Apache License 2.0', chinese_name=None, commit_message='upload model', tag=None, revision='master', original_model_id=None, ignore_file_pattern=None)[source]

Upload model from a given directory to given repository. A valid model directory must contain a configuration.json file.

This function upload the files in given directory to given repository. If the given repository is not exists in remote, it will automatically create it with given visibility, license and chinese_name parameters. If the revision is also not exists in remote repository, it will create a new branch for it.

This function must be called before calling HubApi’s login with a valid token which can be obtained from ModelScope’s website.

Parameters:
  • model_id (str) – The model id to be uploaded, caller must have write permission for it.

  • model_dir (str) – The Absolute Path of the finetune result.

  • visibility (int, optional) – Visibility of the new created model(1-private, 5-public). If the model is not exists in ModelScope, this function will create a new model with this visibility and this parameter is required. You can ignore this parameter if you make sure the model’s existence.

  • license (str, defaults to None) – License of the new created model(see License). If the model is not exists in ModelScope, this function will create a new model with this license and this parameter is required. You can ignore this parameter if you make sure the model’s existence.

  • chinese_name (str, optional, defaults to None) – chinese name of the new created model.

  • commit_message (str, optional, defaults to None) – commit message of the push request.

  • tag (str, optional, defaults to None) – The tag on this commit

  • revision (str, optional, default to DEFAULT_MODEL_REVISION) – which branch to push. If the branch is not exists, It will create a new branch and push to it.

  • original_model_id (str, optional) – The base model id which this model is trained from

  • ignore_file_pattern (Union[List[str], str], optional) – The file pattern to ignore uploading

Raises:
  • InvalidParameter – Parameter invalid.

  • NotLoginException – Not login

  • ValueError – No configuration.json

  • Exception – Create failed.

list_models(owner_or_group, page_number=1, page_size=10)[source]

List models in owner or group.

Parameters:
  • owner_or_group (str) – owner or group.

  • page_number (int, optional) – The page number, default: 1

  • page_size (int, optional) – The page size, default: 10

Raises:

RequestError – The request error.

Returns:

{“models”: “list of models”, “TotalCount”: total_number_of_models_in_owner_or_group}

Return type:

dict

list_model_revisions(model_id, cutoff_timestamp=None, use_cookies=False)[source]

Get model branch and tags.

Parameters:
  • model_id (str) – The model id

  • cutoff_timestamp (int) – Tags created before the cutoff will be included. The timestamp is represented by the seconds elapsed from the epoch time.

  • use_cookies (Union[bool, CookieJar], optional) – If is cookieJar, we will use this cookie, if True, will load cookie from local. Defaults to False.

Returns:

Return list of branch name and tags

Return type:

Tuple[List[str], List[str]]

get_model_branches_and_tags(model_id, use_cookies=False)[source]

Get model branch and tags.

Parameters:
  • model_id (str) – The model id

  • use_cookies (Union[bool, CookieJar], optional) – If is cookieJar, we will use this cookie, if True, will load cookie from local. Defaults to False.

Returns:

Return list of branch name and tags

Return type:

Tuple[List[str], List[str]]

get_model_files(model_id, revision=None, root=None, recursive=False, use_cookies=False, headers={})[source]

List the models files.

Parameters:
  • model_id (str) – The model id

  • revision (Optional[str], optional) – The branch or tag name.

  • root (Optional[str], optional) – The root path. Defaults to None.

  • recursive (Optional[str], optional) – Is recursive list files. Defaults to False.

  • use_cookies (Union[bool, CookieJar], optional) – If is cookieJar, we will use this cookie, if True, will load cookie from local. Defaults to False.

  • headers (dict | None) – request headers

Returns:

Model file list.

Return type:

List[dict]

static fetch_meta_files_from_url(url, out_path, chunk_size=1024, mode='reuse_dataset_if_exists')[source]

Fetch the meta-data files from the url, e.g. csv/jsonl files.