예제 #1
0
def _push_training(model_config, code_dir, endpoint=None, token=None):
    try:
        from datarobot._experimental import CustomTrainingBlueprint, CustomTrainingModel
    except ImportError:
        raise DrumCommonException(
            "You tried to run custom training models using a version of the \n"
            "datarobot client which doesn't have this beta functionality yet. \n"
            "Please pip install datarobot>=2.22.0b0 to access this functionality. \n"
            "This requires adding the internal datarobot artifactory index \n"
            "as your pip index. "
        )
    dr_client.Client(token=token, endpoint=endpoint)
    if "modelID" in model_config:
        model_id = model_config["modelID"]
    else:
        model_id = CustomTrainingModel.create(
            name=model_config["name"],
            target_type=_convert_target_type(model_config["targetType"]),
            description=model_config.get("description", "Pushed from DRUM"),
        ).id
        print(
            "You just created a new custom model. Please add this model ID to your metadata file "
            "by adding the line 'modelID:{}'".format(model_id)
        )

    try:
        dr_client.CustomModelVersion.create_clean(
            model_id,
            base_environment_id=model_config["environmentID"],
            folder_path=code_dir,
            is_major_update=model_config.get("majorVersion", True),
        )
    except dr_client.errors.ClientError as e:
        print("Error adding model with ID {} and dir {}: {}".format(model_id, code_dir, str(e)))
        raise SystemExit(1)

    blueprint = CustomTrainingBlueprint.create(
        environment_id=model_config["environmentID"],
        custom_model_id=model_id,
    )

    print("A blueprint was created with the ID {}".format(blueprint.id))

    _print_model_started_dialogue(model_id)

    if "trainOnProject" in model_config.get("trainingModel", ""):
        try:
            project = dr_client.Project(model_config["trainingModel"]["trainOnProject"])
            model_job_id = project.train(blueprint)
            lid = dr_client.ModelJob.get(project_id=project.id, model_job_id=model_job_id).model_id
        except dr_client.errors.ClientError as e:
            print("There was an error training your model: {}".format(e))
            raise SystemExit()
        print("\nIn addition...")
        print("Model training has started! Follow along at this link: ")
        print(
            MODEL_LOGS_LINK_FORMAT.format(
                url=re.sub(r"/api/v2/?", "", dr_client.client._global_client.endpoint),
                model_id=lid,
                project_id=model_config["trainingModel"]["trainOnProject"],
            )
        )
예제 #2
0
def _push_training(model_config, code_dir, endpoint=None, token=None):
    try:
        from datarobot._experimental import CustomTrainingBlueprint, CustomTrainingModel
    except ImportError:
        raise DrumCommonException(
            "You tried to run custom training models using a version of the \n"
            "datarobot client which doesn't have this beta functionality yet. \n"
            "Please pip install datarobot>=2.22.0b0 to access this functionality. \n"
            "This requires adding the internal datarobot artifactory index \n"
            "as your pip index. ")
    dr_client.Client(token=token, endpoint=endpoint)
    if ModelMetadataKeys.MODEL_ID in model_config:
        model_id = model_config[ModelMetadataKeys.MODEL_ID]
    else:
        model_id = CustomTrainingModel.create(
            name=model_config[ModelMetadataKeys.NAME],
            target_type=_convert_target_type(
                model_config[ModelMetadataKeys.TARGET_TYPE]),
            description=model_config.get("description", "Pushed from DRUM"),
        ).id
        print(
            "You just created a new custom model. Please add this model ID to your metadata file "
            "by adding the line 'modelID:{}'".format(model_id))

    try:
        model_version = dr_client.CustomModelVersion.create_clean(
            model_id,
            base_environment_id=model_config[ModelMetadataKeys.ENVIRONMENT_ID],
            folder_path=code_dir,
            is_major_update=model_config.get(ModelMetadataKeys.MAJOR_VERSION,
                                             True),
        )
    except dr_client.errors.ClientError as e:
        print("Error adding model with ID {} and dir {}: {}".format(
            model_id, code_dir, str(e)))
        raise SystemExit(1)

    # TODO: Update this once the datarobot client is updated
    payload = dict(custom_model_version_id=model_version.id)
    response = dr_client.client.get_client().post("customTrainingBlueprints/",
                                                  data=payload)
    user_blueprint_id = response.json()["userBlueprintId"]

    print("A user blueprint was created with the ID {}".format(
        user_blueprint_id))

    _print_model_started_dialogue(model_id)

    if "trainOnProject" in model_config.get("trainingModel", ""):
        try:
            pid = model_config["trainingModel"]["trainOnProject"]
            current_task = "fetching the specified project {}".format(pid)
            project = dr_client.Project(pid)

            # TODO: Update this once the datarobot client is updated
            payload = dict(user_blueprint_id=user_blueprint_id)
            current_task = "adding your model to the menu"
            response = dr_client.client.get_client().post(
                "projects/{}/blueprints/fromUserBlueprint/".format(pid),
                data=payload)
            blueprint_id = response.json()["id"]

            current_task = "actually training of blueprint {}".format(
                blueprint_id)
            model_job_id = project.train(blueprint_id)
            lid = dr_client.ModelJob.get(project_id=pid,
                                         model_job_id=model_job_id).model_id
        except dr_client.errors.ClientError as e:
            print("There was an error training your model while {}: {}".format(
                current_task, e))
            raise SystemExit(1)
        print("\nIn addition...")
        print("Model training has started! Follow along at this link: ")
        print(
            MODEL_LOGS_LINK_FORMAT.format(
                url=re.sub(r"/api/v2/?", "",
                           dr_client.client._global_client.endpoint),
                model_id=lid,
                project_id=model_config["trainingModel"]["trainOnProject"],
            ))