-
Notifications
You must be signed in to change notification settings - Fork 603
Expand file tree
/
Copy pathrun.py
More file actions
140 lines (120 loc) · 4.76 KB
/
run.py
File metadata and controls
140 lines (120 loc) · 4.76 KB
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
# Apache Software License 2.0
#
# Copyright (c) ZenML GmbH 2025. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Customer Churn Prediction — Training and Deployment Example.
Usage:
python run.py --train # Train the churn prediction model
python run.py --predict # Run inference on sample customer data
python run.py --predict --features '{"account_length": 50, ...}' # Custom prediction
"""
import argparse
import json
from pipelines import churn_inference_pipeline, churn_training_pipeline
def main() -> None:
"""Main entry point for the churn prediction example."""
ap = argparse.ArgumentParser()
ap.add_argument(
"--train",
action="store_true",
help="Train the churn prediction model.",
)
ap.add_argument(
"--predict",
action="store_true",
help="Run inference on customer data.",
)
ap.add_argument(
"--features",
type=str,
help="JSON string of customer features for prediction.",
)
ap.add_argument(
"--samples",
type=int,
default=1000,
help="Number of training samples to generate (default: 1000).",
)
args = ap.parse_args()
if args.train:
print(
f">> Training churn prediction model with {args.samples} samples..."
)
run = churn_training_pipeline(num_samples=args.samples)
print(
">> Training complete! Model tagged as 'production' and ready for serving."
)
elif args.predict:
# Use provided features or default sample customer
if args.features:
try:
customer_features = json.loads(args.features)
except json.JSONDecodeError as e:
print(f"Error parsing features JSON: {e}")
return
else:
# Default sample customer with moderate churn risk
customer_features = {
"account_length": 45,
"customer_service_calls": 3,
"monthly_charges": 65.0,
"total_charges": 2925.0,
"has_internet_service": 1,
"has_phone_service": 1,
"contract_length": 1, # month-to-month (higher churn risk)
"payment_method_electronic": 1,
}
print(f">> Using sample customer features: {customer_features}")
print(">> Running churn prediction...")
from zenml.client import Client
# Run the pipeline and get the result from the last step
run = churn_inference_pipeline(customer_features=customer_features)
client = Client()
pipeline_run = client.get_pipeline_run(run.id)
result = pipeline_run.steps["predict_churn"].output.load()
print("\n=== Churn Prediction Result ===")
print(f"Churn Probability: {result['churn_probability']:.3f}")
print(
f"Churn Prediction: {'Will Churn' if result['churn_prediction'] else 'Will Stay'}"
)
print(f"Model Version: {result.get('model_version', 'Unknown')}")
print(f"Status: {result.get('model_status', 'Unknown')}")
if result.get("error"):
print(f"Error: {result['error']}")
else:
example_features = {
"account_length": 45,
"customer_service_calls": 3,
"monthly_charges": 65.0,
"total_charges": 2925.0,
"has_internet_service": 1,
"has_phone_service": 1,
"contract_length": 1,
"payment_method_electronic": 1,
}
print(f"""Customer Churn Prediction Example
Usage:
python run.py --train # Train the model
python run.py --predict # Predict with sample data
python run.py --predict --features '{{...}}' # Predict with custom data
Workflow:
1. Train model: python run.py --train
2. Test locally: python run.py --predict
3. Deploy service: zenml pipeline deploy pipelines.churn_inference_pipeline.churn_inference_pipeline
4. Open web UI: Navigate to http://localhost:8000 for interactive predictions
5. Test API: curl -X POST <endpoint>/invoke -d '{{"parameters": {{"customer_features": {{...}}}}}}'
Example customer features:
{json.dumps(example_features, indent=2)}""")
if __name__ == "__main__":
main()