-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathrun_seldon_deployment_pipeline.py
142 lines (130 loc) · 4.55 KB
/
run_seldon_deployment_pipeline.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from typing import cast
import click
from pipelines.seldon_deployment_pipeline import (
DeploymentTriggerConfig,
SeldonDeploymentLoaderStepConfig,
continuous_deployment_pipeline,
deployment_trigger,
dynamic_importer,
inference_pipeline,
prediction_service_loader,
predictor,
)
from rich import print
from steps.data_process import drop_cols, encode_cat_cols
from steps.data_splitter import data_splitter
from steps.evaluation import evaluation
from steps.ingest_data import ingest_data
from steps.trainer import model_trainer
from zenml.integrations.seldon.model_deployers import SeldonModelDeployer
from zenml.integrations.seldon.services import (
SeldonDeploymentConfig,
SeldonDeploymentService,
)
from zenml.integrations.seldon.steps import (
SeldonDeployerStepParameters,
seldon_model_deployer_step,
)
@click.command()
@click.option(
"--deploy",
"-d",
is_flag=True,
help="Run the deployment pipeline to train and deploy a model",
)
@click.option(
"--predict",
"-p",
is_flag=True,
help="Run the inference pipeline to send a prediction request "
"to the deployed model",
)
@click.option(
"--min-accuracy",
default=0.50,
help="Minimum accuracy required to deploy the model (default: 0.50)",
)
def main(
deploy: bool,
predict: bool,
min_accuracy: float,
):
"""Run the Seldon example continuous deployment or inference pipeline
Example usage:
python run.py --deploy --predict --min-accuracy 0.50
"""
model_name = "model"
deployment_pipeline_name = "continuous_deployment_pipeline"
deployer_step_name = "seldon_model_deployer_step"
model_deployer = SeldonModelDeployer.get_active_model_deployer()
seldon_implementation = "SKLEARN_SERVER"
if deploy:
# Initialize a continuous deployment pipeline run
deployment = continuous_deployment_pipeline(
ingest_data=ingest_data(),
encode_cat_cols=encode_cat_cols(),
drop_cols=drop_cols(),
data_splitter=data_splitter(),
model_trainer=model_trainer(),
evaluator=evaluation(),
deployment_trigger=deployment_trigger(
params=DeploymentTriggerConfig(
min_accuracy=min_accuracy,
)
),
model_deployer=seldon_model_deployer_step(
params=SeldonDeployerStepParameters(
service_config=SeldonDeploymentConfig(
model_name=model_name,
replicas=1,
implementation=seldon_implementation,
),
timeout=120,
)
),
)
deployment.run(config_path="continuous_pipeline_config.yaml")
if predict:
# Initialize an inference pipeline run
inference = inference_pipeline(
dynamic_importer=dynamic_importer(),
prediction_service_loader=prediction_service_loader(
SeldonDeploymentLoaderStepConfig(
pipeline_name=deployment_pipeline_name,
step_name=deployer_step_name,
model_name=model_name,
)
),
predictor=predictor(),
)
inference.run(config_path="continuous_pipeline_config.yaml")
services = model_deployer.find_model_server(
pipeline_name=deployment_pipeline_name,
pipeline_step_name=deployer_step_name,
model_name=model_name,
)
if services:
service = cast(SeldonDeploymentService, services[0])
if service.is_running:
print(
f"The Seldon prediction server is running remotely as a Kubernetes "
f"service and accepts inference requests at:\n"
f" {service.prediction_url}\n"
f"To stop the service, run "
f"[italic green]`zenml model-deployer models delete "
f"{str(service.uuid)}`[/italic green]."
)
elif service.is_failed:
print(
f"The Seldon prediction server is in a failed state:\n"
f" Last state: '{service.status.state.value}'\n"
f" Last error: '{service.status.last_error}'"
)
else:
print(
"No Seldon prediction server is currently running. The deployment "
"pipeline must run first to train a model and deploy it. Execute "
"the same command with the `--deploy` argument to deploy a model."
)
if __name__ == "__main__":
main()