Skip to content

Commit 999be58

Browse files
committed
ODSC-29065: fix some bug;
1 parent 4d06a9c commit 999be58

File tree

4 files changed

+64
-66
lines changed

4 files changed

+64
-66
lines changed

ads/opctl/backend/local.py

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
)
5353
from ads.pipeline.ads_pipeline import Pipeline, PipelineStep
5454
from ads.common.oci_client import OCIClientFactory
55+
from ads.config import NO_CONTAINER
5556

5657
class CondaPackNotFound(Exception): # pragma: no cover
5758
pass
@@ -679,9 +680,11 @@ def predict(self) -> None:
679680
None
680681
Nothing.
681682
"""
683+
684+
# model artifact in artifact directory
682685
artifact_directory = self.config["execution"].get("artifact_directory")
683686
ocid = self.config["execution"].get("ocid")
684-
data = self.config["execution"].get("payload")
687+
685688
model_folder = os.path.expanduser(
686689
self.config["execution"].get("model_save_folder", DEFAULT_MODEL_FOLDER)
687690
)
@@ -706,70 +709,62 @@ def predict(self) -> None:
706709
timeout=timeout,
707710
force_overwrite=True,
708711
)
709-
conda_slug, conda_path = None, None
710-
if ocid:
712+
if (
713+
not os.path.exists(artifact_directory)
714+
or len(os.listdir(artifact_directory)) == 0
715+
):
716+
raise ValueError(
717+
f"`artifact_directory` {artifact_directory} does not exist or is empty."
718+
)
719+
720+
# conda
721+
conda_slug, conda_path = self.config["execution"].get("conda_slug"), self.config["execution"].get("conda_path")
722+
if not conda_slug and not conda_path and ocid:
711723
conda_slug, conda_path = self._get_conda_info_from_custom_metadata(ocid)
712-
if not conda_path:
713-
if (
714-
not os.path.exists(artifact_directory)
715-
or len(os.listdir(artifact_directory)) == 0
716-
):
717-
raise ValueError(
718-
f"`artifact_directory` {artifact_directory} does not exist or is empty."
719-
)
724+
if not conda_path and not conda_path:
720725
conda_slug, conda_path = self._get_conda_info_from_runtime(
721726
artifact_dir=artifact_directory
722727
)
723-
if not conda_path or not conda_slug:
724-
raise ValueError("Conda information cannot be detected.")
725-
compartment_id = self.config["execution"].get(
726-
"compartment_id", self.config["infrastructure"].get("compartment_id")
727-
)
728-
project_id = self.config["execution"].get(
729-
"project_id", self.config["infrastructure"].get("project_id")
730-
)
731-
if not compartment_id or not project_id:
732-
raise ValueError("`compartment_id` and `project_id` must be provided.")
733-
extra_cmd = (
734-
DEFAULT_MODEL_DEPLOYMENT_FOLDER
735-
+ " "
736-
+ data
737-
+ " "
738-
+ compartment_id
739-
+ " "
740-
+ project_id
741-
)
728+
729+
self.config["execution"]["image"] = ML_JOB_IMAGE
730+
731+
# bind_volumnes
742732
bind_volumes = {}
733+
SCRIPT = "script.py"
743734
if not is_in_notebook_session():
744735
bind_volumes = {
745736
os.path.expanduser(
746737
os.path.dirname(self.config["execution"]["oci_config"])
747738
): {"bind": os.path.join(DEFAULT_IMAGE_HOME_DIR, ".oci")}
748739
}
749740
dir_path = os.path.dirname(os.path.realpath(__file__))
750-
script = "script.py"
741+
751742
self.config["execution"]["source_folder"] = os.path.abspath(
752743
os.path.join(dir_path, "..")
753744
)
754-
self.config["execution"]["entrypoint"] = script
745+
self.config["execution"]["entrypoint"] = SCRIPT
755746
bind_volumes[artifact_directory] = {"bind": DEFAULT_MODEL_DEPLOYMENT_FOLDER}
756-
if self.config["execution"].get("conda_slug", conda_slug):
757-
self.config["execution"]["image"] = ML_JOB_IMAGE
758-
if not self.config["execution"].get("conda_slug"):
759-
self.config["execution"]["conda_slug"] = conda_slug
760-
self.config["execution"]["slug"] = conda_slug
761-
self.config["execution"]["conda_path"] = conda_path
762-
exit_code = self._run_with_conda_pack(
763-
bind_volumes, extra_cmd, install=True, conda_uri=conda_path
764-
)
747+
748+
# payload
749+
data = self.config["execution"].get("payload")
750+
751+
if is_in_notebook_session() or NO_CONTAINER:
752+
script_path = os.path.join(self.config['execution']['source_folder'], SCRIPT)
753+
run_command(cmd=f"python {script_path} " + f"{artifact_directory} "+ f"'{data}'", shell=True)
765754
else:
766-
raise ValueError("Either conda pack info or image should be specified.")
767-
768-
if exit_code != 0:
769-
raise RuntimeError(
770-
f"`predict` did not complete successfully. Exit code: {exit_code}. "
771-
f"Run with the --debug argument to view container logs."
772-
)
755+
extra_cmd = (
756+
DEFAULT_MODEL_DEPLOYMENT_FOLDER
757+
+ " "
758+
+ data
759+
)
760+
exit_code = self._run_with_conda_pack(
761+
bind_volumes, extra_cmd, install=True, conda_uri=conda_path
762+
)
763+
if exit_code != 0:
764+
raise RuntimeError(
765+
f"`predict` did not complete successfully. Exit code: {exit_code}. "
766+
f"Run with the --debug argument to view container logs."
767+
)
773768

774769
def _get_conda_info_from_custom_metadata(self, ocid):
775770
"""

ads/opctl/cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ def init(debug: bool, **kwargs: Dict[str, Any]) -> None:
544544
suppress_traceback(debug)(init_cmd)(**kwargs)
545545

546546

547+
@commands.command()
547548
@click.option(
548549
"--ocid",
549550
nargs=1,
@@ -597,7 +598,13 @@ def init(debug: bool, **kwargs: Dict[str, Any]) -> None:
597598
"--conda-slug",
598599
nargs=1,
599600
required=False,
600-
help="The conda env used to load the model and conduct the prediction. This is only used when model id is passed to `ocid` and a local predict is conducted. It should match the inference conda env specified in the runtime.yaml file which is the conda pack being used when conducting real model deployment.",
601+
help="The conda slug used to load the model and conduct the prediction. This is only used when model id is passed to `ocid` and a local predict is conducted. It should match the inference conda env specified in the runtime.yaml file which is the conda pack being used when conducting real model deployment.",
602+
)
603+
@click.option(
604+
"--conda-path",
605+
nargs=1,
606+
required=False,
607+
help="The conda path used to load the model and conduct the prediction. This is only used when model id is passed to `ocid` and a local predict is conducted. It should match the inference conda env specified in the runtime.yaml file which is the conda pack being used when conducting real model deployment.",
601608
)
602609
@click.option(
603610
"--model-version",

ads/opctl/model/cmds.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def _download_model(
5757
ocid, artifact_directory, oci_auth, region, bucket_uri, timeout, force_overwrite
5858
):
5959
os.makedirs(artifact_directory, exist_ok=True)
60-
os.chmod(artifact_directory, 777)
6160

6261
try:
6362
dsc_model = DataScienceModel.from_id(ocid)

ads/opctl/script.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
import json
22
import sys
3-
import tempfile
43

54
from ads.model.generic_model import GenericModel
65

76

8-
def verify(artifact_dir, data, compartment_id, project_id): # pragma: no cover
9-
with tempfile.TemporaryDirectory() as td:
10-
model = GenericModel.from_model_artifact(
11-
uri=artifact_dir,
12-
artifact_dir=artifact_dir,
13-
force_overwrite=True,
14-
compartment_id=compartment_id,
15-
project_id=project_id,
16-
)
7+
def verify(artifact_dir, data): # pragma: no cover
178

18-
try:
19-
data = json.loads(data)
20-
except:
21-
pass
22-
print(model.verify(data, auto_serialize_data=False))
9+
model = GenericModel.from_model_artifact(
10+
uri=artifact_dir,
11+
artifact_dir=artifact_dir,
12+
force_overwrite=True,
13+
)
14+
15+
try:
16+
data = json.loads(data)
17+
except:
18+
pass
19+
print(model.verify(data, auto_serialize_data=False))
2320

2421

2522
def main(): # pragma: no cover
2623
args = sys.argv[1:]
2724
verify(
28-
artifact_dir=args[0], data=args[1], compartment_id=args[2], project_id=args[3]
25+
artifact_dir=args[0], data=args[1]
2926
)
3027
return 0
3128

0 commit comments

Comments
 (0)