Skip to content

Commit 7298b2c

Browse files
committed
feat: upload local model without any extra file generation (PMMODEL-682)
1 parent a789660 commit 7298b2c

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/sasctl/tasks.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pathlib import Path
1616
from typing import Union
1717
from warnings import warn
18+
import zipfile
1819

1920
import pandas as pd
2021

@@ -998,3 +999,55 @@ def score_model_with_cas(
998999
print(score_execution_poll)
9991000
score_results = se.get_score_execution_results(score_execution, use_cas_gateway)
10001001
return score_results
1002+
1003+
def upload_local_model(
1004+
path: Union[str, Path],
1005+
model_name: str,
1006+
project_name: str,
1007+
repo_name: Union[str, dict] = None,
1008+
version: str = "latest",
1009+
):
1010+
"""A barebones function to upload a model and any associated files to the model repository.
1011+
Parameters
1012+
----------
1013+
path : Union[str, Path]
1014+
The path to the model and any associated files.
1015+
model_name : str
1016+
The name of the model.
1017+
project_name : str
1018+
The name of the project to which the model will be uploaded.
1019+
"""
1020+
# Use default repository if not specified
1021+
try:
1022+
if repo_name is None:
1023+
repository = mr.default_repository()
1024+
else:
1025+
repository = mr.get_repository(repo_name)
1026+
except HTTPError as e:
1027+
if e.code == 403:
1028+
raise AuthorizationError(
1029+
"Unable to register model. User account does not have read permissions "
1030+
"for the /modelRepository/repositories/ URL. Please contact your SAS "
1031+
"Viya administrator."
1032+
)
1033+
raise e
1034+
1035+
# Unable to find or create the repo.
1036+
if repository is None and repo_name is None:
1037+
raise ValueError("Unable to find a default repository")
1038+
1039+
if repository is None:
1040+
raise ValueError("Unable to find repository '{}'".format(repository))
1041+
p = mr.get_project(project_name)
1042+
if p is None:
1043+
mr.create_project(project_name, repository)
1044+
zip_name = str(Path(path) / (model_name + ".zip"))
1045+
file_names = sorted(Path(path).glob("*[!zip]"))
1046+
with zipfile.ZipFile(
1047+
str(zip_name), mode="w"
1048+
) as zFile:
1049+
for file in file_names:
1050+
zFile.write(str(file), arcname=file.name)
1051+
with open(zip_name, "rb") as zip_file:
1052+
model = mr.import_model_from_zip(model_name, project_name, zip_file, version=version)
1053+
return model

0 commit comments

Comments
 (0)