예제 #1
0
    def get_project(self, project_name: str) -> Project:
        """
        Get a project in Merlin and optionally assign list of readers and administrators.
        The identity used for creating the project will be automatically included as project's administrators.

        :param project_name: project name
        :return: project
        """
        if not valid_name_check(project_name):
            raise ValueError(
                '''Your project/model name contains invalid characters.\
                    \nUse only the following characters\
                    \n- Characters: a-z (Lowercase ONLY)\
                    \n- Numbers: 0-9\
                    \n- Symbols: -
                ''')

        p_list = self._project_api.projects_get(name=project_name)
        p = None
        for prj in p_list:
            if prj.name == project_name:
                p = prj

        if p is None:
            raise Exception(
                f"{project_name} does not exist or you don't have access to the project. Please create new "
                f"project using MLP console or ask the project's administrator to be able to access "
                f"existing project.")

        return Project(p, self.url, self._api_client)
예제 #2
0
def project(url, mlflow_url, api_client):
    prj = cl.Project(
        id=1,
        name="my-project",
        mlflow_tracking_url=mlflow_url,
        created_at="2019-08-29T08:13:12.377Z",
        updated_at="2019-08-29T08:13:12.377Z",
    )
    return Project(prj, url, api_client)
예제 #3
0
    def list_project(self) -> List[Project]:
        """
        List project in the connected MLP server

        :return: list of Project
        """
        p_list = self._project_api.projects_get()
        result = []
        for p in p_list:
            result.append(Project(p, self.url, self._api_client))
        return result
예제 #4
0
def test_create_model(mock_url, api_client, mock_oauth, use_google_oauth):
    project_id = '1010'
    mlflow_experiment_id = 1
    model_name = "my-model"
    project_name = "my-project"
    model_type = ModelType.XGBOOST
    mlflow_url = "http://mlflow.api.merlin.dev"

    responses.add('GET',
                  f"/api/v1/projects/{project_id}/models",
                  body='[]',
                  status=200,
                  content_type='application/json')
    responses.add('POST',
                  f"/api/v1/projects/{project_id}/models",
                  body=f"""{{
                        "id": 0,
                        "project_id": {project_id},
                        "mlflow_experiment_id": {mlflow_experiment_id},
                        "name": "{model_name}",
                        "type": "{model_type.value}",
                        "mlflow_url": "{mlflow_url}",
                        "endpoints": [],
                        "created_at": "{created_at}",
                        "updated_at": "{updated_at}"
                      }}""",
                  status=200,
                  content_type='application/json')

    client = MerlinClient(mock_url, use_google_oauth=use_google_oauth)
    prj = cl.Project(project_id, project_name, mlflow_tracking_url, created_at,
                     updated_at)
    project = Project(prj, mock_url, api_client)
    with mock.patch.object(client, "get_project", return_value=project):
        model = client.get_or_create_model("my-model",
                                           project_name=project_name,
                                           model_type=model_type)

        assert json.loads(responses.calls[-1].request.body) == json.loads(f"""
        {{
            "name" : "{model_name}",
            "type" : "{model_type.value}"
        }}
        """)
        assert model.id == 0
        assert model.mlflow_experiment_id == mlflow_experiment_id
        assert model.name == model_name
        assert model.type == model_type
        assert model.mlflow_url == mlflow_tracking_url
        assert model.mlflow_experiment_id == mlflow_experiment_id
        assert isinstance(model.created_at, datetime.datetime)
        assert isinstance(model.updated_at, datetime.datetime)
        assert model.project == project
예제 #5
0
def test_get_model(mock_url, api_client, mock_oauth, use_google_oauth):
    project_id = '1010'
    mlflow_experiment_id = 1
    model_name = "my-model"
    project_name = "my-project"
    model_type = ModelType.XGBOOST
    mlflow_url = "http://mlflow.api.merlin.dev"

    responses.add('GET',
                  f"/api/v1/projects/{project_id}/models",
                  body=f"""[{{
                        "id": 1,
                        "project_id": {project_id},
                        "mlflow_experiment_id": {mlflow_experiment_id},
                        "name": "{model_name}",
                        "type": "{model_type.value}",
                        "mlflow_url": "{mlflow_url}",
                        "endpoints": [],
                        "created_at": "{created_at}",
                        "updated_at": "{updated_at}"
                      }}]""",
                  status=200,
                  content_type='application/json')

    responses.add("GET",
                  f"/api/v1/models/1/endpoints",
                  body=json.dumps([mdl_endpoint_1.to_dict()]),
                  status=200,
                  content_type='application/json')

    client = MerlinClient(mock_url, use_google_oauth=use_google_oauth)
    prj = cl.Project(project_id, project_name, mlflow_tracking_url, created_at,
                     updated_at)
    project = Project(prj, mock_url, api_client)
    with mock.patch.object(client, "get_project", return_value=project):
        model = client.get_or_create_model("my-model",
                                           project_name=project_name,
                                           model_type=model_type)
        assert model.id == 1
        assert model.name == model_name
        assert model.type == model_type
        assert model.mlflow_url == mlflow_tracking_url
        assert model.mlflow_experiment_id == mlflow_experiment_id
        assert isinstance(model.created_at, datetime.datetime)
        assert isinstance(model.updated_at, datetime.datetime)
        assert model.project == project

        default_model_endpoint = model.endpoint
        assert default_model_endpoint is not None
        assert default_model_endpoint.status == Status.SERVING
        assert default_model_endpoint.environment_name == env_1.name
예제 #6
0
def test_new_model_version(mock_url, api_client, mock_oauth, use_google_oauth):
    project_id = 1
    model_id = 1
    version_id = 2
    model_name = "my-model"
    project_name = "my-project"
    mlflow_experiment_id = 1
    mlflow_run_id = "c5c3b6b220b34c7496de8c0400b7c793"
    model_type = ModelType.TENSORFLOW
    mlflow_url = "http://mlflow.api.merlin.dev"
    artifact_uri = "gs://zltest/model"
    created_at = "2019-09-04T03:09:13.842Z"
    updated_at = "2019-09-04T03:09:13.843Z"

    responses.add('POST',
                  f"/api/v1/models/{model_id}/versions",
                  body=f"""{{
                        "id": {version_id},
                        "model_id": {model_id},
                        "mlflow_run_id": "{mlflow_run_id}",
                        "mlflow_url": "{mlflow_url}",
                        "artifact_uri": "{artifact_uri}",
                        "endpoints": [],
                        "mlflow_url": "{mlflow_url}",
                        "created_at": "{created_at}",
                        "updated_at": "{updated_at}"
                      }}""",
                  status=200,
                  content_type='application/json')

    client = MerlinClient(mock_url, use_google_oauth=use_google_oauth)
    prj = cl.Project(project_id, project_name, mlflow_tracking_url, created_at,
                     updated_at)
    project = Project(prj, mock_url, api_client)
    mdl = cl.Model(model_id, project_id, mlflow_experiment_id, model_name,
                   model_type.value, mlflow_url, None, created_at, updated_at)
    mdl = Model(mdl, project, api_client)
    with mock.patch.object(client, "get_model", return_value=mdl):
        mv = client.new_model_version(model_name, project_name)

        assert mv.id == version_id
        assert mv.mlflow_run_id == mlflow_run_id
        assert mv.mlflow_url == mlflow_url
        assert mv.properties is None
        assert isinstance(mv.created_at, datetime.datetime)
        assert isinstance(mv.updated_at, datetime.datetime)
        assert mv.model == mdl
        ui_url = guess_mlp_ui_url(mock_url)
        assert mv.url == f"{ui_url}/projects/1/models/{model_id}/versions"