22import json
33import hashlib
44from adk .classes import FileData
5+ from adk .mlops import MLOps
56
67
78class ModelData (object ):
8- def __init__ (self , client , model_manifest_path ):
9+ def __init__ (self , client , model_manifest_path , mlops = False ):
910 self .manifest_reg_path = model_manifest_path
1011 self .manifest_frozen_path = "{}.freeze" .format (self .manifest_reg_path )
1112 self .manifest_data = self .get_manifest ()
1213 self .client = client
1314 self .models = {}
1415 self .usr_key = "__user__"
1516 self .using_frozen = True
17+ self .use_mlops = mlops
1618
1719 def __getitem__ (self , key ):
1820 return getattr (self , self .usr_key + key )
@@ -38,6 +40,8 @@ def available(self):
3840 def initialize (self ):
3941 if self .client is None :
4042 raise Exception ("Client was not defined, please define a Client when using Model Manifests." )
43+ if self .use_mlops :
44+ self .mlops_init ()
4145 for required_file in self .manifest_data ['required_files' ]:
4246 name = required_file ['name' ]
4347 source_uri = required_file ['source_uri' ]
@@ -88,16 +92,16 @@ def find_optional_model(self, file_name):
8892 else :
8993 self .models [file_name ] = FileData (real_hash , local_data_path )
9094
91-
9295 def get_manifest (self ):
9396 if os .path .exists (self .manifest_frozen_path ):
9497 with open (self .manifest_frozen_path ) as f :
9598 manifest_data = json .load (f )
9699 if check_lock (manifest_data ):
97100 return manifest_data
98101 else :
99- raise Exception ("Manifest FreezeFile Tamper Detected; please use the CLI and 'algo freeze' to rebuild your "
100- "algorithm's freeze file." )
102+ raise Exception (
103+ "Manifest FreezeFile Tamper Detected; please use the CLI and 'algo freeze' to rebuild your "
104+ "algorithm's freeze file." )
101105 elif os .path .exists (self .manifest_reg_path ):
102106 with open (self .manifest_reg_path ) as f :
103107 manifest_data = json .load (f )
@@ -106,6 +110,18 @@ def get_manifest(self):
106110 else :
107111 return None
108112
113+ def mlops_init (self ):
114+ mlops = self .manifest_data ['mlops' ]
115+ model_id = mlops ['model_id' ]
116+ deployment_id = mlops ['deployment_id' ]
117+ datarobot_api_endpoint = mlops ['datarobot_api_endpoint' ]
118+
119+ api_token = os .environ .get ('DATAROBOT_MLOPS_API_TOKEN' )
120+ if api_token is None :
121+ raise Exception ("'DATAROBOT_MLOPS_API_TOKEN' environment variable not found.\n Please ensure that you have a"
122+ "valid API token and add it as a secret to this algorithm." )
123+ self .mlops = MLOps (datarobot_api_endpoint , api_token , model_id , deployment_id )
124+
109125
110126def check_lock (manifest_data ):
111127 expected_lock_checksum = manifest_data .get ('lock_checksum' )
0 commit comments