Skip to content

Commit e28e31f

Browse files
use yaml to read MLFlowModel file
1 parent 5e8d8fd commit e28e31f

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

src/sasctl/pzmm/mlflow_model.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +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 json
4+
import yaml
55
from pathlib import Path
66

77

@@ -31,32 +31,28 @@ def read_mlflow_model_file(cls, m_path=Path.cwd()):
3131
Model output variables
3232
"""
3333
with open(Path(m_path) / "MLmodel", "r") as m_file:
34-
m_lines = m_file.readlines()
34+
m_yml = yaml.safe_load(m_file)
3535

3636
# 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
37+
try:
38+
var_dict = {
39+
"python_version": m_yml["flavors"]["python_function"]["python_version"],
40+
"model_path": m_yml["flavors"]["python_function"]["model_path"],
41+
"serialization_format": m_yml["flavors"]["sklearn"]["serialization_format"],
42+
"run_id": m_yml["run_id"],
43+
"mlflowPath": m_path
44+
}
45+
except KeyError:
46+
raise ValueError("This MLFlow model type is not currently supported.") from None
4647

4748
# 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:
49+
try:
50+
inputs_dict = m_yml["signature"]["inputs"]
51+
outputs_dict = m_yml["signature"]["outputs"]
52+
except KeyError:
5853
raise ValueError(
5954
"Improper or unset signature values for model. No input or output "
6055
"dicts could be generated. "
61-
)
56+
) from None
57+
6258
return var_dict, inputs_dict, outputs_dict

0 commit comments

Comments
 (0)