Esempio n. 1
0
def load_model(model_path):
    signature_path = os.path.join(model_path, "contract.protobin")
    files_path = os.path.join(model_path, "files")
    src_path = os.path.join(files_path, "src")
    lib_path = os.path.join(src_path, "lib")

    with open(signature_path, "rb") as file:
        try:
            signature = ModelSignature()
            signature.ParseFromString(file.read())
        except Exception as e:
            raise ValueError(
                f"Couldn't parse serialized signature at {signature_path}: {e}"
            ) from e

    logging.info("Added model `{}` to PYTHON_PATH".format(model_path))
    sys.path.append(model_path)

    logging.info("Added src `{}` to PYTHON_PATH".format(src_path))
    sys.path.append(src_path)

    if os.path.exists(lib_path):
        logging.info("Added lib `{}` to PYTHON_PATH".format(lib_path))
        sys.path.append(lib_path)

    module = importlib.import_module("func_main")
    executable = getattr(module, signature.signature_name)
    logging.info(f"Got {signature.signature_name} function from the module")

    return PythonGRPCService(executable, signature)
Esempio n. 2
0
def test_pipeline_apply(mock_app_build, mock_app_delete, mock_app_find,
                        mock_mv_find, singular_yaml_path):
    conn = Cluster("http://")
    mock_mv_find.return_value = ModelVersion(cluster=conn,
                                             id=1,
                                             model_id=1,
                                             name="claims-model",
                                             version=1,
                                             signature=ModelSignature(),
                                             status="Released",
                                             image=DockerImage(name="aaa",
                                                               tag="aaa"),
                                             runtime=DockerImage(name="aaa",
                                                                 tag="aaa"),
                                             is_external=False)
    mock_app_find.return_value = HS_APP(cluster=conn,
                                        id=1,
                                        name="app",
                                        execution_graph=ExecutionGraph([]),
                                        status="Ready",
                                        signature=ModelSignature(),
                                        kafka_streaming=[])
    mock_app_build.return_value = HS_APP(cluster=conn,
                                         id=1,
                                         name="app",
                                         execution_graph=ExecutionGraph([]),
                                         status="Ready",
                                         signature=ModelSignature(),
                                         kafka_streaming=[])
    app: Application = Application.parse_file(singular_yaml_path)
    app.apply(conn, os.path.dirname(singular_yaml_path))
 def generate_signature():
     signature = ModelSignature(
         signature_name="add",
         inputs=[
             ModelField(name="a", dtype=DT_INT8),
             ModelField(name="b", dtype=DT_INT8)
         ],
         outputs=[ModelField(name="sum", dtype=DT_INT8)])
     with open("test/models/calculator/contract.protobin", "wb") as file:
         file.write(signature.SerializeToString())
Esempio n. 4
0
 def build(self) -> ModelSignature:
     """
     Creates ModelSignature
     :return: ModelSignature proto object
     """
     return ModelSignature(
         signature_name=self.name,
         inputs=self.inputs,
         outputs=self.outputs
     )
Esempio n. 5
0
def signature_dict_to_ModelSignature(data: dict) -> ModelSignature:
    """
    Internal method.
    A method that creates ModelSignature out of a signature dict.
    :param data:
    :return:
    """
    signature_name = data.get("signatureName")
    inputs = data.get("inputs")  # list of dicts
    outputs = data.get("outputs")  # list of dicts

    frmt_inputs = []
    for input_ in inputs:

        field_name = input_.pop("name", None)
        if not field_name:
            [(field_name, field_dict)] = input_.items()
        else:
            field_dict = input_

        frmt_inputs.append(field_from_dict(field_name=field_name, field_dict=field_dict))

    frmt_outputs = []
    for output in outputs:
        field_name = output.pop("name", None)
        if not field_name:
            [(field_name, field_dict)] = output.items()
        else:
            field_dict = output

        frmt_outputs.append(field_from_dict(field_name=field_name, field_dict=field_dict))

    return ModelSignature(
        signature_name=signature_name,
        inputs=frmt_inputs,
        outputs=frmt_outputs
    )