Skip to content

Commit 09422ca

Browse files
committed
initial testable commit
1 parent b1bd418 commit 09422ca

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

adk/ADK.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import os
44
import sys
55
import Algorithmia
6+
import yaml
7+
import os
8+
import subprocess
9+
610
from adk.io import create_exception, format_data, format_response
711
from adk.modeldata import ModelData
812

@@ -92,7 +96,24 @@ def process_local(self, local_payload, pprint):
9296
result = self.apply(local_payload)
9397
self.write_to_pipe(result, pprint=pprint)
9498

95-
def init(self, local_payload=None, pprint=print):
99+
def mlops_initialize(self):
100+
os.environ["MLOPS_SPOOLER_TYPE"] = "FILESYSTEM"
101+
os.environ["MLOPS_FILESYSTEM_DIRECTORY"] = self.mlops_spool_dir
102+
with open(f'{agents_dir}/conf/mlops.agent.conf.yaml') as f:
103+
documents = yaml.load(f, Loader=yaml.FullLoader)
104+
documents['mlopsUrl'] = DATAROBOT_ENDPOINT
105+
documents['apiToken'] = DATAROBOT_API_TOKEN
106+
with open(f'{agents_dir}/conf/mlops.agent.conf.yaml', 'w') as f:
107+
yaml.dump(documents, f)
108+
subprocess.call(f'{agents_dir}/bin/start-agent.sh')
109+
check = subprocess.Popen([f'{agents_dir}/bin/status-agent.sh'], stdout=subprocess.PIPE)
110+
check.terminate()
111+
112+
113+
114+
def init(self, local_payload=None, pprint=print, mlops=False):
115+
if mlops and not self.is_local:
116+
self.mlops_initialize()
96117
self.load()
97118
if self.is_local and local_payload is not None:
98119
if self.loading_exception:

adk/mlops.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import yaml
2+
import os
3+
import subprocess
4+
5+
6+
class MLOps(Object):
7+
def __init__(self, endpoint, api_token, model_id, deployment_id):
8+
self.token = api_token
9+
self.endpoint = endpoint
10+
self.model_id = model_id
11+
self.deployment_id = deployment_id
12+
self.spool_dir = "/tmp/ta"
13+
self.agent_dir = "/opt/mlops-agent/datarobot_mlops_package-8.1.2"
14+
15+
def init(self):
16+
with open(f'{self.agent_dir}/conf/mlops.agent.conf.yaml') as f:
17+
documents = yaml.load(f, Loader=yaml.FullLoader)
18+
documents['mlopsUrl'] = self.endpoint
19+
documents['apiToken'] = self.token
20+
with open(f'{agents_dir}/conf/mlops.agent.conf.yaml', 'w') as f:
21+
yaml.dump(documents, f)
22+
23+
subprocess.call(f'{agents_dir}/bin/start-agent.sh')
24+
check = subprocess.Popen([f'{agents_dir}/bin/status-agent.sh'], stdout=subprocess.PIPE)
25+
output = check.stdout.readlines()
26+
check.terminate()
27+
if "DataRobot MLOps-Agent is running as a service." in output:
28+
return True
29+
else:
30+
return False
31+
32+
def env_vars(self):
33+
os.environ['MLOPS_DEPLOYMENT_ID'] = self.deployment_id
34+
os.environ['MLOPS_MODEL_ID'] = self.model_id
35+
os.environ['MLOPS_SPOOLER_TYPE'] = "FILESYSTEM"
36+
os.environ['MLOPS_FILESYSTEM_DIRECTORY'] = "/tmp/ta"

adk/modeldata.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
import json
33
import hashlib
44
from adk.classes import FileData
5+
from adk.mlops import MLOps
56

67

78
class 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.\nPlease 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

110126
def check_lock(manifest_data):
111127
expected_lock_checksum = manifest_data.get('lock_checksum')
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"mlops": {
3+
"model_id": "",
4+
"deployment_id": "",
5+
"datarobot_api_endpoint": "https://app.datarobot.com"
6+
},
7+
"required_models": [],
8+
"optional_models": []
9+
}

0 commit comments

Comments
 (0)