55# MAGIC %restart_python
66
77# COMMAND ----------
8+ import hashlib
89import time
910
1011import mlflow
1112import pandas as pd
13+ import requests
1214from databricks .sdk import WorkspaceClient
1315from databricks .sdk .service .serving import EndpointCoreConfigInput , ServedEntityInput
1416from lightgbm import LGBMRegressor
1517from mlflow import MlflowClient
1618from mlflow .models import infer_signature
19+ from pyspark .dbutils import DBUtils
1720from pyspark .sql import SparkSession
1821from sklearn .compose import ColumnTransformer
1922from sklearn .impute import SimpleImputer
2023from sklearn .metrics import mean_absolute_error , mean_squared_error , r2_score
2124from sklearn .pipeline import Pipeline
22- from sklearn .preprocessing import OneHotEncoder
2325from sklearn .preprocessing import StandardScaler
24- import hashlib
25- import requests
2626
2727from wine_quality .config import ProjectConfig
2828
5858 "max_depth" : ab_test_params ["max_depth_b" ],
5959}
6060
61+ # COMMAND ----------
62+ spark = SparkSession .builder .getOrCreate ()
63+ dbutils = DBUtils (spark )
64+
6165# COMMAND ----------
6266
6367# MAGIC %md
108112
109113 # Train the model
110114 pipeline .fit (X_train , y_train )
111- y_pred = pipeline .predict (X_test )
115+ y_pred = pipeline .predict (X_test )
112116
113117 # Calculate performance metrics
114118 mse = mean_squared_error (y_test , y_pred )
124128 signature = infer_signature (model_input = X_train , model_output = y_pred )
125129
126130 # Log the input dataset for tracking reproducibility
127- dataset = mlflow .data .from_spark (train_set_spark ,
128- table_name = f"{ catalog_name } .{ schema_name } .train_set" ,
129- version = "0" )
131+ dataset = mlflow .data .from_spark (train_set_spark , table_name = f"{ catalog_name } .{ schema_name } .train_set" , version = "0" )
130132 mlflow .log_input (dataset , context = "training" )
131133
132134 # Log the pipeline model in MLflow with a unique artifact path
178180 mlflow .log_metric ("r2_score" , r2 )
179181 signature = infer_signature (model_input = X_train , model_output = y_pred )
180182
181- dataset = mlflow .data .from_spark (train_set_spark ,
182- table_name = f"{ catalog_name } .{ schema_name } .train_set" , version = "0" )
183+ dataset = mlflow .data .from_spark (train_set_spark , table_name = f"{ catalog_name } .{ schema_name } .train_set" , version = "0" )
183184 mlflow .log_input (dataset , context = "training" )
184185 mlflow .sklearn .log_model (sk_model = pipeline , artifact_path = "lightgbm-pipeline-model" , signature = signature )
185186
@@ -233,16 +234,14 @@ def predict(self, context, model_input):
233234
234235# COMMAND ----------
235236X_train = train_set [num_features + ["id" ]]
236- X_test = test_set [num_features + ["id" ]]
237+ X_test = test_set [num_features + ["id" ]]
237238
238239
239240# COMMAND ----------
240241models = [model_A , model_B ]
241242wrapped_model = WineQualityModelWrapper (models ) # we pass the loaded models to the wrapper
242243example_input = X_test .iloc [0 :1 ] # Select the first row for prediction as example
243- example_prediction = wrapped_model .predict (
244- context = None ,
245- model_input = example_input )
244+ example_prediction = wrapped_model .predict (context = None , model_input = example_input )
246245print ("Example Prediction:" , example_prediction )
247246
248247# COMMAND ----------
@@ -251,22 +250,16 @@ def predict(self, context, model_input):
251250
252251with mlflow .start_run () as run :
253252 run_id = run .info .run_id
254- signature = infer_signature (model_input = X_train ,
255- model_output = {"Prediction" : 1234.5 ,
256- "model" : "Model B" })
257- dataset = mlflow .data .from_spark (train_set_spark ,
258- table_name = f"{ catalog_name } .{ schema_name } .train_set" ,
259- version = "0" )
253+ signature = infer_signature (model_input = X_train , model_output = {"Prediction" : 1234.5 , "model" : "Model B" })
254+ dataset = mlflow .data .from_spark (train_set_spark , table_name = f"{ catalog_name } .{ schema_name } .train_set" , version = "0" )
260255 mlflow .log_input (dataset , context = "training" )
261256 mlflow .pyfunc .log_model (
262- python_model = wrapped_model ,# passing wrapped model here instead sklearn model
257+ python_model = wrapped_model , # passing wrapped model here instead sklearn model
263258 artifact_path = "pyfunc-wine-quality-model-ab" ,
264- signature = signature
259+ signature = signature ,
265260 )
266261model_version = mlflow .register_model (
267- model_uri = f"runs:/{ run_id } /pyfunc-wine-quality-model-ab" ,
268- name = model_name ,
269- tags = {"git_sha" : f"{ git_sha } " }
262+ model_uri = f"runs:/{ run_id } /pyfunc-wine-quality-model-ab" , name = model_name , tags = {"git_sha" : f"{ git_sha } " }
270263)
271264
272265# COMMAND ----------
@@ -276,7 +269,7 @@ def predict(self, context, model_input):
276269predictions = model .predict (X_test .iloc [0 :1 ])
277270
278271# Display predictions
279- predictions
272+ # predictions
280273
281274# COMMAND ----------
282275
@@ -313,7 +306,6 @@ def predict(self, context, model_input):
313306# MAGIC ### Call the endpoint
314307
315308# COMMAND ----------
316-
317309token = dbutils .notebook .entry_point .getDbutils ().notebook ().getContext ().apiToken ().get ()
318310host = spark .conf .get ("spark.databricks.workspaceUrl" )
319311
@@ -342,9 +334,7 @@ def predict(self, context, model_input):
342334
343335start_time = time .time ()
344336
345- model_serving_endpoint = (
346- f"https://{ host } /serving-endpoints/wine-quality-model-serving-ab-test/invocations"
347- )
337+ model_serving_endpoint = f"https://{ host } /serving-endpoints/wine-quality-model-serving-ab-test/invocations"
348338
349339response = requests .post (
350340 f"{ model_serving_endpoint } " ,
0 commit comments