import os
GCP_PROJECT = 'dp-workspace'
GCS_BUCKET = 'gs://cluster19-bkt'
GCS_REGION = 'us-central1'
os.putenv("REGION", GCS_REGION)
TF_RECORD_SEQEX = GCS_BUCKET+'/synthea/serv/seqex*'
os.putenv("SEQEX_IN_GCS", TF_RECORD_SEQEX)
MODEL_PATH = GCS_BUCKET+'/synthea/model/'
os.putenv("MODEL_IN_GCS", MODEL_PATH+"*")
SAVED_MODEL_PATH = MODEL_PATH + 'export'
os.putenv("SAVED_MODEL_IN_GCS", SAVED_MODEL_PATH+"*")
SERVING_DATASET = GCS_BUCKET+'/synthea/serv/seqex-00002-of-00003.tfrecords'
os.putenv("SERVING_DATASET", SERVING_DATASET)
INFERENCE_PATH = MODEL_PATH + 'infer'
os.putenv("INFERENCE_PATH", INFERENCE_PATH)
os.putenv("MODEL_NAME", "tf_fhir_los")

"""
Explanation: <h1> Structured Machine Learning using Tensorflow, Google Cloud Datalab and Cloud ML</h1>
<hr />
<b>This notebook demonstrates a process to deploy a ML model to CloudML. It leverages a pre-built machine learning model to predict Length of Stay in ED and inpatient care settings. Finally it runs an inference job on the CloudML Engine to render predictions. This is step 2 of 2.</b>
<h3>
<br />
<ol>
<li> Setup Environment </li> <br />
<li> Deploy and Run a ML Model on CloudML </li>
</ol></h3>
<hr />

<h2> 1. Setup Environment</h2>
<ul>
    <li>Initialize environment variables for your environment</li>
    <li>Please change the values of the following before executing rest of the cells in this notebook: <br />
        <b>1. GCP_PROJECT and </b> <br />
        <b>2. GCS_BUCKET </b> <br />
        <b>3. GCS_REGION </b>
    </li>
</ul>
End of explanation
"""


# from apache_beam.options.pipeline_options import PipelineOptions
# from apache_beam.options.pipeline_options import GoogleCloudOptions
# from apache_beam.options.pipeline_options import StandardOptions
# import apache_beam as beam
from tensorflow.core.example import example_pb2
import tensorflow as tf
import time

from proto import version_config_pb2
from proto.stu3 import fhirproto_extensions_pb2
from proto.stu3 import resources_pb2

from google.protobuf import text_format
from py.google.fhir.labels import label
from py.google.fhir.labels import bundle_to_label
from py.google.fhir.seqex import bundle_to_seqex
from py.google.fhir.models import model
from py.google.fhir.models.model import make_estimator

"""
Explanation: <b>Import dependencies. </b>
End of explanation
"""


import logging
logger = logging.getLogger()
#logger.setLevel(logging.INFO)
logger.setLevel(logging.ERROR)

"""
Explanation: <b>Optionally, enable logging for debugging.</b>
End of explanation
"""


%bash
gsutil ls -l ${SEQEX_IN_GCS}

"""
Explanation: <b> Previous step saved Sequence Examples into GCS. Let's examine file size and location of the Sequence Examples we will use of the inference. </b>
End of explanation
"""


from py.google.fhir.models.model import get_serving_input_fn
hparams = model.create_hparams()
time_crossed_features = [
        cross.split(':') for cross in hparams.time_crossed_features if cross
    ]
LABEL_VALUES = ['less_or_equal_3', '3_7', '7_14', 'above_14']
estimator = make_estimator(hparams, LABEL_VALUES, MODEL_PATH)
serving_input_fn = get_serving_input_fn(hparams.dedup, hparams.time_windows, hparams.include_age, hparams.categorical_context_features, hparams.sequence_features, time_crossed_features)
export_dir = estimator.export_savedmodel(SAVED_MODEL_PATH, serving_input_fn)
os.putenv("MODEL_BINARY", export_dir)

"""
Explanation: <h2> 2. Deploy and Run ML Model on Cloud ML</h2>
<ul>
    <li>A pre-trained ML Model which was exported to GCS in step 1 will be deployed to Cloud ML Serving.</li>
</ul>
<b>2a. Let's start with exporting our model for serving.<b>
End of explanation
"""


%%bash
gcloud ml-engine models list

"""
Explanation: <b>2b. List all the models deployed currently in the Cloud ML Engine</b>
End of explanation
"""


%%bash
gcloud ml-engine versions delete v1 --model ${MODEL_NAME} -q
gcloud ml-engine models delete $MODEL_NAME -q

"""
Explanation: <b>2c. Optionally run following cell to delete previously deployed model. </b>
End of explanation
"""


%%bash
gcloud ml-engine models create $MODEL_NAME --regions=$REGION

"""
Explanation: <b>2d. Run following cell to create a new model if it does not exist </b>
End of explanation
"""


%%bash
gcloud ml-engine versions list --model ${MODEL_NAME}

"""
Explanation: <b> 2e. List versions of the Model</b>
End of explanation
"""


%%bash
#gcloud ml-engine versions delete v1 --model ${MODEL_NAME} -q
gcloud ml-engine versions create v1 \
    --model ${MODEL_NAME} \
    --origin ${MODEL_BINARY} \
    --runtime-version 1.12

"""
Explanation: <b> 2f. Run following cell to create a new version of the model. Increment the version number like v1, v2, v3 </b> <br />
Optionally, you can delete a version using: <br />
gcloud ml-engine versions delete v1 --model ${MODEL_NAME} -q
End of explanation
"""


%%bash
INFER_JOB_NAME="job_inf_$(date +%Y%m%d_%H%M%S)"
gcloud ml-engine jobs submit prediction $INFER_JOB_NAME --model $MODEL_NAME     --version v1     --data-format tf-record     --region $REGION     --input-paths $SERVING_DATASET     --output-path $INFERENCE_PATH


"""
Explanation: <b> 2g. Run an inference job on CloudML engine </b>
End of explanation
"""


%%bash
gsutil cat ${INFERENCE_PATH}/prediction.results-00000-of-00001

"""
Explanation: <b>You can check the status of the job and other information on <a href="https://console.cloud.google.com/mlengine/jobs">GCP CloudML page</a> </b>
<b> 2h. View the prediction (output) generated by the inference job </b>
End of explanation
"""


