@@ -19,14 +19,12 @@ def read_lines(path):
19
19
return f .read ().splitlines ()
20
20
21
21
22
- def get_pip_requirements (run_id , artifact_path , return_constraints = False ):
23
- req_path = download_artifacts (run_id = run_id , artifact_path = f"{ artifact_path } /requirements.txt" )
22
+ def get_pip_requirements (artifact_uri , return_constraints = False ):
23
+ req_path = download_artifacts (artifact_uri = f"{ artifact_uri } /requirements.txt" )
24
24
reqs = read_lines (req_path )
25
25
26
26
if return_constraints :
27
- con_path = download_artifacts (
28
- run_id = run_id , artifact_path = f"{ artifact_path } /constraints.txt"
29
- )
27
+ con_path = download_artifacts (artifact_uri = f"{ artifact_uri } /constraints.txt" )
30
28
cons = read_lines (con_path )
31
29
return set (reqs ), set (cons )
32
30
@@ -43,30 +41,28 @@ def main():
43
41
xgb_req = f"xgboost=={ xgb .__version__ } "
44
42
sklearn_req = f"scikit-learn=={ sklearn .__version__ } "
45
43
46
- with mlflow .start_run () as run :
47
- run_id = run .info .run_id
48
-
44
+ with mlflow .start_run ():
49
45
# Default (both `pip_requirements` and `extra_pip_requirements` are unspecified)
50
46
artifact_path = "default"
51
- mlflow .xgboost .log_model (model , artifact_path , signature = signature )
52
- pip_reqs = get_pip_requirements (run_id , artifact_path )
47
+ model_info = mlflow .xgboost .log_model (model , artifact_path , signature = signature )
48
+ pip_reqs = get_pip_requirements (model_info . model_uri )
53
49
assert xgb_req in pip_reqs , pip_reqs
54
50
55
51
# Overwrite the default set of pip requirements using `pip_requirements`
56
52
artifact_path = "pip_requirements"
57
- mlflow .xgboost .log_model (
53
+ model_info = mlflow .xgboost .log_model (
58
54
model , artifact_path , pip_requirements = [sklearn_req ], signature = signature
59
55
)
60
- pip_reqs = get_pip_requirements (run_id , artifact_path )
56
+ pip_reqs = get_pip_requirements (model_info . model_uri )
61
57
assert sklearn_req in pip_reqs , pip_reqs
62
58
63
59
# Add extra pip requirements on top of the default set of pip requirements
64
60
# using `extra_pip_requirements`
65
61
artifact_path = "extra_pip_requirements"
66
- mlflow .xgboost .log_model (
62
+ model_info = mlflow .xgboost .log_model (
67
63
model , artifact_path , extra_pip_requirements = [sklearn_req ], signature = signature
68
64
)
69
- pip_reqs = get_pip_requirements (run_id , artifact_path )
65
+ pip_reqs = get_pip_requirements (model_info . model_uri )
70
66
assert pip_reqs .issuperset ({xgb_req , sklearn_req }), pip_reqs
71
67
72
68
# Specify pip requirements using a requirements file
@@ -76,21 +72,21 @@ def main():
76
72
77
73
# Path to a pip requirements file
78
74
artifact_path = "requirements_file_path"
79
- mlflow .xgboost .log_model (
75
+ model_info = mlflow .xgboost .log_model (
80
76
model , artifact_path , pip_requirements = f .name , signature = signature
81
77
)
82
- pip_reqs = get_pip_requirements (run_id , artifact_path )
78
+ pip_reqs = get_pip_requirements (model_info . model_uri )
83
79
assert sklearn_req in pip_reqs , pip_reqs
84
80
85
81
# List of pip requirement strings
86
82
artifact_path = "requirements_file_list"
87
- mlflow .xgboost .log_model (
83
+ model_info = mlflow .xgboost .log_model (
88
84
model ,
89
85
artifact_path ,
90
86
pip_requirements = [xgb_req , f"-r { f .name } " ],
91
87
signature = signature ,
92
88
)
93
- pip_reqs = get_pip_requirements (run_id , artifact_path )
89
+ pip_reqs = get_pip_requirements (model_info . model_uri )
94
90
assert pip_reqs .issuperset ({xgb_req , sklearn_req }), pip_reqs
95
91
96
92
# Using a constraints file
@@ -99,15 +95,13 @@ def main():
99
95
f .flush ()
100
96
101
97
artifact_path = "constraints_file"
102
- mlflow .xgboost .log_model (
98
+ model_info = mlflow .xgboost .log_model (
103
99
model ,
104
100
artifact_path ,
105
101
pip_requirements = [xgb_req , f"-c { f .name } " ],
106
102
signature = signature ,
107
103
)
108
- pip_reqs , pip_cons = get_pip_requirements (
109
- run_id , artifact_path , return_constraints = True
110
- )
104
+ pip_reqs , pip_cons = get_pip_requirements (model_info .model_uri , return_constraints = True )
111
105
assert pip_reqs .issuperset ({xgb_req , "-c constraints.txt" }), pip_reqs
112
106
assert pip_cons == {sklearn_req }, pip_cons
113
107
0 commit comments