-
Notifications
You must be signed in to change notification settings - Fork 1
/
oplrunonwml.py
169 lines (136 loc) · 5.46 KB
/
oplrunonwml.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
import sys, getopt
try:
sys.modules['sklearn.externals.joblib'] = __import__('joblib')
from watson_machine_learning_client import WatsonMachineLearningAPIClient
except ImportError:
from watson_machine_learning_client import WatsonMachineLearningAPIClient
# THIS IS THE USER CREDENTIALS
wml_credentials = {
"apikey": "xxxxxxxxxxxxxxxxxxxxxxxxx",
"instance_id": "xxxxxxxxxxxxxxxxxxxxxxxxx",
"url": "https://us-south.ml.cloud.ibm.com",
}
# END OF THE USER CREDENTIALS
def main(argv):
mod_file = "mulprod.mod"
dat_file = "mulprod.dat"
try:
opts, args = getopt.getopt(argv,"hm:d:",["mfile=","dfile="])
except getopt.GetoptError:
print('runoplonwml.py -m <mod_file> -d <dat_file>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('runoplonwml.py -m <mod_file> -d <dat_file>')
sys.exit()
elif opt in ("-m", "--mfile"):
mod_file = arg
elif opt in ("-d", "--dfile"):
dat_file = arg
print('Model file is', mod_file)
print('Dat file is', dat_file)
basename = mod_file.split('.')[0]
model_name = basename + "_model"
deployment_name = basename + "_deployment"
print("Creating WML Client")
client = WatsonMachineLearningAPIClient(wml_credentials)
print("Getting deployment")
deployments = client.deployments.get_details()
deployment_uid = None
for res in deployments['resources']:
if res['entity']['name'] == deployment_name:
deployment_uid = res['metadata']['guid']
print("Found deployment", deployment_uid)
break
if deployment_uid == None:
print("Creating model")
import tarfile
def reset(tarinfo):
tarinfo.uid = tarinfo.gid = 0
tarinfo.uname = tarinfo.gname = "root"
return tarinfo
tar = tarfile.open("model.tar.gz", "w:gz")
tar.add(mod_file, arcname=mod_file, filter=reset)
tar.close()
print("Storing model")
model_metadata = {
client.repository.ModelMetaNames.NAME: model_name,
client.repository.ModelMetaNames.DESCRIPTION: model_name,
client.repository.ModelMetaNames.TYPE: "do-opl_12.10",
client.repository.ModelMetaNames.RUNTIME_UID: "do_12.10"
}
model_details = client.repository.store_model(model='./model.tar.gz', meta_props=model_metadata)
model_uid = client.repository.get_model_uid(model_details)
print(model_uid)
print("Creating deployment")
deployment_props = {
client.deployments.ConfigurationMetaNames.NAME: deployment_name,
client.deployments.ConfigurationMetaNames.DESCRIPTION: deployment_name,
client.deployments.ConfigurationMetaNames.BATCH: {},
client.deployments.ConfigurationMetaNames.COMPUTE: {'name': 'S', 'nodes': 1}
}
deployment_details = client.deployments.create(model_uid, meta_props=deployment_props)
deployment_uid = client.deployments.get_uid(deployment_details)
print('deployment_id:', deployment_uid)
print("Creating job")
import pandas as pd
with open(dat_file, 'r') as file:
data = file.read();
import base64
data = data.encode("UTF-8")
data = base64.b64encode(data)
data = data.decode("UTF-8")
df_dat = pd.DataFrame(columns=['___TEXT___'], data=[[data]])
solve_payload = {
client.deployments.DecisionOptimizationMetaNames.SOLVE_PARAMETERS: {
'oaas.logAttachmentName': 'log.txt',
'oaas.logTailEnabled': 'true',
'oaas.includeInputData': 'false',
'oaas.resultsFormat': 'JSON'
},
client.deployments.DecisionOptimizationMetaNames.INPUT_DATA: [
{
"id": dat_file,
"values": df_dat
}
],
client.deployments.DecisionOptimizationMetaNames.OUTPUT_DATA: [
{
"id": ".*\.csv"
},
{
"id": ".*\.json"
},
{
"id": ".*\.txt"
}
]
}
job_details = client.deployments.create_job(deployment_uid, solve_payload)
job_uid = client.deployments.get_job_uid(job_details)
print('job_id', job_uid)
from time import sleep
while job_details['entity']['decision_optimization']['status']['state'] not in ['completed', 'failed', 'canceled']:
print(job_details['entity']['decision_optimization']['status']['state'] + '...')
sleep(5)
job_details = client.deployments.get_job_details(job_uid)
print(job_details['entity']['decision_optimization']['status']['state'])
for output_data in job_details['entity']['decision_optimization']['output_data']:
if output_data['id'].endswith('csv'):
print('Solution table:' + output_data['id'])
solution = pd.DataFrame(output_data['values'],
columns=output_data['fields'])
solution.head()
else:
print(output_data['id'])
output = output_data['values'][0][0]
output = output.encode("UTF-8")
output = base64.b64decode(output)
output = output.decode("UTF-8")
print(output)
with open(output_data['id'], 'wt') as file:
file.write(output)
# print ("Deleting deployment")
# client.deployments.delete(deployment_uid)
if __name__ == '__main__':
main(sys.argv[1:])