-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
63 lines (52 loc) · 2.32 KB
/
runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import asyncio
import logging
import os
import joblib
import numpy
import pandas as pd
# -------- Add your dependencies here --------
# Logger setup
logger = logging.getLogger(__name__)
async def pre_process(
model_files_path: str, request_id: str, input_data_frame: pd.DataFrame
) -> numpy.ndarray:
# ----------------------------------------------
# - Replace the following lines with your code -
# ----------------------------------------------
# Feature scaling: can be either manual (hardcoded) or loaded as a file
# (e.g. if the StandardScaler was pickled from training) as follows
standard_scaler = os.path.join(model_files_path, "example_standard_scaler.pkl")
# Feature selection: can be either manual (hardcoded) or loaded as a file
# (e.g. if the ordered list of feature names was pickled from training) as follows
training_cols = os.path.join(model_files_path, "training_cols.pkl")
# Scale the test data in the same way you scaled your training and validation data
scaler = joblib.load(standard_scaler) # load our scaler
# Order cols in the same way you did in training
training_cols = joblib.load(training_cols) # load your ordered training cols
# Transform the inputs for model execution.
# Their shape should match exactly what your model expects for inference.
return scaler.transform(input_data_frame[training_cols].values)
async def post_process(
model_files_path: str, request_id: str, quantized_inference: float
) -> float:
# ----------------------------------------------
# - Replace the following lines with your code -
# ----------------------------------------------
return quantized_inference
if __name__ == "__main__":
loop = asyncio.get_event_loop()
model_files_path = os.path.join(os.path.curdir, "model_files")
request_id = "some-request-id"
data_frame_path = os.path.join(
os.path.curdir, "assets", "sample-input-data-frame.parquet"
)
data_frame = pd.read_parquet(data_frame_path)
scaled_inputs = loop.run_until_complete(
pre_process(model_files_path, request_id, data_frame)
)
print("scaled inputs:", scaled_inputs)
scaled_inference = loop.run_until_complete(
post_process(model_files_path, request_id, 3.14159)
)
print("scaled inference:", scaled_inference)
loop.close()