-
Notifications
You must be signed in to change notification settings - Fork 0
/
9_build_project.py
executable file
·294 lines (242 loc) · 10.5 KB
/
9_build_project.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Run this file to auto deploy the model, run a job, and deploy the application
# Install the requirements
!pip3 install -r requirements.txt --progress-bar off
import subprocess
import datetime
import xml.etree.ElementTree as ET
import requests
import json
import time
import os
from IPython.display import Javascript, HTML
from cmlbootstrap import CMLBootstrap
try:
os.environ["SPARK_HOME"]
print("Spark is enabled")
except:
print('Spark is not enabled, please enable spark before running this script')
raise KeyError('Spark is not enabled, please enable spark before running this script')
run_time_suffix = datetime.datetime.now()
run_time_suffix = run_time_suffix.strftime("%d%m%Y%H%M%S")
# Instantiate API Wrapper
cml = CMLBootstrap()
# Set the STORAGE environment variable
try :
storage=os.environ["STORAGE"]
except:
storage = cml.get_cloud_storage()
storage_environment_params = {"STORAGE":storage}
storage_environment = cml.create_environment_variable(storage_environment_params)
os.environ["STORAGE"] = storage
# Create the directories and upload data
!hadoop fs -mkdir -p $STORAGE/datalake
!hadoop fs -mkdir -p $STORAGE/datalake/data
!hadoop fs -mkdir -p $STORAGE/datalake/data/churn
!hadoop fs -copyFromLocal /home/cdsw/raw/WA_Fn-UseC_-Telco-Customer-Churn-.csv $STORAGE/datalake/data/churn/WA_Fn-UseC_-Telco-Customer-Churn-.csv
# This will run the data ingest file. You need this to create the hive table from the
# csv file.
exec(open("1_data_ingest.py").read())
# Get User Details
user_details = cml.get_user({})
user_obj = {"id": user_details["id"], "username": os.getenv("CDSW_PROJECT_URL").split("/")[6],
"name": user_details["name"],
"type": user_details["type"],
"html_url": user_details["html_url"],
"url": user_details["url"]
}
# Get Project Details
project_details = cml.get_project({})
project_id = project_details["id"]
#Get the runtime_id
runtime_id = 14
for ids in cml.get_runtimes()["runtimes"]:
if ids["kernel"] == "Python 3.7" and ids["edition"] == "Standard" and ids["shortVersion"] == "2021.09" and ids["editor"] == "Workbench":
runtime_id = ids["id"]
#Get runtime addon numbers
addon_val = cml.get_runtimes_addons()[0]['identifier']
# Create Job
create_jobs_params = {"name": "Train Model",
"type": "manual",
"script": "4_train_models.py",
"timezone": "America/Los_Angeles",
"environment": {},
"kernel": "python3",
"cpu": 1,
"memory": 2,
"nvidia_gpu": 0,
"include_logs": True,
"notifications": [
{"user_id": user_obj["id"],
"user": user_obj,
"success": False, "failure": False, "timeout": False, "stopped": False
}
],
"recipients": {},
"attachments": [],
"include_logs": True,
"report_attachments": [],
"success_recipients": [],
"failure_recipients": [],
"timeout_recipients": [],
"stopped_recipients": []
}
if os.getenv("ML_RUNTIME_EDITION") != None:
create_jobs_params["runtime_id"] = runtime_id
create_jobs_params["addons"] = [addon_val-1,addon_val]
create_jobs_params["kernel"] = ""
new_job = cml.create_job(create_jobs_params)
new_job_id = new_job["id"]
print("Created new job with jobid", new_job_id)
##
# Start a job
job_env_params = {}
start_job_params = {"environment": job_env_params}
job_id = new_job_id
job_status = cml.start_job(job_id, start_job_params)
print("Job started")
# Stop a job
#job_dict = cml.start_job(job_id, start_job_params)
#cml.stop_job(job_id, start_job_params)
# Get Default Engine Details
default_engine_details = cml.get_default_engine({})
default_engine_image_id = default_engine_details["id"]
# Create the YAML file for the model lineage
yaml_text = \
""""Model Explainer {}":
hive_table_qualified_names: # this is a predefined key to link to training data
- "default.telco_churn@cm" # the qualifiedName of the hive_table object representing
metadata: # this is a predefined key for additional metadata
query: "select * from historical_data" # suggested use case: query used to extract training data
training_file: "4_train_models.py" # suggested use case: training file used
""".format(run_time_suffix)
with open('lineage.yml', 'w') as lineage:
lineage.write(yaml_text)
# Create Model
example_model_input = {"StreamingTV": "No", "MonthlyCharges": 70.35, "PhoneService": "No", "PaperlessBilling": "No", "Partner": "No", "OnlineBackup": "No", "gender": "Female", "Contract": "Month-to-month", "TotalCharges": 1397.475,
"StreamingMovies": "No", "DeviceProtection": "No", "PaymentMethod": "Bank transfer (automatic)", "tenure": 29, "Dependents": "No", "OnlineSecurity": "No", "MultipleLines": "No", "InternetService": "DSL", "SeniorCitizen": "No", "TechSupport": "No"}
create_model_params = {
"projectId": project_id,
"name": "Model Explainer 2",
"description": "Explain a given model prediction",
"visibility": "private",
"enableAuth": False,
"targetFilePath": "5_model_serve_explainer.py",
"targetFunctionName": "explain",
"engineImageId": default_engine_image_id,
"kernel": "python3",
"examples": [
{
"request": example_model_input,
"response": {}
}],
"cpuMillicores": 1000,
"memoryMb": 2048,
"nvidiaGPUs": 0,
"replicationPolicy": {"type": "fixed", "numReplicas": 1},
"environment": {}}
if os.getenv("ML_RUNTIME_EDITION") != None:
create_model_params["runtimeId"] = runtime_id
new_model_details = cml.create_model(create_model_params)
access_key = new_model_details["accessKey"] # todo check for bad response
model_id = new_model_details["id"]
print("New model created with access key", access_key)
# Disable model_authentication
cml.set_model_auth({"id": model_id, "enableAuth": False})
# Wait for the model to deploy.
is_deployed = False
while is_deployed == False:
model = cml.get_model({"id": str(
new_model_details["id"]), "latestModelDeployment": True, "latestModelBuild": True})
if model["latestModelDeployment"]["status"] == 'deployed':
print("Model is deployed")
break
else:
print("Deploying Model.....")
time.sleep(10)
# Change the line in the flask/single_view.html file.
subprocess.call(["sed", "-i", 's/const\saccessKey.*/const accessKey = "' +
access_key + '";/', "/home/cdsw/flask/single_view.html"])
# Change the model_id value in the 7a_model_operations.py, 7b_ml_ops_visual.py and 8_check_model.py file
subprocess.call(["sed", "-i", 's/model_id =.*/model_id = "' +
model_id + '"/', "/home/cdsw/7a_ml_ops_simulation.py"])
subprocess.call(["sed", "-i", 's/model_id =.*/model_id = "' +
model_id + '"/', "/home/cdsw/7b_ml_ops_visual.py"])
subprocess.call(["sed", "-i", 's/model_id =.*/model_id = "' +
model_id + '"/', "/home/cdsw/8_check_model.py"])
# Create Application
create_application_params = {
"name": "Explainer App",
"subdomain": run_time_suffix[:],
"description": "Explainer web application",
"type": "manual",
"script": "6_application.py", "environment": {},
"kernel": "python3", "cpu": 1, "memory": 2,
"nvidia_gpu": 0
}
if os.getenv("ML_RUNTIME_EDITION") != "":
create_application_params["runtime_id"] = runtime_id
create_application_params["addons"] = [addon_val-1,addon_val]
create_application_params["kernel"] = ""
new_application_details = cml.create_application(create_application_params)
application_url = new_application_details["url"]
application_id = new_application_details["id"]
# print("Application may need a few minutes to finish deploying. Open link below in about a minute ..")
print("Application created, deploying at ", application_url)
# Wait for the application to deploy.
is_deployed = False
while is_deployed == False:
# Wait for the application to deploy.
app = cml.get_application(str(application_id), {})
if app["status"] == 'running':
print("Application is deployed")
break
else:
print("Deploying Application.....")
time.sleep(10)
HTML("<a href='{}'>Open Application UI</a>".format(application_url))
# This will run the model operations section that makes calls to the model to track
# mertics and track metric aggregations
exec(open("7a_ml_ops_simulation.py").read())
# Change the job_id value in the 8_check_model.py file
subprocess.call(["sed", "-i", 's/job_id =.*/job_id = "' +
str(new_job_id) + '"/', "/home/cdsw/8_check_model.py"])
# Create the check model Job
# Create Job
create_jobs_params = {"name": "Check Model",
"type": "manual",
"script": "8_check_model.py",
"timezone": "America/Los_Angeles",
"environment": {},
"kernel": "python3",
"cpu": 1,
"memory": 2,
"nvidia_gpu": 0,
"include_logs": True,
"notifications": [
{"user_id": user_obj["id"],
"user": user_obj,
"success": False, "failure": False, "timeout": False, "stopped": False
}
],
"recipients": {},
"attachments": [],
"include_logs": True,
"report_attachments": [],
"success_recipients": [],
"failure_recipients": [],
"timeout_recipients": [],
"stopped_recipients": []
}
if os.getenv("ML_RUNTIME_EDITION") != None:
create_jobs_params["runtime_id"] = runtime_id
create_jobs_params["addons"] = [addon_val-1,addon_val]
create_jobs_params["kernel"] = ""
new_job = cml.create_job(create_jobs_params)
new_job_id = new_job["id"]
print("Created new job with jobid", new_job_id)
# Start a job
job_env_params = {}
start_job_params = {"environment": job_env_params}
job_id = new_job_id
job_status = cml.start_job(job_id, start_job_params)
print("Job started")