|
1 | 1 | # Copyright (c) 2020, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
|
2 | 2 | # SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
| 4 | +import yaml |
4 | 5 | import json
|
5 | 6 | from pathlib import Path
|
6 | 7 |
|
@@ -31,32 +32,30 @@ def read_mlflow_model_file(cls, m_path=Path.cwd()):
|
31 | 32 | Model output variables
|
32 | 33 | """
|
33 | 34 | with open(Path(m_path) / "MLmodel", "r") as m_file:
|
34 |
| - m_lines = m_file.readlines() |
| 35 | + m_yml = yaml.safe_load(m_file) |
35 | 36 |
|
36 | 37 | # 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 |
46 | 50 |
|
47 | 51 | # 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: |
58 | 56 | raise ValueError(
|
59 | 57 | "Improper or unset signature values for model. No input or output "
|
60 | 58 | "dicts could be generated. "
|
61 |
| - ) |
| 59 | + ) from None |
| 60 | + |
62 | 61 | return var_dict, inputs_dict, outputs_dict
|
0 commit comments