forked from mlflow/mlflow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.java
125 lines (104 loc) · 3.69 KB
/
Model.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package org.mlflow.models;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.mlflow.Flavor;
import org.mlflow.utils.FileUtils;
import org.mlflow.utils.SerializationUtils;
/**
* Represents an MLflow model. This class includes utility functions for parsing a serialized MLflow
* model configuration (`MLModel`) as a {@link Model} object.
*/
public class Model {
public static class Signature {
@JsonProperty("inputs")
private String inputsSchemaJson;
@JsonProperty("outputs")
private String outputSchemaJson;
}
@JsonProperty("artifact_path")
private String artifactPath;
@JsonProperty("run_id")
private String runId;
@JsonProperty("utc_time_created")
private String utcTimeCreated;
@JsonProperty("flavors")
private Map<String, Object> flavors;
@JsonProperty("signature")
Signature signature;
@JsonProperty("input_example")
private Map<String, Object> input_example;
@JsonProperty("model_uuid")
private String modelUuid;
private String rootPath;
/**
* Loads the configuration of an MLflow model and parses it as a {@link Model} object.
*
* @param modelRootPath The path to the root directory of the MLflow model
*/
public static Model fromRootPath(String modelRootPath) throws IOException {
String configPath = FileUtils.join(modelRootPath, "MLmodel");
return fromConfigPath(configPath);
}
/**
* Loads the configuration of an MLflow model and parses it as a {@link Model} object.
*
* @param configPath The path to the `MLModel` configuration file
*/
public static Model fromConfigPath(String configPath) throws IOException {
File configFile = new File(configPath);
Model model = SerializationUtils.parseYamlFromFile(configFile, Model.class);
// Set the model uuid if it's absent.
if (!model.getModelUuid().isPresent()) {
String uuid = UUID.randomUUID().toString().replace("-", "");
model.setModelUuid(uuid);
}
// Set the root path to the directory containing the configuration file.
// This will be used to create an absolute path to the serialized model
model.setRootPath(configFile.getParentFile().getAbsolutePath());
return model;
}
/** @return The MLflow model's artifact path */
public Optional<String> getArtifactPath() {
return Optional.ofNullable(this.artifactPath);
}
/** @return The MLflow model's time of creation */
public Optional<String> getUtcTimeCreated() {
return Optional.ofNullable(this.utcTimeCreated);
}
/** @return The MLflow model's run id */
public Optional<String> getRunId() {
return Optional.ofNullable(this.runId);
}
/** @return The MLflow model's uuid */
public Optional<String> getModelUuid() {
return Optional.ofNullable(this.modelUuid);
}
/** @return The path to the root directory of the MLflow model */
public Optional<String> getRootPath() {
return Optional.ofNullable(this.rootPath);
}
/**
* Reads the configuration corresponding to the specified flavor name and parses it as a `Flavor`
* object
*/
public <T extends Flavor> Optional<T> getFlavor(String flavorName, Class<T> flavorClass) {
if (this.flavors.containsKey(flavorName)) {
final ObjectMapper mapper = new ObjectMapper();
T flavor = mapper.convertValue(this.flavors.get(flavorName), flavorClass);
return Optional.of(flavor);
} else {
return Optional.<T>empty();
}
}
private void setRootPath(String rootPath) {
this.rootPath = rootPath;
}
private void setModelUuid(String modelUuid) {
this.modelUuid = modelUuid;
}
}