|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import pickle |
| 4 | +import yaml |
| 5 | +import joblib |
| 6 | +from ibm_watson_machine_learning import APIClient |
| 7 | + |
| 8 | +MODEL_PATH = os.path.abspath(sys.argv[1]) |
| 9 | +PROJ_PATH = os.path.abspath(sys.argv[2]) |
| 10 | +CRED_PATH = os.path.abspath(sys.argv[3]) |
| 11 | +META_PATH = PROJ_PATH + "/metadata.yaml" |
| 12 | + |
| 13 | + |
| 14 | +with open(CRED_PATH) as stream: |
| 15 | + try: |
| 16 | + credentials = yaml.safe_load(stream) |
| 17 | + except yaml.YAMLError as exc: |
| 18 | + print(exc) |
| 19 | + |
| 20 | + |
| 21 | +with open(META_PATH) as stream: |
| 22 | + try: |
| 23 | + metadata = yaml.safe_load(stream) |
| 24 | + except yaml.YAMLError as exc: |
| 25 | + print(exc) |
| 26 | + |
| 27 | +with open(MODEL_PATH, "rb") as file: |
| 28 | + # pickle_model = pickle.load(file) |
| 29 | + pipeline = joblib.load(file) |
| 30 | + |
| 31 | +wml_credentials = {"url": credentials["url"], "apikey": credentials["apikey"]} |
| 32 | + |
| 33 | +client = APIClient(wml_credentials) |
| 34 | +client.spaces.list() |
| 35 | + |
| 36 | +MODEL_NAME = metadata["project_name"] + "_" + metadata["project_version"] |
| 37 | +DEPLOY_NAME = MODEL_NAME + "-Deployment" |
| 38 | +MODEL = pipeline |
| 39 | +SPACE_ID = credentials["space_id"] |
| 40 | + |
| 41 | +client.set.default_space(SPACE_ID) |
| 42 | + |
| 43 | +model_props = { |
| 44 | + client.repository.ModelMetaNames.NAME: MODEL_NAME, |
| 45 | + client.repository.ModelMetaNames.TYPE: metadata["model_type"], |
| 46 | + client.repository.ModelMetaNames.SOFTWARE_SPEC_UID: client.software_specifications.get_id_by_name( |
| 47 | + "default_py3.7" |
| 48 | + ), |
| 49 | +} |
| 50 | + |
| 51 | +model_details = client.repository.store_model(model=MODEL, meta_props=model_props) |
| 52 | +model_uid = client.repository.get_model_uid(model_details) |
| 53 | + |
| 54 | +deployment_props = { |
| 55 | + client.deployments.ConfigurationMetaNames.NAME: DEPLOY_NAME, |
| 56 | + client.deployments.ConfigurationMetaNames.ONLINE: {}, |
| 57 | +} |
| 58 | + |
| 59 | +deployment = client.deployments.create( |
| 60 | + artifact_uid=model_uid, |
| 61 | + meta_props=deployment_props, |
| 62 | +) |
0 commit comments