# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernel_info:
#     name: python3-azureml
#   kernelspec:
#     display_name: Python 3.8 - AzureML
#     language: python
#     name: python38-azureml
# ---

# # Load the MLFlow model locally and try predictions
#
# ## Prerequisites
#
# 1. You need to have run successfully the training notebook related to this model, available in this same folder, where at the end of the notebook, after training the model, it downloads the 'artifacts' with the MLFlow model folder ("./artifact_downloads/outputs/mlflow-model").
#
# 2. Create a conda environment with the 'conda.yaml' file provided within the "mlflow-model" folder, doing like the following:
#
#    1. (base) /> conda env create --file conda.yaml --name automl-model-image-object-detection-env
#    
#    1. (base) /> conda activate automl-model-image-object-detection-env
#    
#    1. (automl-model-image-object-detection-env) /> conda install jupyter nb_conda
#
# 3. Run Jupyter and make sure you are using the related 'automl-model-image-object-detection-env' Kernel.
#
# 4. Run this notebook.

# If the MLFlow model files were downloaded successfully by the training notebook, you should see the files here.

# +
import os

# Show the contents of the MLFlow model folder
os.listdir("./artifact_downloads/outputs/mlflow-model")

# You should see a list of files such as the following:
# ['artifacts', 'conda.yaml', 'MLmodel', 'python_env.yaml', 'python_model.pkl', 'requirements.txt']
# -

# ### Load the test data into a Pandas DataFrame
#
# Load some test images into a Pandas DataFrame in order to try some predictions with it.

test_image_paths = [
    "./data/odFridgeObjects/images/1.jpg",
    "./data/odFridgeObjects/images/2.jpg",
    "./data/odFridgeObjects/images/3.jpg",
]

# +
import pandas as pd
import base64


def read_image(image_path):
    with open(image_path, "rb") as f:
        return f.read()


test_df = pd.DataFrame(
    data=[
        base64.encodebytes(read_image(image_path)) for image_path in test_image_paths
    ],
    columns=["image"],
)
test_df.shape
# -

# ## Load the best model in memory
#
# Load the model using MLflow flavor. Check MLmodel under the downloaded folder (artifact_downloads/outputs/mlflow-model). For this particular example (and for AutoML for Images scenario), MLmodel file will describe python_function flavor. We show how to load model using pyfunc flavor. For more information on MLflow flavors, visit: https://www.mlflow.org/docs/latest/models.html#storage-format
#
# Loading the models locally assume that you are running the notebook in an environment compatible with the model. The list of dependencies that is expected by the model is specified in the MLFlow model produced by AutoML (in the 'conda.yaml' file within the mlflow-model folder).

import mlflow.pyfunc

# +
# Way #1: Get the MLFlow model from the downloaded MLFlow model files

pyfunc_model = mlflow.pyfunc.load_model("./artifact_downloads/outputs/mlflow-model")

# +
# Make the predictions

result = pyfunc_model.predict(test_df).to_json(orient="records")
result
# -

# # Next Steps
# You can see further examples of other AutoML tasks such as Regression, Image-Classification, NLP-Text-Classification, Time-Series-Forcasting, etc.

#

StarcoderdataJupyterScriptsDedupFiltered