Skip to content

Commit 97570aa

Browse files
Fix RAI CLI examples in azureml-examples (#3687)
1 parent 934205a commit 97570aa

File tree

6 files changed

+74
-40
lines changed

6 files changed

+74
-40
lines changed

cli/responsible-ai/cli-responsibleaidashboard-housing-classification/cli-responsibleaidashboard-housing-classification.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939

4040
create_rai_job:
4141
type: command
42-
component: azureml://registries/azureml/components/rai_tabular_insight_constructor/versions/0.18.0
42+
component: azureml://registries/azureml/components/rai_tabular_insight_constructor/versions/0.21.0
4343
limits:
4444
timeout: 3600
4545
inputs:
@@ -54,7 +54,7 @@ jobs:
5454

5555
explain_01:
5656
type: command
57-
component: azureml://registries/azureml/components/rai_tabular_explanation/versions/0.18.0
57+
component: azureml://registries/azureml/components/rai_tabular_explanation/versions/0.21.0
5858
limits:
5959
timeout: 7200
6060
inputs:
@@ -63,7 +63,7 @@ jobs:
6363

6464
causal_01:
6565
type: command
66-
component: azureml://registries/azureml/components/rai_tabular_causal/versions/0.18.0
66+
component: azureml://registries/azureml/components/rai_tabular_causal/versions/0.21.0
6767
limits:
6868
timeout: 7200
6969
inputs:
@@ -75,7 +75,7 @@ jobs:
7575

7676
counterfactual_01:
7777
type: command
78-
component: azureml://registries/azureml/components/rai_tabular_counterfactual/versions/0.18.0
78+
component: azureml://registries/azureml/components/rai_tabular_counterfactual/versions/0.21.0
7979
limits:
8080
timeout: 7200
8181
inputs:
@@ -88,14 +88,14 @@ jobs:
8888
limits:
8989
timeout: 7200
9090
type: command
91-
component: azureml://registries/azureml/components/rai_tabular_erroranalysis/versions/0.18.0
91+
component: azureml://registries/azureml/components/rai_tabular_erroranalysis/versions/0.21.0
9292
inputs:
9393
rai_insights_dashboard: ${{parent.jobs.create_rai_job.outputs.rai_insights_dashboard}}
9494
max_depth: 3
9595

9696
gather_01:
9797
type: command
98-
component: azureml://registries/azureml/components/rai_tabular_insight_gather/versions/0.18.0
98+
component: azureml://registries/azureml/components/rai_tabular_insight_gather/versions/0.21.0
9999
limits:
100100
timeout: 7200
101101
inputs:
@@ -107,7 +107,7 @@ jobs:
107107

108108
scorecard_01:
109109
type: command
110-
component: azureml://registries/azureml/components/rai_tabular_score_card/versions/0.18.0
110+
component: azureml://registries/azureml/components/rai_tabular_score_card/versions/0.21.0
111111
inputs:
112112
dashboard: ${{parent.jobs.gather_01.outputs.dashboard}}
113113
pdf_generation_config:

cli/responsible-ai/cli-responsibleaidashboard-housing-classification/train.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import argparse
55
import json
66
import os
7+
import tempfile
78

89
from sklearn.ensemble import RandomForestClassifier
910
from sklearn.compose import ColumnTransformer
@@ -18,8 +19,6 @@
1819

1920
import time
2021

21-
from azureml.core.run import Run
22-
2322

2423
def parse_args():
2524
# setup arg parser
@@ -69,11 +68,8 @@ def get_classification_model_pipeline(continuous_features, categorical_features)
6968

7069

7170
def main(args):
72-
current_experiment = Run.get_context().experiment
73-
tracking_uri = current_experiment.workspace.get_mlflow_tracking_uri()
71+
tracking_uri = mlflow.get_tracking_uri()
7472
print("tracking_uri: {0}".format(tracking_uri))
75-
mlflow.set_tracking_uri(tracking_uri)
76-
mlflow.set_experiment(current_experiment.name)
7773

7874
# Read in data
7975
print("Reading data")
@@ -93,18 +89,39 @@ def main(args):
9389
model = pipeline.fit(X_train, y_train)
9490

9591
# Saving model with mlflow
96-
print("Saving model with MLFlow to temporary directory")
92+
print("Saving model with MLFlow to model_output directory")
9793
mlflow.sklearn.save_model(sk_model=model, path=args.model_output)
9894

9995
suffix = int(time.time())
10096
registered_name = "{0}_{1}".format(args.model_name, suffix)
10197
print(f"Registering model as {registered_name}")
10298

103-
mlflow.sklearn.log_model(
104-
sk_model=model,
105-
registered_model_name=registered_name,
106-
artifact_path=registered_name,
107-
)
99+
# Use save_model and create_model_version with file:// URI to let Azure ML handle the upload
100+
with tempfile.TemporaryDirectory() as temp_dir:
101+
model_dir = os.path.join(temp_dir, registered_name)
102+
mlflow.sklearn.save_model(model, model_dir)
103+
104+
# Use the older model registry API directly to avoid logged-models search
105+
from mlflow.tracking import MlflowClient
106+
107+
client = MlflowClient()
108+
109+
try:
110+
# Try to create the registered model (will fail if it already exists)
111+
client.create_registered_model(registered_name)
112+
print(f"Created new registered model: {registered_name}")
113+
except Exception as e:
114+
print(f"Registered model {registered_name} already exists: {e}")
115+
116+
# Create a new version of the model using file:// URI
117+
# Azure ML will handle the upload and generate the proper azureml:// URI
118+
file_uri = f"file://{model_dir}"
119+
print("Registering model with file_uri: {0}".format(file_uri))
120+
121+
model_version = client.create_model_version(
122+
name=registered_name, source=file_uri
123+
)
124+
print(f"Created model version {model_version.version} for {registered_name}")
108125

109126
model_info = {"id": "{0}:1".format(registered_name)}
110127
output_path = os.path.join(args.model_output_json, "model_info.json")

cli/responsible-ai/cli-responsibleaidashboard-housing-classification/train_housing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ outputs:
2121
type: path
2222

2323
code: ./train.py
24-
environment: azureml://registries/azureml/environments/responsibleai-tabular/versions/14
24+
environment: azureml://registries/azureml/environments/responsibleai-tabular/versions/26
2525
command: >-
2626
python train.py
2727
--training_data ${{inputs.training_data}}

cli/responsible-ai/cli-responsibleaidashboard-programmer-regression/cli-responsibleaidashboard-programmer-regression.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333

3434
create_rai_job:
3535
type: command
36-
component: azureml://registries/azureml/components/rai_tabular_insight_constructor/versions/0.18.0
36+
component: azureml://registries/azureml/components/rai_tabular_insight_constructor/versions/0.21.0
3737
limits:
3838
timeout: 3600
3939
inputs:
@@ -47,7 +47,7 @@ jobs:
4747

4848
explain_01:
4949
type: command
50-
component: azureml://registries/azureml/components/rai_tabular_explanation/versions/0.18.0
50+
component: azureml://registries/azureml/components/rai_tabular_explanation/versions/0.21.0
5151
limits:
5252
timeout: 7200
5353
inputs:
@@ -56,7 +56,7 @@ jobs:
5656

5757
causal_01:
5858
type: command
59-
component: azureml://registries/azureml/components/rai_tabular_causal/versions/0.18.0
59+
component: azureml://registries/azureml/components/rai_tabular_causal/versions/0.21.0
6060
limits:
6161
timeout: 7200
6262
inputs:
@@ -68,7 +68,7 @@ jobs:
6868

6969
counterfactual_01:
7070
type: command
71-
component: azureml://registries/azureml/components/rai_tabular_counterfactual/versions/0.18.0
71+
component: azureml://registries/azureml/components/rai_tabular_counterfactual/versions/0.21.0
7272
limits:
7373
timeout: 7200
7474
inputs:
@@ -81,15 +81,15 @@ jobs:
8181
limits:
8282
timeout: 7200
8383
type: command
84-
component: azureml://registries/azureml/components/rai_tabular_erroranalysis/versions/0.18.0
84+
component: azureml://registries/azureml/components/rai_tabular_erroranalysis/versions/0.21.0
8585
inputs:
8686
rai_insights_dashboard: ${{parent.jobs.create_rai_job.outputs.rai_insights_dashboard}}
8787
max_depth: 3
8888
filter_features: '["style", "Employer"]'
8989

9090
gather_01:
9191
type: command
92-
component: azureml://registries/azureml/components/rai_tabular_insight_gather/versions/0.18.0
92+
component: azureml://registries/azureml/components/rai_tabular_insight_gather/versions/0.21.0
9393
limits:
9494
timeout: 7200
9595
inputs:
@@ -101,7 +101,7 @@ jobs:
101101

102102
scorecard_01:
103103
type: command
104-
component: azureml://registries/azureml/components/rai_tabular_score_card/versions/0.18.0
104+
component: azureml://registries/azureml/components/rai_tabular_score_card/versions/0.21.0
105105
inputs:
106106
dashboard: ${{parent.jobs.gather_01.outputs.dashboard}}
107107
pdf_generation_config:

cli/responsible-ai/cli-responsibleaidashboard-programmer-regression/train.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import argparse
55
import json
66
import os
7+
import tempfile
78

89
from sklearn.ensemble import RandomForestRegressor
910
from sklearn.compose import ColumnTransformer
@@ -18,8 +19,6 @@
1819

1920
import time
2021

21-
from azureml.core.run import Run
22-
2322

2423
def parse_args():
2524
# setup arg parser
@@ -69,11 +68,8 @@ def get_regression_model_pipeline(continuous_features, categorical_features):
6968

7069

7170
def main(args):
72-
current_experiment = Run.get_context().experiment
73-
tracking_uri = current_experiment.workspace.get_mlflow_tracking_uri()
71+
tracking_uri = mlflow.get_tracking_uri()
7472
print("tracking_uri: {0}".format(tracking_uri))
75-
mlflow.set_tracking_uri(tracking_uri)
76-
mlflow.set_experiment(current_experiment.name)
7773

7874
# Read in data
7975
print("Reading data")
@@ -93,18 +89,39 @@ def main(args):
9389
model = pipeline.fit(X_train, y_train)
9490

9591
# Saving model with mlflow
96-
print("Saving model with MLFlow to temporary directory")
92+
print("Saving model with MLFlow to model_output directory")
9793
mlflow.sklearn.save_model(sk_model=model, path=args.model_output)
9894

9995
suffix = int(time.time())
10096
registered_name = "{0}_{1}".format(args.model_name, suffix)
10197
print(f"Registering model as {registered_name}")
10298

103-
mlflow.sklearn.log_model(
104-
sk_model=model,
105-
registered_model_name=registered_name,
106-
artifact_path=registered_name,
107-
)
99+
# Use save_model and create_model_version with file:// URI to let Azure ML handle the upload
100+
with tempfile.TemporaryDirectory() as temp_dir:
101+
model_dir = os.path.join(temp_dir, registered_name)
102+
mlflow.sklearn.save_model(model, model_dir)
103+
104+
# Use the older model registry API directly to avoid logged-models search
105+
from mlflow.tracking import MlflowClient
106+
107+
client = MlflowClient()
108+
109+
try:
110+
# Try to create the registered model (will fail if it already exists)
111+
client.create_registered_model(registered_name)
112+
print(f"Created new registered model: {registered_name}")
113+
except Exception as e:
114+
print(f"Registered model {registered_name} already exists: {e}")
115+
116+
# Create a new version of the model using file:// URI
117+
# Azure ML will handle the upload and generate the proper azureml:// URI
118+
file_uri = f"file://{model_dir}"
119+
print("Registering model with file_uri: {0}".format(file_uri))
120+
121+
model_version = client.create_model_version(
122+
name=registered_name, source=file_uri
123+
)
124+
print(f"Created model version {model_version.version} for {registered_name}")
108125

109126
model_info = {"id": "{0}:1".format(registered_name)}
110127
output_path = os.path.join(args.model_output_json, "model_info.json")

cli/responsible-ai/cli-responsibleaidashboard-programmer-regression/train_programmers.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ outputs:
2121
type: path
2222

2323
code: ./train.py
24-
environment: azureml://registries/azureml/environments/responsibleai-tabular/versions/14
24+
environment: azureml://registries/azureml/environments/responsibleai-tabular/versions/26
2525
command: >-
2626
python train.py
2727
--training_data ${{inputs.training_data}}

0 commit comments

Comments
 (0)