Creating online deployments in Watson Machine Learning
Creating online deployments in Watson Machine Learning Create an online (also called Web service) deployment to load a model or Python code when the deployment is created to generate predictions online, in real time. For example, if you create a classification model to test whether a new customer is likely to participate in a sales promotion, you can create an online deployment for the model. Then, you can enter the new customer data to get an immediate prediction. Supported frameworks Online deployment is supported for these frameworks: * PMML * Python Function * PyTorch-Onnx * Tensorflow * Scikit-Learn * Spark MLlib * SPSS * XGBoost You can create an online deployment [from the user interface](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/deploy-online.html?context=cdpaas&locale=enonline-interface) or [programmatically](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/deploy-online.html?context=cdpaas&locale=enonline-programmatically). To send payload data to an asset that is deployed online, you must know the endpoint URL of the deployment. Examples include, classification of data, or making predictions from the data. For more information, see [Retrieving the deployment endpoint](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/deploy-online.html?context=cdpaas&locale=enget-online-endpoint). Additionally, you can: * [Test your online deployment](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/deploy-online.html?context=cdpaas&locale=entest-online-deployment) * [Access the deployment details](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/deploy-online.html?context=cdpaas&locale=enaccess-online-details) Creating an online deployment from the User Interface 1. From the deployment space, click the name of the asset that you want to deploy. The details page opens. 2. Click New deployment. 3. Choose Online as the deployment type. 4. Provide a name and an optional description for the deployment. 5. If you want to specify a name to be used instead of deployment ID, use the Serving name field. * The name must be validated to be unique per IBM cloud region (all names in a specific region share a global namespace). * The name must contain only these characters: [a-z,0-9,_] and must be a maximum 36 characters long. * Serving name works only as part of the prediction URL. In some cases, you must still use the deployment ID. 6. Click Create to create the deployment. Creating an online deployment programmatically Refer to [Machine learning samples and examples](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-samples-overview.html) for links to sample notebooks. These notebooks demonstrate creating online deployments that use the Watson Machine Learning [REST API](https://cloud.ibm.com/apidocs/machine-learning) and Watson Machine Learning [Python client library](https://ibm.github.io/watson-machine-learning-sdk/). Retrieving the online deployment endpoint You can find the endpoint URL of a deployment in these ways: * From the Deployments tab of your space, click your deployment name. A page with deployment details opens. You can find the endpoint there. * Using the Watson Machine Learning Python client: 1. List the deployments by calling the [Python client method](https://ibm.github.io/watson-machine-learning-sdk/core_api.htmlclient.Deployments.list)client.deployments.list() 2. Find the row with your deployment. The deployment endpoint URL is listed in the url column. Notes: * If you added Serving name to the deployment, two alternative endpoint URLs show on the screen; one containing the deployment ID, and the other containing your serving name. You can use either one of these URLs with your deployment. * The API Reference tab also shows code snippets in various programming languages that illustrate how to access the deployment. For more information, see [Endpoint URLs](https://cloud.ibm.com/apidocs/machine-learningendpoint-url). Testing your online deployment From the Deployments tab of your space, click your deployment name. A page with deployment details opens. The Test tab provides a place where you can enter data and get a prediction back from the deployed model. If your model has a defined schema, a form shows on screen. In the form, you can enter data in one of these ways: * Enter data directly in the form * Download a CSV template, enter values, and upload the input data * Upload a file that contains input data from your local file system or from the space * Change to the JSON tab and enter your input data as JSON code Regardless of method, the input data must match the schema of the model. Submit the input data and get a score, or prediction, back. Sample deployment code When you submit JSON code as the payload, or input data, for a deployment, your input data must match the schema of the model. The 'fields' must match the column headers for the data, and the 'values' must contain the data, in the same order. Use this format: {"input_data":[{ "fields": <field1>, <field2>, ...], "values": <value1>, <value2>, ...]] }]} Refer to this example: {"input_data":[{ "fields": "PassengerId","Pclass","Name","Sex","Age","SibSp","Parch","Ticket","Fare","Cabin","Embarked"], "values": 1,3,"Braund, Mr. Owen Harris",0,22,1,0,"A/5 21171",7.25,null,"S"]] }]} Notes: * All strings are enclosed in double quotation marks. The Python notation for dictionaries looks similar, but Python strings in single quotation marks are not accepted in the JSON data. * Missing values can be indicated with null. * You can specify a hardware specification for an online deployment, for example if you are [scaling a deployment](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-deploy-scaling.html). Preparing payload that matches the schema of an existing model Refer to this sample code: model_details = client.repository.get_details("<model_id>") retrieves details and includes schema columns_in_schema = [] for i in range(0, len(model_details['entity']['input'].get('fields'))): columns_in_schema.append(model_details['entity']['input'].get('fields')[i]) X = X[columns_in_schema] where X is a pandas