Skip to content

Commit 9f6c8f3

Browse files
authored
[RAPTOR-12324] Using logging as best practice in model_templates (#1407)
* [RAPTOR-12324] Using logging as best practive in model_templates * [RAPTOR-12324] updated log formatting
1 parent 26b64dd commit 9f6c8f3

File tree

8 files changed

+69
-39
lines changed

8 files changed

+69
-39
lines changed

model_templates/gpu_nim_textgen/custom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
def load_model(code_dir: str):
6-
# print(f"Downloading model to {MODEL_DIR}...")
6+
# logger.info("Downloading model to %s...", MODEL_DIR)
77

88
# Here is where you can put code that downloads the model artifacts
99
# from an internal source. See the official documentation for more details:

model_templates/gpu_vllm_textgen/custom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
def load_model(code_dir: str):
6-
# print(f"Downloading model to {MODEL_DIR}...")
6+
# logger.info("Downloading model to %s...", MODEL_DIR)
77

88
# Add custom code to download supported OSS LLM here, otherwise we will
99
# download the weights from the HuggingFace Hub based on the model name

model_templates/python3_pytorch_multiclass/custom.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99
This example shows how to create a multiclass neural net with pytorch
1010
"""
11+
import logging
1112
import os
1213
import pickle
1314
from typing import List, Optional, Any, Dict
@@ -20,6 +21,10 @@
2021
from preprocessing import dense_preprocessing_pipeline
2122
from model_utils import build_classifier, train_classifier, save_torch_model
2223

24+
25+
logger = logging.getLogger(__name__)
26+
27+
2328
preprocessor = None
2429

2530

@@ -76,12 +81,12 @@ def fit(
7681
so that the trained object can be used during scoring.
7782
"""
7883

79-
print("Fitting Preprocessing pipeline")
84+
logger.info("Fitting Preprocessing pipeline")
8085
preprocessor = dense_preprocessing_pipeline.fit(X)
8186
lb = LabelEncoder().fit(y)
8287

8388
# write out the class labels file
84-
print("Serializing preprocessor and class labels")
89+
logger.info("Serializing preprocessor and class labels")
8590
with open(os.path.join(output_dir, "class_labels.txt"), mode="w") as f:
8691
f.write("\n".join(str(label) for label in lb.classes_))
8792

@@ -93,15 +98,15 @@ def fit(
9398
with open(os.path.join(output_dir, "preprocessor.pkl"), mode="wb") as f:
9499
pickle.dump(preprocessor, f)
95100

96-
print("Transforming input data")
101+
logger.info("Transforming input data")
97102
X = preprocessor.transform(X)
98103
y = lb.transform(y)
99104

100105
# For reproducible results
101106
torch.manual_seed(0)
102107

103108
estimator, optimizer, criterion = build_classifier(X, len(lb.classes_))
104-
print("Training classifier")
109+
logger.info("Training classifier")
105110
train_classifier(X, y, estimator, optimizer, criterion)
106111
artifact_name = "artifact.pth"
107112
save_torch_model(estimator, output_dir, artifact_name)

model_templates/python3_sklearn_runtime_params/custom.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,55 @@
44
This is proprietary source code of DataRobot, Inc. and its affiliates.
55
Released under the terms of DataRobot Tool and Utility Agreement.
66
"""
7+
import logging
8+
79
# Use this helper class to access the runtime parameter values in your model
810
from datarobot_drum import RuntimeParameters
911

1012

13+
logger = logging.getLogger(__name__)
14+
15+
1116
# This is a naive function so as to not dump the full credential values
1217
# during this demonstration.
1318
def mask(value, visible=3):
1419
return value[:visible] + ("*" * len(value[visible:]))
1520

1621

1722
def transform(data, model):
18-
print("=" * 40)
19-
print("Loading the following Runtime Parameters:")
23+
logger.info("=" * 40)
2024
option1 = RuntimeParameters.get("option1")
21-
print(f"\toption1: {option1}")
2225
option2 = RuntimeParameters.get("option2")
23-
print(f"\toption2: {option2}")
2426
option3 = RuntimeParameters.get("option3")
25-
print(f"\toption3: {option3}")
27+
logger.info(
28+
"Loading the following Runtime Parameters: "
29+
f"option1: {option1}, option2: {option2}, option3: {option3}",
30+
)
2631

2732
credential = RuntimeParameters.get("encryption_key")
2833
if credential is not None:
2934
credential_type = credential.pop("credentialType")
30-
print(
31-
f"\tapi_key(type={credential_type}): "
32-
+ str({k: mask(v) for k, v in credential.items()})
35+
logger.info(
36+
"Using credentials api_key: ",
37+
extra={
38+
"credential_type": credential_type,
39+
"api_key": str({k: mask(v) for k, v in credential.items()}),
40+
},
3341
)
3442
else:
35-
print("No credential data set")
43+
logger.info("No credential data set")
3644

3745
# boolean runtime param
3846
bool_var = RuntimeParameters.get("bool_var")
39-
print(f"\tbool_var: {bool_var}")
47+
logger.info("\tbool_var: %s", bool_var)
4048

4149
# numeric runtime param
4250
number1 = RuntimeParameters.get("number1")
43-
print(f"\tnumber1: {number1}")
51+
logger.info("\tnumber1: %s", number1)
4452
number2 = RuntimeParameters.get("number2")
45-
print(f"\tnumber2: {number2}")
53+
logger.info("\tnumber2: %s", number2)
4654

47-
print("=" * 40)
55+
logger.info("=" * 40)
4856

4957
# This transform function is just for illustrative purposes so just
5058
# return the data back unaltered.

model_templates/python3_unstructured/custom.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,31 @@
44
This is proprietary source code of DataRobot, Inc. and its affiliates.
55
Released under the terms of DataRobot Tool and Utility Agreement.
66
"""
7+
import logging
78

89
from io import BytesIO
910
import werkzeug
1011

1112

13+
logger = logging.getLogger(__name__)
14+
15+
1216
def load_model(input_dir):
1317
return "dummy"
1418

1519

1620
def score_unstructured(model, data, query, **kwargs):
17-
print("Model: ", model)
18-
print("Incoming content type params: ", kwargs)
19-
print("Incoming data type: ", type(data))
20-
print("Incoming data: ", data)
21+
logger.info("Running scoring for unstructured model: %s", model)
22+
logger.info("Incoming content type params: %s", kwargs)
23+
logger.info("Incoming data type: %s", type(data))
24+
logger.info("Incoming data: %s", data)
2125

2226
mlops = kwargs.get("mlops")
23-
print(f"MLOps supported: {mlops is not None}")
27+
logger.info("MLOps supported: %s", mlops is not None)
2428

2529
headers = kwargs.get("headers")
26-
print("Incoming request headers: ", headers)
27-
print("Incoming query params: ", query)
30+
logger.info("Incoming request headers: %s", headers)
31+
logger.info("Incoming query params: %s", query)
2832

2933
if headers and "multipart/form-data" in headers.get("Content-Type"):
3034
# For more information refer to:

model_templates/python3_unstructured_with_mlops_reporting/custom.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,30 @@
99
the usage of the `mlops` instance.
1010
The
1111
"""
12+
import logging
1213

1314
import pickle
1415
import time
1516
from pathlib import Path
16-
import sys
1717

1818
import pandas as pd
1919
import tempfile
2020

2121

22+
logger = logging.getLogger(__name__)
23+
24+
2225
def load_model(input_dir):
2326
model_path = str(Path(input_dir) / "model.pkl")
24-
print(f"Loading model: {model_path}")
27+
logger.info("Loading model: %s", model_path)
2528
return pickle.load(open(model_path, "rb"))
2629

2730

2831
def score_unstructured(model, data, query, **kwargs):
29-
print(f"Model: {model} ", flush=True)
30-
print(f"Incoming data type: {type(data)}", flush=True)
31-
print(f"Incoming kwargs: {kwargs}", flush=True)
32-
print(f"Incoming query params: {query}", flush=True)
32+
logger.info("Running scoring for unstructured model: %s", model)
33+
logger.info("Incoming data type: %s", type(data))
34+
logger.info("Incoming kwargs: %s", kwargs)
35+
logger.info("Incoming query params: %s", query)
3336

3437
# The 'mlops' instance is available only when the 'MLOPS_REPORTING_FROM_UNSTRUCTURED_MODELS'
3538
# feature-flag is enabled.
@@ -51,7 +54,7 @@ def score_unstructured(model, data, query, **kwargs):
5154
(end_time - start_time) * 1000, # Prediction execution's time
5255
)
5356
else:
54-
print("Skip mlops reporting because mlops is not enabled.", flush=True)
57+
logger.info("Skip mlops reporting because mlops is not enabled.")
5558

5659
reporting_predictions = _prepare_reporting_predictions(predictions_array)
5760

model_templates/python_multi_codejen_reg_class/custom.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
This is proprietary source code of DataRobot, Inc. and its affiliates.
55
Released under the terms of DataRobot Tool and Utility Agreement.
66
"""
7+
import logging
8+
79
from datarobot_drum.drum.language_predictors.java_predictor.java_predictor import JavaPredictor
810

911

12+
logger = logging.getLogger(__name__)
13+
14+
1015
class ScoringCodePredictor(JavaPredictor):
1116
def __init__(
1217
self,
@@ -65,7 +70,7 @@ def score(data, model, **kwargs):
6570
pred_df2 = model[1].predict(data)
6671
print(pred_df2)
6772
except Exception as e:
68-
print(e)
73+
logger.error(e, exc_info=True)
6974

7075
# Predict and print on regression predictor
7176
pred_df3 = model[2].predict(data)

model_templates/triton_onnx_unstructured/client/datarobot-predict.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
We highly recommend that you update SSL certificates with:
88
pip install -U "urllib3[secure]" certifi
99
"""
10+
import logging
1011
import sys
1112
from json import JSONDecoder
1213

@@ -17,6 +18,10 @@
1718
import requests
1819
import json
1920

21+
22+
logger = logging.getLogger(__name__)
23+
24+
2025
# See README.md on how to set up those keys
2126
API_URL = "<DATAROBOT_API_URL>"
2227
API_KEY = "<DATAROBOT_API_KEY>"
@@ -135,18 +140,18 @@ def main(filename, deployment_id, mimetype, charset):
135140

136141
data_size = sys.getsizeof(data)
137142
if data_size >= MAX_PREDICTION_FILE_SIZE_BYTES:
138-
print(
139-
("Input file is too large: {} bytes. " "Max allowed size is: {} bytes.").format(
140-
data_size, MAX_PREDICTION_FILE_SIZE_BYTES
141-
)
143+
logger.warning(
144+
"Input file is too large: %s bytes. Max allowed size is: %s bytes.",
145+
data_size,
146+
MAX_PREDICTION_FILE_SIZE_BYTES,
142147
)
143148
return 1
144149
try:
145150
response = make_datarobot_deployment_unstructured_predictions(
146151
data, deployment_id, mimetype, charset
147152
)
148153
except DataRobotPredictionError as exc:
149-
print(exc)
154+
logger.error(exc, exc_info=True)
150155
return 1
151156

152157
predictions = binary_response_as_numpy(response)

0 commit comments

Comments
 (0)