# Make sure you have the processed data there.
!ls ./data

"""
Explanation: <h1>About this Notebook</h1>

This notebook demonstrates the experience of using ML Workbench to create a machine learning model for text classification and setting it up for online prediction. This is the "cloud run" version of previous notebook. Preprocessing, training, batch prediction, are all done in cloud with various of services. The cloud run can be distributed, so it can handle really large data. Although in this case there is little benefit with the small demo data, the purpose is to demonstrate the usage of cloud run mode of ML Workbench.
There are only a few things that need to change between "local run" and "cloud run":

all data sources or file paths must be on GCS.
the --cloud flag must be set for each step.
"cloud_config" can be set for cloud specific settings, such as project_id, machine_type. In some cases it is required.

Other than this, nothing else changes from local to cloud!
If you have any feedback, please send them to datalab-feedback@google.com.
Validate Data
Assuming you have run the previous notebook (Text classification with MLWorkbench (small dataset experience)) so you already have cleaned data saved.
End of explanation
"""


!gsutil mb gs://datalab-mlworkbench-20newslab

!gsutil -m cp ./data/train.csv ./data/eval.csv gs://datalab-mlworkbench-20newslab

import google.datalab.contrib.mlworkbench.commands  # This loads the '%%ml' magics

%%ml dataset create
name: newsgroup_data_gcs
format: csv
schema:
  - name: news_label
    type: STRING
  - name: text
    type: STRING  
train: gs://datalab-mlworkbench-20newslab/train.csv
eval: gs://datalab-mlworkbench-20newslab/eval.csv    

"""
Explanation: Create Model with ML Workbench
The MLWorkbench Magics are a set of Datalab commands that allow an easy code-free experience to training, deploying, and predicting ML models. This notebook will take the cleaned data from the previous notebook and build a text classification model. The MLWorkbench Magics are a collection of magic commands for each step in ML workflows: analyzing input data to build transforms, transforming data, training a model, evaluating a model, and deploying a model.
For details of each command, run with --help. For example, "%%ml train --help".
This notebook shows the cloud version of every command, and gives the normal experience when building models are large datasets. However, we will still use the 20 newsgroup data. 
Setup: Move the data to GCS
The csv files, and all input files to the MLWorkbench magics must exist on GCS first. Therefore the first step is to make a new GCS bucket and copy the local csv files to GCS.
End of explanation
"""


%%ml analyze --cloud
output: gs://datalab-mlworkbench-20newslab/analysis
data: newsgroup_data_gcs
features:
    news_label:
        transform: target
    text:
        transform: bag_of_words

"""
Explanation: Step 1: Analyze
In cloud run, analysis is implemented with BigQuery. Running it may incur some costs.
End of explanation
"""


!gsutil -m rm -rf gs://datalab-mlworkbench-20newslab/transform # Delete previous results if any.

%%ml transform --cloud
output: gs://datalab-mlworkbench-20newslab/transform
analysis: gs://datalab-mlworkbench-20newslab/analysis
data: newsgroup_data_gcs

"""
Explanation: Step 2: Transform
In cloud run, analysis is implemented with Cloud DataFlow. Running it may incur some costs.
End of explanation
"""


!gsutil ls gs://datalab-mlworkbench-20newslab/transform

%%ml dataset create
name: newsgroup_data_gcs_transformed
format: transformed
train: gs://datalab-mlworkbench-20newslab/transform/train-*
eval: gs://datalab-mlworkbench-20newslab/transform/eval-*    

"""
Explanation: Click the links in output cell to monitor the jobs progress. Once they are completed (usually within 15 minutes with the job startup overhead), check the output.
End of explanation
"""


# Training should use an empty output folder. So if you run training multiple times,
# use different folders or remove the output from the previous run.
!gsutil -m rm -fr gs://datalab-mlworkbench-20newslab/train

"""
Explanation: Step 3: Training
In cloud run, training is implemented with Cloud ML Engine Training service. Running it may incur some costs.
End of explanation
"""


%%ml train --cloud
output: gs://datalab-mlworkbench-20newslab/train
analysis: gs://datalab-mlworkbench-20newslab/analysis
data: newsgroup_data_gcs_transformed
model_args:
    model: linear_classification
    top-n: 5
cloud_config:
    scale_tier: BASIC
    region: us-central1
    runtime_version: '1.2'

# Once training is done, check the output.
!gsutil list gs://datalab-mlworkbench-20newslab/train

"""
Explanation: Note that, "runtime_version: '1.2'" specifies which TensorFlow version is used at training. The first time training is a bit slower because of warm up, but if you train it multiple times the runs after first will be faster.
End of explanation
"""


%%ml batch_predict
model: gs://datalab-mlworkbench-20newslab/train/evaluation_model
output: gs://datalab-mlworkbench-20newslab/prediction
format: csv
data:
  csv: gs://datalab-mlworkbench-20newslab/eval.csv

!gsutil ls gs://datalab-mlworkbench-20newslab/prediction/

%%ml evaluate confusion_matrix --plot
size: 15
csv: gs://datalab-mlworkbench-20newslab/prediction/predict_results_eval.csv

%%ml evaluate accuracy
csv: gs://datalab-mlworkbench-20newslab/prediction/predict_results_eval.csv

"""
Explanation: Step 4: Evaluation using batch prediction
See previous notebook (Text Classification --- 20NewsGroup (small data)). You can do local batch prediction with the model trained in cloud.
End of explanation
"""


%%ml predict
model: gs://datalab-mlworkbench-20newslab/train/model
data:
  - nasa
  - windows xp

"""
Explanation: Prediction
Local Instant Prediction
Local instant prediction works with model trained in cloud too.
End of explanation
"""


%%ml model deploy
name: newsgroup.alpha
path: gs://datalab-mlworkbench-20newslab/train/model

"""
Explanation: Deploying Model to ML Engine
End of explanation
"""


# Let's create a CSV file from eval.csv by removing the target column.
with open('./data/eval.csv', 'r') as f, open('./data/test.csv', 'w') as fout:
    for l in f:
        fout.write(l.split(',')[1])

!gsutil cp ./data/test.csv gs://datalab-mlworkbench-20newslab/test.csv

%%ml batch_predict --cloud
model: newsgroup.alpha
output: gs://datalab-mlworkbench-20newslab/test
format: json
data:
    csv: gs://datalab-mlworkbench-20newslab/test.csv
cloud_config:
    region: us-central1

"""
Explanation: Batch Prediction
End of explanation
"""


!gsutil ls -lh gs://datalab-mlworkbench-20newslab/test

!gsutil cat gs://datalab-mlworkbench-20newslab/test/prediction.results* | head -n 2

"""
Explanation: Once job is completed, take a look at the results.
End of explanation
"""


%%ml model delete
name: newsgroup.alpha

%%ml model delete
name: newsgroup

# Delete the files in the GCS bucket, and delete the bucket
!gsutil -m rm -r gs://datalab-mlworkbench-20newslab

"""
Explanation: Prediction from a python client
See the previous notebook in this sequence for the example. 
Clean up
This section is optional. We will delete all the GCP resources and local files created in this sequence of notebooks. If you are not ready to delete anything, don't run any of the following cells.
End of explanation
"""


