# Setup your dependencies
import os

# The Google Cloud Notebook product has specific requirements
IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists("/opt/deeplearning/metadata/env_version")

USER_FLAG = ""
# Google Cloud Notebook requires dependencies to be installed with '--user'
if IS_GOOGLE_CLOUD_NOTEBOOK:
    USER_FLAG = "--user"

"""
Explanation: Vertex AI Model Builder SDK: AutoML Tabular Training and Prediction
Overview
In this notebook, you learn how to use the Vertex AI Python client library to train and deploy a tabular classification model for online prediction.
Learning Objective
In this notebook, you learn how to:

Create a Vertex AI model training job.
Train an AutoML tabular model.
Deploy the model resource to a serving endpoint resource.
Make a prediction by sending data.
Undeploy the model resource.

Introduction
In this notebook, you will use Vertex AI Python client library to train and make predictions on an AutoML model based on a tabular dataset. Alternatively, you can train and make predictions on models by using the gcloud command-line tool or by using the online Cloud Console.
Each learning objective will correspond to a #TODO in the notebook, where you will complete the notebook cell's code before running the cell. Refer to the solution notebook for reference.
Make sure to enable the Vertex AI API and Compute Engine API.
Installation
End of explanation
"""


# Upgrade the specified package to the newest available version
! pip install {USER_FLAG} --upgrade google-cloud-aiplatform

"""
Explanation: Install the latest version of the Vertex AI client library.
Run the following command in your virtual environment to install the Vertex SDK for Python:
End of explanation
"""


# Upgrade the specified package to the newest available version
! pip install {USER_FLAG} --upgrade google-cloud-storage

"""
Explanation: Install the Cloud Storage library:
End of explanation
"""


# Automatically restart kernel after installs
import os

if not os.getenv("IS_TESTING"):
    # Automatically restart kernel after installs
    import IPython

    app = IPython.Application.instance()
    app.kernel.do_shutdown(True)

"""
Explanation: Restart the kernel
After you install the additional packages, you need to restart the notebook kernel so it can find the packages.
End of explanation
"""


import os

PROJECT_ID = ""

# Get your Google Cloud project ID from gcloud
if not os.getenv("IS_TESTING"):
    shell_output=!gcloud config list --format 'value(core.project)' 2>/dev/null
    PROJECT_ID = shell_output[0]
    print("Project ID: ", PROJECT_ID)

"""
Explanation: Set your project ID
If you don't know your project ID, you may be able to get your project ID using gcloud.
End of explanation
"""


if PROJECT_ID == "" or PROJECT_ID is None:
    PROJECT_ID = "[your-project-id]"  # @param {type:"string"}

"""
Explanation: Otherwise, set your project ID here.
End of explanation
"""


# Import necessary libraries
from datetime import datetime

# Use a timestamp to ensure unique resources
TIMESTAMP = datetime.now().strftime("%Y%m%d%H%M%S")

"""
Explanation: Timestamp
If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append it onto the name of resources you create in this tutorial.
End of explanation
"""


BUCKET_NAME = "gs://[your-bucket-name]"  # @param {type:"string"}
REGION = "[your-region]"  # @param {type:"string"}

if BUCKET_NAME == "" or BUCKET_NAME is None or BUCKET_NAME == "gs://[your-bucket-name]":
    BUCKET_NAME = "gs://" + PROJECT_ID + "aip-" + TIMESTAMP

"""
Explanation: Create a Cloud Storage bucket
The following steps are required, regardless of your notebook environment.
This notebook demonstrates how to use Model Builder SDK to create an AutoML model based on a tabular dataset. You will need to provide a Cloud Storage bucket where the dataset will be stored.
Set the name of your Cloud Storage bucket below. It must be unique across all of your 
Cloud Storage buckets.
You may also change the REGION variable, which is used for operations
throughout the rest of this notebook. Make sure to choose a region where Vertex AI services are
available. You may
not use a Multi-Regional Storage bucket for training with Vertex AI.
End of explanation
"""


! gsutil mb -l $REGION $BUCKET_NAME

"""
Explanation: Only if your bucket doesn't already exist: Run the following cell to create your Cloud Storage bucket.
End of explanation
"""


! gsutil ls -al $BUCKET_NAME

"""
Explanation: Finally, validate access to your Cloud Storage bucket by examining its contents:
End of explanation
"""


IMPORT_FILE = "petfinder-tabular-classification_toy.csv"
! gsutil cp gs://cloud-training/mlongcp/v3.0_MLonGC/toy_data/{IMPORT_FILE} {BUCKET_NAME}/data/

gcs_source = f"{BUCKET_NAME}/data/{IMPORT_FILE}"

"""
Explanation: Copy dataset into your Cloud Storage bucket
End of explanation
"""


# Import necessary libraries
import os

from google.cloud import aiplatform

aiplatform.init(project=PROJECT_ID, location=REGION)

"""
Explanation: Import Vertex SDK for Python
Import the Vertex SDK into your Python environment and initialize it.
End of explanation
"""


ds = dataset = aiplatform.TabularDataset.create(
    display_name="petfinder-tabular-dataset",
    gcs_source=gcs_source,
)

ds.resource_name

"""
Explanation: Tutorial
Now you are ready to create your AutoML Tabular model.
Create a Managed Tabular Dataset from a CSV
This section will create a dataset from a CSV file stored on your GCS bucket.
End of explanation
"""


# Constructs a AutoML Tabular Training Job
job = # TODO 1 -- Your code goes here(
    display_name="train-petfinder-automl-1",
    optimization_prediction_type="classification",
    column_transformations=[
        {"categorical": {"column_name": "Type"}},
        {"numeric": {"column_name": "Age"}},
        {"categorical": {"column_name": "Breed1"}},
        {"categorical": {"column_name": "Color1"}},
        {"categorical": {"column_name": "Color2"}},
        {"categorical": {"column_name": "MaturitySize"}},
        {"categorical": {"column_name": "FurLength"}},
        {"categorical": {"column_name": "Vaccinated"}},
        {"categorical": {"column_name": "Sterilized"}},
        {"categorical": {"column_name": "Health"}},
        {"numeric": {"column_name": "Fee"}},
        {"numeric": {"column_name": "PhotoAmt"}},
    ],
)

# Create and train the model object
# This will take around two hour and half to run
model = # TODO 2a -- Your code goes here(
    dataset=ds,
    target_column="Adopted",
    # Define training, validation and test fraction for training
    # TODO 2b -- Your code goes here 
    model_display_name="adopted-prediction-model",
    disable_early_stopping=False,
)

"""
Explanation: Launch a Training Job to Create a Model
Once we have defined your training script, we will create a model. The run function creates a training pipeline that trains and creates a Model object. After the training pipeline completes, the run function returns the Model object.
NOTE: It takes nearly 2 hours 15 minutes to complete the training. Please wait till the training get completed.
End of explanation
"""


# Deploy the model resource to the serving endpoint resource 
endpoint = # TODO 3 -- Your code goes here(
    machine_type="n1-standard-4",
)

"""
Explanation: Deploy your model
Before you use your model to make predictions, you need to deploy it to an Endpoint. You can do this by calling the deploy function on the Model resource. This function does two things:

Creates an Endpoint resource to which the Model resource will be deployed.
Deploys the Model resource to the Endpoint resource.

Deploy your model.
NOTE: Wait until the model FINISHES deployment before proceeding to prediction.
End of explanation
"""


# Make a prediction using the sample values 
prediction = # TODO 4 -- Your code goes here(
    [
        {
            "Type": "Cat",
            "Age": "3",
            "Breed1": "Tabby",
            "Gender": "Male",
            "Color1": "Black",
            "Color2": "White",
            "MaturitySize": "Small",
            "FurLength": "Short",
            "Vaccinated": "No",
            "Sterilized": "No",
            "Health": "Healthy",
            "Fee": "100",
            "PhotoAmt": "2",
        }
    ]
)

print(prediction)

"""
Explanation: Predict on the endpoint

This sample instance is taken from an observation in which Adopted = Yes
Note that the values are all strings. Since the original data was in CSV format, everything is treated as a string. The transformations you defined when creating your AutoMLTabularTrainingJob inform Vertex AI to transform the inputs to their defined types.
End of explanation
"""


# Undeploy the model resource 
# TODO 5 -- Your code goes here

"""
Explanation: Undeploy the model
To undeploy your Model resource from the serving Endpoint resource, use the endpoint's undeploy method with the following parameter:

deployed_model_id: The model deployment identifier returned by the prediction service when the Model resource is deployed. You can retrieve the deployed_model_id using the prediction object's deployed_model_id property.
End of explanation
"""


delete_training_job = True
delete_model = True
delete_endpoint = True

# Warning: Setting this to true will delete everything in your bucket
delete_bucket = False

# Delete the training job
job.delete()

# Delete the model
model.delete()

# Delete the endpoint
endpoint.delete()

if delete_bucket and "BUCKET_NAME" in globals():
    ! gsutil -m rm -r $BUCKET_NAME

"""
Explanation: Cleaning up
To clean up all Google Cloud resources used in this project, you can delete the Google Cloud project you used for the tutorial.
Otherwise, you can delete the individual resources you created in this tutorial:

Training Job
Model
Endpoint
Cloud Storage Bucket

Note: You must delete any Model resources deployed to the Endpoint resource before deleting the Endpoint resource.
End of explanation
"""


