Skip to main content
Version: v0.61.1

Scoring Local Files

Use local file system as source and sink for MLOps batch scoring.

Prerequisites

Setup

Install the h2o_mlops_scoring_client with pip.

Notes

  • If running locally, the number of cores used (and thus parallel processes) can be overridden with:
num_cores = 10
h2o_mlops_scoring_client.spark_master = f"local[{num_cores}]"

Example Usage

import h2o_mlops_scoring_client

Choose the MLOps scoring endpoint.

MLOPS_ENDPOINT_URL = "https://model.internal.dedicated.h2o.ai/f325d002-3c3f-4283-9585-1569afc5dd12/model/score"

Set the folders or file (using the file URI) to use along with a unique ID column used to identify each score. Note the source can be either a folder or file while the sink must be a folder.

ID_COLUMN = "ID"
SOURCE_DATA = "file:///Users/jgranados/datasets/kaggle/bnpparibas/test.csv"
SINK_LOCATION = "file:///Users/jgranados/datasets/output/"

Set the source and sink file type (here we demonstrate CSV).

SOURCE_FORMAT = h2o_mlops_scoring_client.Format.CSV
SINK_FORMAT = h2o_mlops_scoring_client.Format.CSV

Set the sink write mode. Look at the WriteMode value to see its behavior.

h2o_mlops_scoring_client.WriteMode.OVERWRITE.value
'Overwrite existing files'
SINK_WRITE_MODE = h2o_mlops_scoring_client.WriteMode.OVERWRITE

If the file count of the source is small enough, you will want to preprocess the table into partitions to take advantage of parallel scoring. The number of partitions should equal the number of cores times 3. If the file count is larger than the number of cores, repartitioning may slow down scoring, as each individual file will already count as a partition.

def preprocess(spark_df):
return spark_df.repartition(30)

And now we score.

h2o_mlops_scoring_client.score_source_sink(
mlops_endpoint_url=MLOPS_ENDPOINT_URL,
id_column=ID_COLUMN,
source_data=SOURCE_DATA,
source_format=SOURCE_FORMAT,
sink_location=SINK_LOCATION,
sink_format=SINK_FORMAT,
sink_write_mode=SINK_WRITE_MODE,
preprocess_method=preprocess,
)
23/04/29 15:50:55 INFO h2o_mlops_scoring_client: Starting Spark context


23/04/29 15:50:55 WARN Utils: Your hostname, M16Max-100638.local resolves to a loopback address: 127.0.0.1; using 192.168.1.8 instead (on interface en0)
23/04/29 15:50:55 WARN Utils: Set SPARK_LOCAL_IP if you need to bind to another address


Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).


23/04/29 15:50:56 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
23/04/29 15:50:56 WARN Utils: Service 'SparkUI' could not bind on port 4040. Attempting port 4041.


23/04/29 15:50:56 INFO h2o_mlops_scoring_client: Connecting to H2O.ai MLOps scorer at 'https://model.internal.dedicated.h2o.ai/f325d002-3c3f-4283-9585-1569afc5dd12/model/score'
23/04/29 15:51:00 INFO h2o_mlops_scoring_client: Applying preprocess method
23/04/29 15:51:00 INFO h2o_mlops_scoring_client: Starting scoring from 'file:///Users/jgranados/datasets/kaggle/bnpparibas/test.csv' to 'file:///Users/jgranados/datasets/output/'


23/04/29 15:51:00 WARN package: Truncated the string representation of a plan since it was too large. This behavior can be adjusted by setting 'spark.sql.debug.maxToStringFields'.


23/04/29 15:51:52 INFO h2o_mlops_scoring_client: Scoring complete
23/04/29 15:51:52 INFO h2o_mlops_scoring_client: Total run time: 0:00:57
23/04/29 15:51:52 INFO h2o_mlops_scoring_client: Scoring run time: 0:00:52
23/04/29 15:51:52 INFO h2o_mlops_scoring_client: Stopping Spark context

Feedback