Skip to content

Commit 44e4786

Browse files
authored
Merge pull request #180 from SilvestriStefano/mlflow-fix
MLFlowModel rewrite
2 parents 7c7d9f1 + bb9a00e commit 44e4786

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/sasctl/pzmm/mlflow_model.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4+
import yaml
45
import json
56
from pathlib import Path
67

@@ -31,32 +32,30 @@ def read_mlflow_model_file(cls, m_path=Path.cwd()):
3132
Model output variables
3233
"""
3334
with open(Path(m_path) / "MLmodel", "r") as m_file:
34-
m_lines = m_file.readlines()
35+
m_yml = yaml.safe_load(m_file)
3536

3637
# Read in metadata and properties from the MLFlow model
37-
var_list = ["python_version", "serialization_format", "run_id", "model_path"]
38-
for i, var_string in enumerate(var_list):
39-
index = [i for i, s in enumerate(m_lines) if var_string in s]
40-
if not index:
41-
raise ValueError("This MLFlow model type is not currently supported.")
42-
var_list[i] = {var_list[i]: m_lines[index[0]].strip().split(" ")[1]}
43-
44-
var_dict = {k: v for d in var_list for k, v in d.items()}
45-
var_dict["mlflowPath"] = m_path
38+
try:
39+
var_dict = {
40+
"python_version": m_yml["flavors"]["python_function"]["python_version"],
41+
"model_path": m_yml["flavors"]["python_function"]["model_path"],
42+
"serialization_format": m_yml["flavors"]["sklearn"]["serialization_format"],
43+
"run_id": m_yml["run_id"],
44+
"mlflowPath": m_path
45+
}
46+
except KeyError:
47+
raise ValueError("This MLFlow model type is not currently supported.") from None
48+
except TypeError:
49+
raise ValueError("This MLFlow model type is not currently supported.") from None
4650

4751
# Read in the input and output variables
48-
ind_in = [i for i, s in enumerate(m_lines) if "inputs:" in s]
49-
ind_out = [i for i, s in enumerate(m_lines) if "outputs:" in s]
50-
51-
if ind_in and ind_out:
52-
inputs = m_lines[ind_in[0] : ind_out[0]]
53-
outputs = m_lines[ind_out[0] : -1]
54-
55-
inputs_dict = json.loads("".join([s.strip() for s in inputs])[9:-1])
56-
outputs_dict = json.loads("".join([s.strip() for s in outputs])[10:-1])
57-
else:
52+
try:
53+
inputs_dict = json.loads(m_yml["signature"]["inputs"])
54+
outputs_dict = json.loads(m_yml["signature"]["outputs"])
55+
except KeyError:
5856
raise ValueError(
5957
"Improper or unset signature values for model. No input or output "
6058
"dicts could be generated. "
61-
)
59+
) from None
60+
6261
return var_dict, inputs_dict, outputs_dict

0 commit comments

Comments
 (0)