Beispiel #1
0
def generate_component_run_script(component: ApiComponent, component_template_url, run_parameters=dict(),
                                  run_name: str = None):

    name = component.name + " " + generate_id(length=4)
    description = component.description.strip()

    pipeline_method_args = generate_pipeline_method_args(component.parameters)

    parameter_names = ",".join([p.name for p in component.parameters])

    parameter_dict = json.dumps({p.name: run_parameters.get(p.name) or p.default or ""
                                 for p in component.parameters},
                                indent=4).replace('"', "'")

    pipeline_server = "" if "POD_NAMESPACE" in os.environ else f"'{_pipeline_service_url}'"

    run_name = (run_name or "").replace("'", "\"") or component.name

    substitutions = dict(locals())

    template_file = f"run_component.TEMPLATE.py"

    with open(join(CODE_TEMPLATE_DIR, template_file), 'r') as f:
        template_raw = f.read()

    template_rendered = Template(template_raw).substitute(substitutions)

    run_script = autopep8.fix_code(template_rendered, options={"aggressive": 2})

    return run_script
def _store_pipeline(yaml_file_content: AnyStr, name=None, description=None):

    yaml_dict = yaml.load(yaml_file_content, Loader=yaml.FullLoader)

    template_metadata = yaml_dict.get("metadata") or dict()
    annotations = template_metadata.get("annotations", {})
    pipeline_spec = json.loads(annotations.get("pipelines.kubeflow.org/pipeline_spec", "{}"))

    name = name or template_metadata["name"]
    description = pipeline_spec.get("description", "").strip()
    namespace = pipeline_spec.get("namespace", "").strip()
    pipeline_id = "-".join([generate_id(length=l) for l in [8, 4, 4, 4, 12]])
    created_at = datetime.now()

    parameters = [ApiParameter(name=p.get("name"), description=p.get("description"),
                               default=p.get("default"), value=p.get("value"))
                  for p in yaml_dict["spec"].get("params", {})]

    api_pipeline = ApiPipeline(id=pipeline_id,
                               created_at=created_at,
                               name=name,
                               description=description,
                               parameters=parameters,
                               namespace=namespace)

    uuid = store_data(api_pipeline)

    api_pipeline.id = uuid

    store_file(bucket_name="mlpipeline", prefix=f"pipelines/",
               file_name=f"{pipeline_id}", file_content=yaml_file_content)

    enable_anonymous_read_access(bucket_name="mlpipeline", prefix="pipelines/*")

    return api_pipeline
def _upload_model_yaml(yaml_file_content: AnyStr, name=None, existing_id=None):

    model_def = yaml.load(yaml_file_content, Loader=yaml.FullLoader)

    api_model = ApiModel(
        id=existing_id or model_def.get("model_identifier")
        or generate_id(name=name or model_def["name"]),
        created_at=datetime.now(),
        name=name or model_def["name"],
        description=model_def["description"].strip(),
        domain=model_def.get("domain") or "",
        labels=model_def.get("labels") or dict(),
        framework=model_def["framework"],
        filter_categories=model_def.get("filter_categories") or dict(),
        trainable=model_def.get("train", {}).get("trainable") or False,
        trainable_tested_platforms=model_def.get(
            "train", {}).get("tested_platforms") or [],
        trainable_credentials_required=model_def.get(
            "train", {}).get("credentials_required") or False,
        trainable_parameters=model_def.get("train", {}).get("input_params")
        or [],
        servable=model_def.get("serve", {}).get("servable") or False,
        servable_tested_platforms=model_def.get(
            "serve", {}).get("tested_platforms") or [],
        servable_credentials_required=model_def.get(
            "serve", {}).get("credentials_required") or False,
        servable_parameters=model_def.get("serve", {}).get("input_params")
        or [])

    # convert comma-separate strings to lists
    if type(api_model.trainable_tested_platforms) == str:
        api_model.trainable_tested_platforms = api_model.trainable_tested_platforms.replace(
            ", ", ",").split(",")

    if type(api_model.servable_tested_platforms) == str:
        api_model.servable_tested_platforms = api_model.servable_tested_platforms.replace(
            ", ", ",").split(",")

    uuid = store_data(api_model)

    api_model.id = uuid

    store_file(bucket_name="mlpipeline",
               prefix=f"models/{api_model.id}/",
               file_name="template.yaml",
               file_content=yaml_file_content,
               content_type="text/yaml")

    enable_anonymous_read_access(bucket_name="mlpipeline", prefix="models/*")

    return api_model, 201
def _upload_component_yaml(yaml_file_content: AnyStr,
                           name=None,
                           existing_id=None):

    yaml_dict = yaml.load(yaml_file_content, Loader=yaml.FullLoader)

    template_metadata = yaml_dict.get("metadata") or dict()

    component_id = existing_id or generate_id(name=name or yaml_dict["name"])
    created_at = datetime.now()
    name = name or yaml_dict["name"]
    description = (yaml_dict.get("description") or name).strip()[:255]
    filter_categories = yaml_dict.get("filter_categories") or dict()

    metadata = ApiMetadata(annotations=template_metadata.get("annotations"),
                           labels=template_metadata.get("labels"),
                           tags=template_metadata.get("tags"))

    parameters = [
        ApiParameter(name=p.get("name"),
                     description=p.get("description"),
                     default=p.get("default"),
                     value=p.get("value"))
        for p in yaml_dict.get("inputs", [])
    ]

    api_component = ApiComponent(id=component_id,
                                 created_at=created_at,
                                 name=name,
                                 description=description,
                                 metadata=metadata,
                                 parameters=parameters,
                                 filter_categories=filter_categories)

    uuid = store_data(api_component)

    api_component.id = uuid

    store_file(bucket_name="mlpipeline",
               prefix=f"components/{component_id}/",
               file_name="template.yaml",
               file_content=yaml_file_content,
               content_type="text/yaml")

    enable_anonymous_read_access(bucket_name="mlpipeline",
                                 prefix="components/*")

    return api_component, 201
Beispiel #5
0
def generate_dataset_run_script(dataset: ApiDataset, dataset_template_url, run_parameters=dict(),
                                run_name: str = None, fail_on_missing_prereqs=False):

    name = f"{dataset.name} ({generate_id(length=4)})"
    description = dataset.description.strip().replace("'", "\\'")

    # TODO: some of the parameters, template URLs should move out of here

    # dataset_parameters = dataset.parameters
    # TODO: ApiParameters should not be defined here
    dataset_parameters = [ApiParameter(name="action", default="create"),
                          ApiParameter(name="namespace", default=_namespace)]

    pipeline_method_args = generate_pipeline_method_args(dataset_parameters)

    parameter_names = ",".join([p.name for p in dataset_parameters])

    # TODO: the action parameter is required by DLF-to-PVC op, so it should not be dynamically generated here
    parameter_dict = {
        "action": "create",
        "namespace": run_parameters.get("namespace", _namespace)
    }

    # see component name at https://github.com/machine-learning-exchange/mlx/blob/main/components/component-samples/dax-to-dlf/component.yaml#L1
    dax_to_dlf_component_id = generate_id(name="Generate Dataset Metadata")

    # see component name at https://github.com/machine-learning-exchange/mlx/blob/main/components/component-samples/dlf/component.yaml#L1
    dlf_to_pvc_component_id = generate_id(name="Create Dataset Volume")

    dax_to_dlf_component_url = get_object_url(bucket_name="mlpipeline",
                                              prefix=f"components/{dax_to_dlf_component_id}/",
                                              file_extensions=[".yaml"])

    dlf_to_pvc_component_url = get_object_url(bucket_name="mlpipeline",
                                              prefix=f"components/{dlf_to_pvc_component_id}/",
                                              file_extensions=[".yaml"])

    if fail_on_missing_prereqs:

        if not dax_to_dlf_component_url:
            raise ApiError(f"Missing required component '{dax_to_dlf_component_id}'")

        if not dlf_to_pvc_component_url:
            raise ApiError(f"Missing required component '{dlf_to_pvc_component_id}'")

    namespace = run_parameters.get("namespace", _namespace)

    pipeline_server = "" if "POD_NAMESPACE" in os.environ else f"'{_pipeline_service_url}'"

    run_name = (run_name or "").replace("'", "\"") or dataset.name

    substitutions = dict(locals())

    template_file = f"run_dataset.TEMPLATE.py"

    with open(join(CODE_TEMPLATE_DIR, template_file), 'r') as f:
        template_raw = f.read()

    template_rendered = Template(template_raw).substitute(substitutions)

    run_script = autopep8.fix_code(template_rendered, options={"aggressive": 2})

    return run_script
Beispiel #6
0
def _upload_notebook_yaml(yaml_file_content: AnyStr,
                          name=None,
                          access_token=None,
                          existing_id=None):

    yaml_dict = yaml.load(yaml_file_content, Loader=yaml.FullLoader)

    template_metadata = yaml_dict.get("metadata") or dict()

    notebook_id = existing_id or generate_id(name=name or yaml_dict["name"])
    created_at = datetime.now()
    name = name or yaml_dict["name"]
    description = yaml_dict["description"].strip()
    url = yaml_dict["implementation"]["github"]["source"]
    requirements = yaml_dict["implementation"]["github"].get("requirements")

    metadata = ApiMetadata(annotations=template_metadata.get("annotations"),
                           labels=template_metadata.get("labels"),
                           tags=template_metadata.get("tags"))

    notebook_content = _download_notebook(
        url, enterprise_github_api_token=access_token)

    # parameters = _extract_notebook_parameters(notebook_content)
    # TODO: not using Papermill any longer, notebook parameters no longer valid?
    #  kfp-notebook  has inputs and outputs ?
    parameters = dict()

    api_notebook = ApiNotebook(id=notebook_id,
                               created_at=created_at,
                               name=name,
                               description=description,
                               url=url,
                               metadata=metadata,
                               parameters=parameters)

    uuid = store_data(api_notebook)

    api_notebook.id = uuid

    store_file(bucket_name="mlpipeline",
               prefix=f"notebooks/{notebook_id}/",
               file_name="template.yaml",
               file_content=yaml_file_content)

    s3_url = store_file(bucket_name="mlpipeline",
                        prefix=f"notebooks/{notebook_id}/",
                        file_name=url.split("/")[-1].split("?")[0],
                        file_content=json.dumps(notebook_content).encode())

    if requirements:

        if _is_url(requirements):
            requirements_url = requirements
            requirements_txt = download_file_content_from_url(
                requirements_url).decode()
        else:
            requirements_txt = "\n".join(requirements.split(","))

        # TODO: remove this after fixing the Elyra-AI/KFP-Notebook runner so that
        #   Elyra should install its own requirements in addition to the provided requirements
        requirements_elyra_url = "https://github.com/elyra-ai/kfp-notebook/blob/master/etc/requirements-elyra.txt"
        requirements_elyra_txt = download_file_content_from_url(
            requirements_elyra_url).decode()
        requirements_elyra = "\n".join([
            line for line in requirements_elyra_txt.split("\n")
            if not line.startswith("#")
        ])

        requirements_all = f"# Required packages for {api_notebook.name}:\n" \
                           f"{requirements_txt}\n" \
                           f"# Requirements from {requirements_elyra_url}:\n" \
                           f"{requirements_elyra}"

        store_file(bucket_name="mlpipeline",
                   prefix=f"notebooks/{notebook_id}/",
                   file_name="requirements.txt",
                   file_content=requirements_all.encode())

    # if the url included an access token, replace the original url with the s3 url
    if "?token=" in url or "github.ibm.com" in url:
        api_notebook.url = s3_url
        update_multiple(ApiNotebook, [notebook_id], "url", s3_url)
        enable_anonymous_read_access(bucket_name="mlpipeline",
                                     prefix="notebooks/*")

    return api_notebook, 201
def _upload_dataset_yaml(yaml_file_content: AnyStr,
                         name=None,
                         existing_id=None):

    yaml_dict = yaml.load(yaml_file_content, Loader=yaml.FullLoader)

    name = name or yaml_dict["name"]
    description = yaml_dict["description"]
    dataset_id = existing_id or generate_id(name=yaml_dict.get("id", name))
    created_at = datetime.now()

    # if yaml_dict.get("id") != dataset_id:
    #     raise ValueError(f"Dataset.id contains non k8s character: {yaml_dict.get('id')}")

    # TODO: re-evaluate if we should use dataset update time as our MLX "created_at" time
    if "updated" in yaml_dict:
        created_at = datetime.strptime(str(yaml_dict["updated"]), "%Y-%m-%d")
    elif "created" in yaml_dict:
        created_at = datetime.strptime(str(yaml_dict["created"]), "%Y-%m-%d")

    license_name = yaml_dict["license"]["name"]
    domain = yaml_dict["domain"]
    format_type = yaml_dict["format"][0]["type"]
    size = yaml_dict["content"][0].get("size")
    version = yaml_dict["version"]

    # # extract number of records and convert thousand separators based on Locale
    # num_records_str = yaml_dict["statistics"]["number_of_records"]
    # num_records_number_str = num_records_str.split()[0]. \
    #     replace("~", ""). \
    #     replace("+", ""). \
    #     replace("k", "000"). \
    #     replace(",", "")  # assumes thousand separators in locale.en_US.UTF-8
    # # locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')  # setting locale does not work reliably in Docker
    # # number_of_records = locale.atoi(num_records_number_str)
    # number_of_records = int(num_records_number_str)
    number_of_records = yaml_dict["content"][0].get("records", 0)

    related_assets = [
        a["application"].get("asset_id")
        for a in yaml_dict.get("related_assets", [])
        if "MLX" in a.get("application", {}).get("name", "")
        and "asset_id" in a.get("application", {})
    ]

    template_metadata = yaml_dict.get("metadata") or dict()
    metadata = ApiMetadata(annotations=template_metadata.get("annotations"),
                           labels=template_metadata.get("labels"),
                           tags=template_metadata.get("tags")
                           or yaml_dict.get("seo_tags"))

    # TODO: add "version" to ApiDataset

    api_dataset = ApiDataset(id=dataset_id,
                             created_at=created_at,
                             name=name,
                             description=description,
                             domain=domain,
                             format=format_type,
                             size=size,
                             number_of_records=number_of_records,
                             license=license_name,
                             metadata=metadata,
                             related_assets=related_assets)

    uuid = store_data(api_dataset)

    api_dataset.id = uuid

    store_file(bucket_name="mlpipeline",
               prefix=f"datasets/{api_dataset.id}/",
               file_name="template.yaml",
               file_content=yaml_file_content)

    enable_anonymous_read_access(bucket_name="mlpipeline", prefix="datasets/*")

    return api_dataset, 201