<jupyter_start><jupyter_text>Using Model Endpoint  Requirements- Authenticated to gcloud (```gcloud auth application-default login```) This notebook demonstrate how we can leverage model endpoint for integrating with external service. In the previous examples, we deployed models to create a version endpoints. The version endpoint is specific to a certain model version and the url will change everytime a new version is deployed. Hence, version endpoint is not suitable to be shared publicly or being used for integration. For publicly sharing your model URL it is recommended to use model endpoint. Model endpoint is a model URL that is not specific to certain version. The benefit of using model endpoint is that we can specify which version of the model being used (by redirecting traffic from one model version to another) without having our client to change the url.<jupyter_code>!pip install --upgrade -r requirements.txt > /dev/null
import merlin
import warnings
import os
from sklearn import svm
from sklearn import datasets
from joblib import dump
from sklearn.datasets import load_iris
from merlin.model import ModelType
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>1. Initialize MLP Resources  1.1 Set MLP Server<jupyter_code># Set MLP Server
merlin.set_url("localhost:8080/api/merlin")<jupyter_output><empty_output><jupyter_text>1.2 Set Active Project`project` represent a project in real life. You may have multiple model within a project.`merlin.set_project()` will set the active project into the name matched by argument. You can only set it to an existing project. If you would like to create a new project, please do so from the MLP console at http://localhost:8080/projects/create.<jupyter_code>merlin.set_project("sample")<jupyter_output><empty_output><jupyter_text>1.3 Set Active Model`model` represents an abstract ML model. Conceptually, `model` in MLP is similar to a class in programming language. To instantiate a `model` you'll have to create a `model_version`.Each `model` has a type, currently model type supported by MLP are: sklearn, xgboost, tensorflow, pytorch, and user defined model (i.e. pyfunc model).`model_version` represents a snapshot of particular `model` iteration. You'll be able to attach information such as metrics and tag to a given `model_version` as well as deploy it as a model service.`merlin.set_model(, )` will set the active model to the name given by parameter, if the model with given name is not found, a new model will be created.<jupyter_code>merlin.set_model("sklearn-sample", ModelType.SKLEARN)<jupyter_output><empty_output><jupyter_text>2. Train and Deploy  2.1 Train and Upload Model `merlin.new_model_version()` is a convenient method to create a model version and start its development process. It is equal to following codes:```v = model.new_model_version()v.start()v.log_model(model_dir=model_dir)v.finish()```<jupyter_code>model_dir = "sklearn-model"
MODEL_FILE = "model.joblib"

url = ""

# Create new version of the model
with merlin.new_model_version() as v:
    clf = svm.SVC(gamma='scale')
    iris = datasets.load_iris()
    X, y = iris.data, iris.target
    clf.fit(X, y)
    dump(clf, os.path.join(model_dir, MODEL_FILE))
    
    # Upload the serialized model to MLP
    merlin.log_model(model_dir=model_dir)<jupyter_output><empty_output><jupyter_text>2.2 Deploy Model<jupyter_code>endpoint = merlin.deploy(v)<jupyter_output><empty_output><jupyter_text>Once deployed you will receive a version endpoint. Notice that the url has following format: sklean-sample-http://./v1/models/sklearn-sample- . Everytime a new version is created, the  part will also change.    2.3 Send Test Request<jupyter_code>%%bash -s "$endpoint.url"
curl -X POST $1 -d '{
  "instances": [
    [2.8,  1.0,  6.8,  0.4],
    [3.1,  1.4,  4.5,  1.6]
  ]
}'<jupyter_output><empty_output><jupyter_text>2.4 Create Model Endpoint To create a model endpoint we can use `serve_traffic` method passing in a dictionary containing the version endpoint which will serve the traffic and the percentage of the traffic. In this case, we are going to use the version endpoint that we deployed before with 100% of traffic.<jupyter_code>model_endpoint = merlin.serve_traffic({endpoint:100})
model_endpoint<jupyter_output><empty_output><jupyter_text>Sending request to the model endpoint will return the same result as the version endpoint<jupyter_code>%%bash -s "$model_endpoint.url"
curl -X POST $1 -d '{
  "instances": [
    [2.8,  1.0,  6.8,  0.4],
    [3.1,  1.4,  4.5,  1.6]
  ]
}'<jupyter_output><empty_output><jupyter_text>2.5 Undeploy Model Endpoint To undeploy model endpoint we can use `stop_serving_traffic` method passing in an environment name where the model endpoint is. By undeploying model endpoint you stop serving traffic to the model endpoint URL. The version endpoint target is still running.<jupyter_code>merlin.stop_serving_traffic(endpoint.environment_name)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08175974286862193, 'fraction_numerical': 0.013459220570510246, 'mean_word_length': 5.23153942428035, 'pattern_counts': {'":': 2, '<': 41, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '376278', 'license': 'Apache-2.0', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1472, 'n_words': 582, 'path': 'examples/model-endpoint/Model Endpoint.ipynb', 'repo_name': 'tiopramayudi/merlin', 'size': '4978'}