9
9
from dse_do_utils .scenariomanager import ScenarioManager
10
10
import time
11
11
import pathlib
12
+ import tempfile
12
13
13
14
14
15
class DOModelDeployer (object ):
15
- """Deploys a DO Model in WML. For use in WS Cloud . Retrieves the model from the DO Model Builder.
16
+ """Deploys a DO Model in WML. For use in CPD 4.0 . Retrieves the model from the DO Model Builder.
16
17
17
18
Usage::
18
19
19
- md = DOModelDeployer(wml_credentials, project, model_name, scenario_name, deployment_name, deployment_description)
20
+ md = DOModelDeployer(wml_credentials, model_name, scenario_name, space_name,
21
+ deployment_name, deployment_description)
20
22
deployment_uid = md.deploy_model()
21
23
print(deployment_uid)
22
24
@@ -25,13 +27,19 @@ class DOModelDeployer(object):
25
27
def __init__ (self , wml_credentials : Dict , model_name : str , scenario_name : str , space_name : str ,
26
28
package_paths : List [str ]= [],
27
29
file_paths : List [str ]= [],
28
- deployment_name : str = 'xxx' , deployment_description : str = 'xxx' , project = None ):
30
+ deployment_name : str = 'xxx' , deployment_description : str = 'xxx' , project = None ,
31
+ tmp_dir : str = None ):
29
32
"""
30
33
34
+ :param wml_credentials
35
+ :param model_name (str): name of DO Experiment
36
+ :param scenario_name (str): name of scenario with the Python model
37
+ :param space_name (str): name of deployment space
31
38
:param package_paths (List[str]): list paths to packages that will be included. The 'stem' of the path, with all included files and folders will be included in the root of the deployment archive. E.g. "/userfs/MyPackageDevFolder/my_package" will result in the folder 'my_package' in the root of the archive. Components can be imported using `from my_package.my_module import MyClass`. WARNING: this feature doesn't work yet due to failing import.
32
39
:param file_paths (List[str]): list paths to files that will be included along side the model. Components can be imported using `from my_file import MyClass`
33
40
:param space_name (str): name of deployment space
34
41
:param project (project_lib.Project): for WS Cloud, not required for CP4D on-prem. See ScenarioManager(). Used to connect to DO Experiment.
42
+ :param tmp_dir (str): path to directory where the intermediate files will be written. Make sure this exists. Can be used for debugging to inspect the files. If None, will use `tempfile` to generate a temporary folder that will be cleaned up automatically.
35
43
"""
36
44
self .wml_credentials = wml_credentials
37
45
self .project = project
@@ -43,9 +51,9 @@ def __init__(self, wml_credentials: Dict, model_name: str, scenario_name: str, s
43
51
44
52
self .package_paths = package_paths
45
53
self .file_paths = file_paths
54
+ self .tmp_dir = tmp_dir
46
55
47
56
# Initialize clients
48
- # self.client = WatsonMachineLearningAPIClient(wml_credentials)
49
57
self .client = APIClient (wml_credentials )
50
58
space_id = self .guid_from_space_name (space_name ) # TODO: catch error if space_name cannot be found?
51
59
result = self .client .set .default_space (space_id )
@@ -127,31 +135,39 @@ def deploy_model(self) -> str:
127
135
Returns:
128
136
deployment_uid (str): Deployment UID necessary to call the deployment.
129
137
"""
130
- model_archive_file_path = self .create_model_archive ()
131
- yaml_file_path = self .write_yaml_file ("./main.yml" )
132
- deployment_uid = self .deploy_archive (model_archive_file_path , yaml_file_path )
138
+ if self .tmp_dir is None :
139
+ with tempfile .TemporaryDirectory () as path :
140
+ model_archive_file_path = self .create_model_archive (path )
141
+ yaml_file_path = self .write_yaml_file (os .path .join (path , "main.yml" ))
142
+ deployment_uid = self .deploy_archive (model_archive_file_path , yaml_file_path )
143
+ else :
144
+ model_archive_file_path = self .create_model_archive (self .tmp_dir )
145
+ yaml_file_path = self .write_yaml_file (os .path .join (self .tmp_dir , "main.yml" ))
146
+ deployment_uid = self .deploy_archive (model_archive_file_path , yaml_file_path )
133
147
return deployment_uid
134
148
135
149
############################################
136
150
# Create model archive
137
151
############################################
138
- def create_model_archive (self ):
139
- """Creates a model archive on the default path:
152
+ def create_model_archive (self , path : str ):
153
+ """Creates a model archive on the path:
140
154
The archive contains one .py file: the do-model surrounded by boilerplate code to process
141
155
the inputs and outputs dictionaries.
142
156
Steps:
143
- 1. Creates a directory `model`
144
- 2. Write a file `model/main.py`
145
- 3. Creates an archive file from the model directory
157
+ 1. Write a file `path/main.py`
158
+ 2. Creates an archive file in path
159
+ 3. Adds the main.py
160
+ 4. Adds packages
161
+ 5. Adds (module) files
146
162
"""
147
- path = self . create_model_directory ()
148
- file_path = os .path .join (path , 'main.py' )
149
- self .write_main_file (file_path )
150
- file_path = self .create_archive ()
163
+
164
+ main_file_path = os .path .join (path , 'main.py' )
165
+ self .write_main_file (main_file_path )
166
+ file_path = self .create_archive (main_file_path , path )
151
167
return file_path
152
168
153
169
def create_model_directory (self ) -> str :
154
- """Create a directory in the default path.
170
+ """Create a directory 'model' in the default path.
155
171
Will remove/clear first if exists.
156
172
157
173
Return:
@@ -188,16 +204,21 @@ def get_scenario(self):
188
204
scenario = self .scenario_manager .get_do_scenario (self .model_name , self .scenario_name )
189
205
return scenario
190
206
191
- def create_archive (self ):
207
+ def create_archive (self , main_file_path : str , path : str ):
192
208
"""Create archive.
193
209
For now assume one folder `model` with one file `main.py`
210
+
211
+ :param main_file_path: file path of main.py file
212
+ :param path: folder where archive will be written
194
213
"""
195
214
def reset (tarinfo ):
196
215
tarinfo .uid = tarinfo .gid = 0
197
216
tarinfo .uname = tarinfo .gname = "root"
198
217
return tarinfo
199
- tar = tarfile .open ("model.tar.gz" , "w:gz" )
200
- tar .add ("model/main.py" , arcname = "main.py" , filter = reset )
218
+ tar_file_path = os .path .join (path , "model.tar.gz" )
219
+ tar = tarfile .open (tar_file_path , "w:gz" )
220
+ # tar.add("model/main.py", arcname="main.py", filter=reset)
221
+ tar .add (main_file_path , arcname = "main.py" , filter = reset )
201
222
202
223
def filter_package (tarinfo ):
203
224
tarinfo .uid = tarinfo .gid = 0
@@ -217,8 +238,7 @@ def filter_package(tarinfo):
217
238
tar .add (file_path , arcname = file_name , filter = filter_package )
218
239
219
240
tar .close ()
220
- file_path = "./model.tar.gz" # TODO: avoid hard-coding
221
- return file_path
241
+ return tar_file_path
222
242
223
243
#########################################################
224
244
# Deploy model
0 commit comments