model_as_code_driver.py
1 # This is an example for logging a Python model from code using the 2 # mlflow.pyfunc.log_model API. When a path to a valid Python script is submitted to the 3 # python_model argument, the model code itself is serialized instead of the model object. 4 # Within the targeted script, the model implementation must be defined and set by 5 # using the mlflow.models.set_model API. 6 7 import pandas as pd 8 9 import mlflow 10 11 input_example = ["What is the weather like today?"] 12 13 # Specify the path to the model notebook 14 model_path = "model_as_code.py" 15 print(f"Model path: {model_path}") 16 17 print("Logging model as code using Pyfunc log model API") 18 with mlflow.start_run(): 19 model_info = mlflow.pyfunc.log_model( 20 python_model=model_path, 21 name="ai-model", 22 input_example=input_example, 23 ) 24 25 print("Loading model using Pyfunc load model API") 26 pyfunc_model = mlflow.pyfunc.load_model(model_info.model_uri) 27 output = pyfunc_model.predict(pd.DataFrame(input_example, columns=["input"])) 28 print(f"Output: {output}")