Skip to content

Commit 0ddc2cc

Browse files
authored
Merge pull request #19 from algorithmiaio/error-messages-mlops-manifest
added better error checking
2 parents 9000d87 + 4c6b718 commit 0ddc2cc

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

adk/io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def create_exception(exception, loading_exception=False):
5757
response = json.dumps({
5858
"error": {
5959
"message": str(exception),
60-
"stacktrace": traceback.format_exc(),
60+
"stacktrace": " ".join(traceback.format_exception(etype=type(exception), value=exception, tb=exception.__traceback__)),
6161
"error_type": error_type,
6262
}
6363
})

adk/mlops.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ def __init__(self, api_token, path):
2828
if not os.path.exists(self.agent_dir):
2929
raise Exception("environment is not configured for mlops.\nPlease select a valid mlops enabled environment.")
3030

31+
if self.endpoint is None:
32+
raise Exception("'no endpoint found, please add 'MLOPS_SERVICE_URL' environment variable, or create an "
33+
"mlops.json file")
34+
if self.model_id is None:
35+
raise Exception("no model_id found, please add 'MODEL_ID' environment variable, or create an mlops.json "
36+
"file")
37+
if self.deployment_id is None:
38+
raise Exception("no deployment_id found, please add 'DEPLOYMENT_ID' environment variable, or create an "
39+
"mlops.json file")
40+
3141
def init(self):
3242
os.environ['MLOPS_DEPLOYMENT_ID'] = self.deployment_id
3343
os.environ['MLOPS_MODEL_ID'] = self.model_id

adk/modeldata.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,37 +56,42 @@ def initialize(self):
5656
self.models[name] = FileData(real_hash, local_data_path)
5757

5858
def get_model(self, model_name):
59-
if model_name in self.models:
60-
return self.models[model_name].file_path
61-
elif len([optional for optional in self.manifest_data['optional_files'] if
62-
optional['name'] == model_name]) > 0:
63-
self.find_optional_model(model_name)
64-
return self.models[model_name].file_path
59+
if self.available():
60+
if model_name in self.models:
61+
return self.models[model_name].file_path
62+
elif len([optional for optional in self.manifest_data['optional_files'] if
63+
optional['name'] == model_name]) > 0:
64+
self.find_optional_model(model_name)
65+
return self.models[model_name].file_path
66+
else:
67+
raise Exception("model name " + model_name + " not found in manifest")
6568
else:
66-
raise Exception("model name " + model_name + " not found in manifest")
69+
raise Exception("unable to get model {}, model_manifest.json not found.".format(model_name))
6770

6871
def find_optional_model(self, file_name):
69-
70-
found_models = [optional for optional in self.manifest_data['optional_files'] if
71-
optional['name'] == file_name]
72-
if len(found_models) == 0:
73-
raise Exception("file with name '" + file_name + "' not found in model manifest.")
74-
model_info = found_models[0]
75-
self.models[file_name] = {}
76-
source_uri = model_info['source_uri']
77-
fail_on_tamper = model_info.get("fail_on_tamper", False)
78-
expected_hash = model_info.get('md5_checksum', None)
79-
with self.client.file(source_uri).getFile() as f:
80-
local_data_path = f.name
81-
real_hash = md5_for_file(local_data_path)
82-
if self.using_frozen:
83-
if real_hash != expected_hash and fail_on_tamper:
84-
raise Exception("Model File Mismatch for " + file_name +
85-
"\nexpected hash: " + expected_hash + "\nreal hash: " + real_hash)
72+
if self.available():
73+
found_models = [optional for optional in self.manifest_data['optional_files'] if
74+
optional['name'] == file_name]
75+
if len(found_models) == 0:
76+
raise Exception("file with name '" + file_name + "' not found in model manifest.")
77+
model_info = found_models[0]
78+
self.models[file_name] = {}
79+
source_uri = model_info['source_uri']
80+
fail_on_tamper = model_info.get("fail_on_tamper", False)
81+
expected_hash = model_info.get('md5_checksum', None)
82+
with self.client.file(source_uri).getFile() as f:
83+
local_data_path = f.name
84+
real_hash = md5_for_file(local_data_path)
85+
if self.using_frozen:
86+
if real_hash != expected_hash and fail_on_tamper:
87+
raise Exception("Model File Mismatch for " + file_name +
88+
"\nexpected hash: " + expected_hash + "\nreal hash: " + real_hash)
89+
else:
90+
self.models[file_name] = FileData(real_hash, local_data_path)
8691
else:
8792
self.models[file_name] = FileData(real_hash, local_data_path)
8893
else:
89-
self.models[file_name] = FileData(real_hash, local_data_path)
94+
raise Exception("unable to get model {}, model_manifest.json not found.".format(model_name))
9095

9196
def get_manifest(self):
9297
if os.path.exists(self.manifest_frozen_path):

tests/test_adk_local.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,18 @@ def test_manifest_file_success(self):
135135
self.assertEqual(expected_output, actual_output)
136136

137137
def test_manifest_file_tampered(self):
138-
input = "Algorithmia"
138+
input = 'Algorithmia'
139139
expected_output = {"error": {"error_type": "LoadingError",
140140
"message": "Model File Mismatch for squeezenet\n"
141141
"expected hash: f20b50b44fdef367a225d41f747a0963\n"
142142
"real hash: 46a44d32d2c5c07f7f66324bef4c7266",
143-
"stacktrace": "NoneType: None\n"}}
143+
"stacktrace": ''}}
144144

145145
actual_output = json.loads(self.execute_manifest_example(input, apply_successful_manifest_parsing,
146146
loading_with_manifest,
147147
manifest_path="tests/manifests/bad_model_manifest"
148148
".json"))
149+
actual_output['error']['stacktrace'] = ''
149150
self.assertEqual(expected_output, actual_output)
150151

151152

0 commit comments

Comments
 (0)