コード例 #1
0
def prepare_parameters(kwargs: Dict[str, Any],
                       method: Callable,
                       is_init: bool = False):
    """Prepares paramters passed into components before calling SDK.

    1. Determines the annotation type that should used with the parameter
    2. Reads input values if needed
    3. Deserializes thos value where appropriate
    4. Or casts to the correct type.

    Args:
        kwargs (Dict[str, Any]): The kwargs that will be passed into method. Mutates in place.
        method (Callable): The method the kwargs used to invoke the method.
        is_init (bool): Whether this method is a constructor
    """
    for key, param in inspect.signature(method).parameters.items():
        if key in kwargs:
            value = kwargs[key]
            param_type = utils.resolve_annotation(param.annotation)
            value = resolve_init_args(
                key, value) if is_init else resolve_input_args(
                    value, param_type)
            deserializer = utils.get_deserializer(param_type)
            if deserializer:
                value = deserializer(value)
            else:
                value = cast(value, param_type)
            kwargs[key] = value
コード例 #2
0
def runner(cls_name, method_name, resource_name_output_uri, kwargs):
    cls = getattr(aiplatform, cls_name)

    init_args, method_args = split_args(kwargs)

    serialized_args = {'init': init_args, 'method': method_args}

    project = resolve_project(serialized_args)

    for key, param in inspect.signature(cls.__init__).parameters.items():
        if key in serialized_args['init']:
            serialized_args['init'][key] = resolve_init_args(
                key, serialized_args['init'][key], project)
            param_type = utils.resolve_annotation(param.annotation)
            deserializer = utils.get_deserializer(param_type)
            if deserializer:
                serialized_args['init'][key] = deserializer(
                    serialized_args['init'][key])

    print(serialized_args['init'])
    obj = cls(**serialized_args['init']) if serialized_args['init'] else cls

    method = getattr(obj, method_name)

    for key, param in inspect.signature(method).parameters.items():
        if key in serialized_args['method']:
            param_type = utils.resolve_annotation(param.annotation)
            print(key, param_type)
            serialized_args['method'][key] = resolve_input_args(
                serialized_args['method'][key], param_type, project)
            deserializer = utils.get_deserializer(param_type)
            if deserializer:
                serialized_args['method'][key] = deserializer(
                    serialized_args['method'][key])
            else:
                serialized_args['method'][key] = param_type(
                    serialized_args['method'][key])

    print(serialized_args['method'])
    output = method(**serialized_args['method'])
    print(output)

    if output:
        write_to_gcs(project, resource_name_output_uri, make_output(output))
        return output
コード例 #3
0
def runner(cls_name, method_name, resource_name_output_artifact_path, kwargs):
    cls = getattr(aiplatform, cls_name)

    init_args, method_args = split_args(kwargs)

    serialized_args = {INIT_KEY: init_args, METHOD_KEY: method_args}

    for key, param in inspect.signature(cls.__init__).parameters.items():
        if key in serialized_args[INIT_KEY]:
            serialized_args[INIT_KEY][key] = resolve_init_args(
                key, serialized_args[INIT_KEY][key])
            param_type = utils.resolve_annotation(param.annotation)
            deserializer = utils.get_deserializer(param_type)
            if deserializer:
                serialized_args[INIT_KEY][key] = deserializer(
                    serialized_args[INIT_KEY][key])
    # TODO(chavoshi): use logging instead.
    print(serialized_args[INIT_KEY])
    obj = cls(
        **serialized_args[INIT_KEY]) if serialized_args[INIT_KEY] else cls

    method = getattr(obj, method_name)

    for key, param in inspect.signature(method).parameters.items():
        if key in serialized_args[METHOD_KEY]:
            param_type = utils.resolve_annotation(param.annotation)
            print(key, param_type)
            serialized_args[METHOD_KEY][key] = resolve_input_args(
                serialized_args[METHOD_KEY][key], param_type)
            deserializer = utils.get_deserializer(param_type)
            if deserializer:
                serialized_args[METHOD_KEY][key] = deserializer(
                    serialized_args[METHOD_KEY][key])
            else:
                serialized_args[METHOD_KEY][key] = param_type(
                    serialized_args[METHOD_KEY][key])

    print(serialized_args[METHOD_KEY])
    output = method(**serialized_args[METHOD_KEY])
    print(output)

    if output:
        write_to_artifact(resource_name_output_artifact_path,
                          make_output(output))
        return output